-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): add
waitForApiKey
helper method (#738)
- Loading branch information
Showing
9 changed files
with
187 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Helper: Wait for a task to complete with `indexName` and `taskID`. | ||
* | ||
* @summary Wait for a task to complete. | ||
* @param waitForTaskOptions - The waitForTaskOptions object. | ||
* @param waitForTaskOptions.indexName - The `indexName` where the operation was performed. | ||
* @param waitForTaskOptions.taskID - The `taskID` returned in the method response. | ||
*/ | ||
waitForTask({ | ||
indexName, | ||
taskID, | ||
...createRetryablePromiseOptions | ||
}: WaitForTaskOptions): Promise<GetTaskResponse> { | ||
return createRetryablePromise({ | ||
...createRetryablePromiseOptions, | ||
func: () => this.getTask({ indexName, taskID }), | ||
validate: (response) => response.status === 'published', | ||
}); | ||
}, | ||
|
||
/** | ||
* Helper: Wait for an API key to be valid, updated or deleted based on a given `operation`. | ||
* | ||
* @summary Wait for an API key task to be processed. | ||
* @param waitForApiKeyOptions - The waitForApiKeyOptions object. | ||
* @param waitForApiKeyOptions.operation - The `operation` that was done on a `key`. | ||
* @param waitForApiKeyOptions.key - The `key` that has been added, deleted or updated. | ||
* @param waitForApiKeyOptions.apiKey - Necessary to know if an `update` operation has been processed, compare fields of the response with it. | ||
*/ | ||
waitForApiKey({ | ||
operation, | ||
key, | ||
apiKey, | ||
...createRetryablePromiseOptions | ||
}: WaitForApiKeyOptions): Promise<ApiError | Key> { | ||
if (operation === 'update') { | ||
return createRetryablePromise({ | ||
...createRetryablePromiseOptions, | ||
func: () => this.getApiKey({ key }), | ||
validate: (response) => { | ||
for (const [entry, values] of Object.entries(apiKey)) { | ||
if (Array.isArray(values)) { | ||
if ( | ||
values.length !== response[entry].length || | ||
values.some((val, index) => val !== response[entry][index]) | ||
) { | ||
return false; | ||
} | ||
} else if (values !== response[entry]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
}); | ||
} | ||
|
||
return createRetryablePromise({ | ||
...createRetryablePromiseOptions, | ||
func: () => this.getApiKey({ key }).catch((error) => error), | ||
validate: (error: ApiError) => | ||
operation === 'add' ? error.status !== 404 : error.status === 404, | ||
}); | ||
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
website/docs/clients/guides/copy-index-to-another-application.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: Copy or move an index | ||
title: Copy an index to another application | ||
--- | ||
|
||
:::caution | ||
|
53 changes: 53 additions & 0 deletions
53
website/docs/clients/guides/wait-for-api-key-to-be-valid.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Wait for an API key to be valid | ||
--- | ||
|
||
import { TabsLanguage } from '../../../src/components/TabsLanguage'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
> The `waitForApiKey` method is only available in the `search` client context. | ||
Adding, updating or deleting API keys is not always instantaneous, which is why you might want to ensure the job has been processed before jumping to an other task. | ||
|
||
We provide a `waitForApiKey` helper method for you to easily wait for a specific `operation` made on a `key`. | ||
|
||
<TabsLanguage> | ||
<TabItem value="javascript"> | ||
|
||
> An `operation` can either be `add` | `update` | `delete` | ||
```js | ||
import { algoliasearch } from '@experimental-api-clients-automation/algoliasearch'; | ||
|
||
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>'); | ||
|
||
const { key } = await client.addApiKey({ | ||
acl: ['analytics', 'browse', 'editSettings'], | ||
}); | ||
|
||
// Poll the task status with defaults values | ||
await client.waitForApiKey({ operation: 'add', key }); | ||
|
||
// The fields to update on your API key | ||
const updatesToPerform: ApiKey = { | ||
acl: ['analytics', 'search'], | ||
indexes: ['products'], | ||
}; | ||
|
||
// Call for update | ||
await client.updateApiKey({ | ||
key, | ||
apiKey: updatesToPerform, | ||
}); | ||
|
||
// Wait for update to be done | ||
await client.waitForApiKey({ | ||
operation: 'update', | ||
key, | ||
// We provide the updated fields to check if the changes have been applied | ||
apiKey: updatesToPerform, | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</TabsLanguage> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters