1 | # endpoint.js
|
2 |
|
3 | > Turns GitHub REST API endpoints into generic request options
|
4 |
|
5 | [![@latest](https://img.shields.io/npm/v/@octokit/endpoint.svg)](https://www.npmjs.com/package/@octokit/endpoint)
|
6 | [![Build Status](https://github.com/octokit/endpoint.js/workflows/Test/badge.svg)](https://github.com/octokit/endpoint.js/actions/workflows/test.yml?query=branch%3Amain)
|
7 |
|
8 | `@octokit/endpoint` combines [GitHub REST API routes](https://developer.github.com/v3/) with your parameters and turns them into generic request options that can be used in any request library.
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | - [Usage](#usage)
|
15 | - [API](#api)
|
16 | - [`endpoint(route, options)` or `endpoint(options)`](#endpointroute-options-or-endpointoptions)
|
17 | - [`endpoint.defaults()`](#endpointdefaults)
|
18 | - [`endpoint.DEFAULTS`](#endpointdefaults)
|
19 | - [`endpoint.merge(route, options)` or `endpoint.merge(options)`](#endpointmergeroute-options-or-endpointmergeoptions)
|
20 | - [`endpoint.parse()`](#endpointparse)
|
21 | - [Special cases](#special-cases)
|
22 | - [The `data` parameter – set request body directly](#the-data-parameter-%E2%80%93-set-request-body-directly)
|
23 | - [Set parameters for both the URL/query and the request body](#set-parameters-for-both-the-urlquery-and-the-request-body)
|
24 | - [LICENSE](#license)
|
25 |
|
26 |
|
27 |
|
28 | ## Usage
|
29 |
|
30 | <table>
|
31 | <tbody valign=top align=left>
|
32 | <tr><th>
|
33 | Browsers
|
34 | </th><td width=100%>
|
35 | Load <code>@octokit/endpoint</code> directly from <a href="https://esm.sh">esm.sh</a>
|
36 |
|
37 | ```html
|
38 | <script type="module">
|
39 | import { endpoint } from "https://esm.sh/@octokit/endpoint";
|
40 | </script>
|
41 | ```
|
42 |
|
43 | </td></tr>
|
44 | <tr><th>
|
45 | Node
|
46 | </th><td>
|
47 |
|
48 | Install with <code>npm install @octokit/endpoint</code>
|
49 |
|
50 | ```js
|
51 | import { endpoint } from "@octokit/endpoint";
|
52 | ```
|
53 |
|
54 | </td></tr>
|
55 | </tbody>
|
56 | </table>
|
57 |
|
58 | Example for [List organization repositories](https://developer.github.com/v3/repos/#list-organization-repositories)
|
59 |
|
60 | ```js
|
61 | const requestOptions = endpoint("GET /orgs/{org}/repos", {
|
62 | headers: {
|
63 | authorization: "token 0000000000000000000000000000000000000001",
|
64 | },
|
65 | org: "octokit",
|
66 | type: "private",
|
67 | });
|
68 | ```
|
69 |
|
70 | The resulting `requestOptions` looks as follows
|
71 |
|
72 | ```json
|
73 | {
|
74 | "method": "GET",
|
75 | "url": "https://api.github.com/orgs/octokit/repos?type=private",
|
76 | "headers": {
|
77 | "accept": "application/vnd.github.v3+json",
|
78 | "authorization": "token 0000000000000000000000000000000000000001",
|
79 | "user-agent": "octokit/endpoint.js v1.2.3"
|
80 | }
|
81 | }
|
82 | ```
|
83 |
|
84 | You can pass `requestOptions` to common request libraries
|
85 |
|
86 | ```js
|
87 | const { url, ...options } = requestOptions;
|
88 | // using with fetch (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
|
89 | fetch(url, options);
|
90 | // using with request (https://github.com/request/request)
|
91 | request(requestOptions);
|
92 | // using with got (https://github.com/sindresorhus/got)
|
93 | got[options.method](url, options);
|
94 | // using with axios
|
95 | axios(requestOptions);
|
96 | ```
|
97 |
|
98 | > [!IMPORTANT]
|
99 | > As we use [conditional exports](https://nodejs.org/api/packages.html#conditional-exports), you will need to adapt your `tsconfig.json`. See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).
|
100 |
|
101 | ## API
|
102 |
|
103 | ### `endpoint(route, options)` or `endpoint(options)`
|
104 |
|
105 | <table>
|
106 | <thead align=left>
|
107 | <tr>
|
108 | <th>
|
109 | name
|
110 | </th>
|
111 | <th>
|
112 | type
|
113 | </th>
|
114 | <th width=100%>
|
115 | description
|
116 | </th>
|
117 | </tr>
|
118 | </thead>
|
119 | <tbody align=left valign=top>
|
120 | <tr>
|
121 | <th>
|
122 | <code>route</code>
|
123 | </th>
|
124 | <td>
|
125 | String
|
126 | </td>
|
127 | <td>
|
128 | If set, it has to be a string consisting of URL and the request method, e.g., <code>GET /orgs/{org}</code>. If it’s set to a URL, only the method defaults to <code>GET</code>.
|
129 | </td>
|
130 | </tr>
|
131 | <tr>
|
132 | <th>
|
133 | <code>options.method</code>
|
134 | </th>
|
135 | <td>
|
136 | String
|
137 | </td>
|
138 | <td>
|
139 | <strong>Required unless <code>route</code> is set.</strong> Any supported <a href="https://developer.github.com/v3/#http-verbs">http verb</a>. <em>Defaults to <code>GET</code></em>.
|
140 | </td>
|
141 | </tr>
|
142 | <tr>
|
143 | <th>
|
144 | <code>options.url</code>
|
145 | </th>
|
146 | <td>
|
147 | String
|
148 | </td>
|
149 | <td>
|
150 | <strong>Required unless <code>route</code> is set.</strong> A path or full URL which may contain <code>:variable</code> or <code>{variable}</code> placeholders,
|
151 | e.g., <code>/orgs/{org}/repos</code>. The <code>url</code> is parsed using <a href="https://github.com/bramstein/url-template">url-template</a>.
|
152 | </td>
|
153 | </tr>
|
154 | <tr>
|
155 | <th>
|
156 | <code>options.baseUrl</code>
|
157 | </th>
|
158 | <td>
|
159 | String
|
160 | </td>
|
161 | <td>
|
162 | <em>Defaults to <code>https://api.github.com</code></em>.
|
163 | </td>
|
164 | </tr>
|
165 | <tr>
|
166 | <th>
|
167 | <code>options.headers</code>
|
168 | </th>
|
169 | <td>
|
170 | Object
|
171 | </td>
|
172 | <td>
|
173 | Custom headers. Passed headers are merged with defaults:<br>
|
174 | <em><code>headers['user-agent']</code> defaults to <code>octokit-endpoint.js/1.2.3</code> (where <code>1.2.3</code> is the released version)</em>.<br>
|
175 | <em><code>headers['accept']</code> defaults to <code>application/vnd.github.v3+json</code></em>.<br>
|
176 | </td>
|
177 | </tr>
|
178 | <tr>
|
179 | <th>
|
180 | <code>options.mediaType.format</code>
|
181 | </th>
|
182 | <td>
|
183 | String
|
184 | </td>
|
185 | <td>
|
186 | Media type param, such as <code>raw</code>, <code>diff</code>, or <code>text+json</code>. See <a href="https://developer.github.com/v3/media/">Media Types</a>. Setting <code>options.mediaType.format</code> will amend the <code>headers.accept</code> value.
|
187 | </td>
|
188 | </tr>
|
189 | <tr>
|
190 | <th>
|
191 | <code>options.data</code>
|
192 | </th>
|
193 | <td>
|
194 | Any
|
195 | </td>
|
196 | <td>
|
197 | Set request body directly instead of setting it to JSON based on additional parameters. See <a href="#data-parameter">"The <code>data</code> parameter"</a> below.
|
198 | </td>
|
199 | </tr>
|
200 | <tr>
|
201 | <th>
|
202 | <code>options.request</code>
|
203 | </th>
|
204 | <td>
|
205 | Object
|
206 | </td>
|
207 | <td>
|
208 | Pass custom meta information for the request. The <code>request</code> object will be returned as is.
|
209 | </td>
|
210 | </tr>
|
211 | </tbody>
|
212 | </table>
|
213 |
|
214 | All other options will be passed depending on the `method` and `url` options.
|
215 |
|
216 | 1. If the option key has a placeholder in the `url`, it will be used as the replacement. For example, if the passed options are `{url: '/orgs/{org}/repos', org: 'foo'}` the returned `options.url` is `https://api.github.com/orgs/foo/repos`.
|
217 | 2. If the `method` is `GET` or `HEAD`, the option is passed as a query parameter.
|
218 | 3. Otherwise, the parameter is passed in the request body as a JSON key.
|
219 |
|
220 | **Result**
|
221 |
|
222 | `endpoint()` is a synchronous method and returns an object with the following keys:
|
223 |
|
224 | <table>
|
225 | <thead align=left>
|
226 | <tr>
|
227 | <th>
|
228 | key
|
229 | </th>
|
230 | <th>
|
231 | type
|
232 | </th>
|
233 | <th width=100%>
|
234 | description
|
235 | </th>
|
236 | </tr>
|
237 | </thead>
|
238 | <tbody align=left valign=top>
|
239 | <tr>
|
240 | <th><code>method</code></th>
|
241 | <td>String</td>
|
242 | <td>The http method. Always lowercase.</td>
|
243 | </tr>
|
244 | <tr>
|
245 | <th><code>url</code></th>
|
246 | <td>String</td>
|
247 | <td>The url with placeholders replaced with passed parameters.</td>
|
248 | </tr>
|
249 | <tr>
|
250 | <th><code>headers</code></th>
|
251 | <td>Object</td>
|
252 | <td>All header names are lowercased.</td>
|
253 | </tr>
|
254 | <tr>
|
255 | <th><code>body</code></th>
|
256 | <td>Any</td>
|
257 | <td>The request body if one is present. Only for <code>PATCH</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code> requests.</td>
|
258 | </tr>
|
259 | <tr>
|
260 | <th><code>request</code></th>
|
261 | <td>Object</td>
|
262 | <td>Request meta option, it will be returned as it was passed into <code>endpoint()</code></td>
|
263 | </tr>
|
264 | </tbody>
|
265 | </table>
|
266 |
|
267 | ### `endpoint.defaults()`
|
268 |
|
269 | Override or set default options. Example:
|
270 |
|
271 | ```js
|
272 | const request = require("request");
|
273 | const myEndpoint = require("@octokit/endpoint").defaults({
|
274 | baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
|
275 | headers: {
|
276 | "user-agent": "myApp/1.2.3",
|
277 | authorization: `token 0000000000000000000000000000000000000001`,
|
278 | },
|
279 | org: "my-project",
|
280 | per_page: 100,
|
281 | });
|
282 |
|
283 | request(myEndpoint(`GET /orgs/{org}/repos`));
|
284 | ```
|
285 |
|
286 | You can call `.defaults()` again on the returned method, the defaults will cascade.
|
287 |
|
288 | ```js
|
289 | const myProjectEndpoint = endpoint.defaults({
|
290 | baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
|
291 | headers: {
|
292 | "user-agent": "myApp/1.2.3",
|
293 | },
|
294 | org: "my-project",
|
295 | });
|
296 | const myProjectEndpointWithAuth = myProjectEndpoint.defaults({
|
297 | headers: {
|
298 | authorization: `token 0000000000000000000000000000000000000001`,
|
299 | },
|
300 | });
|
301 | ```
|
302 |
|
303 | `myProjectEndpointWithAuth` now defaults the `baseUrl`, `headers['user-agent']`,
|
304 | `org` and `headers['authorization']` on top of `headers['accept']` that is set
|
305 | by the global default.
|
306 |
|
307 | ### `endpoint.DEFAULTS`
|
308 |
|
309 | The current default options.
|
310 |
|
311 | ```js
|
312 | endpoint.DEFAULTS.baseUrl; // https://api.github.com
|
313 | const myEndpoint = endpoint.defaults({
|
314 | baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
|
315 | });
|
316 | myEndpoint.DEFAULTS.baseUrl; // https://github-enterprise.acme-inc.com/api/v3
|
317 | ```
|
318 |
|
319 | ### `endpoint.merge(route, options)` or `endpoint.merge(options)`
|
320 |
|
321 | Get the defaulted endpoint options, but without parsing them into request options:
|
322 |
|
323 | ```js
|
324 | const myProjectEndpoint = endpoint.defaults({
|
325 | baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
|
326 | headers: {
|
327 | "user-agent": "myApp/1.2.3",
|
328 | },
|
329 | org: "my-project",
|
330 | });
|
331 | myProjectEndpoint.merge("GET /orgs/{org}/repos", {
|
332 | headers: {
|
333 | authorization: `token 0000000000000000000000000000000000000001`,
|
334 | },
|
335 | org: "my-secret-project",
|
336 | type: "private",
|
337 | });
|
338 |
|
339 | // {
|
340 | // baseUrl: 'https://github-enterprise.acme-inc.com/api/v3',
|
341 | // method: 'GET',
|
342 | // url: '/orgs/{org}/repos',
|
343 | // headers: {
|
344 | // accept: 'application/vnd.github.v3+json',
|
345 | // authorization: `token 0000000000000000000000000000000000000001`,
|
346 | // 'user-agent': 'myApp/1.2.3'
|
347 | // },
|
348 | // org: 'my-secret-project',
|
349 | // type: 'private'
|
350 | // }
|
351 | ```
|
352 |
|
353 | ### `endpoint.parse()`
|
354 |
|
355 | Stateless method to turn endpoint options into request options. Calling
|
356 | `endpoint(options)` is the same as calling `endpoint.parse(endpoint.merge(options))`.
|
357 |
|
358 | ## Special cases
|
359 |
|
360 | <a name="data-parameter"></a>
|
361 |
|
362 | ### The `data` parameter – set request body directly
|
363 |
|
364 | Some endpoints such as [Render a Markdown document in raw mode](https://developer.github.com/v3/markdown/#render-a-markdown-document-in-raw-mode) don’t have parameters that are sent as request body keys, instead, the request body needs to be set directly. In these cases, set the `data` parameter.
|
365 |
|
366 | ```js
|
367 | const options = endpoint("POST /markdown/raw", {
|
368 | data: "Hello world github/linguist#1 **cool**, and #1!",
|
369 | headers: {
|
370 | accept: "text/html;charset=utf-8",
|
371 | "content-type": "text/plain",
|
372 | },
|
373 | });
|
374 |
|
375 | // options is
|
376 | // {
|
377 | // method: 'post',
|
378 | // url: 'https://api.github.com/markdown/raw',
|
379 | // headers: {
|
380 | // accept: 'text/html;charset=utf-8',
|
381 | // 'content-type': 'text/plain',
|
382 | // 'user-agent': userAgent
|
383 | // },
|
384 | // body: 'Hello world github/linguist#1 **cool**, and #1!'
|
385 | // }
|
386 | ```
|
387 |
|
388 | ### Set parameters for both the URL/query and the request body
|
389 |
|
390 | There are API endpoints that accept both query parameters as well as a body. In that case, you need to add the query parameters as templates to `options.url`, as defined in the [RFC 6570 URI Template specification](https://tools.ietf.org/html/rfc6570).
|
391 |
|
392 | Example
|
393 |
|
394 | ```js
|
395 | endpoint(
|
396 | "POST https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
|
397 | {
|
398 | name: "example.zip",
|
399 | label: "short description",
|
400 | headers: {
|
401 | "content-type": "text/plain",
|
402 | "content-length": 14,
|
403 | authorization: `token 0000000000000000000000000000000000000001`,
|
404 | },
|
405 | data: "Hello, world!",
|
406 | },
|
407 | );
|
408 | ```
|
409 |
|
410 | ## LICENSE
|
411 |
|
412 | [MIT](LICENSE)
|