Skip to content

Commit 50bd073

Browse files
authored
fix(javascript): assert helpers and fix setClientApiKey helper (#3663)
1 parent bfc6519 commit 50bd073

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

clients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.browser.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ describe('api', () => {
2020

2121
it('provides a `clearCache` method', () => {
2222
expect(client.clearCache).not.toBeUndefined();
23+
expect(() => client.clearCache()).not.toThrow();
24+
});
25+
26+
it('provides a `setClientApiKey` method', () => {
27+
const _client = algoliasearch('foo', 'bar', {
28+
requester: echoRequester(),
29+
});
30+
31+
expect(_client.transporter.baseQueryParameters['x-algolia-api-key']).toEqual('bar')
32+
expect(_client.setClientApiKey).not.toBeUndefined();
33+
_client.setClientApiKey({apiKey:'tabac'})
34+
expect(_client.transporter.baseQueryParameters['x-algolia-api-key']).toEqual('tabac')
2335
});
2436

2537
it('sets the user agent', async () => {

clients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.node.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ describe('api', () => {
2020

2121
it('provides a `clearCache` method', () => {
2222
expect(client.clearCache).not.toBeUndefined();
23+
expect(() => client.clearCache()).not.toThrow();
24+
});
25+
26+
it('provides a `setClientApiKey` method', () => {
27+
const _client = algoliasearch('foo', 'bar', {
28+
requester: echoRequester(),
29+
});
30+
31+
expect(_client.transporter.baseHeaders['x-algolia-api-key']).toEqual('bar')
32+
expect(_client.setClientApiKey).not.toBeUndefined();
33+
_client.setClientApiKey({apiKey:'tabac'})
34+
expect(_client.transporter.baseHeaders['x-algolia-api-key']).toEqual('tabac')
2335
});
2436

2537
it('sets the user agent', async () => {

templates/javascript/clients/api-single.mustache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
7575
},
7676

7777
/**
78-
* Helper method to switch the API key used to authenticate the requests.
79-
*
80-
* @param params - Method params.
81-
* @param params.apiKey - The new API Key to use.
82-
*/
78+
* Helper method to switch the API key used to authenticate the requests.
79+
*
80+
* @param params - Method params.
81+
* @param params.apiKey - The new API Key to use.
82+
*/
8383
setClientApiKey({ apiKey }: { apiKey: string }): void {
84-
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
84+
if (!authMode || authMode === 'WithinHeaders') {
85+
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
86+
} else {
87+
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
88+
}
8589
},
8690

8791
{{#isSearchClient}}

0 commit comments

Comments
 (0)