Skip to content

Commit

Permalink
feat(clients): helper to switch API key in use (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3616

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
3 people committed Sep 6, 2024
1 parent 08b5f96 commit d742815
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 27 deletions.
10 changes: 10 additions & 0 deletions packages/algoliasearch/lite/src/liteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ export function createLiteClient({
addAlgoliaAgent(segment: string, version?: string): void {
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},
/**
* Helper: calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
* 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.
Expand Down
13 changes: 11 additions & 2 deletions packages/client-abtesting/src/abtestingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export function createAbtestingClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* Creates a new A/B test.
*
Expand Down Expand Up @@ -337,14 +347,13 @@ export function createAbtestingClient({
const requestPath = '/2/abtests';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (offset !== undefined) {
queryParameters.offset = offset.toString();
}

if (limit !== undefined) {
queryParameters.limit = limit.toString();
}

if (indexPrefix !== undefined) {
queryParameters.indexPrefix = indexPrefix.toString();
}
Expand Down
33 changes: 21 additions & 12 deletions packages/client-analytics/src/analyticsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ export function createAnalyticsClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* This method allow you to send requests to the Algolia REST API.
*
Expand Down Expand Up @@ -277,7 +287,6 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -325,12 +334,14 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -419,6 +430,7 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -466,7 +478,6 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -517,10 +528,10 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -562,14 +573,12 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -608,10 +617,10 @@ export function createAnalyticsClient({
const requestPath = '/2/conversions/purchaseRate';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -659,10 +668,10 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -711,10 +720,10 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -755,6 +764,7 @@ export function createAnalyticsClient({
const requestPath = '/2/searches/noClicks';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (index !== undefined) {
queryParameters.index = index.toString();
}
Expand All @@ -767,7 +777,6 @@ export function createAnalyticsClient({
if (limit !== undefined) {
queryParameters.limit = limit.toString();
}

if (offset !== undefined) {
queryParameters.offset = offset.toString();
}
Expand Down Expand Up @@ -814,7 +823,6 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -900,6 +908,7 @@ export function createAnalyticsClient({
const requestPath = '/2/countries';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (index !== undefined) {
queryParameters.index = index.toString();
}
Expand All @@ -912,7 +921,6 @@ export function createAnalyticsClient({
if (limit !== undefined) {
queryParameters.limit = limit.toString();
}

if (offset !== undefined) {
queryParameters.offset = offset.toString();
}
Expand Down Expand Up @@ -957,10 +965,10 @@ export function createAnalyticsClient({
const requestPath = '/2/filters';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (index !== undefined) {
queryParameters.index = index.toString();
}

if (search !== undefined) {
queryParameters.search = search.toString();
}
Expand Down Expand Up @@ -1042,6 +1050,7 @@ export function createAnalyticsClient({
if (offset !== undefined) {
queryParameters.offset = offset.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down
10 changes: 10 additions & 0 deletions packages/client-insights/src/insightsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ export function createInsightsClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* This method allow you to send requests to the Algolia REST API.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/client-personalization/src/personalizationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export function createPersonalizationClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* This method allow you to send requests to the Algolia REST API.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/client-query-suggestions/src/querySuggestionsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export function createQuerySuggestionsClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* Creates a new Query Suggestions configuration. You can have up to 100 configurations per Algolia application.
*
Expand Down
18 changes: 13 additions & 5 deletions packages/client-search/src/searchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ export function createSearchClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* Helper: Wait for a task to be published (completed) for a given `indexName` and `taskID`.
*
Expand Down Expand Up @@ -1471,7 +1481,6 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -1610,6 +1619,7 @@ export function createSearchClient({
if (offset !== undefined) {
queryParameters.offset = offset.toString();
}

if (length !== undefined) {
queryParameters.length = length.toString();
}
Expand Down Expand Up @@ -2019,7 +2029,6 @@ export function createSearchClient({
if (page !== undefined) {
queryParameters.page = page.toString();
}

if (hitsPerPage !== undefined) {
queryParameters.hitsPerPage = hitsPerPage.toString();
}
Expand Down Expand Up @@ -2052,10 +2061,10 @@ export function createSearchClient({
const requestPath = '/1/clusters/mapping';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (page !== undefined) {
queryParameters.page = page.toString();
}

if (hitsPerPage !== undefined) {
queryParameters.hitsPerPage = hitsPerPage.toString();
}
Expand Down Expand Up @@ -2355,7 +2364,6 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -2399,10 +2407,10 @@ export function createSearchClient({
const requestPath = '/1/indexes/{indexName}/rules/batch'.replace('{indexName}', encodeURIComponent(indexName));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}

if (clearExistingRules !== undefined) {
queryParameters.clearExistingRules = clearExistingRules.toString();
}
Expand Down
10 changes: 10 additions & 0 deletions packages/client-usage/src/usageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export function createUsageClient({
transporter.algoliaAgent.add({ segment, version });
},

/**
* Helper method to switch the API key used to authenticate the requests.
*
* @param params - Method params.
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
},

/**
* This method allow you to send requests to the Algolia REST API.
*
Expand Down
Loading

0 comments on commit d742815

Please sign in to comment.