-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.ts
144 lines (126 loc) · 3.69 KB
/
config.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { ShipEngineError } from "./errors";
import * as assert from "./utils/assert";
/**
* Configures the ShipEngine API client.
*/
export interface ShipEngineConfig {
/**
* Your ShipEngine API key.
*
* This can be a production or sandbox key. Sandbox keys start with "TEST_".
*/
apiKey: string;
/**
* ShipEngine child account API key (partner API)
*
* This can be a production or sandbox key. Sandbox keys start with "TEST_".
*/
onBehalfOf?: string;
/**
* The URL of the ShipEngine API. You can usually leave this unset and it will
* default to our public API.
*/
baseURL?: string | URL;
/**
* Some ShipEngine API endpoints return paged data. This lets you control the
* number of items returned per request. Larger numbers will use more memory
* but will require fewer HTTP requests.
*
* Defaults to 50.
*/
pageSize?: number;
/**
* If the ShipEngine client receives a rate limit error it can automatically
* retry the request after a few seconds. This setting lets you control how
* many times it will retry before giving up.
*
* Defaults to 1, which means up to 2 attempts will be made (the original
* attempt, plus one retry).
*/
retries?: number;
/**
* The maximum amount of time (in milliseconds) to wait for a response from
* the ShipEngine server.
*
* Defaults to 65000 (1 minute & 5 seconds).
*/
timeout?: number;
}
/**
* Normalized and sanitized config settings.
*/
export class NormalizedConfig {
public apiKey: string;
public baseURL: URL;
public pageSize: number;
public retries: number;
public timeout: number;
public onBehalfOf?: string;
public constructor(config: string | ShipEngineConfig) {
if (typeof config === "string") {
// Only an API key was specified
config = { apiKey: config };
}
if (
!config ||
(typeof config.apiKey === "string" && config.apiKey.trim().length === 0)
) {
throw new ShipEngineError(
"validation",
"field_value_required",
"A ShipEngine API key must be specified."
);
}
assert.isPOJO("Config", config);
// API Key
assert.isNonWhitespaceString("API Key", config.apiKey);
this.apiKey = config.apiKey;
// Make request on behalf of child account (partner API)
if (config.onBehalfOf) {
assert.isNonWhitespaceString("On Behalf of API Key", config.onBehalfOf);
this.onBehalfOf = config.onBehalfOf;
}
// Base URL
if (config.baseURL instanceof URL) {
this.baseURL = config.baseURL;
} else if (config.baseURL) {
assert.isNonWhitespaceString("Base URL", config.baseURL);
this.baseURL = new URL(config.baseURL);
} else {
this.baseURL = new URL("https://api.shipengine.com/");
}
// Page Size
if (config.pageSize === undefined) {
this.pageSize = 50;
} else {
assert.isPositiveInteger("Page Size", config.pageSize);
this.pageSize = config.pageSize;
}
// Retries
if (config.retries === undefined) {
this.retries = 0;
} else {
assert.isNonNegativeInteger("Retries", config.retries);
this.retries = config.retries;
}
// Timeout
if (config.timeout === undefined) {
this.timeout = 65000;
} else {
assert.isPositiveInteger("Timeout", config.timeout);
this.timeout = config.timeout;
}
}
/**
* Merges a base configuration and overrides, returning a new NormalizedConfig object.
*/
public static merge(
baseConfig: ShipEngineConfig,
overrides?: ShipEngineConfig
): NormalizedConfig {
if (overrides) {
assert.isPOJO("Config", overrides);
}
return new NormalizedConfig({ ...baseConfig, ...overrides });
}
}