diff --git a/src/client.ts b/src/client.ts index d2b0f5c..9a55559 100644 --- a/src/client.ts +++ b/src/client.ts @@ -41,9 +41,9 @@ export class FarosClient { readonly visibility?: string; constructor( - cfg: FarosClientConfig, + private readonly cfg: FarosClientConfig, readonly logger: Logger = pino({name: 'faros-client'}), - axiosConfig: AxiosRequestConfig = DEFAULT_AXIOS_CONFIG + private readonly axiosConfig: AxiosRequestConfig = DEFAULT_AXIOS_CONFIG ) { const url = Utils.urlWithoutTrailingSlashes(cfg.url); @@ -69,6 +69,24 @@ export class FarosClient { this.visibility = cfg.visibility; } + copy( + cfg?: Partial, + logger?: Logger, + axiosConfig?: AxiosRequestConfig + ): FarosClient { + return new FarosClient( + { + ...this.cfg, + ...cfg, + }, + logger ?? this.logger, + { + ...this.axiosConfig, + ...axiosConfig + } + ); + } + async tenant(): Promise { try { const {data} = await this.api.get('/users/me'); diff --git a/test/client.test.ts b/test/client.test.ts index 3b48ce9..aaf9f19 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -267,6 +267,47 @@ describe('client', () => { expect(res).toEqual({result: 'ok'}); }); + test('clone', async () => { + async function test(client: FarosClient, params: URLSearchParams) { + const mock = nock(apiUrl) + .post( + '/graphs/g1/graphql', + JSON.stringify({query: '{ tms_Task { uid } }'}) + ) + .query(params) + .reply(200, {data: {result: 'ok'}}); + const res = await client.gql('g1', '{ tms_Task { uid } }'); + mock.done(); + expect(res).toEqual({result: 'ok'}); + } + const clientConfig = { + url: apiUrl, + apiKey: 'test-key', + useGraphQLV2: true, + }; + const client = new FarosClient(clientConfig); + // baseline test + await test( + client, + new URLSearchParams({phantoms: Phantom.IncludeNestedOnly}), + ); + // test clone w/ different params + await test( + client.copy({phantoms: Phantom.Exclude}), + new URLSearchParams({phantoms: Phantom.Exclude}), + ); + // test clone with no changes + await test( + client.copy(), + new URLSearchParams({phantoms: Phantom.IncludeNestedOnly}), + ); + // test client again to ensure it wasn't changed + await test( + client, + new URLSearchParams({phantoms: Phantom.IncludeNestedOnly}), + ); + }); + test('gql with variables', async () => { const query = `{ query ($pageSize: Int = 10, $after: Cursor) {