From edc629d0aa3622c9fb7e029eddf2b86dc662ebec Mon Sep 17 00:00:00 2001 From: Thomas Raffray Date: Wed, 7 Jun 2023 17:45:33 +0200 Subject: [PATCH] fix(search): fix wait method for updateApiKey request (#1464) --- package.json | 2 +- .../src/methods/client/updateApiKey.ts | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 73966faae..990128e7d 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "bundlesize": [ { "path": "packages/algoliasearch/dist/algoliasearch.umd.js", - "maxSize": "7.95KB" + "maxSize": "8KB" }, { "path": "packages/algoliasearch/dist/algoliasearch-lite.umd.js", diff --git a/packages/client-search/src/methods/client/updateApiKey.ts b/packages/client-search/src/methods/client/updateApiKey.ts index d45c0821c..5a4b11e76 100644 --- a/packages/client-search/src/methods/client/updateApiKey.ts +++ b/packages/client-search/src/methods/client/updateApiKey.ts @@ -9,6 +9,7 @@ import { MethodEnum } from '@algolia/requester-common'; import { RequestOptions } from '@algolia/transporter'; import { + ApiKeyACLType, getApiKey, GetApiKeyResponse, SearchClient, @@ -37,6 +38,7 @@ export const updateApiKey = (base: SearchClient) => { 'maxHitsPerQuery', ] as const; + // Check that all the fields retrieved through getApiKey are the same as the ones we wanted to update const hasChanged = (getApiKeyResponse: GetApiKeyResponse): boolean => { return Object.keys(updatedFields) .filter( @@ -44,7 +46,24 @@ export const updateApiKey = (base: SearchClient) => { apiKeyFields.indexOf(updatedField) !== -1 ) .every(updatedField => { - return getApiKeyResponse[updatedField] === updatedFields[updatedField]; + // If the field is an array, we need to check that they are the same length and that all the values are the same + if ( + Array.isArray(getApiKeyResponse[updatedField]) && + Array.isArray(updatedFields[updatedField]) + ) { + const getApiKeyResponseArray = getApiKeyResponse[updatedField] as + | readonly ApiKeyACLType[] + | readonly string[]; + + return ( + getApiKeyResponseArray.length === updatedFields[updatedField].length && + getApiKeyResponseArray.every( + (value, index) => value === updatedFields[updatedField][index] + ) + ); + } else { + return getApiKeyResponse[updatedField] === updatedFields[updatedField]; + } }); };