From 887675c554feddd0e80bbb76f577448436008d64 Mon Sep 17 00:00:00 2001 From: Jordan Drake Date: Mon, 29 May 2023 17:23:31 +0100 Subject: [PATCH 1/2] Allow consumers to pass arbitrary api urls --- .changeset/cool-dancers-doubt.md | 5 +++++ src/client.ts | 3 ++- src/context.ts | 1 + src/request.ts | 4 +++- src/tests/raw-request.test.ts | 23 +++++++++++++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .changeset/cool-dancers-doubt.md diff --git a/.changeset/cool-dancers-doubt.md b/.changeset/cool-dancers-doubt.md new file mode 100644 index 0000000..ac8efd0 --- /dev/null +++ b/.changeset/cool-dancers-doubt.md @@ -0,0 +1,5 @@ +--- +'@team-plain/typescript-sdk': minor +--- + +Allow configuration of api url diff --git a/src/client.ts b/src/client.ts index f68d139..8e0d4de 100644 --- a/src/client.ts +++ b/src/client.ts @@ -47,9 +47,10 @@ function unwrapData( export class PlainClient { #ctx: Context; - constructor(options: { apiKey: string }) { + constructor(options: { apiKey: string; apiUrl?: string }) { this.#ctx = { apiKey: options.apiKey, + apiUrl: options.apiUrl, }; } diff --git a/src/context.ts b/src/context.ts index fa26189..bbeec03 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,3 +1,4 @@ export type Context = { apiKey: string; + apiUrl?: string; }; diff --git a/src/request.ts b/src/request.ts index 198a9c6..1da34e4 100644 --- a/src/request.ts +++ b/src/request.ts @@ -6,6 +6,8 @@ import type { Context } from './context'; import type { PlainSDKError } from './error'; import { getMutationErrorFromResponse, isPlainGraphQLResponse } from './graphql-utlities'; +const defaultUrl = 'https://core-api.uk.plain.com/graphql/v1'; + export async function request( ctx: Context, args: { @@ -20,7 +22,7 @@ export async function request( Authorization: `Bearer ${ctx.apiKey}`, }; - const url = 'https://core-api.uk.plain.com/graphql/v1'; + const url = ctx.apiUrl || defaultUrl; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const { data: res } = await axios.post( diff --git a/src/tests/raw-request.test.ts b/src/tests/raw-request.test.ts index 543cbd8..82a28eb 100644 --- a/src/tests/raw-request.test.ts +++ b/src/tests/raw-request.test.ts @@ -36,6 +36,29 @@ describe('raw request', () => { scope.done(); }); + test('uses custom url if provided', async () => { + const query = ''; + const variables = {}; + const scope = nock('https://core-api.uk.getresolve.io') + .post('/graphql/v1', { + query, + variables, + }) + .reply(200, {}); + + const client = new PlainClient({ + apiKey: 'abc', + apiUrl: 'https://core-api.uk.getresolve.io/graphql/v1', + }); + + await client.rawRequest({ + query, + variables, + }); + + scope.done(); + }); + test('handles graphql errors', async () => { const graphqlErrors: PlainGraphQLError[] = [ { From bd6f0cbfab64b2992928be27e30d5558abd38ffd Mon Sep 17 00:00:00 2001 From: Mathias Vagni Date: Wed, 31 May 2023 07:52:29 +0200 Subject: [PATCH 2/2] copy change --- .changeset/cool-dancers-doubt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/cool-dancers-doubt.md b/.changeset/cool-dancers-doubt.md index ac8efd0..4afe625 100644 --- a/.changeset/cool-dancers-doubt.md +++ b/.changeset/cool-dancers-doubt.md @@ -2,4 +2,4 @@ '@team-plain/typescript-sdk': minor --- -Allow configuration of api url +Support providing a custom API endpoint for testing internally at Plain against staging and other environments.