Skip to content

Commit 2a04df9

Browse files
committed
try to use safely-retry as local file
1 parent c6b0c67 commit 2a04df9

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

packages/common/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
],
1919
"dependencies": {
2020
"axios": "^1.7.9",
21-
"safely-try": "^1.1.0",
2221
"tslib": "^2.8.1",
2322
"uuid": "^11.0.5"
2423
},

packages/common/src/lib/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { v1 as uuidv1 } from 'uuid';
88
import os from 'os';
99
import fs from 'fs';
1010
import { spawnSync } from 'child_process';
11-
import safelyTry from 'safely-try';
11+
import safelyTry from './safely-retry.js';
1212
import axios from 'axios';
1313

1414
const UNKNOWN_VALUE = '<unknown>';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
function isPromise<T, S>(obj: PromiseLike<T> | S): obj is PromiseLike<T> {
4+
return (
5+
!!obj &&
6+
(typeof obj === 'object' || typeof obj === 'function') &&
7+
'then' in obj &&
8+
typeof obj.then === 'function'
9+
);
10+
}
11+
12+
function safelyTry<T, E = Error>(
13+
fn: ((...args: any[]) => T) | (() => T),
14+
...args: any[]
15+
): T extends PromiseLike<any>
16+
?
17+
| Promise<{ data: undefined; error: E }>
18+
| Promise<{ data: Awaited<T>; error: undefined }>
19+
: { data: undefined; error: E } | { data: T; error: undefined } {
20+
try {
21+
// try calling the function
22+
const x = fn(...args);
23+
24+
// asynchronous functions
25+
if (isPromise(x)) {
26+
// @ts-ignore
27+
return Promise.resolve(x).then(
28+
(value) => ({ data: value as Awaited<T>, error: undefined }),
29+
(error) => ({ data: undefined, error: error as E }),
30+
);
31+
}
32+
33+
// synchronous functions
34+
// @ts-ignore
35+
return { data: x, error: undefined };
36+
} catch (error) {
37+
// @ts-ignore
38+
return { data: undefined, error: error as E };
39+
}
40+
}
41+
42+
export default safelyTry;

0 commit comments

Comments
 (0)