{"name":"@octokit-next/endpoint","version":"1.15.4","keywords":["octokit","github","api"],"author":{"url":"https://github.com/gr2m","name":"Gregor Martynus"},"license":"MIT","_id":"@octokit-next/endpoint@1.15.4","maintainers":[{"name":"gr2m","email":"gregor@martynus.net"},{"name":"octokitbot","email":"security+octokitbot@github.com"}],"homepage":"https://github.com/octokit/octokit-next.js/tree/main/packages/endpoint#readme","bugs":{"url":"https://github.com/octokit/octokit-next.js/issues"},"c8":{"lines":100,"branches":100,"functions":100,"statements":100,"check-coverage":true},"dist":{"shasum":"1dc9411f42eb737e56a04f8f2b313e6147301ea7","tarball":"http://registry-npm.ywwl.com/@octokit-next/endpoint/download/@octokit-next/endpoint-1.15.4.tgz","fileCount":22,"integrity":"sha512-s5KYfSFt0xGg/b0tdLh9LmskUPY2T5+5Rd3d14NNUQptv6zlFIagIFHrqmLyxpF3B3GL7DqOOGF4+rjCdogUUA==","signatures":[{"sig":"MEQCIG5pkz0TOWXf69wtiVQ2yYS99TV98h01p6fKNdCfkilPAiATJ/NqKH51qArcfae4q5n5V9Q6DCLun4XItmjCZV8hSg==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":55640,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi1zQ0ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmrm5A//fB7kScYEDbQh67un5woHEvcSQdCpNi0/W/62y6gBgPfwc/Fr\r\nuJKyZTjqIvQnG/hFDSNmHs7kw41BY5yvuSTocYli1UxST870vDKdMX9r+kNJ\r\nSIbfopeWHytfkR+xIYe9ws4o2xPjiPhxgWXTwO1PIHteL6TeYbyfn64lY5zU\r\nN3qsEyw2UqNxSuyxG1NmyUr24RKqBjzWGh/C3N/U39NHlmfYxqu9EQTTY1FY\r\nDvp9JZu38My/hPegiaz9S/zfUNjJ5U/IO9qCsIjD1qG2Vd3d9Be65nc2N0ps\r\nP/fkltEgP5T8lNmPH12tVTlZHsVSutZKA/dK6mTing6Jn97Jzck95SJR8FTW\r\nesJV+j3pqwSRGpoElZ4xYbCenSsCHK77ZBAdW+vm/bZRkOUHG7sPH9l1Xewa\r\n82cagGJ3Ja6zkijHkBtQm3qxIzDIqYNmZuexRR021QCb6g4ru/dD5go8UzXi\r\n5yVfy8TPQ8UOsPnMZPuhAq1UdCsArJYmIvOkTKog8sQ4TCXIGSc98ilfTuhZ\r\nB1w+ACPpoh2a9as5oTRnKY1b+A/VVs7m3DH5FvQO+ToOeXZlCGj77Bmv6QwO\r\n/7guSCmNppDOeEIVHyt90Suyo/X9Il85Q2ED3Gb6p2vDiTMWtr7GKbow5JSo\r\nw5f8U/wMYM0//QBlUmnC2FvHGVh8+l6dJ0E=\r\n=OYLx\r\n-----END PGP SIGNATURE-----\r\n","size":12801,"noattachment":false,"key":"/@octokit-next/endpoint/-/@octokit-next/endpoint-1.15.4.tgz"},"type":"module","types":"./index.d.ts","exports":"./index.js","gitHead":"319e49b4aa9f01eb7fc48e02eae3b647b6bb375f","scripts":{"test":"npm run test:code && npm run test:types","test:code":"c8 uvu test","test:types":"tsd"},"_npmUser":{"name":"octokitbot","email":"security+octokitbot@github.com"},"repository":{"url":"git+https://github.com/octokit/octokit-next.js.git","type":"git","directory":"packages/endpoint"},"_npmVersion":"8.14.0","description":"Turns REST API endpoints into generic request options","directories":{},"_nodeVersion":"16.16.0","dependencies":{"type-fest":"^2.3.4","is-plain-obj":"^4.0.0","@octokit-next/types":"1.15.4","universal-user-agent":"^7.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/endpoint_1.15.4_1658270772369_0.338672461098835","host":"s3://npm-registry-packages"},"readme":"# endpoint.js\n\n> Turns GitHub REST API endpoints into generic request options\n\n[![@latest](https://img.shields.io/npm/v/@octokit-next/endpoint.svg)](https://www.npmjs.com/package/@octokit-next/endpoint)\n[![Build Status](https://github.com/octokit-next/endpoint.js/workflows/Test/badge.svg)](https://github.com/octokit-next/endpoint.js/actions/workflows/test.yml?query=branch%3Amaster)\n\n`@octokit-next/endpoint` combines [GitHub REST API routes](https://docs.github.com/rest) with parameters and turns them into generic request options that can be used in any request library.\n\n## Usage\n\n<table>\n<tbody valign=top align=left>\n<tr><th>\nBrowsers\n</th><td width=100%>\nLoad <code>@octokit-next/endpoint</code> directly from <a href=\"https://cdn.skypack.dev\">cdn.skypack.dev</a>\n        \n```html\n<script type=\"module\">\nimport { endpoint } from \"https://cdn.skypack.dev/@octokit-next/endpoint\";\n</script>\n```\n\n</td></tr>\n<tr><th>\nNode\n</th><td>\n\nInstall with <code>npm install @octokit-next/endpoint</code>\n\n```js\nimport { endpoint } from \"@octokit-next/endpoint\";\n```\n\n</td></tr>\n<tr><th>\nDeno\n</th><td>\n\nLoad <code>@octokit-next/endpoint</code> directly from <a href=\"https://cdn.skypack.dev\">cdn.skypack.dev</a>, including types.\n\n```js\nimport { endpoint } from \"https://cdn.skypack.dev/@octokit-next/endpoint?dts\";\n```\n\n</td></tr>\n</tbody>\n</table>\n\nExample for [List organization repositories](https://docs.github.com/rest/reference/repos#list-organization-repositories)\n\n```js\nconst requestOptions = endpoint(\"GET /orgs/{org}/repos\", {\n  headers: {\n    authorization: \"token 0000000000000000000000000000000000000001\",\n  },\n  org: \"octokit\",\n  type: \"private\",\n});\n```\n\nThe resulting `requestOptions` looks as follows\n\n```json\n{\n  \"method\": \"GET\",\n  \"url\": \"https://api.github.com/orgs/octokit/repos?type=private\",\n  \"headers\": {\n    \"accept\": \"application/vnd.github.v3+json\",\n    \"authorization\": \"token 0000000000000000000000000000000000000001\",\n    \"user-agent\": \"octokit-next/endpoint.js v1.2.3\"\n  }\n}\n```\n\nYou can pass `requestOptions` to common request libraries\n\n```js\nconst { url, ...options } = requestOptions;\n// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch\nfetch(url, options);\n// https://github.com/sindresorhus/got\ngot[options.method](url, options);\n// https://github.com/axios/axios\naxios(requestOptions);\n```\n\nFor `PUT/POST` endpoints with request body parameters, the code is slightly different\n\n```js\nconst { url, data, ...options } = requestOptions;\n// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch\nfetch(url, { ...options, body: JSON.stringify(data) });\n// https://github.com/sindresorhus/got\ngot[options.method](url, { ...options, json: data });\n// https://github.com/axios/axios\naxios(requestOptions);\n```\n\n## API\n\n### `endpoint(route, options)` or `endpoint(options)`\n\n<table>\n  <thead align=left>\n    <tr>\n      <th>\n        name\n      </th>\n      <th>\n        type\n      </th>\n      <th width=100%>\n        description\n      </th>\n    </tr>\n  </thead>\n  <tbody align=left valign=top>\n    <tr>\n      <th>\n        <code>route</code>\n      </th>\n      <td>\n        String\n      </td>\n      <td>\n        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>.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.method</code>\n      </th>\n      <td>\n        String\n      </td>\n      <td>\n        <strong>Required unless <code>route</code> is set.</strong> Any supported <a href=\"https://docs.github.com/rest/overview/resources-in-the-rest-api#http-verbs\">http verb</a>. <em>Defaults to <code>GET</code></em>.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.url</code>\n      </th>\n      <td>\n        String\n      </td>\n      <td>\n        <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,\n        e.g., <code>/orgs/{org}/repos</code>.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.baseUrl</code>\n      </th>\n      <td>\n        String\n      </td>\n      <td>\n        <em>Defaults to <code>https://api.github.com</code></em>.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.headers</code>\n      </th>\n      <td>\n        Object\n      </td>\n      <td>\n        Custom headers. Passed headers are merged with defaults:<br>\n        <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>\n        <em><code>headers['accept']</code> defaults to <code>application/vnd.github.v3+json</code></em>.<br>\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.mediaType.format</code>\n      </th>\n      <td>\n        String\n      </td>\n      <td>\n        Media type param, such as <code>raw</code>, <code>diff</code>, or <code>text+json</code>. See <a href=\"https://docs.github.com/rest/overview/media-types\">Media Types</a>. Setting <code>options.mediaType.format</code> will amend the <code>headers.accept</code> value.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.mediaType.previews</code>\n      </th>\n      <td>\n        Array of Strings\n      </td>\n      <td>\n        Name of previews, such as <code>mercy</code>, <code>symmetra</code>, or <code>scarlet-witch</code>. See <a href=\"https://docs.github.com/rest/overview/api-previews\">API Previews</a>. If <code>options.mediaType.previews</code> was set as default, the new previews will be merged into the default ones. Setting <code>options.mediaType.previews</code> will amend the <code>headers.accept</code> value. <code>options.mediaType.previews</code> will be merged with an existing array set using <code>.withDefaults()</code>.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.data</code>\n      </th>\n      <td>\n        Any\n      </td>\n      <td>\n        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.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>options.request</code>\n      </th>\n      <td>\n        Object\n      </td>\n      <td>\n        Pass custom meta information for the request. The <code>request</code> object will be returned as is.\n      </td>\n    </tr>\n  </tbody>\n</table>\n\nAll other options will be passed depending on the `method` and `url` options.\n\n1. 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`.\n2. If the `method` is `GET` or `HEAD`, the option is passed as a query parameter.\n3. Otherwise, the parameter is passed in the request body as a JSON key.\n\n**Result**\n\n`endpoint()` is a synchronous method and returns an object with the following keys:\n\n<table>\n  <thead align=left>\n    <tr>\n      <th>\n        key\n      </th>\n      <th>\n        type\n      </th>\n      <th width=100%>\n        description\n      </th>\n    </tr>\n  </thead>\n  <tbody align=left valign=top>\n    <tr>\n      <th><code>method</code></th>\n      <td>String</td>\n      <td>The http method. Always lowercase.</td>\n    </tr>\n    <tr>\n      <th><code>url</code></th>\n      <td>String</td>\n      <td>The url with placeholders replaced with passed parameters.</td>\n    </tr>\n    <tr>\n      <th><code>headers</code></th>\n      <td>Object</td>\n      <td>All header names are lowercased.</td>\n    </tr>\n    <tr>\n      <th><code>body</code></th>\n      <td>Any</td>\n      <td>The request body if one is present. Only for <code>PATCH</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code> requests.</td>\n    </tr>\n    <tr>\n      <th><code>request</code></th>\n      <td>Object</td>\n      <td>Request meta option, it will be returned as it was passed into <code>endpoint()</code></td>\n    </tr>\n  </tbody>\n</table>\n\n### `endpoint.withDefaults()`\n\nOverride or set default options. Example:\n\n```js\nconst myEndpoint = endpoint.withDefaults({\n  baseUrl: \"https://github-enterprise.acme-inc.com/api/v3\",\n  headers: {\n    \"user-agent\": \"myApp/1.2.3\",\n    authorization: `token 0000000000000000000000000000000000000001`,\n  },\n});\n\nconst options = myEndpoint(`GET /orgs/{org}/repos`, {\n  org: \"my-project\",\n  per_page: 100,\n});\n// {\n//   \"method\": \"GET\",\n//   \"url\": \"https://api.github.com/orgs/my-project/repos?per_page=100\",\n//   \"headers\": {\n//     \"accept\": \"application/vnd.github.v3+json\",\n//     \"authorization\": \"token 0000000000000000000000000000000000000001\",\n//     \"user-agent\": \"myApp/1.2.3\"\n//   }\n// }\n```\n\nYou can call `.withDefaults()` again on the returned method, the defaults will cascade.\n\n```js\nconst myEndpointWithToken2 = myEndpoint.withDefaults({\n  headers: {\n    authorization: `token 0000000000000000000000000000000000000002`,\n  },\n});\n\nconst options2 = myEndpointWithToken2(`GET /orgs/{org}/repos`, {\n  org: \"my-project\",\n  per_page: 100,\n});\n// {\n//   \"method\": \"GET\",\n//   \"url\": \"https://api.github.com/orgs/my-project/repos?per_page=100\",\n//   \"headers\": {\n//     \"accept\": \"application/vnd.github.v3+json\",\n//     \"authorization\": \"token 0000000000000000000000000000000000000002\",\n//     \"user-agent\": \"myApp/1.2.3\"\n//   }\n// }\n```\n\n### `endpoint.DEFAULTS`\n\nThe current default options.\n\n```js\nendpoint.DEFAULTS.baseUrl; // https://api.github.com\nconst myEndpoint = endpoint.withDefaults({\n  baseUrl: \"https://github-enterprise.acme-inc.com/api/v3\",\n});\nmyEndpoint.DEFAULTS.baseUrl; // https://github-enterprise.acme-inc.com/api/v3\n```\n\n### `endpoint.merge(route, options)` or `endpoint.merge(options)`\n\nGet the defaulted endpoint options, but without parsing them into request options:\n\n```js\nconst myProjectEndpoint = endpoint.withDefaults({\n  baseUrl: \"https://github-enterprise.acme-inc.com/api/v3\",\n  headers: {\n    \"user-agent\": \"myApp/1.2.3\",\n  },\n  org: \"my-project\",\n});\nmyProjectEndpoint.merge(\"GET /orgs/{org}/repos\", {\n  headers: {\n    authorization: `token 0000000000000000000000000000000000000001`,\n  },\n  org: \"my-secret-project\",\n  type: \"private\",\n});\n\n// {\n//   baseUrl: 'https://github-enterprise.acme-inc.com/api/v3',\n//   method: 'GET',\n//   url: '/orgs/{org}/repos',\n//   headers: {\n//     accept: 'application/vnd.github.v3+json',\n//     authorization: `token 0000000000000000000000000000000000000001`,\n//     'user-agent': 'myApp/1.2.3'\n//   },\n//   org: 'my-secret-project',\n//   type: 'private'\n// }\n```\n\n### `endpoint.parse()`\n\nStateless method to turn endpoint options into request options. Calling\n`endpoint(options)` is the same as calling `endpoint.parse(endpoint.merge(options))`.\n\n## Types\n\n`@octokit-next/endpoint` supports types for all REST API endpoints across all supported targets (github.com, GitHub AE, GitHub Enterprise Server).\n\nIn order to take advantage of the types, you have to install the `@octokit-next/types-rest-api*` packages for the platform(s) you want to target.\n\nFor example, to get types for all of github.com's REST API endpoints, use `@octokit-next/types-rest-api`.\n\n```js\n/// <reference types=\"@octokit-next/types-rest-api\" />\n\nimport { endpoint } from \"@octokit-next/endpoint\";\n\nendpoint(\"\");\n// Set cursor in the route argument and press `Ctrl + Space` to get a type ahead for all 700+ REST API endpoints\n\nconst requestOptions = endpoint(\"GET /orgs/{org}/repos\", { org: \"octokit\" });\n// requestOptions.method is now typed as `\"GET\"` instead of `string`\n// requestOptions.url is now typed as `\"/orgs/{org}/repos\"` instead of `string`\n// requestOptions.data does not exist on types.\n```\n\nTo support GitHub Enterprise Server 3.0 and all new versions, import `@octokit-next/types-rest-api-ghes-3.0` and set the request version:\n\n```js\n/// <reference types=\"@octokit-next/types-rest-api-ghes-3.0\" />\n\nimport { endpoint } from \"@octokit-next/endpoint\";\n\nendpoint(\"\", {\n  request: {\n    version: \"ghes-3.0\",\n  },\n});\n// Set cursor in the route argument and press `Ctrl + Space` to get a type ahead for all GHES 3.0 REST API endpoints\n\nconst requestOptions = endpoint(\"GET /admin/users/{username}\", {\n  request: {\n    version: \"ghes-3.0\",\n  },\n  username: \"octocat\",\n});\n// requestOptions.method is now typed as `\"GET\"` instead of `string`\n// requestOptions.url is now typed as `\"/admin/users/{username}\"` instead of `string`\n// requestOptions.data does not exist on types.\n```\n\nTypes in the `@octokit-next/types-rest-api-ghes` packages are additive. So you can set `request.version` to `ghes-3.1` and `ghes-3.2` as well.\n\nThe version can be set using `endpoint.withDefaults()` as well. You can override the version in each `endpoint()` call.\n\n```js\n/// <reference types=\"@octokit-next/types-rest-api-ghes-3.0\" />\n\nimport { endpoint } from \"@octokit-next/endpoint\";\n\nconst ghes30endpoint = endpoint.withDefaults({\n  request: {\n    version: \"ghes-3.0\",\n  },\n});\n\nendpoint(\"\");\n// Set cursor in the route argument and press `Ctrl + Space` to get a type ahead for all GHES 3.0 REST API endpoints\n```\n\nIf you need your script to work across github.com and a minimal GitHub Enterprise Server version, you can use any of the `@octokit-next/types-rest-api-ghes-*-compatible` packages.\n\n```js\n/// <reference types=\"@octokit-next/types-rest-api-ghes-3.0-compatible\" />\n\nimport { endpoint } from \"@octokit-next/endpoint\";\n\nconst ghes30endpoint = endpoint.withDefaults({\n  request: {\n    version: \"ghes-3.0\",\n  },\n});\n\nendpoint(\"\");\n// Set cursor in the route argument and press `Ctrl + Space` to get a type ahead for all REST API endpoints\n// that exist in both github.com and GitHub Enterprise Server 3.0\n```\n\n## Special cases\n\n<a name=\"data-parameter\"></a>\n\n### The `data` parameter – set request body directly\n\nSome endpoints such as [Render a Markdown document in raw mode](https://docs.github.com/rest/reference/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.\n\n```js\nconst options = endpoint(\"POST /markdown/raw\", {\n  data: \"Hello world github/linguist#1 **cool**, and #1!\",\n  headers: {\n    accept: \"text/html;charset=utf-8\",\n    \"content-type\": \"text/plain\",\n  },\n});\n\n// options is\n// {\n//   method: 'post',\n//   url: 'https://api.github.com/markdown/raw',\n//   headers: {\n//     accept: 'text/html;charset=utf-8',\n//     'content-type': 'text/plain',\n//     'user-agent': userAgent\n//   },\n//   body: 'Hello world github/linguist#1 **cool**, and #1!'\n// }\n```\n\n### Set parameters for both the URL/query and the request body\n\nThere 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).\n\nExample\n\n```js\nendpoint(\n  \"POST https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}\",\n  {\n    name: \"example.zip\",\n    label: \"short description\",\n    headers: {\n      \"content-type\": \"text/plain\",\n      \"content-length\": 14,\n      authorization: `token 0000000000000000000000000000000000000001`,\n    },\n    data: \"Hello, world!\",\n  }\n);\n```\n\n## LICENSE\n\n[MIT](LICENSE)\n","publish_time":1658270772500,"_cnpm_publish_time":1658270772500,"dist-tags":{"latest":"3.0.0"}}