Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-dancers-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@team-plain/typescript-sdk': minor
---

Support providing a custom API endpoint for testing internally at Plain against staging and other environments.
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ function unwrapData<T, X>(
export class PlainClient {
#ctx: Context;

constructor(options: { apiKey: string }) {
constructor(options: { apiKey: string; apiUrl?: string }) {
this.#ctx = {
apiKey: options.apiKey,
apiUrl: options.apiUrl,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type Context = {
apiKey: string;
apiUrl?: string;
};
4 changes: 3 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Query, Variables>(
ctx: Context,
args: {
Expand All @@ -20,7 +22,7 @@ export async function request<Query, Variables>(
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(
Expand Down
23 changes: 23 additions & 0 deletions src/tests/raw-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
{
Expand Down