Skip to content

Commit 6047578

Browse files
chore: generated code for commit 2288d17. [skip ci]
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 2288d17 commit 6047578

File tree

2 files changed

+103
-19
lines changed

2 files changed

+103
-19
lines changed

clients/algoliasearch-client-javascript/packages/client-search/model/clientMethodProps.ts

+45
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CreateRetryablePromiseOptions } from '@experimental-api-clients-automation/client-common';
2+
13
import type { ApiKey } from './apiKey';
24
import type { AssignUserIdParams } from './assignUserIdParams';
35
import type { AttributeOrBuiltInOperation } from './attributeOrBuiltInOperation';
@@ -6,7 +8,9 @@ import type { BatchDictionaryEntriesParams } from './batchDictionaryEntriesParam
68
import type { BatchWriteParams } from './batchWriteParams';
79
import type { BrowseRequest } from './browseRequest';
810
import type { DictionaryType } from './dictionaryType';
11+
import type { GetTaskResponse } from './getTaskResponse';
912
import type { IndexSettings } from './indexSettings';
13+
import type { Key } from './key';
1014
import type { LogType } from './logType';
1115
import type { OperationIndexParams } from './operationIndexParams';
1216
import type { Rule } from './rule';
@@ -719,3 +723,44 @@ export type UpdateApiKeyProps = {
719723
key: string;
720724
apiKey: ApiKey;
721725
};
726+
727+
type WaitForOptions<T> = Omit<
728+
CreateRetryablePromiseOptions<T>,
729+
'func' | 'validate'
730+
>;
731+
732+
export type WaitForTaskOptions = WaitForOptions<GetTaskResponse> & {
733+
/**
734+
* The `indexName` where the operation was performed.
735+
*/
736+
indexName: string;
737+
/**
738+
* The `taskID` returned by the method response.
739+
*/
740+
taskID: number;
741+
};
742+
743+
export type WaitForApiKeyOptions = WaitForOptions<Key> & {
744+
/**
745+
* The API Key.
746+
*/
747+
key: string;
748+
} & (
749+
| {
750+
/**
751+
* The operation that has been performed, used to compute the stop condition.
752+
*/
753+
operation: 'add' | 'delete';
754+
apiKey?: never;
755+
}
756+
| {
757+
/**
758+
* The operation that has been performed, used to compute the stop condition.
759+
*/
760+
operation: 'update';
761+
/**
762+
* The updated fields, used to compute the stop condition.
763+
*/
764+
apiKey: Partial<ApiKey>;
765+
}
766+
);

clients/algoliasearch-client-javascript/packages/client-search/src/searchClient.ts

+58-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
Request,
1313
RequestOptions,
1414
QueryParameters,
15-
CreateRetryablePromiseOptions,
15+
ApiError,
1616
} from '@experimental-api-clients-automation/client-common';
1717

1818
import type { AddApiKeyResponse } from '../model/addApiKeyResponse';
@@ -21,6 +21,8 @@ import type { BatchParams } from '../model/batchParams';
2121
import type { BatchResponse } from '../model/batchResponse';
2222
import type { BrowseResponse } from '../model/browseResponse';
2323
import type {
24+
WaitForTaskOptions,
25+
WaitForApiKeyOptions,
2426
AddOrUpdateObjectProps,
2527
AssignUserIdProps,
2628
BatchProps,
@@ -183,34 +185,71 @@ export function createSearchClient({
183185
return {
184186
addAlgoliaAgent,
185187
/**
186-
* Wait for a task to complete with `indexName` and `taskID`.
188+
* Helper: Wait for a task to complete with `indexName` and `taskID`.
187189
*
188190
* @summary Wait for a task to complete.
189-
* @param waitForTaskProps - The waitForTaskProps object.
190-
* @param waitForTaskProps.indexName - The index in which to perform the request.
191-
* @param waitForTaskProps.taskID - The unique identifier of the task to wait for.
191+
* @param waitForTaskOptions - The waitForTaskOptions object.
192+
* @param waitForTaskOptions.indexName - The `indexName` where the operation was performed.
193+
* @param waitForTaskOptions.taskID - The `taskID` returned in the method response.
192194
*/
193195
waitForTask({
194196
indexName,
195197
taskID,
196198
...createRetryablePromiseOptions
197-
}: Omit<
198-
CreateRetryablePromiseOptions<GetTaskResponse>,
199-
'func' | 'validate'
200-
> & {
201-
indexName: string;
202-
taskID: number;
203-
}): Promise<void> {
204-
return new Promise<void>((resolve, reject) => {
205-
createRetryablePromise<GetTaskResponse>({
199+
}: WaitForTaskOptions): Promise<GetTaskResponse> {
200+
return createRetryablePromise({
201+
...createRetryablePromiseOptions,
202+
func: () => this.getTask({ indexName, taskID }),
203+
validate: (response) => response.status === 'published',
204+
});
205+
},
206+
207+
/**
208+
* Helper: Wait for an API key to be valid, updated or deleted based on a given `operation`.
209+
*
210+
* @summary Wait for an API key task to be processed.
211+
* @param waitForApiKeyOptions - The waitForApiKeyOptions object.
212+
* @param waitForApiKeyOptions.operation - The `operation` that was done on a `key`.
213+
* @param waitForApiKeyOptions.key - The `key` that has been added, deleted or updated.
214+
* @param waitForApiKeyOptions.apiKey - Necessary to know if an `update` operation has been processed, compare fields of the response with it.
215+
*/
216+
waitForApiKey({
217+
operation,
218+
key,
219+
apiKey,
220+
...createRetryablePromiseOptions
221+
}: WaitForApiKeyOptions): Promise<ApiError | Key> {
222+
if (operation === 'update') {
223+
return createRetryablePromise({
206224
...createRetryablePromiseOptions,
207-
func: () => this.getTask({ indexName, taskID }),
208-
validate: (response) => response.status === 'published',
209-
})
210-
.then(() => resolve())
211-
.catch(reject);
225+
func: () => this.getApiKey({ key }),
226+
validate: (response) => {
227+
for (const [entry, values] of Object.entries(apiKey)) {
228+
if (Array.isArray(values)) {
229+
if (
230+
values.length !== response[entry].length ||
231+
values.some((val, index) => val !== response[entry][index])
232+
) {
233+
return false;
234+
}
235+
} else if (values !== response[entry]) {
236+
return false;
237+
}
238+
}
239+
240+
return true;
241+
},
242+
});
243+
}
244+
245+
return createRetryablePromise({
246+
...createRetryablePromiseOptions,
247+
func: () => this.getApiKey({ key }).catch((error) => error),
248+
validate: (error: ApiError) =>
249+
operation === 'add' ? error.status !== 404 : error.status === 404,
212250
});
213251
},
252+
214253
/**
215254
* Add a new API Key with specific permissions/restrictions.
216255
*

0 commit comments

Comments
 (0)