Upgrade octokit to v4.1.2

This commit is contained in:
Angela P Wen 2025-02-19 11:13:12 -08:00
parent dbbcbe019d
commit c1745a9831
1214 changed files with 160765 additions and 0 deletions

View file

@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2019 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,284 @@
# auth-token.js
> GitHub API token authentication for browsers and Node.js
[![@latest](https://img.shields.io/npm/v/@octokit/auth-token.svg)](https://www.npmjs.com/package/@octokit/auth-token)
[![Build Status](https://github.com/octokit/auth-token.js/workflows/Test/badge.svg)](https://github.com/octokit/auth-token.js/actions?query=workflow%3ATest)
`@octokit/auth-token` is the simplest of [GitHubs authentication strategies](https://github.com/octokit/auth.js).
It is useful if you want to support multiple authentication strategies, as its API is compatible with its sibling packages for [basic](https://github.com/octokit/auth-basic.js), [GitHub App](https://github.com/octokit/auth-app.js) and [OAuth app](https://github.com/octokit/auth.js) authentication.
<!-- toc -->
- [Usage](#usage)
- [`createTokenAuth(token) options`](#createtokenauthtoken-options)
- [`auth()`](#auth)
- [Authentication object](#authentication-object)
- [`auth.hook(request, route, options)` or `auth.hook(request, options)`](#authhookrequest-route-options-or-authhookrequest-options)
- [Find more information](#find-more-information)
- [Find out what scopes are enabled for oauth tokens](#find-out-what-scopes-are-enabled-for-oauth-tokens)
- [Find out if token is a personal access token or if it belongs to an OAuth app](#find-out-if-token-is-a-personal-access-token-or-if-it-belongs-to-an-oauth-app)
- [Find out what permissions are enabled for a repository](#find-out-what-permissions-are-enabled-for-a-repository)
- [Use token for git operations](#use-token-for-git-operations)
- [License](#license)
<!-- tocstop -->
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load `@octokit/auth-token` directly from [esm.sh](https://esm.sh)
```html
<script type="module">
import { createTokenAuth } from "https://esm.sh/@octokit/auth-token";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with <code>npm install @octokit/auth-token</code>
```js
import { createTokenAuth } from "@octokit/auth-token";
```
</td></tr>
</tbody>
</table>
```js
const auth = createTokenAuth("ghp_PersonalAccessToken01245678900000000");
const authentication = await auth();
// {
// type: 'token',
// token: 'ghp_PersonalAccessToken01245678900000000',
// tokenType: 'oauth'
// }
```
## `createTokenAuth(token) options`
The `createTokenAuth` method accepts a single argument of type string, which is the token. The passed token can be one of the following:
- [Personal access token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)
- [OAuth access token](https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/)
- [GITHUB_TOKEN provided to GitHub Actions](https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables)
- Installation access token ([server-to-server](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-an-installation))
- User authentication for installation ([user-to-server](https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps))
Examples
```js
// Personal access token or OAuth access token
createTokenAuth("ghp_PersonalAccessToken01245678900000000");
// {
// type: 'token',
// token: 'ghp_PersonalAccessToken01245678900000000',
// tokenType: 'oauth'
// }
// Installation access token or GitHub Action token
createTokenAuth("ghs_InstallallationOrActionToken00000000");
// {
// type: 'token',
// token: 'ghs_InstallallationOrActionToken00000000',
// tokenType: 'installation'
// }
// Installation access token or GitHub Action token
createTokenAuth("ghu_InstallationUserToServer000000000000");
// {
// type: 'token',
// token: 'ghu_InstallationUserToServer000000000000',
// tokenType: 'user-to-server'
// }
```
## `auth()`
The `auth()` method has no options. It returns a promise which resolves with the the authentication object.
## Authentication object
<table width="100%">
<thead align=left>
<tr>
<th width=150>
name
</th>
<th width=70>
type
</th>
<th>
description
</th>
</tr>
</thead>
<tbody align=left valign=top>
<tr>
<th>
<code>type</code>
</th>
<th>
<code>string</code>
</th>
<td>
<code>"token"</code>
</td>
</tr>
<tr>
<th>
<code>token</code>
</th>
<th>
<code>string</code>
</th>
<td>
The provided token.
</td>
</tr>
<tr>
<th>
<code>tokenType</code>
</th>
<th>
<code>string</code>
</th>
<td>
Can be either <code>"oauth"</code> for personal access tokens and OAuth tokens, <code>"installation"</code> for installation access tokens (includes <code>GITHUB_TOKEN</code> provided to GitHub Actions), <code>"app"</code> for a GitHub App JSON Web Token, or <code>"user-to-server"</code> for a user authentication token through an app installation.
</td>
</tr>
</tbody>
</table>
## `auth.hook(request, route, options)` or `auth.hook(request, options)`
`auth.hook()` hooks directly into the request life cycle. It authenticates the request using the provided token.
The `request` option is an instance of [`@octokit/request`](https://github.com/octokit/request.js#readme). The `route`/`options` parameters are the same as for the [`request()` method](https://github.com/octokit/request.js#request).
`auth.hook()` can be called directly to send an authenticated request
```js
const { data: authorizations } = await auth.hook(
request,
"GET /authorizations",
);
```
Or it can be passed as option to [`request()`](https://github.com/octokit/request.js#request).
```js
const requestWithAuth = request.defaults({
request: {
hook: auth.hook,
},
});
const { data: authorizations } = await requestWithAuth("GET /authorizations");
```
## Find more information
`auth()` does not send any requests, it only transforms the provided token string into an authentication object.
Here is a list of things you can do to retrieve further information
### Find out what scopes are enabled for oauth tokens
Note that this does not work for installations. There is no way to retrieve permissions based on an installation access tokens.
```js
const TOKEN = "ghp_PersonalAccessToken01245678900000000";
const auth = createTokenAuth(TOKEN);
const authentication = await auth();
const response = await request("HEAD /");
const scopes = response.headers["x-oauth-scopes"].split(/,\s+/);
if (scopes.length) {
console.log(
`"${TOKEN}" has ${scopes.length} scopes enabled: ${scopes.join(", ")}`,
);
} else {
console.log(`"${TOKEN}" has no scopes enabled`);
}
```
### Find out if token is a personal access token or if it belongs to an OAuth app
```js
const TOKEN = "ghp_PersonalAccessToken01245678900000000";
const auth = createTokenAuth(TOKEN);
const authentication = await auth();
const response = await request("HEAD /");
const clientId = response.headers["x-oauth-client-id"];
if (clientId) {
console.log(
`"${token}" is an OAuth token, its apps client_id is ${clientId}.`,
);
} else {
console.log(`"${token}" is a personal access token`);
}
```
### Find out what permissions are enabled for a repository
Note that the `permissions` key is not set when authenticated using an installation access token.
```js
const TOKEN = "ghp_PersonalAccessToken01245678900000000";
const auth = createTokenAuth(TOKEN);
const authentication = await auth();
const response = await request("GET /repos/{owner}/{repo}", {
owner: "octocat",
repo: "hello-world",
});
console.log(response.data.permissions);
// {
// admin: true,
// push: true,
// pull: true
// }
```
### Use token for git operations
Both OAuth and installation access tokens can be used for git operations. However, when using with an installation, [the token must be prefixed with `x-access-token`](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation).
This example is using the [`execa`](https://github.com/sindresorhus/execa) package to run a `git push` command.
```js
const TOKEN = "ghp_PersonalAccessToken01245678900000000";
const auth = createTokenAuth(TOKEN);
const { token, tokenType } = await auth();
const tokenWithPrefix =
tokenType === "installation" ? `x-access-token:${token}` : token;
const repositoryUrl = `https://${tokenWithPrefix}@github.com/octocat/hello-world.git`;
const { stdout } = await execa("git", ["push", repositoryUrl]);
console.log(stdout);
```
## License
[MIT](LICENSE)

View file

@ -0,0 +1,55 @@
// pkg/dist-src/is-jwt.js
var b64url = "(?:[a-zA-Z0-9_-]+)";
var sep = "\\.";
var jwtRE = new RegExp(`^${b64url}${sep}${b64url}${sep}${b64url}$`);
var isJWT = jwtRE.test.bind(jwtRE);
// pkg/dist-src/auth.js
async function auth(token) {
const isApp = isJWT(token);
const isInstallation = token.startsWith("v1.") || token.startsWith("ghs_");
const isUserToServer = token.startsWith("ghu_");
const tokenType = isApp ? "app" : isInstallation ? "installation" : isUserToServer ? "user-to-server" : "oauth";
return {
type: "token",
token,
tokenType
};
}
// pkg/dist-src/with-authorization-prefix.js
function withAuthorizationPrefix(token) {
if (token.split(/\./).length === 3) {
return `bearer ${token}`;
}
return `token ${token}`;
}
// pkg/dist-src/hook.js
async function hook(token, request, route, parameters) {
const endpoint = request.endpoint.merge(
route,
parameters
);
endpoint.headers.authorization = withAuthorizationPrefix(token);
return request(endpoint);
}
// pkg/dist-src/index.js
var createTokenAuth = function createTokenAuth2(token) {
if (!token) {
throw new Error("[@octokit/auth-token] No token passed to createTokenAuth");
}
if (typeof token !== "string") {
throw new Error(
"[@octokit/auth-token] Token passed to createTokenAuth is not a string"
);
}
token = token.replace(/^(token|bearer) +/i, "");
return Object.assign(auth.bind(null, token), {
hook: hook.bind(null, token)
});
};
export {
createTokenAuth
};

View file

@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["../dist-src/is-jwt.js", "../dist-src/auth.js", "../dist-src/with-authorization-prefix.js", "../dist-src/hook.js", "../dist-src/index.js"],
"sourcesContent": ["const b64url = \"(?:[a-zA-Z0-9_-]+)\";\nconst sep = \"\\\\.\";\nconst jwtRE = new RegExp(`^${b64url}${sep}${b64url}${sep}${b64url}$`);\nconst isJWT = jwtRE.test.bind(jwtRE);\nexport {\n isJWT\n};\n", "import { isJWT } from \"./is-jwt.js\";\nasync function auth(token) {\n const isApp = isJWT(token);\n const isInstallation = token.startsWith(\"v1.\") || token.startsWith(\"ghs_\");\n const isUserToServer = token.startsWith(\"ghu_\");\n const tokenType = isApp ? \"app\" : isInstallation ? \"installation\" : isUserToServer ? \"user-to-server\" : \"oauth\";\n return {\n type: \"token\",\n token,\n tokenType\n };\n}\nexport {\n auth\n};\n", "function withAuthorizationPrefix(token) {\n if (token.split(/\\./).length === 3) {\n return `bearer ${token}`;\n }\n return `token ${token}`;\n}\nexport {\n withAuthorizationPrefix\n};\n", "import { withAuthorizationPrefix } from \"./with-authorization-prefix.js\";\nasync function hook(token, request, route, parameters) {\n const endpoint = request.endpoint.merge(\n route,\n parameters\n );\n endpoint.headers.authorization = withAuthorizationPrefix(token);\n return request(endpoint);\n}\nexport {\n hook\n};\n", "import { auth } from \"./auth.js\";\nimport { hook } from \"./hook.js\";\nconst createTokenAuth = function createTokenAuth2(token) {\n if (!token) {\n throw new Error(\"[@octokit/auth-token] No token passed to createTokenAuth\");\n }\n if (typeof token !== \"string\") {\n throw new Error(\n \"[@octokit/auth-token] Token passed to createTokenAuth is not a string\"\n );\n }\n token = token.replace(/^(token|bearer) +/i, \"\");\n return Object.assign(auth.bind(null, token), {\n hook: hook.bind(null, token)\n });\n};\nexport {\n createTokenAuth\n};\n"],
"mappings": ";AAAA,IAAM,SAAS;AACf,IAAM,MAAM;AACZ,IAAM,QAAQ,IAAI,OAAO,IAAI,MAAM,GAAG,GAAG,GAAG,MAAM,GAAG,GAAG,GAAG,MAAM,GAAG;AACpE,IAAM,QAAQ,MAAM,KAAK,KAAK,KAAK;;;ACFnC,eAAe,KAAK,OAAO;AACzB,QAAM,QAAQ,MAAM,KAAK;AACzB,QAAM,iBAAiB,MAAM,WAAW,KAAK,KAAK,MAAM,WAAW,MAAM;AACzE,QAAM,iBAAiB,MAAM,WAAW,MAAM;AAC9C,QAAM,YAAY,QAAQ,QAAQ,iBAAiB,iBAAiB,iBAAiB,mBAAmB;AACxG,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF;AACF;;;ACXA,SAAS,wBAAwB,OAAO;AACtC,MAAI,MAAM,MAAM,IAAI,EAAE,WAAW,GAAG;AAClC,WAAO,UAAU,KAAK;AAAA,EACxB;AACA,SAAO,SAAS,KAAK;AACvB;;;ACJA,eAAe,KAAK,OAAO,SAAS,OAAO,YAAY;AACrD,QAAM,WAAW,QAAQ,SAAS;AAAA,IAChC;AAAA,IACA;AAAA,EACF;AACA,WAAS,QAAQ,gBAAgB,wBAAwB,KAAK;AAC9D,SAAO,QAAQ,QAAQ;AACzB;;;ACNA,IAAM,kBAAkB,SAAS,iBAAiB,OAAO;AACvD,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,0DAA0D;AAAA,EAC5E;AACA,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,UAAQ,MAAM,QAAQ,sBAAsB,EAAE;AAC9C,SAAO,OAAO,OAAO,KAAK,KAAK,MAAM,KAAK,GAAG;AAAA,IAC3C,MAAM,KAAK,KAAK,MAAM,KAAK;AAAA,EAC7B,CAAC;AACH;",
"names": []
}

View file

@ -0,0 +1,15 @@
import { isJWT } from "./is-jwt.js";
async function auth(token) {
const isApp = isJWT(token);
const isInstallation = token.startsWith("v1.") || token.startsWith("ghs_");
const isUserToServer = token.startsWith("ghu_");
const tokenType = isApp ? "app" : isInstallation ? "installation" : isUserToServer ? "user-to-server" : "oauth";
return {
type: "token",
token,
tokenType
};
}
export {
auth
};

View file

@ -0,0 +1,12 @@
import { withAuthorizationPrefix } from "./with-authorization-prefix.js";
async function hook(token, request, route, parameters) {
const endpoint = request.endpoint.merge(
route,
parameters
);
endpoint.headers.authorization = withAuthorizationPrefix(token);
return request(endpoint);
}
export {
hook
};

View file

@ -0,0 +1,19 @@
import { auth } from "./auth.js";
import { hook } from "./hook.js";
const createTokenAuth = function createTokenAuth2(token) {
if (!token) {
throw new Error("[@octokit/auth-token] No token passed to createTokenAuth");
}
if (typeof token !== "string") {
throw new Error(
"[@octokit/auth-token] Token passed to createTokenAuth is not a string"
);
}
token = token.replace(/^(token|bearer) +/i, "");
return Object.assign(auth.bind(null, token), {
hook: hook.bind(null, token)
});
};
export {
createTokenAuth
};

View file

@ -0,0 +1,7 @@
const b64url = "(?:[a-zA-Z0-9_-]+)";
const sep = "\\.";
const jwtRE = new RegExp(`^${b64url}${sep}${b64url}${sep}${b64url}$`);
const isJWT = jwtRE.test.bind(jwtRE);
export {
isJWT
};

View file

@ -0,0 +1,9 @@
function withAuthorizationPrefix(token) {
if (token.split(/\./).length === 3) {
return `bearer ${token}`;
}
return `token ${token}`;
}
export {
withAuthorizationPrefix
};

View file

@ -0,0 +1,2 @@
import type { Token, Authentication } from "./types.js";
export declare function auth(token: Token): Promise<Authentication>;

View file

@ -0,0 +1,2 @@
import type { AnyResponse, EndpointOptions, RequestInterface, RequestParameters, Route, Token } from "./types.js";
export declare function hook(token: Token, request: RequestInterface, route: Route | EndpointOptions, parameters?: RequestParameters): Promise<AnyResponse>;

View file

@ -0,0 +1,7 @@
import type { StrategyInterface, Token, Authentication } from "./types.js";
export type Types = {
StrategyOptions: Token;
AuthOptions: never;
Authentication: Authentication;
};
export declare const createTokenAuth: StrategyInterface;

View file

@ -0,0 +1 @@
export declare const isJWT: (string: string) => boolean;

View file

@ -0,0 +1,33 @@
import type * as OctokitTypes from "@octokit/types";
export type AnyResponse = OctokitTypes.OctokitResponse<any>;
export type StrategyInterface = OctokitTypes.StrategyInterface<[
Token
], [
], Authentication>;
export type EndpointDefaults = OctokitTypes.EndpointDefaults;
export type EndpointOptions = OctokitTypes.EndpointOptions;
export type RequestParameters = OctokitTypes.RequestParameters;
export type RequestInterface = OctokitTypes.RequestInterface;
export type Route = OctokitTypes.Route;
export type Token = string;
export type OAuthTokenAuthentication = {
type: "token";
tokenType: "oauth";
token: Token;
};
export type InstallationTokenAuthentication = {
type: "token";
tokenType: "installation";
token: Token;
};
export type AppAuthentication = {
type: "token";
tokenType: "app";
token: Token;
};
export type UserToServerAuthentication = {
type: "token";
tokenType: "user-to-server";
token: Token;
};
export type Authentication = OAuthTokenAuthentication | InstallationTokenAuthentication | AppAuthentication | UserToServerAuthentication;

View file

@ -0,0 +1,6 @@
/**
* Prefix token for usage in the Authorization header
*
* @param token OAuth token or JSON Web Token
*/
export declare function withAuthorizationPrefix(token: string): string;

View file

@ -0,0 +1,51 @@
{
"name": "@octokit/auth-token",
"publishConfig": {
"access": "public",
"provenance": true
},
"type": "module",
"version": "5.1.2",
"description": "GitHub API token authentication for browsers and Node.js",
"repository": "github:octokit/auth-token.js",
"keywords": [
"github",
"octokit",
"authentication",
"api"
],
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"devDependencies": {
"@octokit/request": "^9.0.0",
"@octokit/tsconfig": "^4.0.0",
"@octokit/types": "^13.0.0",
"@vitest/coverage-v8": "^3.0.0",
"esbuild": "^0.24.0",
"fetch-mock": "^11.0.0",
"glob": "^11.0.0",
"prettier": "3.4.2",
"semantic-release": "^24.0.0",
"typescript": "^5.3.0",
"vitest": "^3.0.0"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist-*/**",
"bin/**"
],
"types": "./dist-types/index.d.ts",
"exports": {
".": {
"types": "./dist-types/index.d.ts",
"import": "./dist-bundle/index.js",
"default": "./dist-bundle/index.js"
},
"./types": {
"types": "./dist-types/index.d.ts"
}
},
"sideEffects": false
}

View file

@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2019 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,457 @@
# core.js
> Extendable client for GitHub's REST & GraphQL APIs
[![@latest](https://img.shields.io/npm/v/@octokit/core.svg)](https://www.npmjs.com/package/@octokit/core)
[![Build Status](https://github.com/octokit/core.js/workflows/Test/badge.svg)](https://github.com/octokit/core.js/actions?query=workflow%3ATest+branch%3Amain)
<!-- toc -->
- [Usage](#usage)
- [REST API example](#rest-api-example)
- [GraphQL example](#graphql-example)
- [Options](#options)
- [Defaults](#defaults)
- [Authentication](#authentication)
- [Logging](#logging)
- [Hooks](#hooks)
- [Plugins](#plugins)
- [Build your own Octokit with Plugins and Defaults](#build-your-own-octokit-with-plugins-and-defaults)
- [LICENSE](#license)
<!-- tocstop -->
If you need a minimalistic library to utilize GitHub's [REST API](https://developer.github.com/v3/) and [GraphQL API](https://developer.github.com/v4/) which you can extend with plugins as needed, then `@octokit/core` is a great starting point.
If you don't need the Plugin API then using [`@octokit/request`](https://github.com/octokit/request.js/) or [`@octokit/graphql`](https://github.com/octokit/graphql.js/) directly is a good alternative.
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load <code>@octokit/core</code> directly from <a href="https://esm.sh">esm.sh</a>
```html
<script type="module">
import { Octokit } from "https://esm.sh/@octokit/core";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with <code>npm install @octokit/core</code>
```js
import { Octokit } from "@octokit/core";
```
</td></tr>
</tbody>
</table>
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).
### REST API example
```js
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: `personal-access-token123` });
const response = await octokit.request("GET /orgs/{org}/repos", {
org: "octokit",
type: "private",
});
```
See [`@octokit/request`](https://github.com/octokit/request.js) for full documentation of the `.request` method.
### GraphQL example
```js
const octokit = new Octokit({ auth: `secret123` });
const response = await octokit.graphql(
`query ($login: String!) {
organization(login: $login) {
repositories(privacy: PRIVATE) {
totalCount
}
}
}`,
{ login: "octokit" },
);
```
See [`@octokit/graphql`](https://github.com/octokit/graphql.js) for full documentation of the `.graphql` method.
## Options
<table>
<thead align=left>
<tr>
<th>
name
</th>
<th>
type
</th>
<th width=100%>
description
</th>
</tr>
</thead>
<tbody align=left valign=top>
<tr>
<th>
<code>options.authStrategy</code>
</th>
<td>
<code>Function<code>
</td>
<td>
Defaults to <a href="https://github.com/octokit/auth-token.js#readme"><code>@octokit/auth-token</code></a>. See <a href="#authentication">Authentication</a> below for examples.
</td>
</tr>
<tr>
<th>
<code>options.auth</code>
</th>
<td>
<code>String</code> or <code>Object</code>
</td>
<td>
See <a href="#authentication">Authentication</a> below for examples.
</td>
</tr>
<tr>
<th>
<code>options.baseUrl</code>
</th>
<td>
<code>String</code>
</td>
<td>
When using with GitHub Enterprise Server, set `options.baseUrl` to the root URL of the API. For example, if your GitHub Enterprise Server's hostname is `github.acme-inc.com`, then set `options.baseUrl` to `https://github.acme-inc.com/api/v3`. Example
```js
const octokit = new Octokit({
baseUrl: "https://github.acme-inc.com/api/v3",
});
```
</td></tr>
<tr>
<th>
<code>options.previews</code>
</th>
<td>
<code>Array of Strings</code>
</td>
<td>
Some REST API endpoints require preview headers to be set, or enable
additional features. Preview headers can be set on a per-request basis, e.g.
```js
octokit.request("POST /repos/{owner}/{repo}/pulls", {
mediaType: {
previews: ["shadow-cat"],
},
owner,
repo,
title: "My pull request",
base: "main",
head: "my-feature",
draft: true,
});
```
You can also set previews globally, by setting the `options.previews` option on the constructor. Example:
```js
const octokit = new Octokit({
previews: ["shadow-cat"],
});
```
</td></tr>
<tr>
<th>
<code>options.request</code>
</th>
<td>
<code>Object</code>
</td>
<td>
Set a default request timeout (`options.request.timeout`) or an [`http(s).Agent`](https://nodejs.org/api/http.html#http_class_http_agent) e.g. for proxy usage (Node only, `options.request.agent`).
There are more `options.request.*` options, see [`@octokit/request` options](https://github.com/octokit/request.js#request). `options.request` can also be set on a per-request basis.
</td></tr>
<tr>
<th>
<code>options.timeZone</code>
</th>
<td>
<code>String</code>
</td>
<td>
Sets the `Time-Zone` header which defines a timezone according to the [list of names from the Olson database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
```js
const octokit = new Octokit({
timeZone: "America/Los_Angeles",
});
```
The time zone header will determine the timezone used for generating the timestamp when creating commits. See [GitHub's Timezones documentation](https://developer.github.com/v3/#timezones).
</td></tr>
<tr>
<th>
<code>options.userAgent</code>
</th>
<td>
<code>String</code>
</td>
<td>
A custom user agent string for your app or library. Example
```js
const octokit = new Octokit({
userAgent: "my-app/v1.2.3",
});
```
</td></tr>
</tbody>
</table>
## Defaults
You can create a new Octokit class with customized default options.
```js
const MyOctokit = Octokit.defaults({
auth: "personal-access-token123",
baseUrl: "https://github.acme-inc.com/api/v3",
userAgent: "my-app/v1.2.3",
});
const octokit1 = new MyOctokit();
const octokit2 = new MyOctokit();
```
If you pass additional options to your new constructor, the options will be merged shallowly.
```js
const MyOctokit = Octokit.defaults({
foo: {
opt1: 1,
},
});
const octokit = new MyOctokit({
foo: {
opt2: 1,
},
});
// options will be { foo: { opt2: 1 }}
```
If you need a deep or conditional merge, you can pass a function instead.
```js
const MyOctokit = Octokit.defaults((options) => {
return {
foo: Object.assign({}, options.foo, { opt1: 1 }),
};
});
const octokit = new MyOctokit({
foo: { opt2: 1 },
});
// options will be { foo: { opt1: 1, opt2: 1 }}
```
Be careful about mutating the `options` object in the `Octokit.defaults` callback, as it can have unforeseen consequences.
## Authentication
Authentication is optional for some REST API endpoints accessing public data, but is required for GraphQL queries. Using authentication also increases your [API rate limit](https://developer.github.com/v3/#rate-limiting).
By default, Octokit authenticates using the [token authentication strategy](https://github.com/octokit/auth-token.js). Pass in a token using `options.auth`. It can be a personal access token, an OAuth token, an installation access token or a JSON Web Token for GitHub App authentication. The `Authorization` header will be set according to the type of token.
```js
import { Octokit } from "@octokit/core";
const octokit = new Octokit({
auth: "mypersonalaccesstoken123",
});
const { data } = await octokit.request("/user");
```
To use a different authentication strategy, set `options.authStrategy`. A list of authentication strategies is available at [octokit/authentication-strategies.js](https://github.com/octokit/authentication-strategies.js/#readme).
Example
```js
import { Octokit } from "@octokit/core";
import { createAppAuth } from "@octokit/auth-app";
const appOctokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: 123,
privateKey: process.env.PRIVATE_KEY,
},
});
const { data } = await appOctokit.request("/app");
```
The `.auth()` method returned by the current authentication strategy can be accessed at `octokit.auth()`. Example
```js
const { token } = await appOctokit.auth({
type: "installation",
installationId: 123,
});
```
## Logging
There are four built-in log methods
1. `octokit.log.debug(message[, additionalInfo])`
1. `octokit.log.info(message[, additionalInfo])`
1. `octokit.log.warn(message[, additionalInfo])`
1. `octokit.log.error(message[, additionalInfo])`
They can be configured using the [`log` client option](client-options). By default, `octokit.log.debug()` and `octokit.log.info()` are no-ops, while the other two call `console.warn()` and `console.error()` respectively.
This is useful if you build reusable [plugins](#plugins).
If you would like to make the log level configurable using an environment variable or external option, we recommend the [console-log-level](https://github.com/watson/console-log-level) package. Example
```js
import consoleLogLevel from "console-log-level";
const octokit = new Octokit({
log: consoleLogLevel({ level: "info" }),
});
```
## Hooks
You can customize Octokit's request lifecycle with hooks.
```js
octokit.hook.before("request", async (options) => {
validate(options);
});
octokit.hook.after("request", async (response, options) => {
console.log(`${options.method} ${options.url}: ${response.status}`);
});
octokit.hook.error("request", async (error, options) => {
if (error.status === 304) {
return findInCache(error.response.headers.etag);
}
throw error;
});
octokit.hook.wrap("request", async (request, options) => {
// add logic before, after, catch errors or replace the request altogether
return request(options);
});
```
See [before-after-hook](https://github.com/gr2m/before-after-hook#readme) for more documentation on hooks.
## Plugins
Octokits functionality can be extended using plugins. The `Octokit.plugin()` method accepts a plugin (or many) and returns a new constructor.
A plugin is a function which gets two arguments:
1. the current instance
2. the options passed to the constructor.
In order to extend `octokit`'s API, the plugin must return an object with the new methods. Please refrain from adding methods directly to the `octokit` instance, especialy if you depend on keys that do not exist in `@octokit/core`, such as `octokit.rest`.
```js
// index.js
import { Octokit } from "@octokit/core";
import myPlugin from "./lib/my-plugin.js";
import octokitPluginExample from "octokit-plugin-example";
const MyOctokit = Octokit.plugin(
myPlugin,
octokitPluginExample
);
const octokit = new MyOctokit({ greeting: "Moin moin" });
octokit.helloWorld(); // logs "Moin moin, world!"
octokit.request("GET /"); // logs "GET / - 200 in 123ms"
// lib/my-plugin.js
const plugin = (octokit, options = { greeting: "Hello" }) => {
// hook into the request lifecycle
octokit.hook.wrap("request", async (request, options) => {
const time = Date.now();
const response = await request(options);
console.log(
`${options.method} ${options.url} ${response.status} in ${Date.now() -
time}ms`
);
return response;
});
// add a custom method
return {
helloWorld: () => console.log(`${options.greeting}, world!`);
}
};
export default plugin;
```
## Build your own Octokit with Plugins and Defaults
You can build your own Octokit class with preset default options and plugins. In fact, this is mostly how the `@octokit/<context>` modules work, such as [`@octokit/action`](https://github.com/octokit/action.js):
```js
import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { throttling } from "@octokit/plugin-throttling";
import { retry } from "@octokit/plugin-retry";
import { createActionAuth } from "@octokit/auth-action";
const MyActionOctokit = Octokit.plugin(
paginateRest,
throttling,
retry,
).defaults({
throttle: {
onAbuseLimit: (retryAfter, options) => {
/* ... */
},
onRateLimit: (retryAfter, options) => {
/* ... */
},
},
authStrategy: createActionAuth,
userAgent: `my-octokit-action/v1.2.3`,
});
const octokit = new MyActionOctokit();
const installations = await octokit.paginate("GET /app/installations");
```
## LICENSE
[MIT](LICENSE)

View file

@ -0,0 +1,134 @@
import { getUserAgent } from "universal-user-agent";
import Hook from "before-after-hook";
import { request } from "@octokit/request";
import { withCustomRequest } from "@octokit/graphql";
import { createTokenAuth } from "@octokit/auth-token";
import { VERSION } from "./version.js";
const noop = () => {
};
const consoleWarn = console.warn.bind(console);
const consoleError = console.error.bind(console);
const userAgentTrail = `octokit-core.js/${VERSION} ${getUserAgent()}`;
class Octokit {
static VERSION = VERSION;
static defaults(defaults) {
const OctokitWithDefaults = class extends this {
constructor(...args) {
const options = args[0] || {};
if (typeof defaults === "function") {
super(defaults(options));
return;
}
super(
Object.assign(
{},
defaults,
options,
options.userAgent && defaults.userAgent ? {
userAgent: `${options.userAgent} ${defaults.userAgent}`
} : null
)
);
}
};
return OctokitWithDefaults;
}
static plugins = [];
/**
* Attach a plugin (or many) to your Octokit instance.
*
* @example
* const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
*/
static plugin(...newPlugins) {
const currentPlugins = this.plugins;
const NewOctokit = class extends this {
static plugins = currentPlugins.concat(
newPlugins.filter((plugin) => !currentPlugins.includes(plugin))
);
};
return NewOctokit;
}
constructor(options = {}) {
const hook = new Hook.Collection();
const requestDefaults = {
baseUrl: request.endpoint.DEFAULTS.baseUrl,
headers: {},
request: Object.assign({}, options.request, {
// @ts-ignore internal usage only, no need to type
hook: hook.bind(null, "request")
}),
mediaType: {
previews: [],
format: ""
}
};
requestDefaults.headers["user-agent"] = options.userAgent ? `${options.userAgent} ${userAgentTrail}` : userAgentTrail;
if (options.baseUrl) {
requestDefaults.baseUrl = options.baseUrl;
}
if (options.previews) {
requestDefaults.mediaType.previews = options.previews;
}
if (options.timeZone) {
requestDefaults.headers["time-zone"] = options.timeZone;
}
this.request = request.defaults(requestDefaults);
this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
this.log = Object.assign(
{
debug: noop,
info: noop,
warn: consoleWarn,
error: consoleError
},
options.log
);
this.hook = hook;
if (!options.authStrategy) {
if (!options.auth) {
this.auth = async () => ({
type: "unauthenticated"
});
} else {
const auth = createTokenAuth(options.auth);
hook.wrap("request", auth.hook);
this.auth = auth;
}
} else {
const { authStrategy, ...otherOptions } = options;
const auth = authStrategy(
Object.assign(
{
request: this.request,
log: this.log,
// we pass the current octokit instance as well as its constructor options
// to allow for authentication strategies that return a new octokit instance
// that shares the same internal state as the current one. The original
// requirement for this was the "event-octokit" authentication strategy
// of https://github.com/probot/octokit-auth-probot.
octokit: this,
octokitOptions: otherOptions
},
options.auth
)
);
hook.wrap("request", auth.hook);
this.auth = auth;
}
const classConstructor = this.constructor;
for (let i = 0; i < classConstructor.plugins.length; ++i) {
Object.assign(this, classConstructor.plugins[i](this, options));
}
}
// assigned during constructor
request;
graphql;
log;
hook;
// TODO: type `octokit.auth` based on passed options.authStrategy
auth;
}
export {
Octokit
};

View file

@ -0,0 +1,4 @@
const VERSION = "6.1.4";
export {
VERSION
};

View file

@ -0,0 +1,31 @@
import type { HookCollection } from "before-after-hook";
import { request } from "@octokit/request";
import { type graphql } from "@octokit/graphql";
import type { Constructor, Hooks, OctokitOptions, OctokitPlugin, ReturnTypeOf, UnionToIntersection } from "./types.js";
export type { OctokitOptions } from "./types.js";
export declare class Octokit {
static VERSION: string;
static defaults<S extends Constructor<any>>(this: S, defaults: OctokitOptions | Function): S;
static plugins: OctokitPlugin[];
/**
* Attach a plugin (or many) to your Octokit instance.
*
* @example
* const API = Octokit.plugin(plugin1, plugin2, plugin3, ...)
*/
static plugin<S extends Constructor<any> & {
plugins: any[];
}, T extends OctokitPlugin[]>(this: S, ...newPlugins: T): S & Constructor<UnionToIntersection<ReturnTypeOf<T>>>;
constructor(options?: OctokitOptions);
request: typeof request;
graphql: typeof graphql;
log: {
debug: (message: string, additionalInfo?: object) => any;
info: (message: string, additionalInfo?: object) => any;
warn: (message: string, additionalInfo?: object) => any;
error: (message: string, additionalInfo?: object) => any;
[key: string]: any;
};
hook: HookCollection<Hooks>;
auth: (...args: unknown[]) => Promise<unknown>;
}

View file

@ -0,0 +1,44 @@
import type * as OctokitTypes from "@octokit/types";
import type { RequestError } from "@octokit/request-error";
import type { Octokit } from "./index.js";
export type RequestParameters = OctokitTypes.RequestParameters;
export interface OctokitOptions {
authStrategy?: any;
auth?: any;
userAgent?: string;
previews?: string[];
baseUrl?: string;
log?: {
debug: (message: string) => unknown;
info: (message: string) => unknown;
warn: (message: string) => unknown;
error: (message: string) => unknown;
};
request?: OctokitTypes.RequestRequestOptions;
timeZone?: string;
[option: string]: any;
}
export type Constructor<T> = new (...args: any[]) => T;
export type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> = T extends AnyFunction ? ReturnType<T> : T extends AnyFunction[] ? UnionToIntersection<Exclude<ReturnType<T[number]>, void>> : never;
/**
* @author https://stackoverflow.com/users/2887218/jcalz
* @see https://stackoverflow.com/a/50375286/10325032
*/
export type UnionToIntersection<Union> = (Union extends any ? (argument: Union) => void : never) extends (argument: infer Intersection) => void ? Intersection : never;
type AnyFunction = (...args: any) => any;
export type OctokitPlugin = (octokit: Octokit, options: OctokitOptions) => {
[key: string]: any;
} | void;
export type Hooks = {
request: {
Options: Required<OctokitTypes.EndpointDefaults>;
Result: OctokitTypes.OctokitResponse<any>;
Error: RequestError | Error;
};
[key: string]: {
Options: unknown;
Result: unknown;
Error: unknown;
};
};
export {};

View file

@ -0,0 +1 @@
export declare const VERSION = "6.1.4";

View file

@ -0,0 +1,69 @@
{
"name": "@octokit/core",
"version": "6.1.4",
"publishConfig": {
"access": "public",
"provenance": true
},
"type": "module",
"description": "Extendable client for GitHub's REST & GraphQL APIs",
"repository": "github:octokit/core.js",
"keywords": [
"octokit",
"github",
"api",
"sdk",
"toolkit"
],
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/auth-token": "^5.0.0",
"@octokit/graphql": "^8.1.2",
"@octokit/request": "^9.2.1",
"@octokit/request-error": "^6.1.7",
"@octokit/types": "^13.6.2",
"before-after-hook": "^3.0.2",
"universal-user-agent": "^7.0.0"
},
"devDependencies": {
"@octokit/auth-action": "^5.0.0",
"@octokit/auth-app": "^7.0.0",
"@octokit/auth-oauth-app": "^8.0.0",
"@octokit/tsconfig": "^4.0.0",
"@sinonjs/fake-timers": "^14.0.0",
"@types/lolex": "^5.1.0",
"@types/node": "^22.0.0",
"@types/sinonjs__fake-timers": "^8.1.5",
"@vitest/coverage-v8": "^3.0.5",
"esbuild": "^0.25.0",
"fetch-mock": "^12.0.0",
"glob": "^11.0.0",
"prettier": "3.4.2",
"proxy": "^2.0.0",
"semantic-release": "^24.0.0",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"typescript": "^5.0.0",
"undici": "^6.0.0",
"vitest": "^3.0.5"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist-*/**",
"bin/**"
],
"types": "./dist-types/index.d.ts",
"exports": {
".": {
"types": "./dist-types/index.d.ts",
"import": "./dist-src/index.js",
"default": "./dist-src/index.js"
},
"./types": {
"types": "./dist-types/types.d.ts"
}
},
"sideEffects": false
}

View file

@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2018 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,412 @@
# endpoint.js
> Turns GitHub REST API endpoints into generic request options
[![@latest](https://img.shields.io/npm/v/@octokit/endpoint.svg)](https://www.npmjs.com/package/@octokit/endpoint)
[![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)
`@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.
<!-- update table of contents by running `npx markdown-toc README.md -i` -->
<!-- toc -->
- [Usage](#usage)
- [API](#api)
- [`endpoint(route, options)` or `endpoint(options)`](#endpointroute-options-or-endpointoptions)
- [`endpoint.defaults()`](#endpointdefaults)
- [`endpoint.DEFAULTS`](#endpointdefaults)
- [`endpoint.merge(route, options)` or `endpoint.merge(options)`](#endpointmergeroute-options-or-endpointmergeoptions)
- [`endpoint.parse()`](#endpointparse)
- [Special cases](#special-cases)
- [The `data` parameter set request body directly](#the-data-parameter-%E2%80%93-set-request-body-directly)
- [Set parameters for both the URL/query and the request body](#set-parameters-for-both-the-urlquery-and-the-request-body)
- [LICENSE](#license)
<!-- tocstop -->
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load <code>@octokit/endpoint</code> directly from <a href="https://esm.sh">esm.sh</a>
```html
<script type="module">
import { endpoint } from "https://esm.sh/@octokit/endpoint";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with <code>npm install @octokit/endpoint</code>
```js
import { endpoint } from "@octokit/endpoint";
```
</td></tr>
</tbody>
</table>
Example for [List organization repositories](https://developer.github.com/v3/repos/#list-organization-repositories)
```js
const requestOptions = endpoint("GET /orgs/{org}/repos", {
headers: {
authorization: "token 0000000000000000000000000000000000000001",
},
org: "octokit",
type: "private",
});
```
The resulting `requestOptions` looks as follows
```json
{
"method": "GET",
"url": "https://api.github.com/orgs/octokit/repos?type=private",
"headers": {
"accept": "application/vnd.github.v3+json",
"authorization": "token 0000000000000000000000000000000000000001",
"user-agent": "octokit/endpoint.js v1.2.3"
}
}
```
You can pass `requestOptions` to common request libraries
```js
const { url, ...options } = requestOptions;
// using with fetch (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
fetch(url, options);
// using with request (https://github.com/request/request)
request(requestOptions);
// using with got (https://github.com/sindresorhus/got)
got[options.method](url, options);
// using with axios
axios(requestOptions);
```
> [!IMPORTANT]
> 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).
## API
### `endpoint(route, options)` or `endpoint(options)`
<table>
<thead align=left>
<tr>
<th>
name
</th>
<th>
type
</th>
<th width=100%>
description
</th>
</tr>
</thead>
<tbody align=left valign=top>
<tr>
<th>
<code>route</code>
</th>
<td>
String
</td>
<td>
If set, it has to be a string consisting of URL and the request method, e.g., <code>GET /orgs/{org}</code>. If its set to a URL, only the method defaults to <code>GET</code>.
</td>
</tr>
<tr>
<th>
<code>options.method</code>
</th>
<td>
String
</td>
<td>
<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>.
</td>
</tr>
<tr>
<th>
<code>options.url</code>
</th>
<td>
String
</td>
<td>
<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,
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>.
</td>
</tr>
<tr>
<th>
<code>options.baseUrl</code>
</th>
<td>
String
</td>
<td>
<em>Defaults to <code>https://api.github.com</code></em>.
</td>
</tr>
<tr>
<th>
<code>options.headers</code>
</th>
<td>
Object
</td>
<td>
Custom headers. Passed headers are merged with defaults:<br>
<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>
<em><code>headers['accept']</code> defaults to <code>application/vnd.github.v3+json</code></em>.<br>
</td>
</tr>
<tr>
<th>
<code>options.mediaType.format</code>
</th>
<td>
String
</td>
<td>
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.
</td>
</tr>
<tr>
<th>
<code>options.data</code>
</th>
<td>
Any
</td>
<td>
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.
</td>
</tr>
<tr>
<th>
<code>options.request</code>
</th>
<td>
Object
</td>
<td>
Pass custom meta information for the request. The <code>request</code> object will be returned as is.
</td>
</tr>
</tbody>
</table>
All other options will be passed depending on the `method` and `url` options.
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`.
2. If the `method` is `GET` or `HEAD`, the option is passed as a query parameter.
3. Otherwise, the parameter is passed in the request body as a JSON key.
**Result**
`endpoint()` is a synchronous method and returns an object with the following keys:
<table>
<thead align=left>
<tr>
<th>
key
</th>
<th>
type
</th>
<th width=100%>
description
</th>
</tr>
</thead>
<tbody align=left valign=top>
<tr>
<th><code>method</code></th>
<td>String</td>
<td>The http method. Always lowercase.</td>
</tr>
<tr>
<th><code>url</code></th>
<td>String</td>
<td>The url with placeholders replaced with passed parameters.</td>
</tr>
<tr>
<th><code>headers</code></th>
<td>Object</td>
<td>All header names are lowercased.</td>
</tr>
<tr>
<th><code>body</code></th>
<td>Any</td>
<td>The request body if one is present. Only for <code>PATCH</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code> requests.</td>
</tr>
<tr>
<th><code>request</code></th>
<td>Object</td>
<td>Request meta option, it will be returned as it was passed into <code>endpoint()</code></td>
</tr>
</tbody>
</table>
### `endpoint.defaults()`
Override or set default options. Example:
```js
const request = require("request");
const myEndpoint = require("@octokit/endpoint").defaults({
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
headers: {
"user-agent": "myApp/1.2.3",
authorization: `token 0000000000000000000000000000000000000001`,
},
org: "my-project",
per_page: 100,
});
request(myEndpoint(`GET /orgs/{org}/repos`));
```
You can call `.defaults()` again on the returned method, the defaults will cascade.
```js
const myProjectEndpoint = endpoint.defaults({
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
headers: {
"user-agent": "myApp/1.2.3",
},
org: "my-project",
});
const myProjectEndpointWithAuth = myProjectEndpoint.defaults({
headers: {
authorization: `token 0000000000000000000000000000000000000001`,
},
});
```
`myProjectEndpointWithAuth` now defaults the `baseUrl`, `headers['user-agent']`,
`org` and `headers['authorization']` on top of `headers['accept']` that is set
by the global default.
### `endpoint.DEFAULTS`
The current default options.
```js
endpoint.DEFAULTS.baseUrl; // https://api.github.com
const myEndpoint = endpoint.defaults({
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
});
myEndpoint.DEFAULTS.baseUrl; // https://github-enterprise.acme-inc.com/api/v3
```
### `endpoint.merge(route, options)` or `endpoint.merge(options)`
Get the defaulted endpoint options, but without parsing them into request options:
```js
const myProjectEndpoint = endpoint.defaults({
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
headers: {
"user-agent": "myApp/1.2.3",
},
org: "my-project",
});
myProjectEndpoint.merge("GET /orgs/{org}/repos", {
headers: {
authorization: `token 0000000000000000000000000000000000000001`,
},
org: "my-secret-project",
type: "private",
});
// {
// baseUrl: 'https://github-enterprise.acme-inc.com/api/v3',
// method: 'GET',
// url: '/orgs/{org}/repos',
// headers: {
// accept: 'application/vnd.github.v3+json',
// authorization: `token 0000000000000000000000000000000000000001`,
// 'user-agent': 'myApp/1.2.3'
// },
// org: 'my-secret-project',
// type: 'private'
// }
```
### `endpoint.parse()`
Stateless method to turn endpoint options into request options. Calling
`endpoint(options)` is the same as calling `endpoint.parse(endpoint.merge(options))`.
## Special cases
<a name="data-parameter"></a>
### The `data` parameter set request body directly
Some endpoints such as [Render a Markdown document in raw mode](https://developer.github.com/v3/markdown/#render-a-markdown-document-in-raw-mode) dont 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.
```js
const options = endpoint("POST /markdown/raw", {
data: "Hello world github/linguist#1 **cool**, and #1!",
headers: {
accept: "text/html;charset=utf-8",
"content-type": "text/plain",
},
});
// options is
// {
// method: 'post',
// url: 'https://api.github.com/markdown/raw',
// headers: {
// accept: 'text/html;charset=utf-8',
// 'content-type': 'text/plain',
// 'user-agent': userAgent
// },
// body: 'Hello world github/linguist#1 **cool**, and #1!'
// }
```
### Set parameters for both the URL/query and the request body
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).
Example
```js
endpoint(
"POST https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
{
name: "example.zip",
label: "short description",
headers: {
"content-type": "text/plain",
"content-length": 14,
authorization: `token 0000000000000000000000000000000000000001`,
},
data: "Hello, world!",
},
);
```
## LICENSE
[MIT](LICENSE)

View file

@ -0,0 +1,346 @@
// pkg/dist-src/defaults.js
import { getUserAgent } from "universal-user-agent";
// pkg/dist-src/version.js
var VERSION = "0.0.0-development";
// pkg/dist-src/defaults.js
var userAgent = `octokit-endpoint.js/${VERSION} ${getUserAgent()}`;
var DEFAULTS = {
method: "GET",
baseUrl: "https://api.github.com",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent
},
mediaType: {
format: ""
}
};
// pkg/dist-src/util/lowercase-keys.js
function lowercaseKeys(object) {
if (!object) {
return {};
}
return Object.keys(object).reduce((newObj, key) => {
newObj[key.toLowerCase()] = object[key];
return newObj;
}, {});
}
// pkg/dist-src/util/is-plain-object.js
function isPlainObject(value) {
if (typeof value !== "object" || value === null) return false;
if (Object.prototype.toString.call(value) !== "[object Object]") return false;
const proto = Object.getPrototypeOf(value);
if (proto === null) return true;
const Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof Ctor === "function" && Ctor instanceof Ctor && Function.prototype.call(Ctor) === Function.prototype.call(value);
}
// pkg/dist-src/util/merge-deep.js
function mergeDeep(defaults, options) {
const result = Object.assign({}, defaults);
Object.keys(options).forEach((key) => {
if (isPlainObject(options[key])) {
if (!(key in defaults)) Object.assign(result, { [key]: options[key] });
else result[key] = mergeDeep(defaults[key], options[key]);
} else {
Object.assign(result, { [key]: options[key] });
}
});
return result;
}
// pkg/dist-src/util/remove-undefined-properties.js
function removeUndefinedProperties(obj) {
for (const key in obj) {
if (obj[key] === void 0) {
delete obj[key];
}
}
return obj;
}
// pkg/dist-src/merge.js
function merge(defaults, route, options) {
if (typeof route === "string") {
let [method, url] = route.split(" ");
options = Object.assign(url ? { method, url } : { url: method }, options);
} else {
options = Object.assign({}, route);
}
options.headers = lowercaseKeys(options.headers);
removeUndefinedProperties(options);
removeUndefinedProperties(options.headers);
const mergedOptions = mergeDeep(defaults || {}, options);
if (options.url === "/graphql") {
if (defaults && defaults.mediaType.previews?.length) {
mergedOptions.mediaType.previews = defaults.mediaType.previews.filter(
(preview) => !mergedOptions.mediaType.previews.includes(preview)
).concat(mergedOptions.mediaType.previews);
}
mergedOptions.mediaType.previews = (mergedOptions.mediaType.previews || []).map((preview) => preview.replace(/-preview/, ""));
}
return mergedOptions;
}
// pkg/dist-src/util/add-query-parameters.js
function addQueryParameters(url, parameters) {
const separator = /\?/.test(url) ? "&" : "?";
const names = Object.keys(parameters);
if (names.length === 0) {
return url;
}
return url + separator + names.map((name) => {
if (name === "q") {
return "q=" + parameters.q.split("+").map(encodeURIComponent).join("+");
}
return `${name}=${encodeURIComponent(parameters[name])}`;
}).join("&");
}
// pkg/dist-src/util/extract-url-variable-names.js
var urlVariableRegex = /\{[^{}}]+\}/g;
function removeNonChars(variableName) {
return variableName.replace(/(?:^\W+)|(?:(?<!\W)\W+$)/g, "").split(/,/);
}
function extractUrlVariableNames(url) {
const matches = url.match(urlVariableRegex);
if (!matches) {
return [];
}
return matches.map(removeNonChars).reduce((a, b) => a.concat(b), []);
}
// pkg/dist-src/util/omit.js
function omit(object, keysToOmit) {
const result = { __proto__: null };
for (const key of Object.keys(object)) {
if (keysToOmit.indexOf(key) === -1) {
result[key] = object[key];
}
}
return result;
}
// pkg/dist-src/util/url-template.js
function encodeReserved(str) {
return str.split(/(%[0-9A-Fa-f]{2})/g).map(function(part) {
if (!/%[0-9A-Fa-f]/.test(part)) {
part = encodeURI(part).replace(/%5B/g, "[").replace(/%5D/g, "]");
}
return part;
}).join("");
}
function encodeUnreserved(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return "%" + c.charCodeAt(0).toString(16).toUpperCase();
});
}
function encodeValue(operator, value, key) {
value = operator === "+" || operator === "#" ? encodeReserved(value) : encodeUnreserved(value);
if (key) {
return encodeUnreserved(key) + "=" + value;
} else {
return value;
}
}
function isDefined(value) {
return value !== void 0 && value !== null;
}
function isKeyOperator(operator) {
return operator === ";" || operator === "&" || operator === "?";
}
function getValues(context, operator, key, modifier) {
var value = context[key], result = [];
if (isDefined(value) && value !== "") {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
value = value.toString();
if (modifier && modifier !== "*") {
value = value.substring(0, parseInt(modifier, 10));
}
result.push(
encodeValue(operator, value, isKeyOperator(operator) ? key : "")
);
} else {
if (modifier === "*") {
if (Array.isArray(value)) {
value.filter(isDefined).forEach(function(value2) {
result.push(
encodeValue(operator, value2, isKeyOperator(operator) ? key : "")
);
});
} else {
Object.keys(value).forEach(function(k) {
if (isDefined(value[k])) {
result.push(encodeValue(operator, value[k], k));
}
});
}
} else {
const tmp = [];
if (Array.isArray(value)) {
value.filter(isDefined).forEach(function(value2) {
tmp.push(encodeValue(operator, value2));
});
} else {
Object.keys(value).forEach(function(k) {
if (isDefined(value[k])) {
tmp.push(encodeUnreserved(k));
tmp.push(encodeValue(operator, value[k].toString()));
}
});
}
if (isKeyOperator(operator)) {
result.push(encodeUnreserved(key) + "=" + tmp.join(","));
} else if (tmp.length !== 0) {
result.push(tmp.join(","));
}
}
}
} else {
if (operator === ";") {
if (isDefined(value)) {
result.push(encodeUnreserved(key));
}
} else if (value === "" && (operator === "&" || operator === "?")) {
result.push(encodeUnreserved(key) + "=");
} else if (value === "") {
result.push("");
}
}
return result;
}
function parseUrl(template) {
return {
expand: expand.bind(null, template)
};
}
function expand(template, context) {
var operators = ["+", "#", ".", "/", ";", "?", "&"];
template = template.replace(
/\{([^\{\}]+)\}|([^\{\}]+)/g,
function(_, expression, literal) {
if (expression) {
let operator = "";
const values = [];
if (operators.indexOf(expression.charAt(0)) !== -1) {
operator = expression.charAt(0);
expression = expression.substr(1);
}
expression.split(/,/g).forEach(function(variable) {
var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable);
values.push(getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
});
if (operator && operator !== "+") {
var separator = ",";
if (operator === "?") {
separator = "&";
} else if (operator !== "#") {
separator = operator;
}
return (values.length !== 0 ? operator : "") + values.join(separator);
} else {
return values.join(",");
}
} else {
return encodeReserved(literal);
}
}
);
if (template === "/") {
return template;
} else {
return template.replace(/\/$/, "");
}
}
// pkg/dist-src/parse.js
function parse(options) {
let method = options.method.toUpperCase();
let url = (options.url || "/").replace(/:([a-z]\w+)/g, "{$1}");
let headers = Object.assign({}, options.headers);
let body;
let parameters = omit(options, [
"method",
"baseUrl",
"url",
"headers",
"request",
"mediaType"
]);
const urlVariableNames = extractUrlVariableNames(url);
url = parseUrl(url).expand(parameters);
if (!/^http/.test(url)) {
url = options.baseUrl + url;
}
const omittedParameters = Object.keys(options).filter((option) => urlVariableNames.includes(option)).concat("baseUrl");
const remainingParameters = omit(parameters, omittedParameters);
const isBinaryRequest = /application\/octet-stream/i.test(headers.accept);
if (!isBinaryRequest) {
if (options.mediaType.format) {
headers.accept = headers.accept.split(/,/).map(
(format) => format.replace(
/application\/vnd(\.\w+)(\.v3)?(\.\w+)?(\+json)?$/,
`application/vnd$1$2.${options.mediaType.format}`
)
).join(",");
}
if (url.endsWith("/graphql")) {
if (options.mediaType.previews?.length) {
const previewsFromAcceptHeader = headers.accept.match(/(?<![\w-])[\w-]+(?=-preview)/g) || [];
headers.accept = previewsFromAcceptHeader.concat(options.mediaType.previews).map((preview) => {
const format = options.mediaType.format ? `.${options.mediaType.format}` : "+json";
return `application/vnd.github.${preview}-preview${format}`;
}).join(",");
}
}
}
if (["GET", "HEAD"].includes(method)) {
url = addQueryParameters(url, remainingParameters);
} else {
if ("data" in remainingParameters) {
body = remainingParameters.data;
} else {
if (Object.keys(remainingParameters).length) {
body = remainingParameters;
}
}
}
if (!headers["content-type"] && typeof body !== "undefined") {
headers["content-type"] = "application/json; charset=utf-8";
}
if (["PATCH", "PUT"].includes(method) && typeof body === "undefined") {
body = "";
}
return Object.assign(
{ method, url, headers },
typeof body !== "undefined" ? { body } : null,
options.request ? { request: options.request } : null
);
}
// pkg/dist-src/endpoint-with-defaults.js
function endpointWithDefaults(defaults, route, options) {
return parse(merge(defaults, route, options));
}
// pkg/dist-src/with-defaults.js
function withDefaults(oldDefaults, newDefaults) {
const DEFAULTS2 = merge(oldDefaults, newDefaults);
const endpoint2 = endpointWithDefaults.bind(null, DEFAULTS2);
return Object.assign(endpoint2, {
DEFAULTS: DEFAULTS2,
defaults: withDefaults.bind(null, DEFAULTS2),
merge: merge.bind(null, DEFAULTS2),
parse
});
}
// pkg/dist-src/index.js
var endpoint = withDefaults(null, DEFAULTS);
export {
endpoint
};

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,17 @@
import { getUserAgent } from "universal-user-agent";
import { VERSION } from "./version.js";
const userAgent = `octokit-endpoint.js/${VERSION} ${getUserAgent()}`;
const DEFAULTS = {
method: "GET",
baseUrl: "https://api.github.com",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent
},
mediaType: {
format: ""
}
};
export {
DEFAULTS
};

View file

@ -0,0 +1,9 @@
import { DEFAULTS } from "./defaults.js";
import { merge } from "./merge.js";
import { parse } from "./parse.js";
function endpointWithDefaults(defaults, route, options) {
return parse(merge(defaults, route, options));
}
export {
endpointWithDefaults
};

View file

@ -0,0 +1,6 @@
import { withDefaults } from "./with-defaults.js";
import { DEFAULTS } from "./defaults.js";
const endpoint = withDefaults(null, DEFAULTS);
export {
endpoint
};

View file

@ -0,0 +1,27 @@
import { lowercaseKeys } from "./util/lowercase-keys.js";
import { mergeDeep } from "./util/merge-deep.js";
import { removeUndefinedProperties } from "./util/remove-undefined-properties.js";
function merge(defaults, route, options) {
if (typeof route === "string") {
let [method, url] = route.split(" ");
options = Object.assign(url ? { method, url } : { url: method }, options);
} else {
options = Object.assign({}, route);
}
options.headers = lowercaseKeys(options.headers);
removeUndefinedProperties(options);
removeUndefinedProperties(options.headers);
const mergedOptions = mergeDeep(defaults || {}, options);
if (options.url === "/graphql") {
if (defaults && defaults.mediaType.previews?.length) {
mergedOptions.mediaType.previews = defaults.mediaType.previews.filter(
(preview) => !mergedOptions.mediaType.previews.includes(preview)
).concat(mergedOptions.mediaType.previews);
}
mergedOptions.mediaType.previews = (mergedOptions.mediaType.previews || []).map((preview) => preview.replace(/-preview/, ""));
}
return mergedOptions;
}
export {
merge
};

View file

@ -0,0 +1,70 @@
import { addQueryParameters } from "./util/add-query-parameters.js";
import { extractUrlVariableNames } from "./util/extract-url-variable-names.js";
import { omit } from "./util/omit.js";
import { parseUrl } from "./util/url-template.js";
function parse(options) {
let method = options.method.toUpperCase();
let url = (options.url || "/").replace(/:([a-z]\w+)/g, "{$1}");
let headers = Object.assign({}, options.headers);
let body;
let parameters = omit(options, [
"method",
"baseUrl",
"url",
"headers",
"request",
"mediaType"
]);
const urlVariableNames = extractUrlVariableNames(url);
url = parseUrl(url).expand(parameters);
if (!/^http/.test(url)) {
url = options.baseUrl + url;
}
const omittedParameters = Object.keys(options).filter((option) => urlVariableNames.includes(option)).concat("baseUrl");
const remainingParameters = omit(parameters, omittedParameters);
const isBinaryRequest = /application\/octet-stream/i.test(headers.accept);
if (!isBinaryRequest) {
if (options.mediaType.format) {
headers.accept = headers.accept.split(/,/).map(
(format) => format.replace(
/application\/vnd(\.\w+)(\.v3)?(\.\w+)?(\+json)?$/,
`application/vnd$1$2.${options.mediaType.format}`
)
).join(",");
}
if (url.endsWith("/graphql")) {
if (options.mediaType.previews?.length) {
const previewsFromAcceptHeader = headers.accept.match(/(?<![\w-])[\w-]+(?=-preview)/g) || [];
headers.accept = previewsFromAcceptHeader.concat(options.mediaType.previews).map((preview) => {
const format = options.mediaType.format ? `.${options.mediaType.format}` : "+json";
return `application/vnd.github.${preview}-preview${format}`;
}).join(",");
}
}
}
if (["GET", "HEAD"].includes(method)) {
url = addQueryParameters(url, remainingParameters);
} else {
if ("data" in remainingParameters) {
body = remainingParameters.data;
} else {
if (Object.keys(remainingParameters).length) {
body = remainingParameters;
}
}
}
if (!headers["content-type"] && typeof body !== "undefined") {
headers["content-type"] = "application/json; charset=utf-8";
}
if (["PATCH", "PUT"].includes(method) && typeof body === "undefined") {
body = "";
}
return Object.assign(
{ method, url, headers },
typeof body !== "undefined" ? { body } : null,
options.request ? { request: options.request } : null
);
}
export {
parse
};

View file

@ -0,0 +1,16 @@
function addQueryParameters(url, parameters) {
const separator = /\?/.test(url) ? "&" : "?";
const names = Object.keys(parameters);
if (names.length === 0) {
return url;
}
return url + separator + names.map((name) => {
if (name === "q") {
return "q=" + parameters.q.split("+").map(encodeURIComponent).join("+");
}
return `${name}=${encodeURIComponent(parameters[name])}`;
}).join("&");
}
export {
addQueryParameters
};

View file

@ -0,0 +1,14 @@
const urlVariableRegex = /\{[^{}}]+\}/g;
function removeNonChars(variableName) {
return variableName.replace(/(?:^\W+)|(?:(?<!\W)\W+$)/g, "").split(/,/);
}
function extractUrlVariableNames(url) {
const matches = url.match(urlVariableRegex);
if (!matches) {
return [];
}
return matches.map(removeNonChars).reduce((a, b) => a.concat(b), []);
}
export {
extractUrlVariableNames
};

View file

@ -0,0 +1,11 @@
function isPlainObject(value) {
if (typeof value !== "object" || value === null) return false;
if (Object.prototype.toString.call(value) !== "[object Object]") return false;
const proto = Object.getPrototypeOf(value);
if (proto === null) return true;
const Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof Ctor === "function" && Ctor instanceof Ctor && Function.prototype.call(Ctor) === Function.prototype.call(value);
}
export {
isPlainObject
};

View file

@ -0,0 +1,12 @@
function lowercaseKeys(object) {
if (!object) {
return {};
}
return Object.keys(object).reduce((newObj, key) => {
newObj[key.toLowerCase()] = object[key];
return newObj;
}, {});
}
export {
lowercaseKeys
};

View file

@ -0,0 +1,16 @@
import { isPlainObject } from "./is-plain-object.js";
function mergeDeep(defaults, options) {
const result = Object.assign({}, defaults);
Object.keys(options).forEach((key) => {
if (isPlainObject(options[key])) {
if (!(key in defaults)) Object.assign(result, { [key]: options[key] });
else result[key] = mergeDeep(defaults[key], options[key]);
} else {
Object.assign(result, { [key]: options[key] });
}
});
return result;
}
export {
mergeDeep
};

View file

@ -0,0 +1,12 @@
function omit(object, keysToOmit) {
const result = { __proto__: null };
for (const key of Object.keys(object)) {
if (keysToOmit.indexOf(key) === -1) {
result[key] = object[key];
}
}
return result;
}
export {
omit
};

View file

@ -0,0 +1,11 @@
function removeUndefinedProperties(obj) {
for (const key in obj) {
if (obj[key] === void 0) {
delete obj[key];
}
}
return obj;
}
export {
removeUndefinedProperties
};

View file

@ -0,0 +1,133 @@
function encodeReserved(str) {
return str.split(/(%[0-9A-Fa-f]{2})/g).map(function(part) {
if (!/%[0-9A-Fa-f]/.test(part)) {
part = encodeURI(part).replace(/%5B/g, "[").replace(/%5D/g, "]");
}
return part;
}).join("");
}
function encodeUnreserved(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return "%" + c.charCodeAt(0).toString(16).toUpperCase();
});
}
function encodeValue(operator, value, key) {
value = operator === "+" || operator === "#" ? encodeReserved(value) : encodeUnreserved(value);
if (key) {
return encodeUnreserved(key) + "=" + value;
} else {
return value;
}
}
function isDefined(value) {
return value !== void 0 && value !== null;
}
function isKeyOperator(operator) {
return operator === ";" || operator === "&" || operator === "?";
}
function getValues(context, operator, key, modifier) {
var value = context[key], result = [];
if (isDefined(value) && value !== "") {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
value = value.toString();
if (modifier && modifier !== "*") {
value = value.substring(0, parseInt(modifier, 10));
}
result.push(
encodeValue(operator, value, isKeyOperator(operator) ? key : "")
);
} else {
if (modifier === "*") {
if (Array.isArray(value)) {
value.filter(isDefined).forEach(function(value2) {
result.push(
encodeValue(operator, value2, isKeyOperator(operator) ? key : "")
);
});
} else {
Object.keys(value).forEach(function(k) {
if (isDefined(value[k])) {
result.push(encodeValue(operator, value[k], k));
}
});
}
} else {
const tmp = [];
if (Array.isArray(value)) {
value.filter(isDefined).forEach(function(value2) {
tmp.push(encodeValue(operator, value2));
});
} else {
Object.keys(value).forEach(function(k) {
if (isDefined(value[k])) {
tmp.push(encodeUnreserved(k));
tmp.push(encodeValue(operator, value[k].toString()));
}
});
}
if (isKeyOperator(operator)) {
result.push(encodeUnreserved(key) + "=" + tmp.join(","));
} else if (tmp.length !== 0) {
result.push(tmp.join(","));
}
}
}
} else {
if (operator === ";") {
if (isDefined(value)) {
result.push(encodeUnreserved(key));
}
} else if (value === "" && (operator === "&" || operator === "?")) {
result.push(encodeUnreserved(key) + "=");
} else if (value === "") {
result.push("");
}
}
return result;
}
function parseUrl(template) {
return {
expand: expand.bind(null, template)
};
}
function expand(template, context) {
var operators = ["+", "#", ".", "/", ";", "?", "&"];
template = template.replace(
/\{([^\{\}]+)\}|([^\{\}]+)/g,
function(_, expression, literal) {
if (expression) {
let operator = "";
const values = [];
if (operators.indexOf(expression.charAt(0)) !== -1) {
operator = expression.charAt(0);
expression = expression.substr(1);
}
expression.split(/,/g).forEach(function(variable) {
var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable);
values.push(getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
});
if (operator && operator !== "+") {
var separator = ",";
if (operator === "?") {
separator = "&";
} else if (operator !== "#") {
separator = operator;
}
return (values.length !== 0 ? operator : "") + values.join(separator);
} else {
return values.join(",");
}
} else {
return encodeReserved(literal);
}
}
);
if (template === "/") {
return template;
} else {
return template.replace(/\/$/, "");
}
}
export {
parseUrl
};

View file

@ -0,0 +1,4 @@
const VERSION = "10.1.3";
export {
VERSION
};

View file

@ -0,0 +1,16 @@
import { endpointWithDefaults } from "./endpoint-with-defaults.js";
import { merge } from "./merge.js";
import { parse } from "./parse.js";
function withDefaults(oldDefaults, newDefaults) {
const DEFAULTS = merge(oldDefaults, newDefaults);
const endpoint = endpointWithDefaults.bind(null, DEFAULTS);
return Object.assign(endpoint, {
DEFAULTS,
defaults: withDefaults.bind(null, DEFAULTS),
merge: merge.bind(null, DEFAULTS),
parse
});
}
export {
withDefaults
};

View file

@ -0,0 +1,2 @@
import type { EndpointDefaults } from "@octokit/types";
export declare const DEFAULTS: EndpointDefaults;

View file

@ -0,0 +1,3 @@
import type { EndpointOptions, RequestParameters, Route } from "@octokit/types";
import { DEFAULTS } from "./defaults.js";
export declare function endpointWithDefaults(defaults: typeof DEFAULTS, route: Route | EndpointOptions, options?: RequestParameters): import("@octokit/types").RequestOptions;

View file

@ -0,0 +1 @@
export declare const endpoint: import("@octokit/types").EndpointInterface<object>;

View file

@ -0,0 +1,2 @@
import type { EndpointDefaults, RequestParameters, Route } from "@octokit/types";
export declare function merge(defaults: EndpointDefaults | null, route?: Route | RequestParameters, options?: RequestParameters): EndpointDefaults;

View file

@ -0,0 +1,2 @@
import type { EndpointDefaults, RequestOptions } from "@octokit/types";
export declare function parse(options: EndpointDefaults): RequestOptions;

View file

@ -0,0 +1,4 @@
export declare function addQueryParameters(url: string, parameters: {
[x: string]: string | undefined;
q?: string;
}): string;

View file

@ -0,0 +1 @@
export declare function extractUrlVariableNames(url: string): string[];

View file

@ -0,0 +1 @@
export declare function isPlainObject(value: unknown): value is Object;

View file

@ -0,0 +1,5 @@
export declare function lowercaseKeys(object?: {
[key: string]: any;
}): {
[key: string]: any;
};

View file

@ -0,0 +1 @@
export declare function mergeDeep(defaults: any, options: any): object;

View file

@ -0,0 +1,5 @@
export declare function omit(object: {
[key: string]: any;
}, keysToOmit: string[]): {
[key: string]: any;
};

View file

@ -0,0 +1 @@
export declare function removeUndefinedProperties(obj: any): any;

View file

@ -0,0 +1,3 @@
export declare function parseUrl(template: string): {
expand: (context: object) => string;
};

View file

@ -0,0 +1 @@
export declare const VERSION = "10.1.3";

View file

@ -0,0 +1,2 @@
import type { EndpointInterface, RequestParameters, EndpointDefaults } from "@octokit/types";
export declare function withDefaults(oldDefaults: EndpointDefaults | null, newDefaults: RequestParameters): EndpointInterface;

View file

@ -0,0 +1,51 @@
{
"name": "@octokit/endpoint",
"version": "10.1.3",
"type": "module",
"publishConfig": {
"access": "public",
"provenance": true
},
"description": "Turns REST API endpoints into generic request options",
"repository": "github:octokit/endpoint.js",
"keywords": [
"octokit",
"github",
"api",
"rest"
],
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"devDependencies": {
"@octokit/tsconfig": "^4.0.0",
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^3.0.0",
"esbuild": "^0.25.0",
"glob": "^11.0.0",
"prettier": "3.4.2",
"semantic-release": "^24.0.0",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"typescript": "^5.0.0",
"vitest": "^3.0.0"
},
"dependencies": {
"@octokit/types": "^13.6.2",
"universal-user-agent": "^7.0.2"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist-*/**",
"bin/**"
],
"types": "./dist-types/index.d.ts",
"exports": {
".": {
"types": "./dist-types/index.d.ts",
"import": "./dist-bundle/index.js",
"default": "./dist-bundle/index.js"
}
},
"sideEffects": false
}

View file

@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2018 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,408 @@
# graphql.js
> GitHub GraphQL API client for browsers and Node
[![@latest](https://img.shields.io/npm/v/@octokit/graphql.svg)](https://www.npmjs.com/package/@octokit/graphql)
[![Build Status](https://github.com/octokit/graphql.js/workflows/Test/badge.svg)](https://github.com/octokit/graphql.js/actions?query=workflow%3ATest+branch%3Amain)
<!-- toc -->
- [Usage](#usage)
- [Send a simple query](#send-a-simple-query)
- [Authentication](#authentication)
- [Variables](#variables)
- [Pass query together with headers and variables](#pass-query-together-with-headers-and-variables)
- [Use with GitHub Enterprise](#use-with-github-enterprise)
- [Use custom `@octokit/request` instance](#use-custom-octokitrequest-instance)
- [TypeScript](#typescript)
- [Additional Types](#additional-types)
- [Errors](#errors)
- [Partial responses](#partial-responses)
- [Writing tests](#writing-tests)
- [License](#license)
<!-- tocstop -->
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load `@octokit/graphql` directly from [esm.sh](https://esm.sh)
```html
<script type="module">
import { graphql } from "https://esm.sh/@octokit/graphql";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with <code>npm install @octokit/graphql</code>
```js
import { graphql } from "@octokit/graphql";
```
</td></tr>
</tbody>
</table>
### Send a simple query
```js
const { repository } = await graphql(
`
{
repository(owner: "octokit", name: "graphql.js") {
issues(last: 3) {
edges {
node {
title
}
}
}
}
}
`,
{
headers: {
authorization: `token secret123`,
},
},
);
```
### Authentication
The simplest way to authenticate a request is to set the `Authorization` header, e.g. to a [personal access token](https://github.com/settings/tokens/).
```js
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token secret123`,
},
});
const { repository } = await graphqlWithAuth(`
{
repository(owner: "octokit", name: "graphql.js") {
issues(last: 3) {
edges {
node {
title
}
}
}
}
}
`);
```
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 } = await import("@octokit/auth-app");
const auth = createAppAuth({
appId: process.env.APP_ID,
privateKey: process.env.PRIVATE_KEY,
installationId: 123,
});
const graphqlWithAuth = graphql.defaults({
request: {
hook: auth.hook,
},
});
const { repository } = await graphqlWithAuth(
`{
repository(owner: "octokit", name: "graphql.js") {
issues(last: 3) {
edges {
node {
title
}
}
}
}
}`,
);
```
### Variables
⚠️ Do not use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) in the query strings as they make your code vulnerable to query injection attacks (see [#2](https://github.com/octokit/graphql.js/issues/2)). Use variables instead:
```js
const { repository } = await graphql(
`
query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
repository(owner: $owner, name: $repo) {
issues(last: $num) {
edges {
node {
title
}
}
}
}
}
`,
{
owner: "octokit",
repo: "graphql.js",
headers: {
authorization: `token secret123`,
},
},
);
```
### Pass query together with headers and variables
```js
import { graphql } from("@octokit/graphql");
const { repository } = await graphql({
query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
repository(owner: $owner, name: $repo) {
issues(last: $num) {
edges {
node {
title
}
}
}
}
}`,
owner: "octokit",
repo: "graphql.js",
headers: {
authorization: `token secret123`,
},
});
```
### Use with GitHub Enterprise
```js
import { graphql } from "@octokit/graphql";
graphql = graphql.defaults({
baseUrl: "https://github-enterprise.acme-inc.com/api",
headers: {
authorization: `token secret123`,
},
});
const { repository } = await graphql(`
{
repository(owner: "acme-project", name: "acme-repo") {
issues(last: 3) {
edges {
node {
title
}
}
}
}
}
`);
```
### Use custom `@octokit/request` instance
```js
import { request } from "@octokit/request";
import { withCustomRequest } from "@octokit/graphql";
let requestCounter = 0;
const myRequest = request.defaults({
headers: {
authorization: "bearer secret123",
},
request: {
hook(request, options) {
requestCounter++;
return request(options);
},
},
});
const myGraphql = withCustomRequest(myRequest);
await request("/");
await myGraphql(`
{
repository(owner: "acme-project", name: "acme-repo") {
issues(last: 3) {
edges {
node {
title
}
}
}
}
}
`);
// requestCounter is now 2
```
## TypeScript
`@octokit/graphql` is exposing proper types for its usage with TypeScript projects.
### Additional Types
Additionally, `GraphQlQueryResponseData` has been exposed to users:
```ts
import type { GraphQlQueryResponseData } from "@octokit/graphql";
```
## Errors
In case of a GraphQL error, `error.message` is set to a combined message describing all errors returned by the endpoint.
All errors can be accessed at `error.errors`. `error.request` has the request options such as query, variables and headers set for easier debugging.
```js
import { graphql, GraphqlResponseError } from "@octokit/graphql";
graphql = graphql.defaults({
headers: {
authorization: `token secret123`,
},
});
const query = `{
viewer {
bioHtml
}
}`;
try {
const result = await graphql(query);
} catch (error) {
if (error instanceof GraphqlResponseError) {
// do something with the error, allowing you to detect a graphql response error,
// compared to accidentally catching unrelated errors.
// server responds with an object like the following (as an example)
// class GraphqlResponseError {
// "headers": {
// "status": "403",
// },
// "data": null,
// "errors": [{
// "message": "Field 'bioHtml' doesn't exist on type 'User'",
// "locations": [{
// "line": 3,
// "column": 5
// }]
// }]
// }
console.log("Request failed:", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } }
console.log(error.message); // Field 'bioHtml' doesn't exist on type 'User'
} else {
// handle non-GraphQL error
}
}
```
## Partial responses
A GraphQL query may respond with partial data accompanied by errors. In this case we will throw an error but the partial data will still be accessible through `error.data`
```js
import { graphql } from "@octokit/graphql";
graphql = graphql.defaults({
headers: {
authorization: `token secret123`,
},
});
const query = `{
repository(name: "probot", owner: "probot") {
name
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 25, after: "invalid cursor") {
nodes {
message
}
}
}
}
}
}
}`;
try {
const result = await graphql(query);
} catch (error) {
// server responds with
// {
// "data": {
// "repository": {
// "name": "probot",
// "ref": null
// }
// },
// "errors": [
// {
// "type": "INVALID_CURSOR_ARGUMENTS",
// "path": [
// "repository",
// "ref",
// "target",
// "history"
// ],
// "locations": [
// {
// "line": 7,
// "column": 11
// }
// ],
// "message": "`invalid cursor` does not appear to be a valid cursor."
// }
// ]
// }
console.log("Request failed:", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } }
console.log(error.message); // `invalid cursor` does not appear to be a valid cursor.
console.log(error.data); // { repository: { name: 'probot', ref: null } }
}
```
## Writing tests
You can pass a replacement for [the built-in fetch implementation](https://github.com/bitinn/node-fetch) as `request.fetch` option. For example, using [fetch-mock](http://www.wheresrhys.co.uk/fetch-mock/) works great to write tests
```js
import assert from "assert";
import fetchMock from "fetch-mock";
import { graphql } from "@octokit/graphql";
graphql("{ viewer { login } }", {
headers: {
authorization: "token secret123",
},
request: {
fetch: fetchMock
.sandbox()
.post("https://api.github.com/graphql", (url, options) => {
assert.strictEqual(options.headers.authorization, "token secret123");
assert.strictEqual(
options.body,
'{"query":"{ viewer { login } }"}',
"Sends correct query",
);
return { data: {} };
}),
},
});
```
## License
[MIT](LICENSE)

View file

@ -0,0 +1,129 @@
// pkg/dist-src/index.js
import { request } from "@octokit/request";
import { getUserAgent } from "universal-user-agent";
// pkg/dist-src/version.js
var VERSION = "0.0.0-development";
// pkg/dist-src/with-defaults.js
import { request as Request2 } from "@octokit/request";
// pkg/dist-src/graphql.js
import { request as Request } from "@octokit/request";
// pkg/dist-src/error.js
function _buildMessageForResponseErrors(data) {
return `Request failed due to following response errors:
` + data.errors.map((e) => ` - ${e.message}`).join("\n");
}
var GraphqlResponseError = class extends Error {
constructor(request2, headers, response) {
super(_buildMessageForResponseErrors(response));
this.request = request2;
this.headers = headers;
this.response = response;
this.errors = response.errors;
this.data = response.data;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
name = "GraphqlResponseError";
errors;
data;
};
// pkg/dist-src/graphql.js
var NON_VARIABLE_OPTIONS = [
"method",
"baseUrl",
"url",
"headers",
"request",
"query",
"mediaType",
"operationName"
];
var FORBIDDEN_VARIABLE_OPTIONS = ["query", "method", "url"];
var GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/;
function graphql(request2, query, options) {
if (options) {
if (typeof query === "string" && "query" in options) {
return Promise.reject(
new Error(`[@octokit/graphql] "query" cannot be used as variable name`)
);
}
for (const key in options) {
if (!FORBIDDEN_VARIABLE_OPTIONS.includes(key)) continue;
return Promise.reject(
new Error(
`[@octokit/graphql] "${key}" cannot be used as variable name`
)
);
}
}
const parsedOptions = typeof query === "string" ? Object.assign({ query }, options) : query;
const requestOptions = Object.keys(
parsedOptions
).reduce((result, key) => {
if (NON_VARIABLE_OPTIONS.includes(key)) {
result[key] = parsedOptions[key];
return result;
}
if (!result.variables) {
result.variables = {};
}
result.variables[key] = parsedOptions[key];
return result;
}, {});
const baseUrl = parsedOptions.baseUrl || request2.endpoint.DEFAULTS.baseUrl;
if (GHES_V3_SUFFIX_REGEX.test(baseUrl)) {
requestOptions.url = baseUrl.replace(GHES_V3_SUFFIX_REGEX, "/api/graphql");
}
return request2(requestOptions).then((response) => {
if (response.data.errors) {
const headers = {};
for (const key of Object.keys(response.headers)) {
headers[key] = response.headers[key];
}
throw new GraphqlResponseError(
requestOptions,
headers,
response.data
);
}
return response.data.data;
});
}
// pkg/dist-src/with-defaults.js
function withDefaults(request2, newDefaults) {
const newRequest = request2.defaults(newDefaults);
const newApi = (query, options) => {
return graphql(newRequest, query, options);
};
return Object.assign(newApi, {
defaults: withDefaults.bind(null, newRequest),
endpoint: newRequest.endpoint
});
}
// pkg/dist-src/index.js
var graphql2 = withDefaults(request, {
headers: {
"user-agent": `octokit-graphql.js/${VERSION} ${getUserAgent()}`
},
method: "POST",
url: "/graphql"
});
function withCustomRequest(customRequest) {
return withDefaults(customRequest, {
method: "POST",
url: "/graphql"
});
}
export {
GraphqlResponseError,
graphql2 as graphql,
withCustomRequest
};

View file

@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["../dist-src/index.js", "../dist-src/version.js", "../dist-src/with-defaults.js", "../dist-src/graphql.js", "../dist-src/error.js"],
"sourcesContent": ["import { request } from \"@octokit/request\";\nimport { getUserAgent } from \"universal-user-agent\";\nimport { VERSION } from \"./version.js\";\nimport { withDefaults } from \"./with-defaults.js\";\nconst graphql = withDefaults(request, {\n headers: {\n \"user-agent\": `octokit-graphql.js/${VERSION} ${getUserAgent()}`\n },\n method: \"POST\",\n url: \"/graphql\"\n});\nimport { GraphqlResponseError } from \"./error.js\";\nfunction withCustomRequest(customRequest) {\n return withDefaults(customRequest, {\n method: \"POST\",\n url: \"/graphql\"\n });\n}\nexport {\n GraphqlResponseError,\n graphql,\n withCustomRequest\n};\n", "const VERSION = \"0.0.0-development\";\nexport {\n VERSION\n};\n", "import { request as Request } from \"@octokit/request\";\nimport { graphql } from \"./graphql.js\";\nfunction withDefaults(request, newDefaults) {\n const newRequest = request.defaults(newDefaults);\n const newApi = (query, options) => {\n return graphql(newRequest, query, options);\n };\n return Object.assign(newApi, {\n defaults: withDefaults.bind(null, newRequest),\n endpoint: newRequest.endpoint\n });\n}\nexport {\n withDefaults\n};\n", "import { request as Request } from \"@octokit/request\";\nimport { GraphqlResponseError } from \"./error.js\";\nconst NON_VARIABLE_OPTIONS = [\n \"method\",\n \"baseUrl\",\n \"url\",\n \"headers\",\n \"request\",\n \"query\",\n \"mediaType\",\n \"operationName\"\n];\nconst FORBIDDEN_VARIABLE_OPTIONS = [\"query\", \"method\", \"url\"];\nconst GHES_V3_SUFFIX_REGEX = /\\/api\\/v3\\/?$/;\nfunction graphql(request, query, options) {\n if (options) {\n if (typeof query === \"string\" && \"query\" in options) {\n return Promise.reject(\n new Error(`[@octokit/graphql] \"query\" cannot be used as variable name`)\n );\n }\n for (const key in options) {\n if (!FORBIDDEN_VARIABLE_OPTIONS.includes(key)) continue;\n return Promise.reject(\n new Error(\n `[@octokit/graphql] \"${key}\" cannot be used as variable name`\n )\n );\n }\n }\n const parsedOptions = typeof query === \"string\" ? Object.assign({ query }, options) : query;\n const requestOptions = Object.keys(\n parsedOptions\n ).reduce((result, key) => {\n if (NON_VARIABLE_OPTIONS.includes(key)) {\n result[key] = parsedOptions[key];\n return result;\n }\n if (!result.variables) {\n result.variables = {};\n }\n result.variables[key] = parsedOptions[key];\n return result;\n }, {});\n const baseUrl = parsedOptions.baseUrl || request.endpoint.DEFAULTS.baseUrl;\n if (GHES_V3_SUFFIX_REGEX.test(baseUrl)) {\n requestOptions.url = baseUrl.replace(GHES_V3_SUFFIX_REGEX, \"/api/graphql\");\n }\n return request(requestOptions).then((response) => {\n if (response.data.errors) {\n const headers = {};\n for (const key of Object.keys(response.headers)) {\n headers[key] = response.headers[key];\n }\n throw new GraphqlResponseError(\n requestOptions,\n headers,\n response.data\n );\n }\n return response.data.data;\n });\n}\nexport {\n graphql\n};\n", "function _buildMessageForResponseErrors(data) {\n return `Request failed due to following response errors:\n` + data.errors.map((e) => ` - ${e.message}`).join(\"\\n\");\n}\nclass GraphqlResponseError extends Error {\n constructor(request, headers, response) {\n super(_buildMessageForResponseErrors(response));\n this.request = request;\n this.headers = headers;\n this.response = response;\n this.errors = response.errors;\n this.data = response.data;\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n name = \"GraphqlResponseError\";\n errors;\n data;\n}\nexport {\n GraphqlResponseError\n};\n"],
"mappings": ";AAAA,SAAS,eAAe;AACxB,SAAS,oBAAoB;;;ACD7B,IAAM,UAAU;;;ACAhB,SAAS,WAAWA,gBAAe;;;ACAnC,SAAS,WAAW,eAAe;;;ACAnC,SAAS,+BAA+B,MAAM;AAC5C,SAAO;AAAA,IACL,KAAK,OAAO,IAAI,CAAC,MAAM,MAAM,EAAE,OAAO,EAAE,EAAE,KAAK,IAAI;AACvD;AACA,IAAM,uBAAN,cAAmC,MAAM;AAAA,EACvC,YAAYC,UAAS,SAAS,UAAU;AACtC,UAAM,+BAA+B,QAAQ,CAAC;AAC9C,SAAK,UAAUA;AACf,SAAK,UAAU;AACf,SAAK,WAAW;AAChB,SAAK,SAAS,SAAS;AACvB,SAAK,OAAO,SAAS;AACrB,QAAI,MAAM,mBAAmB;AAC3B,YAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,IAChD;AAAA,EACF;AAAA,EACA,OAAO;AAAA,EACP;AAAA,EACA;AACF;;;ADjBA,IAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AACA,IAAM,6BAA6B,CAAC,SAAS,UAAU,KAAK;AAC5D,IAAM,uBAAuB;AAC7B,SAAS,QAAQC,UAAS,OAAO,SAAS;AACxC,MAAI,SAAS;AACX,QAAI,OAAO,UAAU,YAAY,WAAW,SAAS;AACnD,aAAO,QAAQ;AAAA,QACb,IAAI,MAAM,4DAA4D;AAAA,MACxE;AAAA,IACF;AACA,eAAW,OAAO,SAAS;AACzB,UAAI,CAAC,2BAA2B,SAAS,GAAG,EAAG;AAC/C,aAAO,QAAQ;AAAA,QACb,IAAI;AAAA,UACF,uBAAuB,GAAG;AAAA,QAC5B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,QAAM,gBAAgB,OAAO,UAAU,WAAW,OAAO,OAAO,EAAE,MAAM,GAAG,OAAO,IAAI;AACtF,QAAM,iBAAiB,OAAO;AAAA,IAC5B;AAAA,EACF,EAAE,OAAO,CAAC,QAAQ,QAAQ;AACxB,QAAI,qBAAqB,SAAS,GAAG,GAAG;AACtC,aAAO,GAAG,IAAI,cAAc,GAAG;AAC/B,aAAO;AAAA,IACT;AACA,QAAI,CAAC,OAAO,WAAW;AACrB,aAAO,YAAY,CAAC;AAAA,IACtB;AACA,WAAO,UAAU,GAAG,IAAI,cAAc,GAAG;AACzC,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACL,QAAM,UAAU,cAAc,WAAWA,SAAQ,SAAS,SAAS;AACnE,MAAI,qBAAqB,KAAK,OAAO,GAAG;AACtC,mBAAe,MAAM,QAAQ,QAAQ,sBAAsB,cAAc;AAAA,EAC3E;AACA,SAAOA,SAAQ,cAAc,EAAE,KAAK,CAAC,aAAa;AAChD,QAAI,SAAS,KAAK,QAAQ;AACxB,YAAM,UAAU,CAAC;AACjB,iBAAW,OAAO,OAAO,KAAK,SAAS,OAAO,GAAG;AAC/C,gBAAQ,GAAG,IAAI,SAAS,QAAQ,GAAG;AAAA,MACrC;AACA,YAAM,IAAI;AAAA,QACR;AAAA,QACA;AAAA,QACA,SAAS;AAAA,MACX;AAAA,IACF;AACA,WAAO,SAAS,KAAK;AAAA,EACvB,CAAC;AACH;;;AD5DA,SAAS,aAAaC,UAAS,aAAa;AAC1C,QAAM,aAAaA,SAAQ,SAAS,WAAW;AAC/C,QAAM,SAAS,CAAC,OAAO,YAAY;AACjC,WAAO,QAAQ,YAAY,OAAO,OAAO;AAAA,EAC3C;AACA,SAAO,OAAO,OAAO,QAAQ;AAAA,IAC3B,UAAU,aAAa,KAAK,MAAM,UAAU;AAAA,IAC5C,UAAU,WAAW;AAAA,EACvB,CAAC;AACH;;;AFPA,IAAMC,WAAU,aAAa,SAAS;AAAA,EACpC,SAAS;AAAA,IACP,cAAc,sBAAsB,OAAO,IAAI,aAAa,CAAC;AAAA,EAC/D;AAAA,EACA,QAAQ;AAAA,EACR,KAAK;AACP,CAAC;AAED,SAAS,kBAAkB,eAAe;AACxC,SAAO,aAAa,eAAe;AAAA,IACjC,QAAQ;AAAA,IACR,KAAK;AAAA,EACP,CAAC;AACH;",
"names": ["Request", "request", "request", "request", "graphql"]
}

View file

@ -0,0 +1,23 @@
function _buildMessageForResponseErrors(data) {
return `Request failed due to following response errors:
` + data.errors.map((e) => ` - ${e.message}`).join("\n");
}
class GraphqlResponseError extends Error {
constructor(request, headers, response) {
super(_buildMessageForResponseErrors(response));
this.request = request;
this.headers = headers;
this.response = response;
this.errors = response.errors;
this.data = response.data;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
name = "GraphqlResponseError";
errors;
data;
}
export {
GraphqlResponseError
};

View file

@ -0,0 +1,66 @@
import { request as Request } from "@octokit/request";
import { GraphqlResponseError } from "./error.js";
const NON_VARIABLE_OPTIONS = [
"method",
"baseUrl",
"url",
"headers",
"request",
"query",
"mediaType",
"operationName"
];
const FORBIDDEN_VARIABLE_OPTIONS = ["query", "method", "url"];
const GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/;
function graphql(request, query, options) {
if (options) {
if (typeof query === "string" && "query" in options) {
return Promise.reject(
new Error(`[@octokit/graphql] "query" cannot be used as variable name`)
);
}
for (const key in options) {
if (!FORBIDDEN_VARIABLE_OPTIONS.includes(key)) continue;
return Promise.reject(
new Error(
`[@octokit/graphql] "${key}" cannot be used as variable name`
)
);
}
}
const parsedOptions = typeof query === "string" ? Object.assign({ query }, options) : query;
const requestOptions = Object.keys(
parsedOptions
).reduce((result, key) => {
if (NON_VARIABLE_OPTIONS.includes(key)) {
result[key] = parsedOptions[key];
return result;
}
if (!result.variables) {
result.variables = {};
}
result.variables[key] = parsedOptions[key];
return result;
}, {});
const baseUrl = parsedOptions.baseUrl || request.endpoint.DEFAULTS.baseUrl;
if (GHES_V3_SUFFIX_REGEX.test(baseUrl)) {
requestOptions.url = baseUrl.replace(GHES_V3_SUFFIX_REGEX, "/api/graphql");
}
return request(requestOptions).then((response) => {
if (response.data.errors) {
const headers = {};
for (const key of Object.keys(response.headers)) {
headers[key] = response.headers[key];
}
throw new GraphqlResponseError(
requestOptions,
headers,
response.data
);
}
return response.data.data;
});
}
export {
graphql
};

View file

@ -0,0 +1,23 @@
import { request } from "@octokit/request";
import { getUserAgent } from "universal-user-agent";
import { VERSION } from "./version.js";
import { withDefaults } from "./with-defaults.js";
const graphql = withDefaults(request, {
headers: {
"user-agent": `octokit-graphql.js/${VERSION} ${getUserAgent()}`
},
method: "POST",
url: "/graphql"
});
import { GraphqlResponseError } from "./error.js";
function withCustomRequest(customRequest) {
return withDefaults(customRequest, {
method: "POST",
url: "/graphql"
});
}
export {
GraphqlResponseError,
graphql,
withCustomRequest
};

View file

@ -0,0 +1,4 @@
const VERSION = "8.2.1";
export {
VERSION
};

View file

@ -0,0 +1,15 @@
import { request as Request } from "@octokit/request";
import { graphql } from "./graphql.js";
function withDefaults(request, newDefaults) {
const newRequest = request.defaults(newDefaults);
const newApi = (query, options) => {
return graphql(newRequest, query, options);
};
return Object.assign(newApi, {
defaults: withDefaults.bind(null, newRequest),
endpoint: newRequest.endpoint
});
}
export {
withDefaults
};

View file

@ -0,0 +1,13 @@
import type { ResponseHeaders } from "@octokit/types";
import type { GraphQlEndpointOptions, GraphQlQueryResponse } from "./types.js";
type ServerResponseData<T> = Required<GraphQlQueryResponse<T>>;
export declare class GraphqlResponseError<ResponseData> extends Error {
readonly request: GraphQlEndpointOptions;
readonly headers: ResponseHeaders;
readonly response: ServerResponseData<ResponseData>;
name: string;
readonly errors: GraphQlQueryResponse<never>["errors"];
readonly data: ResponseData;
constructor(request: GraphQlEndpointOptions, headers: ResponseHeaders, response: ServerResponseData<ResponseData>);
}
export {};

View file

@ -0,0 +1,3 @@
import { request as Request } from "@octokit/request";
import type { RequestParameters, GraphQlQueryResponseData } from "./types.js";
export declare function graphql<ResponseData = GraphQlQueryResponseData>(request: typeof Request, query: string | RequestParameters, options?: RequestParameters): Promise<ResponseData>;

View file

@ -0,0 +1,5 @@
import { request } from "@octokit/request";
export declare const graphql: import("./types.js").graphql;
export type { GraphQlQueryResponseData } from "./types.js";
export { GraphqlResponseError } from "./error.js";
export declare function withCustomRequest(customRequest: typeof request): import("./types.js").graphql;

View file

@ -0,0 +1,55 @@
import type { EndpointOptions, RequestParameters as RequestParametersType, EndpointInterface } from "@octokit/types";
export type GraphQlEndpointOptions = EndpointOptions & {
variables?: {
[key: string]: unknown;
};
};
export type RequestParameters = RequestParametersType;
export type Query = string;
export interface graphql {
/**
* Sends a GraphQL query request based on endpoint options
* The GraphQL query must be specified in `options`.
*
* @param {object} endpoint Must set `method` and `url`. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
<ResponseData>(options: RequestParameters): GraphQlResponse<ResponseData>;
/**
* Sends a GraphQL query request based on endpoint options
*
* @param {string} query GraphQL query. Example: `'query { viewer { login } }'`.
* @param {object} [parameters] URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
<ResponseData>(query: Query, parameters?: RequestParameters): GraphQlResponse<ResponseData>;
/**
* Returns a new `endpoint` with updated route and parameters
*/
defaults: (newDefaults: RequestParameters) => graphql;
/**
* Octokit endpoint API, see {@link https://github.com/octokit/endpoint.js|@octokit/endpoint}
*/
endpoint: EndpointInterface;
}
export type GraphQlResponse<ResponseData> = Promise<ResponseData>;
export type GraphQlQueryResponseData = {
[key: string]: any;
};
export type GraphQlQueryResponse<ResponseData> = {
data: ResponseData;
errors?: [
{
type: string;
message: string;
path: [string];
extensions: {
[key: string]: any;
};
locations: [
{
line: number;
column: number;
}
];
}
];
};

View file

@ -0,0 +1 @@
export declare const VERSION = "8.2.1";

View file

@ -0,0 +1,3 @@
import { request as Request } from "@octokit/request";
import type { graphql as ApiInterface, RequestParameters } from "./types.js";
export declare function withDefaults(request: typeof Request, newDefaults: RequestParameters): ApiInterface;

View file

@ -0,0 +1,56 @@
{
"name": "@octokit/graphql",
"version": "8.2.1",
"publishConfig": {
"access": "public",
"provenance": true
},
"type": "module",
"description": "GitHub GraphQL API client for browsers and Node",
"repository": "github:octokit/graphql.js",
"keywords": [
"octokit",
"github",
"api",
"graphql"
],
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/request": "^9.2.2",
"@octokit/types": "^13.8.0",
"universal-user-agent": "^7.0.0"
},
"devDependencies": {
"@octokit/tsconfig": "^4.0.0",
"@types/fetch-mock": "^7.2.5",
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^3.0.0",
"esbuild": "^0.25.0",
"fetch-mock": "^12.0.0",
"glob": "^11.0.0",
"prettier": "3.4.2",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"typescript": "^5.3.0",
"vitest": "^3.0.0"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist-*/**",
"bin/**"
],
"types": "./dist-types/index.d.ts",
"exports": {
".": {
"types": "./dist-types/index.d.ts",
"import": "./dist-bundle/index.js",
"default": "./dist-bundle/index.js"
},
"./types": {
"types": "./dist-types/types.d.ts"
}
},
"sideEffects": false
}

View file

@ -0,0 +1,7 @@
MIT License Copyright (c) 2019 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,291 @@
# plugin-paginate-graphql.js
> Octokit plugin to paginate GraphQL API endpoint responses
[![@latest](https://img.shields.io/npm/v/@octokit/plugin-paginate-graphql.svg)](https://www.npmjs.com/package/@octokit/plugin-paginate-graphql)
[![Build Status](https://github.com/octokit/plugin-paginate-graphql.js/workflows/Test/badge.svg)](https://github.com/octokit/plugin-paginate-graphql.js/actions?workflow=Test)
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load `@octokit/plugin-paginate-graphql` and [`@octokit/core`](https://github.com/octokit/core.js) (or core-compatible module) directly from [esm.sh](https://esm.sh)
```html
<script type="module">
import { Octokit } from "https://esm.sh/@octokit/core";
import { paginateGraphQL } from "https://esm.sh/@octokit/plugin-paginate-graphql";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with `npm install @octokit/core @octokit/plugin-paginate-graphql`. Optionally replace `@octokit/core` with a core-compatible module
```js
import { Octokit } from "@octokit/core";
import { paginateGraphQL } from "@octokit/plugin-paginate-graphql";
```
</td></tr>
</tbody>
</table>
> [!IMPORTANT]
> As we use [conditional exports](https://nodejs.org/api/packages.html#conditional-exports), you will need to adapt your `tsconfig.json` by setting `"moduleResolution": "node16", "module": "node16"`.
>
> See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).<br>
> See this [helpful guide on transitioning to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) from [@sindresorhus](https://github.com/sindresorhus)
```js
const MyOctokit = Octokit.plugin(paginateGraphQL);
const octokit = new MyOctokit({ auth: "secret123" });
const { repository } = await octokit.graphql.paginate(
`query paginate($cursor: String) {
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`,
);
console.log(`Found ${repository.issues.nodes.length} issues!`);
```
There are two conventions this plugin relies on:
1. The name of the cursor variable must be `$cursor`
2. You must include a valid `pageInfo` object in the paginated resource (see [Pagination Direction](#pagination-direction) for more info on what is considered valid)
## `octokit.graphql.paginate()`
The `paginateGraphQL` plugin adds a new `octokit.graphql.paginate()` method which accepts a query with a single `$cursor` variable that is used to paginate.
The query gets passed over to the `octokit.graphql()`-function. The response is then scanned for the required `pageInfo`-object. If `hasNextPage` is `true`, it will automatically use the `endCursor` to execute the next query until `hasNextPage` is `false`.
While iterating, it continually merges all `nodes` and/or `edges` of all responses and returns a combined response in the end.
> **Warning**
> Please note that this plugin only supports pagination of a single resource - so you can **not** execute queries with parallel or nested pagination. You can find more details in [the chapter below](#unsupported-nested-pagination).
## `octokit.graphql.paginate.iterator()`
If your target runtime environments supports async iterators (such as most modern browsers and Node 10+), you can iterate through each response:
```js
const pageIterator = octokit.graphql.paginate.iterator(
`query paginate($cursor: String) {
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`,
);
for await (const response of pageIterator) {
const issues = response.repository.issues;
console.log(`${issues.length} issues found.`);
}
```
### Variables
Just like with [octokit/graphql.js](https://github.com/octokit/graphql.js/#variables), you can pass your own variables as a second parameter to the `paginate` or `iterator` function.
```js
await octokit.graphql.paginate(
`
query paginate($cursor: String, $organization: String!) {
repository(owner: $organization, name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`,
{
organization: "octokit",
},
);
```
You can also use this to pass a initial cursor value:
```js
await octokit.graphql.paginate(
`
query paginate($cursor: String, $organization: String!) {
repository(owner: $organization, name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`,
{
organization: "octokit",
cursor: "initialValue",
},
);
```
### Pagination Direction
You can control the pagination direction by the properties defined in the `pageInfo` resource.
For a forward pagination, use:
```gql
pageInfo {
hasNextPage
endCursor
}
```
For a backwards pagination, use:
```gql
pageInfo {
hasPreviousPage
startCursor
}
```
If you provide all 4 properties in a `pageInfo`, the plugin will default to forward pagination.
### Unsupported: Nested pagination
Nested pagination with GraphQL is complicated, so the following **is not supported**:
```js
await octokit.graphql.paginate((cursor) => {
const issuesCursor = cursor.create("issuesCursor");
const commentsCursor = cursor.create("issuesCursor");
return `{
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: ${issuesCursor}) {
nodes {
title,
comments(first: 10, after: ${commentsCursor}) {
nodes: {
body
}
pageInfo {
hasNextPage
endCursor
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
});
```
There is a great video from GitHub Universe 2019 [Advanced patterns for GitHub's GraphQL API](https://www.youtube.com/watch?v=i5pIszu9MeM&t=719s) by [@ReaLoretta](https://github.com/ReaLoretta) that goes into depth why this is so hard to achieve and patterns and ways around it.
### TypeScript Support
You can type the response of the `paginateGraphQL()` and `iterator()` functions like this:
```ts
await octokit.graphql.paginate<RepositoryIssueResponseType>((cursor) => {
return `{
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: ${cursor.create()}) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
});
```
You can utilize the `PageInfoForward` and `PageInfoBackward`-Interfaces exported from this library to construct your response-types:
```ts
import { PageInfoForward } from "@octokit/plugin-paginate-graphql";
type Issues = {
title: string;
};
type IssueResponseType = {
repository: {
issues: {
nodes: Issues[];
pageInfo: PageInfoForward;
};
};
};
// Response will be of type IssueResponseType
const response = await octokit.graphql.paginate<IssueResponseType>((cursor) => {
return `{
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: ${cursor.create()}) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
});
```
The `PageInfoBackward` contains the properties `hasPreviousPage` and `startCursor` and can be used accordingly when doing backwards pagination.
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md)
## License
[MIT](LICENSE)

View file

@ -0,0 +1,181 @@
// pkg/dist-src/errors.js
var generateMessage = (path, cursorValue) => `The cursor at "${path.join(
","
)}" did not change its value "${cursorValue}" after a page transition. Please make sure your that your query is set up correctly.`;
var MissingCursorChange = class extends Error {
constructor(pageInfo, cursorValue) {
super(generateMessage(pageInfo.pathInQuery, cursorValue));
this.pageInfo = pageInfo;
this.cursorValue = cursorValue;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
name = "MissingCursorChangeError";
};
var MissingPageInfo = class extends Error {
constructor(response) {
super(
`No pageInfo property found in response. Please make sure to specify the pageInfo in your query. Response-Data: ${JSON.stringify(
response,
null,
2
)}`
);
this.response = response;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
name = "MissingPageInfo";
};
// pkg/dist-src/object-helpers.js
var isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
function findPaginatedResourcePath(responseData) {
const paginatedResourcePath = deepFindPathToProperty(
responseData,
"pageInfo"
);
if (paginatedResourcePath.length === 0) {
throw new MissingPageInfo(responseData);
}
return paginatedResourcePath;
}
var deepFindPathToProperty = (object, searchProp, path = []) => {
for (const key of Object.keys(object)) {
const currentPath = [...path, key];
const currentValue = object[key];
if (isObject(currentValue)) {
if (currentValue.hasOwnProperty(searchProp)) {
return currentPath;
}
const result = deepFindPathToProperty(
currentValue,
searchProp,
currentPath
);
if (result.length > 0) {
return result;
}
}
}
return [];
};
var get = (object, path) => {
return path.reduce((current, nextProperty) => current[nextProperty], object);
};
var set = (object, path, mutator) => {
const lastProperty = path[path.length - 1];
const parentPath = [...path].slice(0, -1);
const parent = get(object, parentPath);
if (typeof mutator === "function") {
parent[lastProperty] = mutator(parent[lastProperty]);
} else {
parent[lastProperty] = mutator;
}
};
// pkg/dist-src/extract-page-info.js
var extractPageInfos = (responseData) => {
const pageInfoPath = findPaginatedResourcePath(responseData);
return {
pathInQuery: pageInfoPath,
pageInfo: get(responseData, [...pageInfoPath, "pageInfo"])
};
};
// pkg/dist-src/page-info.js
var isForwardSearch = (givenPageInfo) => {
return givenPageInfo.hasOwnProperty("hasNextPage");
};
var getCursorFrom = (pageInfo) => isForwardSearch(pageInfo) ? pageInfo.endCursor : pageInfo.startCursor;
var hasAnotherPage = (pageInfo) => isForwardSearch(pageInfo) ? pageInfo.hasNextPage : pageInfo.hasPreviousPage;
// pkg/dist-src/iterator.js
var createIterator = (octokit) => {
return (query, initialParameters = {}) => {
let nextPageExists = true;
let parameters = { ...initialParameters };
return {
[Symbol.asyncIterator]: () => ({
async next() {
if (!nextPageExists) return { done: true, value: {} };
const response = await octokit.graphql(
query,
parameters
);
const pageInfoContext = extractPageInfos(response);
const nextCursorValue = getCursorFrom(pageInfoContext.pageInfo);
nextPageExists = hasAnotherPage(pageInfoContext.pageInfo);
if (nextPageExists && nextCursorValue === parameters.cursor) {
throw new MissingCursorChange(pageInfoContext, nextCursorValue);
}
parameters = {
...parameters,
cursor: nextCursorValue
};
return { done: false, value: response };
}
})
};
};
};
// pkg/dist-src/merge-responses.js
var mergeResponses = (response1, response2) => {
if (Object.keys(response1).length === 0) {
return Object.assign(response1, response2);
}
const path = findPaginatedResourcePath(response1);
const nodesPath = [...path, "nodes"];
const newNodes = get(response2, nodesPath);
if (newNodes) {
set(response1, nodesPath, (values) => {
return [...values, ...newNodes];
});
}
const edgesPath = [...path, "edges"];
const newEdges = get(response2, edgesPath);
if (newEdges) {
set(response1, edgesPath, (values) => {
return [...values, ...newEdges];
});
}
const pageInfoPath = [...path, "pageInfo"];
set(response1, pageInfoPath, get(response2, pageInfoPath));
return response1;
};
// pkg/dist-src/paginate.js
var createPaginate = (octokit) => {
const iterator = createIterator(octokit);
return async (query, initialParameters = {}) => {
let mergedResponse = {};
for await (const response of iterator(
query,
initialParameters
)) {
mergedResponse = mergeResponses(mergedResponse, response);
}
return mergedResponse;
};
};
// pkg/dist-src/version.js
var VERSION = "0.0.0-development";
// pkg/dist-src/index.js
function paginateGraphQL(octokit) {
return {
graphql: Object.assign(octokit.graphql, {
paginate: Object.assign(createPaginate(octokit), {
iterator: createIterator(octokit)
})
})
};
}
export {
VERSION,
paginateGraphQL
};

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,34 @@
const generateMessage = (path, cursorValue) => `The cursor at "${path.join(
","
)}" did not change its value "${cursorValue}" after a page transition. Please make sure your that your query is set up correctly.`;
class MissingCursorChange extends Error {
constructor(pageInfo, cursorValue) {
super(generateMessage(pageInfo.pathInQuery, cursorValue));
this.pageInfo = pageInfo;
this.cursorValue = cursorValue;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
name = "MissingCursorChangeError";
}
class MissingPageInfo extends Error {
constructor(response) {
super(
`No pageInfo property found in response. Please make sure to specify the pageInfo in your query. Response-Data: ${JSON.stringify(
response,
null,
2
)}`
);
this.response = response;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
name = "MissingPageInfo";
}
export {
MissingCursorChange,
MissingPageInfo
};

View file

@ -0,0 +1,11 @@
import { findPaginatedResourcePath, get } from "./object-helpers.js";
const extractPageInfos = (responseData) => {
const pageInfoPath = findPaginatedResourcePath(responseData);
return {
pathInQuery: pageInfoPath,
pageInfo: get(responseData, [...pageInfoPath, "pageInfo"])
};
};
export {
extractPageInfos
};

View file

@ -0,0 +1,16 @@
import { createIterator } from "./iterator.js";
import { createPaginate } from "./paginate.js";
import { VERSION } from "./version.js";
function paginateGraphQL(octokit) {
return {
graphql: Object.assign(octokit.graphql, {
paginate: Object.assign(createPaginate(octokit), {
iterator: createIterator(octokit)
})
})
};
}
export {
VERSION,
paginateGraphQL
};

View file

@ -0,0 +1,34 @@
import { extractPageInfos } from "./extract-page-info.js";
import { getCursorFrom, hasAnotherPage } from "./page-info.js";
import { MissingCursorChange } from "./errors.js";
const createIterator = (octokit) => {
return (query, initialParameters = {}) => {
let nextPageExists = true;
let parameters = { ...initialParameters };
return {
[Symbol.asyncIterator]: () => ({
async next() {
if (!nextPageExists) return { done: true, value: {} };
const response = await octokit.graphql(
query,
parameters
);
const pageInfoContext = extractPageInfos(response);
const nextCursorValue = getCursorFrom(pageInfoContext.pageInfo);
nextPageExists = hasAnotherPage(pageInfoContext.pageInfo);
if (nextPageExists && nextCursorValue === parameters.cursor) {
throw new MissingCursorChange(pageInfoContext, nextCursorValue);
}
parameters = {
...parameters,
cursor: nextCursorValue
};
return { done: false, value: response };
}
})
};
};
};
export {
createIterator
};

View file

@ -0,0 +1,27 @@
import { findPaginatedResourcePath, get, set } from "./object-helpers.js";
const mergeResponses = (response1, response2) => {
if (Object.keys(response1).length === 0) {
return Object.assign(response1, response2);
}
const path = findPaginatedResourcePath(response1);
const nodesPath = [...path, "nodes"];
const newNodes = get(response2, nodesPath);
if (newNodes) {
set(response1, nodesPath, (values) => {
return [...values, ...newNodes];
});
}
const edgesPath = [...path, "edges"];
const newEdges = get(response2, edgesPath);
if (newEdges) {
set(response1, edgesPath, (values) => {
return [...values, ...newEdges];
});
}
const pageInfoPath = [...path, "pageInfo"];
set(response1, pageInfoPath, get(response2, pageInfoPath));
return response1;
};
export {
mergeResponses
};

View file

@ -0,0 +1,50 @@
import { MissingPageInfo } from "./errors.js";
const isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
function findPaginatedResourcePath(responseData) {
const paginatedResourcePath = deepFindPathToProperty(
responseData,
"pageInfo"
);
if (paginatedResourcePath.length === 0) {
throw new MissingPageInfo(responseData);
}
return paginatedResourcePath;
}
const deepFindPathToProperty = (object, searchProp, path = []) => {
for (const key of Object.keys(object)) {
const currentPath = [...path, key];
const currentValue = object[key];
if (isObject(currentValue)) {
if (currentValue.hasOwnProperty(searchProp)) {
return currentPath;
}
const result = deepFindPathToProperty(
currentValue,
searchProp,
currentPath
);
if (result.length > 0) {
return result;
}
}
}
return [];
};
const get = (object, path) => {
return path.reduce((current, nextProperty) => current[nextProperty], object);
};
const set = (object, path, mutator) => {
const lastProperty = path[path.length - 1];
const parentPath = [...path].slice(0, -1);
const parent = get(object, parentPath);
if (typeof mutator === "function") {
parent[lastProperty] = mutator(parent[lastProperty]);
} else {
parent[lastProperty] = mutator;
}
};
export {
findPaginatedResourcePath,
get,
set
};

View file

@ -0,0 +1,9 @@
const isForwardSearch = (givenPageInfo) => {
return givenPageInfo.hasOwnProperty("hasNextPage");
};
const getCursorFrom = (pageInfo) => isForwardSearch(pageInfo) ? pageInfo.endCursor : pageInfo.startCursor;
const hasAnotherPage = (pageInfo) => isForwardSearch(pageInfo) ? pageInfo.hasNextPage : pageInfo.hasPreviousPage;
export {
getCursorFrom,
hasAnotherPage
};

View file

@ -0,0 +1,18 @@
import { mergeResponses } from "./merge-responses.js";
import { createIterator } from "./iterator.js";
const createPaginate = (octokit) => {
const iterator = createIterator(octokit);
return async (query, initialParameters = {}) => {
let mergedResponse = {};
for await (const response of iterator(
query,
initialParameters
)) {
mergedResponse = mergeResponses(mergedResponse, response);
}
return mergedResponse;
};
};
export {
createPaginate
};

View file

@ -0,0 +1,4 @@
const VERSION = "5.2.4";
export {
VERSION
};

View file

@ -0,0 +1,13 @@
import type { CursorValue, PageInfoContext } from "./page-info.js";
declare class MissingCursorChange extends Error {
readonly pageInfo: PageInfoContext;
readonly cursorValue: CursorValue;
name: string;
constructor(pageInfo: PageInfoContext, cursorValue: CursorValue);
}
declare class MissingPageInfo extends Error {
readonly response: any;
name: string;
constructor(response: any);
}
export { MissingCursorChange, MissingPageInfo };

View file

@ -0,0 +1,3 @@
import type { PageInfoContext } from "./page-info.js";
declare const extractPageInfos: (responseData: any) => PageInfoContext;
export { extractPageInfos };

View file

@ -0,0 +1,13 @@
import type { Octokit } from "@octokit/core";
import { createIterator } from "./iterator.js";
import { createPaginate } from "./paginate.js";
export type { PageInfoForward, PageInfoBackward } from "./page-info.js";
export { VERSION } from "./version.js";
export type paginateGraphQLInterface = {
graphql: Octokit["graphql"] & {
paginate: ReturnType<typeof createPaginate> & {
iterator: ReturnType<typeof createIterator>;
};
};
};
export declare function paginateGraphQL(octokit: Octokit): paginateGraphQLInterface;

View file

@ -0,0 +1,10 @@
import type { Octokit } from "@octokit/core";
declare const createIterator: (octokit: Octokit) => <ResponseType = any>(query: string, initialParameters?: Record<string, any>) => {
[Symbol.asyncIterator]: () => {
next(): Promise<{
done: boolean;
value: ResponseType;
}>;
};
};
export { createIterator };

View file

@ -0,0 +1,2 @@
declare const mergeResponses: <ResponseType extends object = any>(response1: ResponseType, response2: ResponseType) => ResponseType;
export { mergeResponses };

View file

@ -0,0 +1,13 @@
declare function findPaginatedResourcePath(responseData: any): string[];
/**
* The interfaces of the "get" and "set" functions are equal to those of lodash:
* https://lodash.com/docs/4.17.15#get
* https://lodash.com/docs/4.17.15#set
*
* They are cut down to our purposes, but could be replaced by the lodash calls
* if we ever want to have that dependency.
*/
declare const get: (object: any, path: string[]) => any;
type Mutator = any | ((value: unknown) => any);
declare const set: (object: any, path: string[], mutator: Mutator) => void;
export { findPaginatedResourcePath, get, set };

View file

@ -0,0 +1,18 @@
type CursorValue = string | null;
type PageInfoForward = {
hasNextPage: boolean;
endCursor: CursorValue;
};
type PageInfoBackward = {
hasPreviousPage: boolean;
startCursor: CursorValue;
};
type PageInfo = PageInfoForward | PageInfoBackward;
type PageInfoContext = {
pageInfo: PageInfo;
pathInQuery: string[];
};
declare const getCursorFrom: (pageInfo: PageInfo) => CursorValue;
declare const hasAnotherPage: (pageInfo: PageInfo) => boolean;
export { getCursorFrom, hasAnotherPage };
export type { PageInfo, PageInfoForward, PageInfoBackward, PageInfoContext, CursorValue, };

View file

@ -0,0 +1,3 @@
import type { Octokit } from "@octokit/core";
declare const createPaginate: (octokit: Octokit) => <ResponseType extends object = any>(query: string, initialParameters?: Record<string, any>) => Promise<ResponseType>;
export { createPaginate };

View file

@ -0,0 +1 @@
export declare const VERSION = "5.2.4";

View file

@ -0,0 +1,54 @@
{
"name": "@octokit/plugin-paginate-graphql",
"publishConfig": {
"access": "public",
"provenance": true
},
"type": "module",
"version": "5.2.4",
"description": "Octokit plugin to paginate GraphQL API endpoint responses",
"repository": "github:octokit/plugin-paginate-graphql.js",
"keywords": [
"github",
"api",
"sdk",
"toolkit"
],
"license": "MIT",
"peerDependencies": {
"@octokit/core": ">=6"
},
"devDependencies": {
"@octokit/core": "^6.0.0",
"@octokit/plugin-rest-endpoint-methods": "^13.0.0",
"@octokit/tsconfig": "^4.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"esbuild": "^0.24.0",
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
"glob": "^10.2.6",
"jest": "^29.0.0",
"npm-run-all2": "^6.0.0",
"prettier": "3.3.3",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist-*/**",
"bin/**"
],
"types": "dist-types/index.d.ts",
"exports": {
".": {
"types": "./dist-types/index.d.ts",
"import": "./dist-bundle/index.js",
"default": "./dist-bundle/index.js"
}
},
"sideEffects": false
}

View file

@ -0,0 +1,7 @@
MIT License Copyright (c) 2019 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,272 @@
# plugin-paginate-rest.js
> Octokit plugin to paginate REST API endpoint responses
[![@latest](https://img.shields.io/npm/v/@octokit/plugin-paginate-rest.svg)](https://www.npmjs.com/package/@octokit/plugin-paginate-rest)
[![Build Status](https://github.com/octokit/plugin-paginate-rest.js/workflows/Test/badge.svg)](https://github.com/octokit/plugin-paginate-rest.js/actions?workflow=Test)
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load `@octokit/plugin-paginate-rest` and [`@octokit/core`](https://github.com/octokit/core.js) (or core-compatible module) directly from [esm.sh](https://esm.sh)
```html
<script type="module">
import { Octokit } from "https://esm.sh/@octokit/core";
import {
paginateRest,
composePaginateRest,
} from "https://esm.sh/@octokit/plugin-paginate-rest";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with `npm install @octokit/core @octokit/plugin-paginate-rest`. Optionally replace `@octokit/core` with a core-compatible module
```js
import { Octokit } from "@octokit/core";
import {
paginateRest,
composePaginateRest,
} from "@octokit/plugin-paginate-rest";
```
</td></tr>
</tbody>
</table>
```js
const MyOctokit = Octokit.plugin(paginateRest);
const octokit = new MyOctokit({ auth: "secret123" });
// See https://developer.github.com/v3/issues/#list-issues-for-a-repository
const issues = await octokit.paginate("GET /repos/{owner}/{repo}/issues", {
owner: "octocat",
repo: "hello-world",
since: "2010-10-01",
per_page: 100,
});
```
If you want to utilize the pagination methods in another plugin, use `composePaginateRest`.
```js
function myPlugin(octokit, options) {
return {
allStars({owner, repo}) => {
return composePaginateRest(
octokit,
"GET /repos/{owner}/{repo}/stargazers",
{owner, repo }
)
}
}
}
```
> [!IMPORTANT]
> 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).
## `octokit.paginate()`
The `paginateRest` plugin adds a new `octokit.paginate()` method which accepts the same parameters as [`octokit.request`](https://github.com/octokit/request.js#request). Only "List ..." endpoints such as [List issues for a repository](https://developer.github.com/v3/issues/#list-issues-for-a-repository) are supporting pagination. Their [response includes a Link header](https://developer.github.com/v3/issues/#response-1). For other endpoints, `octokit.paginate()` behaves the same as `octokit.request()`.
The `per_page` parameter is usually defaulting to `30`, and can be set to up to `100`, which helps retrieving a big amount of data without hitting the rate limits too soon.
An optional `mapFunction` can be passed to map each page response to a new value, usually an array with only the data you need. This can help to reduce memory usage, as only the relevant data has to be kept in memory until the pagination is complete.
```js
const issueTitles = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues",
{
owner: "octocat",
repo: "hello-world",
since: "2010-10-01",
per_page: 100,
},
(response) => response.data.map((issue) => issue.title),
);
```
The `mapFunction` gets a 2nd argument `done` which can be called to end the pagination early.
```js
const issues = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues",
{
owner: "octocat",
repo: "hello-world",
since: "2010-10-01",
per_page: 100,
},
(response, done) => {
if (response.data.find((issue) => issue.title.includes("something"))) {
done();
}
return response.data;
},
);
```
Alternatively you can pass a `request` method as first argument. This is great when using in combination with [`@octokit/plugin-rest-endpoint-methods`](https://github.com/octokit/plugin-rest-endpoint-methods.js/):
```js
const issues = await octokit.paginate(octokit.rest.issues.listForRepo, {
owner: "octocat",
repo: "hello-world",
since: "2010-10-01",
per_page: 100,
});
```
## `octokit.paginate.iterator()`
If your target runtime environments supports async iterators (such as most modern browsers and Node 10+), you can iterate through each response
```js
const parameters = {
owner: "octocat",
repo: "hello-world",
since: "2010-10-01",
per_page: 100,
};
for await (const response of octokit.paginate.iterator(
"GET /repos/{owner}/{repo}/issues",
parameters,
)) {
// do whatever you want with each response, break out of the loop, etc.
const issues = response.data;
console.log("%d issues found", issues.length);
}
```
Alternatively you can pass a `request` method as first argument. This is great when using in combination with [`@octokit/plugin-rest-endpoint-methods`](https://github.com/octokit/plugin-rest-endpoint-methods.js/):
```js
const parameters = {
owner: "octocat",
repo: "hello-world",
since: "2010-10-01",
per_page: 100,
};
for await (const response of octokit.paginate.iterator(
octokit.rest.issues.listForRepo,
parameters,
)) {
// do whatever you want with each response, break out of the loop, etc.
const issues = response.data;
console.log("%d issues found", issues.length);
}
```
## `composePaginateRest` and `composePaginateRest.iterator`
The `compose*` methods work just like their `octokit.*` counterparts described above, with the differenct that both methods require an `octokit` instance to be passed as first argument
## How it works
`octokit.paginate()` wraps `octokit.request()`. As long as a `rel="next"` link value is present in the response's `Link` header, it sends another request for that URL, and so on.
Most of GitHub's paginating REST API endpoints return an array, but there are a few exceptions which return an object with a key that includes the items array. For example:
- [Search repositories](https://developer.github.com/v3/search/#example) (key `items`)
- [List check runs for a specific ref](https://developer.github.com/v3/checks/runs/#response-3) (key: `check_runs`)
- [List check suites for a specific ref](https://developer.github.com/v3/checks/suites/#response-1) (key: `check_suites`)
- [List repositories](https://developer.github.com/v3/apps/installations/#list-repositories) for an installation (key: `repositories`)
- [List installations for a user](https://developer.github.com/v3/apps/installations/#response-1) (key `installations`)
`octokit.paginate()` is working around these inconsistencies so you don't have to worry about it.
If a response is lacking the `Link` header, `octokit.paginate()` still resolves with an array, even if the response returns a single object.
## Types
The plugin also exposes some types and runtime type guards for TypeScript projects.
<table>
<tbody valign=top align=left>
<tr><th>
Types
</th><td>
```typescript
import {
PaginateInterface,
PaginatingEndpoints,
} from "@octokit/plugin-paginate-rest";
```
</td></tr>
<tr><th>
Guards
</th><td>
```typescript
import { isPaginatingEndpoint } from "@octokit/plugin-paginate-rest";
```
</td></tr>
</tbody>
</table>
### PaginateInterface
An `interface` that declares all the overloads of the `.paginate` method.
### PaginatingEndpoints
An `interface` which describes all API endpoints supported by the plugin. Some overloads of `.paginate()` method and `composePaginateRest()` function depend on `PaginatingEndpoints`, using the `keyof PaginatingEndpoints` as a type for one of its arguments.
```typescript
import { Octokit } from "@octokit/core";
import {
PaginatingEndpoints,
composePaginateRest,
} from "@octokit/plugin-paginate-rest";
type DataType<T> = "data" extends keyof T ? T["data"] : unknown;
async function myPaginatePlugin<E extends keyof PaginatingEndpoints>(
octokit: Octokit,
endpoint: E,
parameters?: PaginatingEndpoints[E]["parameters"],
): Promise<DataType<PaginatingEndpoints[E]["response"]>> {
return await composePaginateRest(octokit, endpoint, parameters);
}
```
### isPaginatingEndpoint
A type guard, `isPaginatingEndpoint(arg)` returns `true` if `arg` is one of the keys in `PaginatingEndpoints` (is `keyof PaginatingEndpoints`).
```typescript
import { Octokit } from "@octokit/core";
import {
isPaginatingEndpoint,
composePaginateRest,
} from "@octokit/plugin-paginate-rest";
async function myPlugin(octokit: Octokit, arg: unknown) {
if (isPaginatingEndpoint(arg)) {
return await composePaginateRest(octokit, arg);
}
// ...
}
```
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md)
## License
[MIT](LICENSE)

Some files were not shown because too many files have changed in this diff Show more