@@ -12,7 +12,7 @@ import type {
12
12
Request ,
13
13
RequestOptions ,
14
14
QueryParameters ,
15
- CreateRetryablePromiseOptions ,
15
+ ApiError ,
16
16
} from '@experimental-api-clients-automation/client-common' ;
17
17
18
18
import type { AddApiKeyResponse } from '../model/addApiKeyResponse' ;
@@ -21,6 +21,8 @@ import type { BatchParams } from '../model/batchParams';
21
21
import type { BatchResponse } from '../model/batchResponse' ;
22
22
import type { BrowseResponse } from '../model/browseResponse' ;
23
23
import type {
24
+ WaitForTaskOptions ,
25
+ WaitForApiKeyOptions ,
24
26
AddOrUpdateObjectProps ,
25
27
AssignUserIdProps ,
26
28
BatchProps ,
@@ -183,34 +185,71 @@ export function createSearchClient({
183
185
return {
184
186
addAlgoliaAgent,
185
187
/**
186
- * Wait for a task to complete with `indexName` and `taskID`.
188
+ * Helper: Wait for a task to complete with `indexName` and `taskID`.
187
189
*
188
190
* @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 .
192
194
*/
193
195
waitForTask ( {
194
196
indexName,
195
197
taskID,
196
198
...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 ( {
206
224
...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 ,
212
250
} ) ;
213
251
} ,
252
+
214
253
/**
215
254
* Add a new API Key with specific permissions/restrictions.
216
255
*
0 commit comments