|
| 1 | +/** |
| 2 | + * Helper: Wait for a task to complete with `indexName` and `taskID`. |
| 3 | + * |
| 4 | + * @summary Wait for a task to complete. |
| 5 | + * @param waitForTaskOptions - The waitForTaskOptions object. |
| 6 | + * @param waitForTaskOptions.indexName - The `indexName` where the operation was performed. |
| 7 | + * @param waitForTaskOptions.taskID - The `taskID` returned in the method response. |
| 8 | + */ |
| 9 | +waitForTask({ |
| 10 | + indexName, |
| 11 | + taskID, |
| 12 | + ...createRetryablePromiseOptions |
| 13 | +}: WaitForTaskOptions): Promise<GetTaskResponse> { |
| 14 | + return createRetryablePromise({ |
| 15 | + ...createRetryablePromiseOptions, |
| 16 | + func: () => this.getTask({ indexName, taskID }), |
| 17 | + validate: (response) => response.status === 'published', |
| 18 | + }); |
| 19 | +}, |
| 20 | + |
| 21 | +/** |
| 22 | + * Helper: Wait for an API key to be valid, updated or deleted based on a given `operation`. |
| 23 | + * |
| 24 | + * @summary Wait for an API key task to be processed. |
| 25 | + * @param waitForApiKeyOptions - The waitForApiKeyOptions object. |
| 26 | + * @param waitForApiKeyOptions.operation - The `operation` that was done on a `key`. |
| 27 | + * @param waitForApiKeyOptions.key - The `key` that has been added, deleted or updated. |
| 28 | + * @param waitForApiKeyOptions.apiKey - Necessary to know if an `update` operation has been processed, compare fields of the response with it. |
| 29 | + */ |
| 30 | +waitForApiKey({ |
| 31 | + operation, |
| 32 | + key, |
| 33 | + apiKey, |
| 34 | + ...createRetryablePromiseOptions |
| 35 | +}: WaitForApiKeyOptions): Promise<ApiError | Key> { |
| 36 | + if (operation === 'update') { |
| 37 | + return createRetryablePromise({ |
| 38 | + ...createRetryablePromiseOptions, |
| 39 | + func: () => this.getApiKey({ key }), |
| 40 | + validate: (response) => { |
| 41 | + for (const [entry, values] of Object.entries(apiKey)) { |
| 42 | + if (Array.isArray(values)) { |
| 43 | + if ( |
| 44 | + values.length !== response[entry].length || |
| 45 | + values.some((val, index) => val !== response[entry][index]) |
| 46 | + ) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + } else if (values !== response[entry]) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | + }, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return createRetryablePromise({ |
| 60 | + ...createRetryablePromiseOptions, |
| 61 | + func: () => this.getApiKey({ key }).catch((error) => error), |
| 62 | + validate: (error: ApiError) => |
| 63 | + operation === 'add' ? error.status !== 404 : error.status === 404, |
| 64 | + }); |
| 65 | +}, |
0 commit comments