Skip to content

Commit 7fc5f89

Browse files
committed
feat: add waitForCompositionTask for composition client
1 parent b0779e2 commit 7fc5f89

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class AlgoliaJavascriptGenerator extends TypeScriptNodeClientCodegen {
1818

1919
private String CLIENT;
2020
private boolean isAlgoliasearchClient;
21+
private boolean isAlgoliaCompositionClient;
2122

2223
@Override
2324
public String getName() {
@@ -30,6 +31,7 @@ public void processOpts() {
3031

3132
CLIENT = Helpers.camelize((String) additionalProperties.get("client"));
3233
isAlgoliasearchClient = CLIENT.equals("algoliasearch");
34+
isAlgoliaCompositionClient = CLIENT.equals("composition");
3335

3436
// generator specific options
3537
setSupportsES6(true);
@@ -155,6 +157,7 @@ private void setDefaultGeneratorOptions() {
155157
additionalProperties.put("isSearchClient", CLIENT.equals("search") || isAlgoliasearchClient);
156158
additionalProperties.put("isIngestionClient", CLIENT.equals("ingestion"));
157159
additionalProperties.put("isAlgoliasearchClient", isAlgoliasearchClient);
160+
additionalProperties.put("isAlgoliaCompositionClient", isAlgoliaCompositionClient);
158161
additionalProperties.put("packageVersion", Helpers.getPackageJsonVersion(packageName));
159162
additionalProperties.put("packageName", packageName);
160163
additionalProperties.put("npmPackageName", isAlgoliasearchClient ? packageName : "@algolia/" + packageName);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
method:
2+
get:
3+
x-helper: true
4+
tags:
5+
- Records
6+
operationId: waitForCompositionTask
7+
summary: Wait for operation to complete
8+
description: |
9+
Wait for a task to complete to ensure synchronized composition updates.
10+
11+
All Algolia write operations are asynchronous. When you make a request for a write operation, for example, to upsert or delete a composition, Algolia creates a task on a queue and returns a taskID. The task itself runs separately, depending on the server load.
12+
parameters:
13+
- in: query
14+
name: compositionID
15+
description: The ID of the composition on which the operation was performed.
16+
required: true
17+
schema:
18+
type: string
19+
- in: query
20+
name: taskID
21+
description: The taskID returned by the operation.
22+
required: true
23+
schema:
24+
type: integer
25+
format: int64
26+
responses:
27+
'200':
28+
description: OK
29+
content:
30+
application/json:
31+
schema:
32+
$ref: '../common/schemas/GetTaskResponse.yml'
33+
'400':
34+
$ref: '../../common/responses/CompositionNotFound.yml'

specs/composition/spec.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ paths:
7373
# ###############
7474
/setClientApiKey:
7575
$ref: '../common/helpers/setClientApiKey.yml#/method'
76+
77+
/waitForCompositionTask:
78+
$ref: 'helpers/waitForCompositionTask.yml#/method'

templates/javascript/clients/api-single.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export function create{{#lambda.titlecase}}{{clientName}}{{/lambda.titlecase}}({
9191
{{#isSearchClient}}
9292
{{> client/api/helpers}}
9393
{{/isSearchClient}}
94+
{{#isAlgoliaCompositionClient}}
95+
{{> client/api/compositionHelpers}}
96+
{{/isAlgoliaCompositionClient}}
9497
{{#operation}}
9598
{{> client/api/operation/jsdoc}}
9699
{{nickname}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}( {{> client/api/operation/parameters}} ) : Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}> {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Helper: Wait for a composition-level task to be published (completed) for a given `compositionID` and `taskID`.
3+
*
4+
* @summary Helper method that waits for a task to be published (completed).
5+
* @param WaitForCompositionTaskOptions - The `WaitForCompositionTaskOptions` object.
6+
* @param WaitForCompositionTaskOptions.compositionID - The `compositionID` where the operation was performed.
7+
* @param WaitForCompositionTaskOptions.taskID - The `taskID` returned in the method response.
8+
* @param WaitForCompositionTaskOptions.maxRetries - The maximum number of retries. 50 by default.
9+
* @param WaitForCompositionTaskOptions.timeout - The function to decide how long to wait between retries.
10+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
11+
*/
12+
waitForCompositionTask(
13+
{
14+
compositionID,
15+
taskID,
16+
maxRetries = 50,
17+
timeout = (retryCount: number): number =>
18+
Math.min(retryCount * 200, 5000),
19+
}: WaitForCompositionTaskOptions,
20+
requestOptions?: RequestOptions
21+
): Promise<GetTaskResponse> {
22+
let retryCount = 0;
23+
24+
return createIterablePromise({
25+
func: () => this.getTask({ compositionID, taskID }, requestOptions),
26+
validate: (response) => response.status === 'published',
27+
aggregator: () => (retryCount += 1),
28+
error: {
29+
validate: () => retryCount >= maxRetries,
30+
message: () =>
31+
`The maximum number of retries exceeded. (${retryCount}/${maxRetries})`,
32+
},
33+
timeout: () => timeout(retryCount),
34+
});
35+
},

templates/javascript/clients/client/api/imports.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
serializeQueryParameters,
1010
createIterablePromise,
1111
{{/isSearchClient}}
12+
{{#isAlgoliaCompositionClient}}
13+
createIterablePromise,
14+
{{/isAlgoliaCompositionClient}}
1215
} from '@algolia/client-common';
1316
import type {
1417
CreateClientOptions,
@@ -39,6 +42,9 @@ import type {
3942
WaitForAppTaskOptions,
4043
WaitForTaskOptions,
4144
{{/isSearchClient}}
45+
{{#isAlgoliaCompositionClient}}
46+
WaitForCompositionTaskOptions,
47+
{{/isAlgoliaCompositionClient}}
4248
{{#operation}}
4349
{{#vendorExtensions}}
4450
{{#x-create-wrapping-object}}

templates/javascript/clients/client/model/clientMethodProps.mustache

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,28 @@ export type ReplaceAllObjectsOptions = {
184184
}
185185
{{/isAlgoliasearchClient}}
186186
{{/isSearchClient}}
187+
{{#isAlgoliaCompositionClient}}
188+
export type WaitForCompositionTaskOptions = {
189+
/**
190+
* The maximum number of retries. 50 by default.
191+
*/
192+
maxRetries?: number;
193+
194+
/**
195+
* The function to decide how long to wait between retries.
196+
*/
197+
timeout?: (retryCount: number) => number;
198+
199+
/**
200+
* The `taskID` returned by the method response.
201+
*/
202+
203+
taskID: number;
204+
/**
205+
* The `compositionID` where the operation was performed.
206+
*/
207+
compositionID: string;
208+
};
209+
{{/isAlgoliaCompositionClient}}
187210

188211
{{/apiInfo.apis.0}}

0 commit comments

Comments
 (0)