-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.ts
More file actions
110 lines (93 loc) · 2.69 KB
/
config.ts
File metadata and controls
110 lines (93 loc) · 2.69 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { NearestNode, NodeConfiguration, TsNode } from "@/node";
import { initializeNodes } from "@/node";
import type { MakeRequestInit } from "@/http/fetch/request";
interface BaseConfiguration {
apiKey: string;
randomizeNodes?: boolean;
connectionTimeoutSeconds?: number;
timeoutSeconds?: number;
healthcheckIntervalSeconds?: number;
numRetries?: number;
retryIntervalSeconds?: number;
sendApiKeyAsQueryParam?: boolean;
additionalHeaders?: Record<string, string>;
fetch?: {
fn?: typeof fetch;
init?: MakeRequestInit;
}
}
type MakeKeysRequired<T, K extends keyof T> = T & {
[P in K]-?: T[P];
};
interface CreateConfiguration extends BaseConfiguration {
nodes: NodeConfiguration[];
nearestNode?: NodeConfiguration;
}
interface Configuration
extends MakeKeysRequired<
BaseConfiguration,
"healthcheckIntervalSeconds" | "numRetries" | "retryIntervalSeconds"
> {
nodes: TsNode[];
nearestNode?: NearestNode;
}
function configure(config: CreateConfiguration): Configuration {
const { nodes, nearestNode } = initializeNodes({
nodes: config.nodes,
nearestNode: config.nearestNode,
});
const numRetries =
config.numRetries && config.numRetries >= 0 ?
config.numRetries
: nodes.length + (nearestNode ? 0 : 1);
const healthcheckIntervalSeconds = config.healthcheckIntervalSeconds ?? 60;
const retryIntervalSeconds = config.retryIntervalSeconds ?? 1;
return {
...config,
nodes,
nearestNode,
numRetries,
healthcheckIntervalSeconds,
retryIntervalSeconds,
};
}
let defaultConfiguration: Configuration | null = null;
/**
* Sets the default configuration that will be used by all HTTP methods
* when no config is explicitly provided.
*/
function setDefaultConfiguration(config: CreateConfiguration): void {
defaultConfiguration = configure(config);
}
/**
* Gets the current default configuration.
* Throws an error if no default configuration has been set.
*/
function getDefaultConfiguration(): Configuration {
if (!defaultConfiguration) {
throw new Error(
"No default configuration has been set. Please call `setDefaultConfiguration()` first or pass a config parameter to the method.",
);
}
return defaultConfiguration;
}
/**
* Gets the configuration to use - either the provided config or the default one.
*/
function getConfiguration(config?: Configuration): Configuration {
return config ?? getDefaultConfiguration();
}
/**
* Clears the default configuration.
*/
function clearDefaultConfiguration(): void {
defaultConfiguration = null;
}
export type { Configuration };
export {
configure,
setDefaultConfiguration,
getDefaultConfiguration,
getConfiguration,
clearDefaultConfiguration,
};