Skip to content

Commit 98d16f0

Browse files
Improve SDK stability for long-running operations
1 parent c8a3332 commit 98d16f0

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linkedapi-node",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "Official TypeScript SDK for Linked API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/poll-results.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ export async function pollWorkflowResult<TResult>(
1111
const { pollInterval = 5000, timeout = 24 * 60 * 60 * 1000 } = options;
1212
const startTime = Date.now();
1313

14+
let invalidAttempts = 0;
15+
const maxInvalidAttempts = 15;
16+
1417
while (Date.now() - startTime < timeout) {
15-
const result = await workflowResultFn();
18+
try {
19+
const result = await workflowResultFn();
1620

17-
if (result !== 'running') {
18-
return result;
21+
if (result !== 'running') {
22+
return result;
23+
}
24+
invalidAttempts = 0;
25+
} catch (error) {
26+
if (error instanceof LinkedApiError && error.type === 'httpError') {
27+
invalidAttempts = invalidAttempts + 1;
28+
if (invalidAttempts > maxInvalidAttempts) {
29+
throw error;
30+
}
31+
} else {
32+
throw error;
33+
}
1934
}
2035

2136
await sleep(pollInterval);

src/types/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const LINKED_API_ERROR = {
6262
linkedinAccountSignedOut: 'linkedinAccountSignedOut',
6363
languageNotSupported: 'languageNotSupported',
6464
workflowTimeout: 'workflowTimeout',
65+
httpError: 'httpError',
6566
} as const;
6667
export type TLinkedApiErrorType = (typeof LINKED_API_ERROR)[keyof typeof LINKED_API_ERROR];
6768
export class LinkedApiError extends Error {

0 commit comments

Comments
 (0)