diff --git a/README.md b/README.md index 0aba65e34..c034e311c 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ const { data: app } = await requestWithAuth( owner: "octocat", repo: "hello-world", title: "Hello from the engine room", - } + }, ); ``` @@ -245,35 +245,36 @@ const { data: app } = await requestWithAuth( - options.mediaType.format + options.method String - Media type param, such as `raw`, `html`, or `full`. See Media Types. + Any supported http verb, case insensitive. Defaults to Get. - options.mediaType.previews + options.mediaType.format - Array of strings + String - Name of previews, such as `mercy`, `symmetra`, or `scarlet-witch`. See API Previews. + Media type param, such as `raw`, `html`, or `full`. See Media Types. - options.method + options.mediaType.previews - String + Array of strings - Any supported http verb, case insensitive. Defaults to Get. + Name of previews, such as `mercy`, `symmetra`, or `scarlet-witch`. See GraphQL Schema Previews. + Note that these only apply to GraphQL requests and have no effect on REST routes. @@ -299,17 +300,7 @@ const { data: app } = await requestWithAuth( Set request body directly instead of setting it to JSON based on additional parameters. See "The `data` parameter" below. - - - options.request.agent - - - http(s).Agent instance - - - Node only. Useful for custom proxy, certificate, or dns lookup. - - + options.request.fetch @@ -532,7 +523,7 @@ request( authorization: `token 0000000000000000000000000000000000000001`, }, data: "Hello, world!", - } + }, ); ``` diff --git a/package-lock.json b/package-lock.json index 413c3dd0e..e720632ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@octokit/endpoint": "^8.0.1", "@octokit/request-error": "^4.0.1", - "@octokit/types": "^10.0.0", + "@octokit/types": "^11.0.0", "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, @@ -28,7 +28,7 @@ "glob": "^10.2.4", "jest": "^29.0.0", "lolex": "^6.0.0", - "prettier": "2.8.8", + "prettier": "3.0.0", "semantic-release-plugin-update-version-in-files": "^1.0.0", "string-to-arraybuffer": "^1.0.2", "ts-jest": "^29.0.0", @@ -2000,6 +2000,14 @@ "node": ">= 18" } }, + "node_modules/@octokit/endpoint/node_modules/@octokit/types": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-10.0.0.tgz", + "integrity": "sha512-Vm8IddVmhCgU1fxC1eyinpwqzXPEYu0NrYzD3YZjlGjyftdLBTeqNblRC0jmJmgxbJIsQlyogVeGnrNaaMVzIg==", + "dependencies": { + "@octokit/openapi-types": "^18.0.0" + } + }, "node_modules/@octokit/oauth-authorization-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/@octokit/oauth-authorization-url/-/oauth-authorization-url-5.0.0.tgz", @@ -2083,6 +2091,14 @@ "node": ">= 18" } }, + "node_modules/@octokit/request-error/node_modules/@octokit/types": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-10.0.0.tgz", + "integrity": "sha512-Vm8IddVmhCgU1fxC1eyinpwqzXPEYu0NrYzD3YZjlGjyftdLBTeqNblRC0jmJmgxbJIsQlyogVeGnrNaaMVzIg==", + "dependencies": { + "@octokit/openapi-types": "^18.0.0" + } + }, "node_modules/@octokit/request/node_modules/@octokit/endpoint": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-7.0.6.tgz", @@ -2113,9 +2129,9 @@ "dev": true }, "node_modules/@octokit/types": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/@octokit/types/-/types-10.0.0.tgz", - "integrity": "sha512-Vm8IddVmhCgU1fxC1eyinpwqzXPEYu0NrYzD3YZjlGjyftdLBTeqNblRC0jmJmgxbJIsQlyogVeGnrNaaMVzIg==", + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.0.0.tgz", + "integrity": "sha512-h4iyfMpQUdub1itwTn6y7z2a3EtPuer1paKfsIbZErv0LBbZYGq6haiPUPJys/LetPqgcX3ft33O16XuS03Anw==", "dependencies": { "@octokit/openapi-types": "^18.0.0" } @@ -5791,15 +5807,15 @@ } }, "node_modules/prettier": { - "version": "2.8.8", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", - "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.0.tgz", + "integrity": "sha512-zBf5eHpwHOGPC47h0zrPyNn+eAEIdEzfywMoYn2XPi0P44Zp0tSq64rq0xAREh4auw2cJZHo9QUob+NqCQky4g==", "dev": true, "bin": { - "prettier": "bin-prettier.js" + "prettier": "bin/prettier.cjs" }, "engines": { - "node": ">=10.13.0" + "node": ">=14" }, "funding": { "url": "https://github.com/prettier/prettier?sponsor=1" diff --git a/package.json b/package.json index e0583f1d2..97bc727e6 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "dependencies": { "@octokit/endpoint": "^8.0.1", "@octokit/request-error": "^4.0.1", - "@octokit/types": "^10.0.0", + "@octokit/types": "^11.0.0", "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, @@ -41,7 +41,7 @@ "glob": "^10.2.4", "jest": "^29.0.0", "lolex": "^6.0.0", - "prettier": "2.8.8", + "prettier": "3.0.0", "semantic-release-plugin-update-version-in-files": "^1.0.0", "string-to-arraybuffer": "^1.0.2", "ts-jest": "^29.0.0", diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 2158fdc3a..edcfd0f69 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -7,7 +7,7 @@ import getBuffer from "./get-buffer-response"; export default function fetchWrapper( requestOptions: ReturnType & { redirect?: "error" | "follow" | "manual"; - } + }, ) { const log = requestOptions.request && requestOptions.request.log @@ -32,27 +32,20 @@ export default function fetchWrapper( if (!fetch) { throw new Error( - 'Global "fetch" not found. Please provide `options.request.fetch` to octokit or upgrade to node@18 or newer.' + 'Global "fetch" not found. Please provide `options.request.fetch` to octokit or upgrade to node@18 or newer.', ); } - return fetch( - requestOptions.url, - Object.assign( - { - method: requestOptions.method, - body: requestOptions.body, - headers: requestOptions.headers as HeadersInit, - redirect: requestOptions.redirect, - // duplex must be set if request.body is ReadableStream or Async Iterables. - // See https://fetch.spec.whatwg.org/#dom-requestinit-duplex. - ...(requestOptions.body && { duplex: "half" }), - }, - // `requestOptions.request.agent` type is incompatible - // see https://github.com/octokit/types.ts/pull/264 - requestOptions.request as any - ) - ) + return fetch(requestOptions.url, { + method: requestOptions.method, + body: requestOptions.body, + headers: requestOptions.headers as HeadersInit, + signal: (requestOptions as any).signal, + data: (requestOptions as any).data, + // duplex must be set if request.body is ReadableStream or Async Iterables. + // See https://fetch.spec.whatwg.org/#dom-requestinit-duplex. + ...(requestOptions.body && { duplex: "half" }), + }) .then(async (response) => { url = response.url; status = response.status; @@ -70,7 +63,7 @@ export default function fetchWrapper( requestOptions.url }" is deprecated. It is scheduled to be removed on ${headers.sunset}${ deprecationLink ? `. See ${deprecationLink}` : "" - }` + }`, ); } diff --git a/src/with-defaults.ts b/src/with-defaults.ts index c6a094c46..88c7ab741 100644 --- a/src/with-defaults.ts +++ b/src/with-defaults.ts @@ -10,12 +10,12 @@ import type { export default function withDefaults( oldEndpoint: EndpointInterface, - newDefaults: RequestParameters + newDefaults: RequestParameters, ): RequestInterface { const endpoint = oldEndpoint.defaults(newDefaults); const newApi = function ( route: Route | EndpointOptions, - parameters?: RequestParameters + parameters?: RequestParameters, ): Promise> { const endpointOptions = endpoint.merge(route, parameters); @@ -25,10 +25,10 @@ export default function withDefaults( const request = ( route: Route | EndpointOptions, - parameters?: RequestParameters + parameters?: RequestParameters, ) => { return fetchWrapper( - endpoint.parse(endpoint.merge(route, parameters)) + endpoint.parse(endpoint.merge(route, parameters)), ); }; diff --git a/test/defaults.test.ts b/test/defaults.test.ts index 1130db90c..6098ec9b2 100644 --- a/test/defaults.test.ts +++ b/test/defaults.test.ts @@ -19,7 +19,7 @@ describe("endpoint.defaults()", () => { authorization: "token 0000000000000000000000000000000000000001", "user-agent": "myApp/1.2.3", }, - } + }, ); const myRequest = request.defaults({ @@ -52,7 +52,7 @@ describe("endpoint.defaults()", () => { authorization: "token 0000000000000000000000000000000000000001", "user-agent": "myApp/1.2.3", }, - } + }, ); const myProjectRequest = request.defaults({ @@ -77,7 +77,7 @@ describe("endpoint.defaults()", () => { return myProjectRequestWithAuth(`GET /orgs/{org}/repos`).then( (response) => { expect(response.status).toEqual(200); - } + }, ); }); }); diff --git a/test/request.test.ts b/test/request.test.ts index 12c3119fd..59263941b 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -122,7 +122,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== "user-agent": userAgent, authorization: `bearer ${BEARER}`, }, - } + }, ) .postOnce( "https://api.github.com/repos/octocat/hello-world/issues", @@ -133,7 +133,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== "user-agent": userAgent, authorization: `token secret123`, }, - } + }, ); const auth = createAppAuth({ appId: APP_ID, @@ -238,7 +238,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== return request( `HEAD /repos/{owner}/{repo}/pulls/{number}`, - Object.assign(options, { number: 2 }) + Object.assign(options, { number: 2 }), ); }) @@ -260,13 +260,13 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== status: 200, body: Buffer.from( "1f8b0800000000000003cb4f2ec9cfce2cd14dcbac28292d4ad5cd2f4ad74d4f2dd14d2c4acec82c4bd53580007d060a0050bfb9b9a90203c428741ac2313436343307222320dbc010a8dc5c81c194124b8905a5c525894540a714e5e797e05347481edd734304e41319ff41ae8e2ebeae7ab92964d801d46f66668227fe0d4d51e3dfc8d0c8d808284f75df6201233cfe951590627ba01d330a46c1281805a3806e000024cb59d6000a0000", - "hex" + "hex", ), headers: { "content-type": "application/x-gzip", "content-length": "172", }, - } + }, ); return request("GET /repos/{owner}/{repo}/{archive_format}/{ref}", { @@ -295,13 +295,13 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== // body: Buffer.from('1f8b0800000000000003cb4f2ec9cfce2cd14dcbac28292d4ad5cd2f4ad74d4f2dd14d2c4acec82c4bd53580007d060a0050bfb9b9a90203c428741ac2313436343307222320dbc010a8dc5c81c194124b8905a5c525894540a714e5e797e05347481edd734304e41319ff41ae8e2ebeae7ab92964d801d46f66668227fe0d4d51e3dfc8d0c8d808284f75df6201233cfe951590627ba01d330a46c1281805a3806e000024cb59d6000a0000', 'hex'), body: Buffer.from( "1f8b0800000000000003cb4f2ec9cfce2cd14dcbac28292d4ad5cd2f4ad74d4f2dd14d2c4acec82c4bd53580007d060a0050bfb9b9a90203c428741ac2313436343307222320dbc010a8dc5c81c194124b8905a5c525894540a714e5e797e05347481edd734304e41319ff41ae8e2ebeae7ab92964d801d46f66668227fe0d4d51e3dfc8d0c8d808284f75df6201233cfe951590627ba01d330a46c1281805a3806e000024cb59d6000a0000", - "hex" + "hex", ), headers: { "content-type": "application/x-gzip", "content-length": "172", }, - } + }, ); return request( @@ -310,7 +310,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== request: { fetch: mock, }, - } + }, ); }); @@ -396,7 +396,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== } globalThis.fetch = originalFetch; expect(error?.message).toEqual( - 'Global "fetch" not found. Please provide `options.request.fetch` to octokit or upgrade to node@18 or newer.' + 'Global "fetch" not found. Please provide `options.request.fetch` to octokit or upgrade to node@18 or newer.', ); }); @@ -445,7 +445,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== .get( (_url, { headers }) => (headers as ResponseHeaders)["user-agent"] === "funky boom boom pow", - 200 + 200, ); return request("GET /", { @@ -491,7 +491,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== expect(error.status).toEqual(422); expect(error.response.headers["x-foo"]).toEqual("bar"); expect(error.response.data.documentation_url).toEqual( - "https://developer.github.com/v3/issues/labels/#create-a-label" + "https://developer.github.com/v3/issues/labels/#create-a-label", ); expect(error.response.data.errors).toEqual([ { resource: "Label", code: "invalid", field: "color" }, @@ -531,7 +531,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== }, }).catch((error) => { expect(error.request.url).toEqual( - "https://api.github.com/?client_id=123&client_secret=[REDACTED]" + "https://api.github.com/?client_id=123&client_secret=[REDACTED]", ); }); }); @@ -557,27 +557,8 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== return request("https://www.githubstatus.com/api/v2/status.json").then( ({ url }) => { expect(url).toEqual("https://www.githubstatus.com/api/v2/status.json"); - } - ); - }); - - it("options.request.signal is passed as option to fetch", function () { - return request("/", { - request: { - // We pass a value that is not an `AbortSignal`, and expect `fetch` to - // throw an exception complaining about the value - signal: "funk", }, - }) - .then(() => { - throw new Error("Should not resolve"); - }) - - .catch((error) => { - // We can't match on the entire string because the message differs between - // Node versions. - expect(error.message).toMatch(/AbortSignal/); - }); + ); }); it("options.request.fetch", function () { @@ -608,7 +589,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== headers: { "x-foo": "bar", }, - } + }, ); const hook = (request: RequestInterface, options: EndpointOptions) => { @@ -728,7 +709,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== accept: "application/vnd.github.v3+json", authorization: "token secret123", }, - } + }, ); return request("PUT /repos/{owner}/{repo}/branches/{branch}/protection", { @@ -758,7 +739,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== .catch((error) => { expect(error).toHaveProperty( "message", - `Validation failed: "Only organization repositories can have users and team restrictions", {"resource":"Search","field":"q","code":"invalid"}` + `Validation failed: "Only organization repositories can have users and team restrictions", {"resource":"Search","field":"q","code":"invalid"}`, ); }); }); @@ -782,7 +763,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== authorization: "token 0000000000000000000000000000000000000001", "user-agent": userAgent, }, - } + }, ); const warn = jest.fn(); @@ -797,7 +778,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== expect(response.data).toEqual({ id: 123 }); expect(warn).toHaveBeenCalledTimes(1); expect(warn).toHaveBeenCalledWith( - '[@octokit/request] "GET https://api.github.com/teams/123" is deprecated. It is scheduled to be removed on Mon, 01 Feb 2021 00:00:00 GMT. See https://developer.github.com/changes/2020-01-21-moving-the-team-api-endpoints/' + '[@octokit/request] "GET https://api.github.com/teams/123" is deprecated. It is scheduled to be removed on Mon, 01 Feb 2021 00:00:00 GMT. See https://developer.github.com/changes/2020-01-21-moving-the-team-api-endpoints/', ); }); }); @@ -820,7 +801,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== authorization: "token 0000000000000000000000000000000000000001", "user-agent": userAgent, }, - } + }, ); const warn = jest.fn(); @@ -835,7 +816,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== expect(response.data).toEqual({ id: 123 }); expect(warn).toHaveBeenCalledTimes(1); expect(warn).toHaveBeenCalledWith( - '[@octokit/request] "GET https://api.github.com/teams/123" is deprecated. It is scheduled to be removed on Mon, 01 Feb 2021 00:00:00 GMT' + '[@octokit/request] "GET https://api.github.com/teams/123" is deprecated. It is scheduled to be removed on Mon, 01 Feb 2021 00:00:00 GMT', ); }); }); @@ -861,7 +842,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== expect(error.status).toEqual(404); expect(error.response.data.message).toEqual("Not Found"); expect(error.response.data.documentation_url).toEqual( - "https://docs.github.com/en/rest/reference/repos#get-a-repository" + "https://docs.github.com/en/rest/reference/repos#get-a-repository", ); }); }); @@ -908,7 +889,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== "https://api.github.com/repos/octokit-fixture-org/release-assets/releases/v1.0.0/assets", { status: 200, - } + }, ); return request("POST /repos/{owner}/{repo}/releases/{release_id}/assets", { @@ -939,7 +920,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== "https://api.github.com/repos/octokit-fixture-org/release-assets/releases/tags/v1.0.0", { status: 200, - } + }, ); return request("POST /repos/{owner}/{repo}/releases/tags/{tag}", { @@ -969,7 +950,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== "https://api.github.com/repos/octokit-fixture-org/release-assets/releases/tags/v1.0.0", { status: 200, - } + }, ); return request("POST /repos/{owner}/{repo}/releases/tags/{tag}", { @@ -988,7 +969,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== }).then((response) => { expect(response.status).toEqual(200); expect(mock.lastOptions()?.body).toEqual( - stringToArrayBuffer("Hello, world!\n") + stringToArrayBuffer("Hello, world!\n"), ); expect(mock.done()).toBe(true); }); @@ -1019,7 +1000,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== "https://api.github.com/repos/octokit-fixture-org/release-assets/releases/tags/v1.0.0", new Promise(() => { abortController.abort(); - }) + }), ); return request("POST /repos/{owner}/{repo}/releases/tags/{tag}", {