Skip to content

Commit

Permalink
fix: handle baseUrl suffix for GHES (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Lencioni <[email protected]>
  • Loading branch information
gr2m and lencioni committed Sep 9, 2020
1 parent 9614edf commit fd2b7bf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const NON_VARIABLE_OPTIONS = [
"mediaType",
];

const GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/;

export function graphql<ResponseData = GraphQlQueryResponseData>(
request: typeof Request,
query: string | RequestParameters,
Expand Down Expand Up @@ -45,6 +47,13 @@ export function graphql<ResponseData = GraphQlQueryResponseData>(
{} as GraphQlEndpointOptions
);

// workaround for GitHub Enterprise baseUrl set with /api/v3 suffix
// https://github.com/octokit/auth-app.js/issues/111#issuecomment-657610451
const baseUrl = options.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: ResponseHeaders = {};
Expand Down
26 changes: 26 additions & 0 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,30 @@ describe("graphql.defaults()", () => {
expect(result).toStrictEqual(mockData);
});
});

it("handle baseUrl set with /api/v3 suffix", () => {
const ghesGraphQl = graphql.defaults({
baseUrl: "https://github.acme-inc.com/api/v3",
headers: {
authorization: `token secret123`,
},
request: {
fetch: fetchMock.sandbox().post(
"https://github.acme-inc.com/api/graphql",
{ data: { ok: true } },
{
headers: {
authorization: "token secret123",
},
}
),
},
});

return ghesGraphQl(`query {
viewer {
login
}
}`);
});
});

0 comments on commit fd2b7bf

Please sign in to comment.