From 024d946d28ea270678da52360d8804b8841a7d84 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 13 Oct 2022 11:19:34 -0700 Subject: [PATCH] fix: set defaults on .endpoint (#410) --- src/with-defaults.ts | 2 +- test/defaults.test.ts | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/with-defaults.ts b/src/with-defaults.ts index 53a08e1a..3be2e5fc 100644 --- a/src/with-defaults.ts +++ b/src/with-defaults.ts @@ -16,6 +16,6 @@ export function withDefaults( return Object.assign(newApi, { defaults: withDefaults.bind(null, newRequest), - endpoint: Request.endpoint, + endpoint: newRequest.endpoint, }); } diff --git a/test/defaults.test.ts b/test/defaults.test.ts index d0d6cbfd..2c46a8a6 100644 --- a/test/defaults.test.ts +++ b/test/defaults.test.ts @@ -1,7 +1,11 @@ import fetchMock from "fetch-mock"; +import { getUserAgent } from "universal-user-agent"; +import { VERSION } from "../src/version"; import { graphql } from "../src"; +const userAgent = `octokit-graphql.js/${VERSION} ${getUserAgent()}`; + describe("graphql.defaults()", () => { it("is a function", () => { expect(graphql.defaults).toBeInstanceOf(Function); @@ -147,4 +151,60 @@ describe("graphql.defaults()", () => { } }`); }); + + it("set defaults on .endpoint", () => { + const mockData = { + repository: { + issues: { + edges: [ + { + node: { + title: "Foo", + }, + }, + { + node: { + title: "Bar", + }, + }, + { + node: { + title: "Baz", + }, + }, + ], + }, + }, + }; + + const authenticatedGraphql = graphql.defaults({ + headers: { + authorization: `token secret123`, + }, + request: { + fetch: fetchMock.sandbox().post( + "https://github.acme-inc.com/api/graphql", + { data: mockData }, + { + headers: { + authorization: "token secret123", + }, + } + ), + }, + }); + + const { request: _request, ...requestOptions } = + // @ts-expect-error - TODO: expects to set { url } but it really shouldn't + authenticatedGraphql.endpoint(); + expect(requestOptions).toStrictEqual({ + method: "POST", + url: "https://api.github.com/graphql", + headers: { + accept: "application/vnd.github.v3+json", + authorization: "token secret123", + "user-agent": userAgent, + }, + }); + }); });