Skip to content

Commit d742815

Browse files
algolia-botFluf22millotp
committed
feat(clients): helper to switch API key in use (generated)
algolia/api-clients-automation#3616 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 08b5f96 commit d742815

File tree

11 files changed

+132
-27
lines changed

11 files changed

+132
-27
lines changed

packages/algoliasearch/lite/src/liteClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ export function createLiteClient({
118118
addAlgoliaAgent(segment: string, version?: string): void {
119119
transporter.algoliaAgent.add({ segment, version });
120120
},
121+
122+
/**
123+
* Helper method to switch the API key used to authenticate the requests.
124+
*
125+
* @param params - Method params.
126+
* @param params.apiKey - The new API Key to use.
127+
*/
128+
setClientApiKey({ apiKey }: { apiKey: string }): void {
129+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
130+
},
121131
/**
122132
* Helper: calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
123133
* Disclaimer: We don't assert that the parameters you pass to this method only contains `hits` requests to prevent impacting search performances, this helper is purely for typing purposes.

packages/client-abtesting/src/abtestingClient.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ export function createAbtestingClient({
9999
transporter.algoliaAgent.add({ segment, version });
100100
},
101101

102+
/**
103+
* Helper method to switch the API key used to authenticate the requests.
104+
*
105+
* @param params - Method params.
106+
* @param params.apiKey - The new API Key to use.
107+
*/
108+
setClientApiKey({ apiKey }: { apiKey: string }): void {
109+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
110+
},
111+
102112
/**
103113
* Creates a new A/B test.
104114
*
@@ -337,14 +347,13 @@ export function createAbtestingClient({
337347
const requestPath = '/2/abtests';
338348
const headers: Headers = {};
339349
const queryParameters: QueryParameters = {};
340-
341350
if (offset !== undefined) {
342351
queryParameters.offset = offset.toString();
343352
}
353+
344354
if (limit !== undefined) {
345355
queryParameters.limit = limit.toString();
346356
}
347-
348357
if (indexPrefix !== undefined) {
349358
queryParameters.indexPrefix = indexPrefix.toString();
350359
}

packages/client-analytics/src/analyticsClient.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ export function createAnalyticsClient({
129129
transporter.algoliaAgent.add({ segment, version });
130130
},
131131

132+
/**
133+
* Helper method to switch the API key used to authenticate the requests.
134+
*
135+
* @param params - Method params.
136+
* @param params.apiKey - The new API Key to use.
137+
*/
138+
setClientApiKey({ apiKey }: { apiKey: string }): void {
139+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
140+
},
141+
132142
/**
133143
* This method allow you to send requests to the Algolia REST API.
134144
*
@@ -277,7 +287,6 @@ export function createAnalyticsClient({
277287
if (index !== undefined) {
278288
queryParameters.index = index.toString();
279289
}
280-
281290
if (startDate !== undefined) {
282291
queryParameters.startDate = startDate.toString();
283292
}
@@ -325,12 +334,14 @@ export function createAnalyticsClient({
325334
if (index !== undefined) {
326335
queryParameters.index = index.toString();
327336
}
337+
328338
if (startDate !== undefined) {
329339
queryParameters.startDate = startDate.toString();
330340
}
331341
if (endDate !== undefined) {
332342
queryParameters.endDate = endDate.toString();
333343
}
344+
334345
if (tags !== undefined) {
335346
queryParameters.tags = tags.toString();
336347
}
@@ -419,6 +430,7 @@ export function createAnalyticsClient({
419430
if (index !== undefined) {
420431
queryParameters.index = index.toString();
421432
}
433+
422434
if (startDate !== undefined) {
423435
queryParameters.startDate = startDate.toString();
424436
}
@@ -466,7 +478,6 @@ export function createAnalyticsClient({
466478
if (index !== undefined) {
467479
queryParameters.index = index.toString();
468480
}
469-
470481
if (startDate !== undefined) {
471482
queryParameters.startDate = startDate.toString();
472483
}
@@ -517,10 +528,10 @@ export function createAnalyticsClient({
517528
if (startDate !== undefined) {
518529
queryParameters.startDate = startDate.toString();
519530
}
520-
521531
if (endDate !== undefined) {
522532
queryParameters.endDate = endDate.toString();
523533
}
534+
524535
if (tags !== undefined) {
525536
queryParameters.tags = tags.toString();
526537
}
@@ -562,14 +573,12 @@ export function createAnalyticsClient({
562573
if (index !== undefined) {
563574
queryParameters.index = index.toString();
564575
}
565-
566576
if (startDate !== undefined) {
567577
queryParameters.startDate = startDate.toString();
568578
}
569579
if (endDate !== undefined) {
570580
queryParameters.endDate = endDate.toString();
571581
}
572-
573582
if (tags !== undefined) {
574583
queryParameters.tags = tags.toString();
575584
}
@@ -608,10 +617,10 @@ export function createAnalyticsClient({
608617
const requestPath = '/2/conversions/purchaseRate';
609618
const headers: Headers = {};
610619
const queryParameters: QueryParameters = {};
611-
612620
if (index !== undefined) {
613621
queryParameters.index = index.toString();
614622
}
623+
615624
if (startDate !== undefined) {
616625
queryParameters.startDate = startDate.toString();
617626
}
@@ -659,10 +668,10 @@ export function createAnalyticsClient({
659668
if (index !== undefined) {
660669
queryParameters.index = index.toString();
661670
}
662-
663671
if (startDate !== undefined) {
664672
queryParameters.startDate = startDate.toString();
665673
}
674+
666675
if (endDate !== undefined) {
667676
queryParameters.endDate = endDate.toString();
668677
}
@@ -711,10 +720,10 @@ export function createAnalyticsClient({
711720
if (startDate !== undefined) {
712721
queryParameters.startDate = startDate.toString();
713722
}
714-
715723
if (endDate !== undefined) {
716724
queryParameters.endDate = endDate.toString();
717725
}
726+
718727
if (tags !== undefined) {
719728
queryParameters.tags = tags.toString();
720729
}
@@ -755,6 +764,7 @@ export function createAnalyticsClient({
755764
const requestPath = '/2/searches/noClicks';
756765
const headers: Headers = {};
757766
const queryParameters: QueryParameters = {};
767+
758768
if (index !== undefined) {
759769
queryParameters.index = index.toString();
760770
}
@@ -767,7 +777,6 @@ export function createAnalyticsClient({
767777
if (limit !== undefined) {
768778
queryParameters.limit = limit.toString();
769779
}
770-
771780
if (offset !== undefined) {
772781
queryParameters.offset = offset.toString();
773782
}
@@ -814,7 +823,6 @@ export function createAnalyticsClient({
814823
if (index !== undefined) {
815824
queryParameters.index = index.toString();
816825
}
817-
818826
if (startDate !== undefined) {
819827
queryParameters.startDate = startDate.toString();
820828
}
@@ -900,6 +908,7 @@ export function createAnalyticsClient({
900908
const requestPath = '/2/countries';
901909
const headers: Headers = {};
902910
const queryParameters: QueryParameters = {};
911+
903912
if (index !== undefined) {
904913
queryParameters.index = index.toString();
905914
}
@@ -912,7 +921,6 @@ export function createAnalyticsClient({
912921
if (limit !== undefined) {
913922
queryParameters.limit = limit.toString();
914923
}
915-
916924
if (offset !== undefined) {
917925
queryParameters.offset = offset.toString();
918926
}
@@ -957,10 +965,10 @@ export function createAnalyticsClient({
957965
const requestPath = '/2/filters';
958966
const headers: Headers = {};
959967
const queryParameters: QueryParameters = {};
960-
961968
if (index !== undefined) {
962969
queryParameters.index = index.toString();
963970
}
971+
964972
if (search !== undefined) {
965973
queryParameters.search = search.toString();
966974
}
@@ -1042,6 +1050,7 @@ export function createAnalyticsClient({
10421050
if (offset !== undefined) {
10431051
queryParameters.offset = offset.toString();
10441052
}
1053+
10451054
if (tags !== undefined) {
10461055
queryParameters.tags = tags.toString();
10471056
}

packages/client-insights/src/insightsClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ export function createInsightsClient({
9292
transporter.algoliaAgent.add({ segment, version });
9393
},
9494

95+
/**
96+
* Helper method to switch the API key used to authenticate the requests.
97+
*
98+
* @param params - Method params.
99+
* @param params.apiKey - The new API Key to use.
100+
*/
101+
setClientApiKey({ apiKey }: { apiKey: string }): void {
102+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
103+
},
104+
95105
/**
96106
* This method allow you to send requests to the Algolia REST API.
97107
*

packages/client-personalization/src/personalizationClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ export function createPersonalizationClient({
9595
transporter.algoliaAgent.add({ segment, version });
9696
},
9797

98+
/**
99+
* Helper method to switch the API key used to authenticate the requests.
100+
*
101+
* @param params - Method params.
102+
* @param params.apiKey - The new API Key to use.
103+
*/
104+
setClientApiKey({ apiKey }: { apiKey: string }): void {
105+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
106+
},
107+
98108
/**
99109
* This method allow you to send requests to the Algolia REST API.
100110
*

packages/client-query-suggestions/src/querySuggestionsClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ export function createQuerySuggestionsClient({
9999
transporter.algoliaAgent.add({ segment, version });
100100
},
101101

102+
/**
103+
* Helper method to switch the API key used to authenticate the requests.
104+
*
105+
* @param params - Method params.
106+
* @param params.apiKey - The new API Key to use.
107+
*/
108+
setClientApiKey({ apiKey }: { apiKey: string }): void {
109+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
110+
},
111+
102112
/**
103113
* Creates a new Query Suggestions configuration. You can have up to 100 configurations per Algolia application.
104114
*

packages/client-search/src/searchClient.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ export function createSearchClient({
225225
transporter.algoliaAgent.add({ segment, version });
226226
},
227227

228+
/**
229+
* Helper method to switch the API key used to authenticate the requests.
230+
*
231+
* @param params - Method params.
232+
* @param params.apiKey - The new API Key to use.
233+
*/
234+
setClientApiKey({ apiKey }: { apiKey: string }): void {
235+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
236+
},
237+
228238
/**
229239
* Helper: Wait for a task to be published (completed) for a given `indexName` and `taskID`.
230240
*
@@ -1471,7 +1481,6 @@ export function createSearchClient({
14711481
.replace('{objectID}', encodeURIComponent(objectID));
14721482
const headers: Headers = {};
14731483
const queryParameters: QueryParameters = {};
1474-
14751484
if (forwardToReplicas !== undefined) {
14761485
queryParameters.forwardToReplicas = forwardToReplicas.toString();
14771486
}
@@ -1610,6 +1619,7 @@ export function createSearchClient({
16101619
if (offset !== undefined) {
16111620
queryParameters.offset = offset.toString();
16121621
}
1622+
16131623
if (length !== undefined) {
16141624
queryParameters.length = length.toString();
16151625
}
@@ -2019,7 +2029,6 @@ export function createSearchClient({
20192029
if (page !== undefined) {
20202030
queryParameters.page = page.toString();
20212031
}
2022-
20232032
if (hitsPerPage !== undefined) {
20242033
queryParameters.hitsPerPage = hitsPerPage.toString();
20252034
}
@@ -2052,10 +2061,10 @@ export function createSearchClient({
20522061
const requestPath = '/1/clusters/mapping';
20532062
const headers: Headers = {};
20542063
const queryParameters: QueryParameters = {};
2055-
20562064
if (page !== undefined) {
20572065
queryParameters.page = page.toString();
20582066
}
2067+
20592068
if (hitsPerPage !== undefined) {
20602069
queryParameters.hitsPerPage = hitsPerPage.toString();
20612070
}
@@ -2355,7 +2364,6 @@ export function createSearchClient({
23552364
.replace('{objectID}', encodeURIComponent(objectID));
23562365
const headers: Headers = {};
23572366
const queryParameters: QueryParameters = {};
2358-
23592367
if (forwardToReplicas !== undefined) {
23602368
queryParameters.forwardToReplicas = forwardToReplicas.toString();
23612369
}
@@ -2399,10 +2407,10 @@ export function createSearchClient({
23992407
const requestPath = '/1/indexes/{indexName}/rules/batch'.replace('{indexName}', encodeURIComponent(indexName));
24002408
const headers: Headers = {};
24012409
const queryParameters: QueryParameters = {};
2402-
24032410
if (forwardToReplicas !== undefined) {
24042411
queryParameters.forwardToReplicas = forwardToReplicas.toString();
24052412
}
2413+
24062414
if (clearExistingRules !== undefined) {
24072415
queryParameters.clearExistingRules = clearExistingRules.toString();
24082416
}

packages/client-usage/src/usageClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ export function createUsageClient({
8686
transporter.algoliaAgent.add({ segment, version });
8787
},
8888

89+
/**
90+
* Helper method to switch the API key used to authenticate the requests.
91+
*
92+
* @param params - Method params.
93+
* @param params.apiKey - The new API Key to use.
94+
*/
95+
setClientApiKey({ apiKey }: { apiKey: string }): void {
96+
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
97+
},
98+
8999
/**
90100
* This method allow you to send requests to the Algolia REST API.
91101
*

0 commit comments

Comments
 (0)