From dac3ea572b92127738a84128b916d9d9a8eda1d6 Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Mon, 9 Sep 2024 06:55:52 +0000 Subject: [PATCH] fix(javascript): assert helpers and fix setClientApiKey helper (generated) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/algolia/api-clients-automation/pull/3663 Co-authored-by: algolia-bot Co-authored-by: Clément Vannicatte --- .../__tests__/algoliasearch.browser.test.ts | 12 +++++++++ .../__tests__/algoliasearch.node.test.ts | 12 +++++++++ packages/algoliasearch/lite/src/liteClient.ts | 6 ++++- .../client-abtesting/src/abtestingClient.ts | 8 ++++-- .../client-analytics/src/analyticsClient.ts | 25 ++++++++++------- .../client-insights/src/insightsClient.ts | 6 ++++- .../src/personalizationClient.ts | 6 ++++- .../src/querySuggestionsClient.ts | 6 ++++- packages/client-search/src/searchClient.ts | 12 +++++---- packages/client-usage/src/usageClient.ts | 6 ++++- packages/ingestion/src/ingestionClient.ts | 27 ++++++++++--------- packages/monitoring/src/monitoringClient.ts | 6 ++++- packages/recommend/src/recommendClient.ts | 6 ++++- 13 files changed, 102 insertions(+), 36 deletions(-) diff --git a/packages/algoliasearch/__tests__/algoliasearch.browser.test.ts b/packages/algoliasearch/__tests__/algoliasearch.browser.test.ts index f45779b9b..d7666a7d6 100644 --- a/packages/algoliasearch/__tests__/algoliasearch.browser.test.ts +++ b/packages/algoliasearch/__tests__/algoliasearch.browser.test.ts @@ -20,6 +20,18 @@ describe('api', () => { it('provides a `clearCache` method', () => { expect(client.clearCache).not.toBeUndefined(); + expect(() => client.clearCache()).not.toThrow(); + }); + + it('provides a `setClientApiKey` method', () => { + const _client = algoliasearch('foo', 'bar', { + requester: echoRequester(), + }); + + expect(_client.transporter.baseQueryParameters['x-algolia-api-key']).toEqual('bar'); + expect(_client.setClientApiKey).not.toBeUndefined(); + _client.setClientApiKey({ apiKey: 'tabac' }); + expect(_client.transporter.baseQueryParameters['x-algolia-api-key']).toEqual('tabac'); }); it('sets the user agent', async () => { diff --git a/packages/algoliasearch/__tests__/algoliasearch.node.test.ts b/packages/algoliasearch/__tests__/algoliasearch.node.test.ts index 42a6f38a3..5fb2aee6d 100644 --- a/packages/algoliasearch/__tests__/algoliasearch.node.test.ts +++ b/packages/algoliasearch/__tests__/algoliasearch.node.test.ts @@ -20,6 +20,18 @@ describe('api', () => { it('provides a `clearCache` method', () => { expect(client.clearCache).not.toBeUndefined(); + expect(() => client.clearCache()).not.toThrow(); + }); + + it('provides a `setClientApiKey` method', () => { + const _client = algoliasearch('foo', 'bar', { + requester: echoRequester(), + }); + + expect(_client.transporter.baseHeaders['x-algolia-api-key']).toEqual('bar'); + expect(_client.setClientApiKey).not.toBeUndefined(); + _client.setClientApiKey({ apiKey: 'tabac' }); + expect(_client.transporter.baseHeaders['x-algolia-api-key']).toEqual('tabac'); }); it('sets the user agent', async () => { diff --git a/packages/algoliasearch/lite/src/liteClient.ts b/packages/algoliasearch/lite/src/liteClient.ts index a9e918005..33746b1b8 100644 --- a/packages/algoliasearch/lite/src/liteClient.ts +++ b/packages/algoliasearch/lite/src/liteClient.ts @@ -127,7 +127,11 @@ export function createLiteClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** * Helper: calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets. diff --git a/packages/client-abtesting/src/abtestingClient.ts b/packages/client-abtesting/src/abtestingClient.ts index 321c0d802..cfe5070a8 100644 --- a/packages/client-abtesting/src/abtestingClient.ts +++ b/packages/client-abtesting/src/abtestingClient.ts @@ -107,7 +107,11 @@ export function createAbtestingClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** @@ -351,10 +355,10 @@ export function createAbtestingClient({ if (offset !== undefined) { queryParameters.offset = offset.toString(); } + if (limit !== undefined) { queryParameters.limit = limit.toString(); } - if (indexPrefix !== undefined) { queryParameters.indexPrefix = indexPrefix.toString(); } diff --git a/packages/client-analytics/src/analyticsClient.ts b/packages/client-analytics/src/analyticsClient.ts index 9e7cb25a8..c18d0af33 100644 --- a/packages/client-analytics/src/analyticsClient.ts +++ b/packages/client-analytics/src/analyticsClient.ts @@ -137,7 +137,11 @@ export function createAnalyticsClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** @@ -288,9 +292,11 @@ export function createAnalyticsClient({ if (index !== undefined) { queryParameters.index = index.toString(); } + if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } + if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } @@ -335,13 +341,13 @@ 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(); } @@ -434,7 +440,6 @@ export function createAnalyticsClient({ if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } - if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } @@ -485,7 +490,6 @@ export function createAnalyticsClient({ if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } - if (tags !== undefined) { queryParameters.tags = tags.toString(); } @@ -527,6 +531,7 @@ export function createAnalyticsClient({ if (index !== undefined) { queryParameters.index = index.toString(); } + if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } @@ -574,13 +579,13 @@ 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(); } @@ -622,7 +627,6 @@ export function createAnalyticsClient({ if (index !== undefined) { queryParameters.index = index.toString(); } - if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } @@ -674,7 +678,6 @@ export function createAnalyticsClient({ if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } - if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } @@ -768,16 +771,17 @@ 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 (limit !== undefined) { queryParameters.limit = limit.toString(); } - if (offset !== undefined) { queryParameters.offset = offset.toString(); } @@ -827,10 +831,10 @@ export function createAnalyticsClient({ if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } + if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } - if (limit !== undefined) { queryParameters.limit = limit.toString(); } @@ -916,6 +920,7 @@ export function createAnalyticsClient({ if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } + if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } @@ -975,7 +980,6 @@ export function createAnalyticsClient({ if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } - if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } @@ -1040,6 +1044,7 @@ export function createAnalyticsClient({ if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } + if (endDate !== undefined) { queryParameters.endDate = endDate.toString(); } diff --git a/packages/client-insights/src/insightsClient.ts b/packages/client-insights/src/insightsClient.ts index 04eaeb40f..89e30ab25 100644 --- a/packages/client-insights/src/insightsClient.ts +++ b/packages/client-insights/src/insightsClient.ts @@ -100,7 +100,11 @@ export function createInsightsClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** diff --git a/packages/client-personalization/src/personalizationClient.ts b/packages/client-personalization/src/personalizationClient.ts index fc0afde15..0e501791b 100644 --- a/packages/client-personalization/src/personalizationClient.ts +++ b/packages/client-personalization/src/personalizationClient.ts @@ -103,7 +103,11 @@ export function createPersonalizationClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** diff --git a/packages/client-query-suggestions/src/querySuggestionsClient.ts b/packages/client-query-suggestions/src/querySuggestionsClient.ts index 365051ee5..a724fe30b 100644 --- a/packages/client-query-suggestions/src/querySuggestionsClient.ts +++ b/packages/client-query-suggestions/src/querySuggestionsClient.ts @@ -107,7 +107,11 @@ export function createQuerySuggestionsClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** diff --git a/packages/client-search/src/searchClient.ts b/packages/client-search/src/searchClient.ts index 8474f5884..a502187bd 100644 --- a/packages/client-search/src/searchClient.ts +++ b/packages/client-search/src/searchClient.ts @@ -233,7 +233,11 @@ export function createSearchClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** @@ -1410,7 +1414,6 @@ export function createSearchClient({ .replace('{objectID}', encodeURIComponent(objectID)); const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (forwardToReplicas !== undefined) { queryParameters.forwardToReplicas = forwardToReplicas.toString(); } @@ -2065,6 +2068,7 @@ export function createSearchClient({ if (page !== undefined) { queryParameters.page = page.toString(); } + if (hitsPerPage !== undefined) { queryParameters.hitsPerPage = hitsPerPage.toString(); } @@ -2188,6 +2192,7 @@ export function createSearchClient({ .replace('{objectID}', encodeURIComponent(objectID)); const headers: Headers = {}; const queryParameters: QueryParameters = {}; + if (createIfNotExists !== undefined) { queryParameters.createIfNotExists = createIfNotExists.toString(); } @@ -2364,7 +2369,6 @@ export function createSearchClient({ .replace('{objectID}', encodeURIComponent(objectID)); const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (forwardToReplicas !== undefined) { queryParameters.forwardToReplicas = forwardToReplicas.toString(); } @@ -2468,7 +2472,6 @@ export function createSearchClient({ .replace('{objectID}', encodeURIComponent(objectID)); const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (forwardToReplicas !== undefined) { queryParameters.forwardToReplicas = forwardToReplicas.toString(); } @@ -2512,7 +2515,6 @@ export function createSearchClient({ const requestPath = '/1/indexes/{indexName}/synonyms/batch'.replace('{indexName}', encodeURIComponent(indexName)); const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (forwardToReplicas !== undefined) { queryParameters.forwardToReplicas = forwardToReplicas.toString(); } diff --git a/packages/client-usage/src/usageClient.ts b/packages/client-usage/src/usageClient.ts index 0518f3720..993905274 100644 --- a/packages/client-usage/src/usageClient.ts +++ b/packages/client-usage/src/usageClient.ts @@ -94,7 +94,11 @@ export function createUsageClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** diff --git a/packages/ingestion/src/ingestionClient.ts b/packages/ingestion/src/ingestionClient.ts index b29c15741..e008fbe25 100644 --- a/packages/ingestion/src/ingestionClient.ts +++ b/packages/ingestion/src/ingestionClient.ts @@ -217,7 +217,11 @@ export function createIngestionClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** @@ -1232,7 +1236,6 @@ export function createIngestionClient({ const requestPath = '/1/authentications'; const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (itemsPerPage !== undefined) { queryParameters.itemsPerPage = itemsPerPage.toString(); } @@ -1249,6 +1252,7 @@ export function createIngestionClient({ if (sort !== undefined) { queryParameters.sort = sort.toString(); } + if (order !== undefined) { queryParameters.order = order.toString(); } @@ -1287,16 +1291,17 @@ export function createIngestionClient({ const requestPath = '/1/destinations'; const headers: Headers = {}; const queryParameters: QueryParameters = {}; + if (itemsPerPage !== undefined) { queryParameters.itemsPerPage = itemsPerPage.toString(); } if (page !== undefined) { queryParameters.page = page.toString(); } - if (type !== undefined) { queryParameters.type = type.toString(); } + if (authenticationID !== undefined) { queryParameters.authenticationID = authenticationID.toString(); } @@ -1348,27 +1353,25 @@ export function createIngestionClient({ const requestPath = '/1/runs/{runID}/events'.replace('{runID}', encodeURIComponent(runID)); const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (itemsPerPage !== undefined) { queryParameters.itemsPerPage = itemsPerPage.toString(); } if (page !== undefined) { queryParameters.page = page.toString(); } - if (status !== undefined) { queryParameters.status = status.toString(); } if (type !== undefined) { queryParameters.type = type.toString(); } + if (sort !== undefined) { queryParameters.sort = sort.toString(); } if (order !== undefined) { queryParameters.order = order.toString(); } - if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } @@ -1413,30 +1416,30 @@ export function createIngestionClient({ const requestPath = '/1/runs'; const headers: Headers = {}; const queryParameters: QueryParameters = {}; + if (itemsPerPage !== undefined) { queryParameters.itemsPerPage = itemsPerPage.toString(); } - if (page !== undefined) { queryParameters.page = page.toString(); } + if (status !== undefined) { queryParameters.status = status.toString(); } if (type !== undefined) { queryParameters.type = type.toString(); } - if (taskID !== undefined) { queryParameters.taskID = taskID.toString(); } if (sort !== undefined) { queryParameters.sort = sort.toString(); } + if (order !== undefined) { queryParameters.order = order.toString(); } - if (startDate !== undefined) { queryParameters.startDate = startDate.toString(); } @@ -1542,10 +1545,10 @@ export function createIngestionClient({ if (page !== undefined) { queryParameters.page = page.toString(); } + if (action !== undefined) { queryParameters.action = action.toString(); } - if (enabled !== undefined) { queryParameters.enabled = enabled.toString(); } @@ -1562,6 +1565,7 @@ export function createIngestionClient({ if (sort !== undefined) { queryParameters.sort = sort.toString(); } + if (order !== undefined) { queryParameters.order = order.toString(); } @@ -1609,10 +1613,10 @@ export function createIngestionClient({ if (page !== undefined) { queryParameters.page = page.toString(); } + if (action !== undefined) { queryParameters.action = action.toString(); } - if (enabled !== undefined) { queryParameters.enabled = enabled.toString(); } @@ -1690,7 +1694,6 @@ export function createIngestionClient({ const requestPath = '/1/transformations'; const headers: Headers = {}; const queryParameters: QueryParameters = {}; - if (itemsPerPage !== undefined) { queryParameters.itemsPerPage = itemsPerPage.toString(); } diff --git a/packages/monitoring/src/monitoringClient.ts b/packages/monitoring/src/monitoringClient.ts index 55b153e79..e960b6ab0 100644 --- a/packages/monitoring/src/monitoringClient.ts +++ b/packages/monitoring/src/monitoringClient.ts @@ -103,7 +103,11 @@ export function createMonitoringClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /** diff --git a/packages/recommend/src/recommendClient.ts b/packages/recommend/src/recommendClient.ts index dfafa2e87..85367ea4b 100644 --- a/packages/recommend/src/recommendClient.ts +++ b/packages/recommend/src/recommendClient.ts @@ -133,7 +133,11 @@ export function createRecommendClient({ * @param params.apiKey - The new API Key to use. */ setClientApiKey({ apiKey }: { apiKey: string }): void { - this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + if (!authMode || authMode === 'WithinHeaders') { + this.transporter.baseHeaders['x-algolia-api-key'] = apiKey; + } else { + this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey; + } }, /**