This commit is contained in:
eric sciple 2020-01-24 12:20:19 -05:00
parent beb1329f9f
commit 2b95e76931
7736 changed files with 1874747 additions and 51184 deletions

View file

@ -23,6 +23,7 @@ the passed options and sends the request using [fetch](https://developer.mozilla
- [REST API example](#rest-api-example)
- [GraphQL example](#graphql-example)
- [Alternative: pass `method` & `url` as part of options](#alternative-pass-method--url-as-part-of-options)
- [Authentication](#authentication)
- [request()](#request)
- [`request.defaults()`](#requestdefaults)
- [`request.endpoint`](#requestendpoint)
@ -42,13 +43,17 @@ request("POST /repos/:owner/:repo/issues/:number/labels", {
mediaType: {
previews: ["symmetra"]
},
owner: "ocotkit",
owner: "octokit",
repo: "request.js",
number: 1,
labels: ["🐛 bug"]
});
```
👶 [Small bundle size](https://bundlephobia.com/result?p=@octokit/request@5.0.3) (\<4kb minified + gzipped)
😎 [Authenticate](#authentication) with any of [GitHubs Authentication Strategies](https://github.com/octokit/auth.js).
👍 Sensible defaults
- `baseUrl`: `https://api.github.com`
@ -59,8 +64,6 @@ request("POST /repos/:owner/:repo/issues/:number/labels", {
🧐 Simple to debug: Sets `error.request` to request options causing the error (with redacted credentials).
👶 Small bundle size (\<5kb minified + gzipped)
## Usage
<table>
@ -110,6 +113,8 @@ console.log(`${result.data.length} repos found.`);
### GraphQL example
For GraphQL request we recommend using [`@octokit/graphql`](https://github.com/octokit/graphql.js#readme)
```js
const result = await request("POST /graphql", {
headers: {
@ -144,6 +149,45 @@ const result = await request({
});
```
## Authentication
The simplest way to authenticate a request is to set the `Authorization` header directly, e.g. to a [personal access token](https://github.com/settings/tokens/).
```js
const requestWithAuth = request.defaults({
headers: {
authorization: "token 0000000000000000000000000000000000000001"
}
});
const result = await request("GET /user");
```
For more complex authentication strategies such as GitHub Apps or Basic, we recommend the according authentication library exported by [`@octokit/auth`](https://github.com/octokit/auth.js).
```js
const { createAppAuth } = require("@octokit/auth-app");
const auth = createAppAuth({
id: process.env.APP_ID,
privateKey: process.env.PRIVATE_KEY,
installationId: 123
});
const requestWithAuth = request.defaults({
request: {
hook: auth.hook
},
mediaType: {
previews: ["machine-man"]
}
});
const { data: app } = await requestWithAuth("GET /app");
const { data: app } = await requestWithAuth("POST /repos/:owner/:repo/issues", {
owner: "octocat",
repo: "hello-world",
title: "Hello from the engine room"
});
```
## request()
`request(route, options)` or `request(options)`.
@ -425,10 +469,10 @@ const options = request.endpoint("GET /orgs/:org/repos", {
All of the [`@octokit/endpoint`](https://github.com/octokit/endpoint.js) API can be used:
- [`ocotkitRequest.endpoint()`](#endpoint)
- [`ocotkitRequest.endpoint.defaults()`](#endpointdefaults)
- [`ocotkitRequest.endpoint.merge()`](#endpointdefaults)
- [`ocotkitRequest.endpoint.parse()`](#endpointmerge)
- [`octokitRequest.endpoint()`](#endpoint)
- [`octokitRequest.endpoint.defaults()`](#endpointdefaults)
- [`octokitRequest.endpoint.merge()`](#endpointdefaults)
- [`octokitRequest.endpoint.parse()`](#endpointmerge)
## Special cases