-
Notifications
You must be signed in to change notification settings - Fork 21
feat(javascript): add waitForApiKey
helper method
#738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for this change,
all
was not allowed anymore 👼🏼