-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathutils.ts
39 lines (36 loc) · 1.18 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { version } from './version.js';
/**
* @private
*/
export const generateClientInfo = () => ({
name: 'node-auth0',
version: version,
env: {
node: process.version.replace('v', ''),
},
});
/**
* @private
*/
export const mtlsPrefix = 'mtls';
type SyncGetter<T> = () => T;
type AsyncGetter<T> = () => Promise<T>;
/**
* Resolves a value that can be a static value, a synchronous function, or an asynchronous function.
*
* @template T - The type of the value to be resolved.
* @param {T | SyncGetter<T> | AsyncGetter<T>} value - The value to be resolved. It can be:
* - A static value of type T.
* - A synchronous function that returns a value of type T.
* - An asynchronous function that returns a Promise of type T.
* @returns {Promise<T>} A promise that resolves to the value of type T.
*/
export const resolveValueToPromise = async <T>(
value: T | SyncGetter<T> | AsyncGetter<T>
): Promise<T> => {
if (typeof value === 'function') {
const result = (value as SyncGetter<T> | AsyncGetter<T>)(); // Call the function
return result instanceof Promise ? result : Promise.resolve(result); // Handle sync/async
}
return Promise.resolve(value); // Static value
};