accessio 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -21
- package/cjs/accessio.cjs +68 -84
- package/cjs/accessio.cjs.map +1 -1
- package/cjs/core/accessioError.cjs +49 -3
- package/cjs/core/accessioError.cjs.map +1 -1
- package/cjs/core/buildURL.cjs +10 -4
- package/cjs/core/buildURL.cjs.map +1 -1
- package/cjs/core/fetchAdapter.cjs +134 -111
- package/cjs/core/fetchAdapter.cjs.map +1 -1
- package/cjs/core/request.cjs +97 -24
- package/cjs/core/request.cjs.map +1 -1
- package/cjs/core/retry.cjs +25 -0
- package/cjs/core/retry.cjs.map +1 -1
- package/cjs/helpers/debug.cjs +7 -1
- package/cjs/helpers/debug.cjs.map +1 -1
- package/cjs/helpers/flattenHeaders.cjs +37 -0
- package/cjs/helpers/flattenHeaders.cjs.map +1 -1
- package/cjs/helpers/rateLimiter.cjs +11 -22
- package/cjs/helpers/rateLimiter.cjs.map +1 -1
- package/cjs/helpers/settle.cjs +1 -1
- package/cjs/helpers/settle.cjs.map +1 -1
- package/cjs/helpers/transformData.cjs +2 -2
- package/cjs/helpers/transformData.cjs.map +1 -1
- package/cjs/interceptors/interceptorManager.cjs +25 -18
- package/cjs/interceptors/interceptorManager.cjs.map +1 -1
- package/index.d.ts +89 -21
- package/package.json +2 -2
- package/src/accessio.ts +104 -98
- package/src/core/accessioError.ts +50 -1
- package/src/core/buildURL.ts +14 -4
- package/src/core/fetchAdapter.ts +166 -130
- package/src/core/request.ts +115 -28
- package/src/core/retry.ts +19 -1
- package/src/helpers/debug.ts +7 -2
- package/src/helpers/flattenHeaders.ts +30 -0
- package/src/helpers/rateLimiter.ts +11 -24
- package/src/helpers/settle.ts +1 -1
- package/src/helpers/transformData.ts +2 -1
- package/src/interceptors/interceptorManager.ts +26 -19
- package/src/types.ts +1 -0
package/src/accessio.ts
CHANGED
|
@@ -15,6 +15,66 @@ import type {
|
|
|
15
15
|
} from './types';
|
|
16
16
|
import defaultsConfig from './defaults/index';
|
|
17
17
|
|
|
18
|
+
function runRequestInterceptorsSync(
|
|
19
|
+
startConfig: AccessioRequestConfig,
|
|
20
|
+
interceptors: InterceptorHandler[],
|
|
21
|
+
): Promise<AccessioRequestConfig> {
|
|
22
|
+
let cfg = startConfig;
|
|
23
|
+
let rejectReason: any = null;
|
|
24
|
+
let isRejected = false;
|
|
25
|
+
|
|
26
|
+
for (const interceptor of interceptors) {
|
|
27
|
+
if (!isRejected) {
|
|
28
|
+
try {
|
|
29
|
+
if (interceptor.fulfilled) {
|
|
30
|
+
cfg = (interceptor.fulfilled as any)(cfg) as AccessioRequestConfig;
|
|
31
|
+
}
|
|
32
|
+
} catch (err) {
|
|
33
|
+
rejectReason = err;
|
|
34
|
+
isRejected = true;
|
|
35
|
+
}
|
|
36
|
+
} else if (interceptor.rejected) {
|
|
37
|
+
try {
|
|
38
|
+
cfg = interceptor.rejected(rejectReason) as AccessioRequestConfig;
|
|
39
|
+
isRejected = false;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
rejectReason = err;
|
|
42
|
+
isRejected = true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return isRejected ? Promise.reject(rejectReason) : Promise.resolve(cfg);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function runRequestInterceptorsAsync(
|
|
51
|
+
startConfig: AccessioRequestConfig,
|
|
52
|
+
interceptors: InterceptorHandler[],
|
|
53
|
+
): Promise<AccessioRequestConfig> {
|
|
54
|
+
let promise: Promise<any> = Promise.resolve(startConfig);
|
|
55
|
+
for (const interceptor of interceptors) {
|
|
56
|
+
promise = promise.then(
|
|
57
|
+
(value: any) => (interceptor.fulfilled ? (interceptor.fulfilled as any)(value) : value),
|
|
58
|
+
interceptor.rejected ?? undefined,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return promise as Promise<AccessioRequestConfig>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function dispatchAndRetry(cfg: AccessioRequestConfig): Promise<AccessioResponse> {
|
|
65
|
+
const fullUrl = buildURL(cfg.url ?? '', cfg.baseURL, cfg.params, cfg.paramsSerializer);
|
|
66
|
+
logRequest(cfg, fullUrl);
|
|
67
|
+
|
|
68
|
+
const enrichedCfg = fullUrl !== (cfg.url || '') ? { ...cfg, _builtUrl: fullUrl } : cfg;
|
|
69
|
+
|
|
70
|
+
const dispatchFn = cfg.rateLimiter
|
|
71
|
+
? (config: AccessioRequestConfig) =>
|
|
72
|
+
rateLimitedRequest(dispatchRequest, config.rateLimiter!, config)
|
|
73
|
+
: dispatchRequest;
|
|
74
|
+
|
|
75
|
+
return retryRequest(dispatchFn, enrichedCfg);
|
|
76
|
+
}
|
|
77
|
+
|
|
18
78
|
export class Accessio {
|
|
19
79
|
defaults: AccessioRequestConfig;
|
|
20
80
|
interceptors: Interceptors;
|
|
@@ -51,86 +111,17 @@ export class Accessio {
|
|
|
51
111
|
);
|
|
52
112
|
}
|
|
53
113
|
|
|
54
|
-
const requestInterceptors
|
|
55
|
-
|
|
56
|
-
let synchronousRequestInterceptors = true;
|
|
114
|
+
const { requestInterceptors, responseInterceptors, synchronous } =
|
|
115
|
+
this.collectInterceptors(mergedConfig);
|
|
57
116
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
|
63
|
-
requestInterceptors.unshift(interceptor);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
this.interceptors.response.forEach((interceptor: InterceptorHandler) => {
|
|
67
|
-
responseInterceptors.push(interceptor);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
let promise: Promise<any>;
|
|
71
|
-
|
|
72
|
-
if (synchronousRequestInterceptors) {
|
|
73
|
-
let newConfig = mergedConfig;
|
|
74
|
-
let rejectReason: any = null;
|
|
75
|
-
let isRejected = false;
|
|
76
|
-
|
|
77
|
-
for (const interceptor of requestInterceptors) {
|
|
78
|
-
if (!isRejected) {
|
|
79
|
-
try {
|
|
80
|
-
if (interceptor.fulfilled) {
|
|
81
|
-
newConfig = interceptor.fulfilled(newConfig);
|
|
82
|
-
}
|
|
83
|
-
} catch (err) {
|
|
84
|
-
rejectReason = err;
|
|
85
|
-
isRejected = true;
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
if (interceptor.rejected) {
|
|
89
|
-
try {
|
|
90
|
-
newConfig = interceptor.rejected(rejectReason);
|
|
91
|
-
isRejected = false;
|
|
92
|
-
} catch (err) {
|
|
93
|
-
rejectReason = err;
|
|
94
|
-
isRejected = true;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (isRejected) {
|
|
101
|
-
promise = Promise.reject(rejectReason);
|
|
102
|
-
} else {
|
|
103
|
-
promise = Promise.resolve(newConfig);
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
promise = Promise.resolve(mergedConfig);
|
|
107
|
-
for (const interceptor of requestInterceptors) {
|
|
108
|
-
promise = promise.then((value: any) => {
|
|
109
|
-
if (interceptor.fulfilled) {
|
|
110
|
-
return interceptor.fulfilled(value);
|
|
111
|
-
}
|
|
112
|
-
return value;
|
|
113
|
-
}, interceptor.rejected);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
117
|
+
let promise: Promise<any> = synchronous
|
|
118
|
+
? runRequestInterceptorsSync(mergedConfig, requestInterceptors)
|
|
119
|
+
: runRequestInterceptorsAsync(mergedConfig, requestInterceptors);
|
|
116
120
|
|
|
117
|
-
promise = promise.then((cfg:
|
|
118
|
-
const fullUrl = buildURL(cfg.url ?? '', cfg.baseURL, cfg.params, cfg.paramsSerializer);
|
|
119
|
-
|
|
120
|
-
logRequest(cfg, fullUrl);
|
|
121
|
-
|
|
122
|
-
const enrichedCfg = fullUrl !== (cfg.url || '') ? { ...cfg, _builtUrl: fullUrl } : cfg;
|
|
123
|
-
|
|
124
|
-
const dispatchFn = cfg.rateLimiter
|
|
125
|
-
? (config: AccessioRequestConfig) =>
|
|
126
|
-
rateLimitedRequest(dispatchRequest, config.rateLimiter!, config)
|
|
127
|
-
: dispatchRequest;
|
|
128
|
-
|
|
129
|
-
return retryRequest(dispatchFn, enrichedCfg);
|
|
130
|
-
});
|
|
121
|
+
promise = promise.then((cfg: AccessioRequestConfig) => dispatchAndRetry(cfg));
|
|
131
122
|
|
|
132
123
|
promise = promise.then(
|
|
133
|
-
(value:
|
|
124
|
+
(value: AccessioResponse) => {
|
|
134
125
|
logResponse(value);
|
|
135
126
|
return value;
|
|
136
127
|
},
|
|
@@ -143,15 +134,37 @@ export class Accessio {
|
|
|
143
134
|
for (const interceptor of responseInterceptors) {
|
|
144
135
|
promise = promise.then((value: any) => {
|
|
145
136
|
if (interceptor.fulfilled) {
|
|
146
|
-
return interceptor.fulfilled(value);
|
|
137
|
+
return (interceptor.fulfilled as any)(value);
|
|
147
138
|
}
|
|
148
139
|
return value;
|
|
149
|
-
}, interceptor.rejected);
|
|
140
|
+
}, interceptor.rejected ?? undefined);
|
|
150
141
|
}
|
|
151
142
|
|
|
152
143
|
return promise;
|
|
153
144
|
}
|
|
154
145
|
|
|
146
|
+
private collectInterceptors(mergedConfig: AccessioRequestConfig): {
|
|
147
|
+
requestInterceptors: InterceptorHandler[];
|
|
148
|
+
responseInterceptors: InterceptorHandler[];
|
|
149
|
+
synchronous: boolean;
|
|
150
|
+
} {
|
|
151
|
+
const requestInterceptors: InterceptorHandler[] = [];
|
|
152
|
+
const responseInterceptors: InterceptorHandler[] = [];
|
|
153
|
+
let synchronous = true;
|
|
154
|
+
|
|
155
|
+
this.interceptors.request.forEach((interceptor: InterceptorHandler) => {
|
|
156
|
+
if (interceptor.runWhen && !interceptor.runWhen(mergedConfig)) return;
|
|
157
|
+
synchronous = synchronous && interceptor.synchronous;
|
|
158
|
+
requestInterceptors.unshift(interceptor);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
this.interceptors.response.forEach((interceptor: InterceptorHandler) => {
|
|
162
|
+
responseInterceptors.push(interceptor);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
return { requestInterceptors, responseInterceptors, synchronous };
|
|
166
|
+
}
|
|
167
|
+
|
|
155
168
|
getUri(config?: AccessioRequestConfig): string {
|
|
156
169
|
const merged = mergeConfig(this.defaults, config);
|
|
157
170
|
return buildURL(merged.url ?? '', merged.baseURL, merged.params, merged.paramsSerializer);
|
|
@@ -197,7 +210,8 @@ export class Accessio {
|
|
|
197
210
|
return this.request<T>(mergeConfig(config || {}, { method: 'patch', url, data }));
|
|
198
211
|
}
|
|
199
212
|
|
|
200
|
-
|
|
213
|
+
private formRequest<T = any>(
|
|
214
|
+
method: 'post' | 'put' | 'patch',
|
|
201
215
|
url: string,
|
|
202
216
|
data?: any,
|
|
203
217
|
config?: AccessioRequestConfig,
|
|
@@ -205,7 +219,7 @@ export class Accessio {
|
|
|
205
219
|
const formData = data && !(data instanceof FormData) ? toFormData(data) : data;
|
|
206
220
|
return this.request<T>(
|
|
207
221
|
mergeConfig(config || {}, {
|
|
208
|
-
method
|
|
222
|
+
method,
|
|
209
223
|
url,
|
|
210
224
|
data: formData,
|
|
211
225
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
@@ -213,20 +227,20 @@ export class Accessio {
|
|
|
213
227
|
);
|
|
214
228
|
}
|
|
215
229
|
|
|
230
|
+
postForm<T = any>(
|
|
231
|
+
url: string,
|
|
232
|
+
data?: any,
|
|
233
|
+
config?: AccessioRequestConfig,
|
|
234
|
+
): Promise<AccessioResponse<T>> {
|
|
235
|
+
return this.formRequest<T>('post', url, data, config);
|
|
236
|
+
}
|
|
237
|
+
|
|
216
238
|
putForm<T = any>(
|
|
217
239
|
url: string,
|
|
218
240
|
data?: any,
|
|
219
241
|
config?: AccessioRequestConfig,
|
|
220
242
|
): Promise<AccessioResponse<T>> {
|
|
221
|
-
|
|
222
|
-
return this.request<T>(
|
|
223
|
-
mergeConfig(config || {}, {
|
|
224
|
-
method: 'put',
|
|
225
|
-
url,
|
|
226
|
-
data: formData,
|
|
227
|
-
headers: { 'Content-Type': 'multipart/form-data' },
|
|
228
|
-
}),
|
|
229
|
-
);
|
|
243
|
+
return this.formRequest<T>('put', url, data, config);
|
|
230
244
|
}
|
|
231
245
|
|
|
232
246
|
patchForm<T = any>(
|
|
@@ -234,15 +248,7 @@ export class Accessio {
|
|
|
234
248
|
data?: any,
|
|
235
249
|
config?: AccessioRequestConfig,
|
|
236
250
|
): Promise<AccessioResponse<T>> {
|
|
237
|
-
|
|
238
|
-
return this.request<T>(
|
|
239
|
-
mergeConfig(config || {}, {
|
|
240
|
-
method: 'patch',
|
|
241
|
-
url,
|
|
242
|
-
data: formData,
|
|
243
|
-
headers: { 'Content-Type': 'multipart/form-data' },
|
|
244
|
-
}),
|
|
245
|
-
);
|
|
251
|
+
return this.formRequest<T>('patch', url, data, config);
|
|
246
252
|
}
|
|
247
253
|
|
|
248
254
|
async *stream<T = any>(
|
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
import ErrorCodes from '../constants/errorCodes';
|
|
2
2
|
import type { AccessioRequestConfig, AccessioResponse } from '../types';
|
|
3
3
|
|
|
4
|
+
function redactHeaders(headers: unknown): unknown {
|
|
5
|
+
if (!headers || typeof headers !== 'object') return headers;
|
|
6
|
+
const out: Record<string, unknown> = {};
|
|
7
|
+
for (const key of Object.keys(headers as Record<string, unknown>)) {
|
|
8
|
+
const value = (headers as Record<string, unknown>)[key];
|
|
9
|
+
if (/^authorization$/i.test(key) || /^cookie$/i.test(key) || /^set-cookie$/i.test(key)) {
|
|
10
|
+
out[key] = '[REDACTED]';
|
|
11
|
+
} else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
12
|
+
out[key] = redactHeaders(value);
|
|
13
|
+
} else {
|
|
14
|
+
out[key] = value;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const SENSITIVE_BODY_KEY =
|
|
21
|
+
/^(password|passwd|pwd|token|access_token|refresh_token|id_token|authorization|api[_-]?key|secret|client[_-]?secret|cookie|set[_-]?cookie|private[_-]?key|session)$/i;
|
|
22
|
+
|
|
23
|
+
export function redactBody(value: unknown, seen?: WeakSet<object>): unknown {
|
|
24
|
+
if (value === null || typeof value !== 'object') return value;
|
|
25
|
+
const visited = seen ?? new WeakSet<object>();
|
|
26
|
+
if (visited.has(value as object)) return '[Circular]';
|
|
27
|
+
visited.add(value as object);
|
|
28
|
+
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
return value.map((item) => redactBody(item, visited));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const out: Record<string, unknown> = {};
|
|
34
|
+
for (const key of Object.keys(value as Record<string, unknown>)) {
|
|
35
|
+
const v = (value as Record<string, unknown>)[key];
|
|
36
|
+
if (SENSITIVE_BODY_KEY.test(key)) {
|
|
37
|
+
out[key] = '[REDACTED]';
|
|
38
|
+
} else {
|
|
39
|
+
out[key] = redactBody(v, visited);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function redactConfig(config: AccessioRequestConfig | null): AccessioRequestConfig | null {
|
|
46
|
+
if (!config) return config;
|
|
47
|
+
const clone = { ...config } as AccessioRequestConfig & { auth?: unknown };
|
|
48
|
+
if ('auth' in clone) delete clone.auth;
|
|
49
|
+
if (clone.headers) clone.headers = redactHeaders(clone.headers) as typeof clone.headers;
|
|
50
|
+
return clone;
|
|
51
|
+
}
|
|
52
|
+
|
|
4
53
|
export class AccessioError extends Error {
|
|
5
54
|
static ERR_BAD_OPTION_VALUE: string = ErrorCodes.ERR_BAD_OPTION_VALUE;
|
|
6
55
|
static ERR_BAD_OPTION: string = ErrorCodes.ERR_BAD_OPTION;
|
|
@@ -32,7 +81,7 @@ export class AccessioError extends Error {
|
|
|
32
81
|
super(message);
|
|
33
82
|
this.name = 'AccessioError';
|
|
34
83
|
this.code = code ?? null;
|
|
35
|
-
this.config = config ?? null;
|
|
84
|
+
this.config = redactConfig(config ?? null);
|
|
36
85
|
this.request = request ?? null;
|
|
37
86
|
this.response = response ?? null;
|
|
38
87
|
this.isAccessioError = true;
|
package/src/core/buildURL.ts
CHANGED
|
@@ -76,11 +76,19 @@ export default function buildURL(
|
|
|
76
76
|
let fullURL = baseURL && !isAbsoluteURL(url) ? combineURLs(baseURL, url) : url || '';
|
|
77
77
|
|
|
78
78
|
let finalParams = params;
|
|
79
|
-
if (params && typeof params === 'object') {
|
|
80
|
-
const unusedParams = {
|
|
79
|
+
if (params && typeof params === 'object' && !(params instanceof URLSearchParams)) {
|
|
80
|
+
const unusedParams: Record<string, unknown> = {};
|
|
81
|
+
for (const key of Object.keys(params)) {
|
|
82
|
+
if (key === '__proto__' || key === 'prototype' || key === 'constructor') continue;
|
|
83
|
+
unusedParams[key] = (params as Record<string, unknown>)[key];
|
|
84
|
+
}
|
|
81
85
|
fullURL = fullURL.replace(/(?::([a-zA-Z0-9_]+))|(?:{([a-zA-Z0-9_]+)})/g, (match, p1, p2) => {
|
|
82
86
|
const key = p1 || p2;
|
|
83
|
-
if (
|
|
87
|
+
if (
|
|
88
|
+
key &&
|
|
89
|
+
Object.prototype.hasOwnProperty.call(unusedParams, key) &&
|
|
90
|
+
unusedParams[key] !== undefined
|
|
91
|
+
) {
|
|
84
92
|
const val = unusedParams[key];
|
|
85
93
|
delete unusedParams[key];
|
|
86
94
|
return encodeURIComponent(String(val));
|
|
@@ -93,10 +101,12 @@ export default function buildURL(
|
|
|
93
101
|
const serialized = serializeParams(finalParams as Record<string, unknown>, paramsSerializer);
|
|
94
102
|
if (serialized) {
|
|
95
103
|
const hashIndex = fullURL.indexOf('#');
|
|
104
|
+
let fragment = '';
|
|
96
105
|
if (hashIndex !== -1) {
|
|
106
|
+
fragment = fullURL.slice(hashIndex);
|
|
97
107
|
fullURL = fullURL.slice(0, hashIndex);
|
|
98
108
|
}
|
|
99
|
-
fullURL += (fullURL.indexOf('?') === -1 ? '?' : '&') + serialized;
|
|
109
|
+
fullURL += (fullURL.indexOf('?') === -1 ? '?' : '&') + serialized + fragment;
|
|
100
110
|
}
|
|
101
111
|
|
|
102
112
|
return fullURL;
|