Skip to content

Commit

Permalink
FAI-9951: Add copy method to FarosClient (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-faros committed Mar 7, 2024
1 parent 788eefb commit f31bc0b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class FarosClient {
readonly visibility?: string;

constructor(
cfg: FarosClientConfig,
private readonly cfg: FarosClientConfig,
readonly logger: Logger<string> = pino({name: 'faros-client'}),
axiosConfig: AxiosRequestConfig = DEFAULT_AXIOS_CONFIG
private readonly axiosConfig: AxiosRequestConfig = DEFAULT_AXIOS_CONFIG
) {
const url = Utils.urlWithoutTrailingSlashes(cfg.url);

Expand All @@ -69,6 +69,24 @@ export class FarosClient {
this.visibility = cfg.visibility;
}

copy(
cfg?: Partial<FarosClientConfig>,
logger?: Logger<string>,
axiosConfig?: AxiosRequestConfig
): FarosClient {
return new FarosClient(
{
...this.cfg,
...cfg,
},
logger ?? this.logger,
{
...this.axiosConfig,
...axiosConfig
}
);
}

async tenant(): Promise<string> {
try {
const {data} = await this.api.get('/users/me');
Expand Down
41 changes: 41 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f31bc0b

Please sign in to comment.