File tree 3 files changed +43
-2
lines changed
3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change 18
18
],
19
19
"dependencies" : {
20
20
"axios" : " ^1.7.9" ,
21
- "safely-try" : " ^1.1.0" ,
22
21
"tslib" : " ^2.8.1" ,
23
22
"uuid" : " ^11.0.5"
24
23
},
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ import { v1 as uuidv1 } from 'uuid';
8
8
import os from 'os' ;
9
9
import fs from 'fs' ;
10
10
import { spawnSync } from 'child_process' ;
11
- import safelyTry from 'safely-try ' ;
11
+ import safelyTry from './ safely-retry.js ' ;
12
12
import axios from 'axios' ;
13
13
14
14
const UNKNOWN_VALUE = '<unknown>' ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments