Skip to content

Commit a106640

Browse files
committed
feat(javascript): switch api key helper
1 parent e3ff9ee commit a106640

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clients/algoliasearch-client-javascript/packages/client-common/src/createAuth.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export function createAuth(
77
): {
88
readonly headers: () => Headers;
99
readonly queryParameters: () => QueryParameters;
10+
readonly setApiKey: (newApiKey: string) => void;
1011
} {
1112
const credentials = {
1213
'x-algolia-api-key': apiKey,
@@ -21,5 +22,9 @@ export function createAuth(
2122
queryParameters(): QueryParameters {
2223
return authMode === 'WithinQueryParameters' ? credentials : {};
2324
},
25+
26+
setApiKey(newApiKey: string): void {
27+
credentials['x-algolia-api-key'] = newApiKey;
28+
},
2429
};
2530
}

templates/javascript/clients/api-single.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
7575
transporter.algoliaAgent.add({ segment, version });
7676
},
7777

78+
/**
79+
* Helper method to switch the API key used to authenticate the requests.
80+
*
81+
* @param apiKey - The new API Key to use.
82+
*/
83+
setApiKey(apiKey: string): void {
84+
auth.setApiKey(apiKey);
85+
},
86+
7887
{{#isSearchClient}}
7988
{{> client/api/helpers}}
8089
{{/isSearchClient}}

0 commit comments

Comments
 (0)