Skip to content

Commit 887675c

Browse files
committed
Allow consumers to pass arbitrary api urls
1 parent 4bcbd28 commit 887675c

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

.changeset/cool-dancers-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@team-plain/typescript-sdk': minor
3+
---
4+
5+
Allow configuration of api url

src/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ function unwrapData<T, X>(
4747
export class PlainClient {
4848
#ctx: Context;
4949

50-
constructor(options: { apiKey: string }) {
50+
constructor(options: { apiKey: string; apiUrl?: string }) {
5151
this.#ctx = {
5252
apiKey: options.apiKey,
53+
apiUrl: options.apiUrl,
5354
};
5455
}
5556

src/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export type Context = {
22
apiKey: string;
3+
apiUrl?: string;
34
};

src/request.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type { Context } from './context';
66
import type { PlainSDKError } from './error';
77
import { getMutationErrorFromResponse, isPlainGraphQLResponse } from './graphql-utlities';
88

9+
const defaultUrl = 'https://core-api.uk.plain.com/graphql/v1';
10+
911
export async function request<Query, Variables>(
1012
ctx: Context,
1113
args: {
@@ -20,7 +22,7 @@ export async function request<Query, Variables>(
2022
Authorization: `Bearer ${ctx.apiKey}`,
2123
};
2224

23-
const url = 'https://core-api.uk.plain.com/graphql/v1';
25+
const url = ctx.apiUrl || defaultUrl;
2426

2527
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2628
const { data: res } = await axios.post(

src/tests/raw-request.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ describe('raw request', () => {
3636
scope.done();
3737
});
3838

39+
test('uses custom url if provided', async () => {
40+
const query = '';
41+
const variables = {};
42+
const scope = nock('https://core-api.uk.getresolve.io')
43+
.post('/graphql/v1', {
44+
query,
45+
variables,
46+
})
47+
.reply(200, {});
48+
49+
const client = new PlainClient({
50+
apiKey: 'abc',
51+
apiUrl: 'https://core-api.uk.getresolve.io/graphql/v1',
52+
});
53+
54+
await client.rawRequest({
55+
query,
56+
variables,
57+
});
58+
59+
scope.done();
60+
});
61+
3962
test('handles graphql errors', async () => {
4063
const graphqlErrors: PlainGraphQLError[] = [
4164
{

0 commit comments

Comments
 (0)