Skip to content

Commit

Permalink
fix(javascript): allow async param on createIterablePromise (#4144)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Nov 22, 2024
1 parent 9330265 commit c044401
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"skipLibCheck": true
},
"include": ["*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"build": "yarn clean && yarn tsup",
"clean": "rm -rf ./dist || true",
"test": "vitest --run",
"test": "tsc --noEmit && vitest --run",
"test:bundle": "publint . && attw --pack ."
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('browser local storage cache', () => {

await cache.clear();

const defaultValue = (): Promise<void> => Promise.resolve({ bar: 2 });
const defaultValue = (): Promise<{ bar: number }> => Promise.resolve({ bar: 2 });

expect(localStorage.length).toBe(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ describe('createIterablePromise', () => {
await expect(promise).resolves.toEqual(3);
expect(calls).toBe(3);
});

test('allow async function', async () => {
createIterablePromise({
func: () => {
return Promise.resolve({
hits: [],
cursor: '',
});
},
validate: async () => {
return await Promise.resolve(true);
},
aggregator: async (res) => {
return await Promise.resolve(res);
},
timeout: async () => {
return await Promise.resolve(1000);
},
});
});
});

describe('aggregator', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ export function createIterablePromise<TResponse>({
const retry = (previousResponse?: TResponse): Promise<TResponse> => {
return new Promise<TResponse>((resolve, reject) => {
func(previousResponse)
.then((response) => {
.then(async (response) => {
if (aggregator) {
aggregator(response);
await aggregator(response);
}

if (validate(response)) {
if (await validate(response)) {
return resolve(response);
}

if (error && error.validate(response)) {
return reject(new Error(error.message(response)));
if (error && (await error.validate(response))) {
return reject(new Error(await error.message(response)));
}

return setTimeout(() => {
retry(response).then(resolve).catch(reject);
}, timeout());
return setTimeout(
() => {
retry(response).then(resolve).catch(reject);
},
await timeout(),
);
})
.catch((err) => {
reject(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type IterableOptions<TResponse> = Partial<{
/**
* The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
*/
aggregator: (response: TResponse) => void;
aggregator: (response: TResponse) => unknown | PromiseLike<unknown>;

/**
* The `validate` condition to throw an error and its message.
Expand All @@ -11,18 +11,18 @@ export type IterableOptions<TResponse> = Partial<{
/**
* The function to validate the error condition.
*/
validate: (response: TResponse) => boolean;
validate: (response: TResponse) => boolean | PromiseLike<boolean>;

/**
* The error message to throw.
*/
message: (response: TResponse) => string;
message: (response: TResponse) => string | PromiseLike<string>;
};

/**
* The function to decide how long to wait between iterations.
*/
timeout: () => number;
timeout: () => number | PromiseLike<number>;
}>;

export type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
Expand All @@ -36,5 +36,5 @@ export type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
/**
* The validator function. It receive the resolved return of the API call.
*/
validate: (response: TResponse) => boolean;
validate: (response: TResponse) => boolean | PromiseLike<boolean>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"],
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["dist", "node_modules", "src/__tests__"]
"exclude": ["dist", "node_modules"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"build": "yarn clean && yarn tsup",
"clean": "rm -rf ./dist || true",
"test": "vitest --run",
"test": "tsc --noEmit && vitest --run",
"test:bundle": "publint . && attw --pack ."
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"],
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src", "index.ts"],
"exclude": ["dist", "node_modules", "src/__tests__"]
"exclude": ["dist", "node_modules"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"build": "yarn clean && yarn tsup",
"clean": "rm -rf ./dist || true",
"test": "vitest --run",
"test": "tsc --noEmit && vitest --run",
"test:bundle": "publint . && attw --pack . --ignore-rules cjs-resolves-to-esm"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('timeout handling', () => {

afterAll(
() =>
new Promise((done) => {
new Promise<void>((done) => {
done();
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"],
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["dist", "node_modules", "src/__tests__"]
"exclude": ["dist", "node_modules"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"scripts": {
"build": "yarn clean && yarn tsup",
"clean": "rm -rf ./dist || true",
"test": "vitest --run",
"test": "tsc --noEmit && vitest --run",
"test:bundle": "publint . && attw --pack ."
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('timeout handling', () => {

afterAll(
() =>
new Promise((done) => {
new Promise<void>((done) => {
done();
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"],
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["dist", "node_modules", "src/__tests__"]
"exclude": ["dist", "node_modules"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"build": "yarn clean && yarn tsup",
"clean": "rm -rf ./dist || true",
"test": "vitest --run",
"test": "tsc --noEmit && vitest --run",
"test:bundle": "publint . && attw --pack ."
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('timeout handling', () => {

afterAll(
() =>
new Promise((done) => {
new Promise<void>((done) => {
done();
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"],
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["dist", "node_modules", "src/__tests__"]
"exclude": ["dist", "node_modules"]
}
4 changes: 2 additions & 2 deletions templates/javascript/clients/package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "yarn clean && yarn tsup && yarn rollup -c rollup.config.js",
"clean": "rm -rf ./dist || true",
{{#isAlgoliasearchClient}}
"test": "vitest --run",
"test": "tsc -p __tests__/tsconfig.json && vitest --run",
{{/isAlgoliasearchClient}}
"test:bundle": "publint . && attw --pack ."
},
Expand Down Expand Up @@ -145,4 +145,4 @@
"engines": {
"node": ">= 14.0.0"
}
}
}

0 comments on commit c044401

Please sign in to comment.