accessio 1.1.2 → 1.3.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 +109 -14
- package/cjs/accessio.cjs +59 -3
- package/cjs/accessio.cjs.map +1 -1
- package/cjs/core/accessioError.cjs +28 -3
- package/cjs/core/accessioError.cjs.map +1 -1
- package/cjs/core/buildURL.cjs +15 -1
- package/cjs/core/buildURL.cjs.map +1 -1
- package/cjs/core/fetchAdapter.cjs +54 -10
- package/cjs/core/fetchAdapter.cjs.map +1 -1
- package/cjs/core/request.cjs +107 -31
- package/cjs/core/request.cjs.map +1 -1
- package/cjs/core/retry.cjs +48 -4
- package/cjs/core/retry.cjs.map +1 -1
- package/cjs/helpers/flattenHeaders.cjs +37 -0
- package/cjs/helpers/flattenHeaders.cjs.map +1 -1
- package/cjs/helpers/memoryCache.cjs +51 -0
- package/cjs/helpers/memoryCache.cjs.map +1 -0
- package/cjs/helpers/rateLimiter.cjs +11 -22
- package/cjs/helpers/rateLimiter.cjs.map +1 -1
- package/cjs/helpers/toFormData.cjs +50 -0
- package/cjs/helpers/toFormData.cjs.map +1 -0
- package/cjs/helpers/transformData.cjs +2 -2
- package/cjs/helpers/transformData.cjs.map +1 -1
- package/cjs/index.cjs +4 -1
- package/cjs/index.cjs.map +1 -1
- package/index.d.ts +7 -0
- package/package.json +3 -2
- package/src/accessio.ts +81 -3
- package/src/core/accessioError.ts +26 -1
- package/src/core/buildURL.ts +16 -1
- package/src/core/fetchAdapter.ts +62 -10
- package/src/core/request.ts +134 -44
- package/src/core/retry.ts +44 -6
- package/src/helpers/flattenHeaders.ts +30 -0
- package/src/helpers/memoryCache.ts +30 -0
- package/src/helpers/rateLimiter.ts +11 -24
- package/src/helpers/toFormData.ts +25 -0
- package/src/helpers/transformData.ts +2 -1
- package/src/index.ts +4 -1
- package/src/types.ts +27 -0
package/src/core/retry.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { ERR_CANCELED, ERR_NETWORK
|
|
1
|
+
import { ERR_BAD_OPTION, ERR_CANCELED, ERR_NETWORK } from '../constants/errorCodes';
|
|
2
|
+
import AccessioErrorClass from './accessioError';
|
|
2
3
|
import type {
|
|
3
4
|
AccessioRequestConfig,
|
|
4
|
-
AccessioResponse,
|
|
5
5
|
AccessioError,
|
|
6
6
|
RetryConditionFunction,
|
|
7
7
|
OnRetryFunction,
|
|
8
8
|
} from '../types';
|
|
9
9
|
|
|
10
|
+
function isUnretriableBody(data: unknown): boolean {
|
|
11
|
+
if (data == null) return false;
|
|
12
|
+
if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) return true;
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
10
16
|
function defaultRetryCondition(error: any): boolean {
|
|
11
17
|
if (error.code === ERR_CANCELED) {
|
|
12
18
|
return false;
|
|
@@ -20,6 +26,10 @@ function defaultRetryCondition(error: any): boolean {
|
|
|
20
26
|
return true;
|
|
21
27
|
}
|
|
22
28
|
|
|
29
|
+
if (error.config?.retryOn429 && error.response && error.response.status === 429) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
23
33
|
return false;
|
|
24
34
|
}
|
|
25
35
|
|
|
@@ -62,7 +72,7 @@ async function retryRequest(
|
|
|
62
72
|
): Promise<any> {
|
|
63
73
|
const maxRetries = config.retry ?? 0;
|
|
64
74
|
|
|
65
|
-
if (maxRetries <= 0) {
|
|
75
|
+
if (maxRetries <= 0 && !config.retryOn429) {
|
|
66
76
|
return dispatchFn(config);
|
|
67
77
|
}
|
|
68
78
|
|
|
@@ -70,22 +80,50 @@ async function retryRequest(
|
|
|
70
80
|
const retryCondition: RetryConditionFunction = config.retryCondition ?? defaultRetryCondition;
|
|
71
81
|
|
|
72
82
|
let lastError: any;
|
|
83
|
+
const actualMaxRetries = Math.max(maxRetries, config.retryOn429 ? 3 : 0);
|
|
73
84
|
|
|
74
|
-
for (let attempt = 0; attempt <=
|
|
85
|
+
for (let attempt = 0; attempt <= actualMaxRetries; attempt++) {
|
|
75
86
|
try {
|
|
76
87
|
const response = await dispatchFn(config);
|
|
77
88
|
return response;
|
|
78
89
|
} catch (error) {
|
|
79
90
|
lastError = error;
|
|
80
91
|
|
|
81
|
-
const isLastAttempt = attempt >=
|
|
92
|
+
const isLastAttempt = attempt >= actualMaxRetries;
|
|
82
93
|
const shouldRetry = !isLastAttempt && retryCondition(error as AccessioError);
|
|
83
94
|
|
|
84
95
|
if (!shouldRetry) {
|
|
85
96
|
throw error;
|
|
86
97
|
}
|
|
87
98
|
|
|
88
|
-
|
|
99
|
+
if (isUnretriableBody(config.data)) {
|
|
100
|
+
throw new AccessioErrorClass(
|
|
101
|
+
'Request body is a ReadableStream and cannot be retried after consumption. ' +
|
|
102
|
+
'Buffer the stream upstream or set retry: 0 for this call.',
|
|
103
|
+
ERR_BAD_OPTION,
|
|
104
|
+
config,
|
|
105
|
+
null,
|
|
106
|
+
(error as AccessioError).response ?? null,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let delay = calculateDelay(attempt, retryDelay);
|
|
111
|
+
|
|
112
|
+
if (config.retryOn429 && (error as any).response?.status === 429) {
|
|
113
|
+
const headers = (error as any).response?.headers;
|
|
114
|
+
const retryAfterStr = headers?.['retry-after'] || headers?.['Retry-After'];
|
|
115
|
+
if (retryAfterStr) {
|
|
116
|
+
const parsed = parseInt(retryAfterStr, 10);
|
|
117
|
+
if (!isNaN(parsed)) {
|
|
118
|
+
delay = parsed * 1000;
|
|
119
|
+
} else {
|
|
120
|
+
const date = new Date(retryAfterStr);
|
|
121
|
+
if (!isNaN(date.getTime())) {
|
|
122
|
+
delay = Math.max(0, date.getTime() - Date.now());
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
89
127
|
|
|
90
128
|
if (typeof config.onRetry === 'function') {
|
|
91
129
|
(config.onRetry as OnRetryFunction)(attempt + 1, error as AccessioError, config);
|
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
import AccessioError from '../core/accessioError';
|
|
2
|
+
import { ERR_BAD_OPTION } from '../constants/errorCodes';
|
|
3
|
+
|
|
4
|
+
const HEADER_FORBIDDEN_CHAR = /[\r\n\0]/;
|
|
5
|
+
|
|
6
|
+
function assertSafeHeader(name: string, value: string | string[]): void {
|
|
7
|
+
if (typeof name !== 'string' || HEADER_FORBIDDEN_CHAR.test(name)) {
|
|
8
|
+
throw new AccessioError(
|
|
9
|
+
`Invalid header name "${String(name)}": CR, LF and NUL are not allowed`,
|
|
10
|
+
ERR_BAD_OPTION,
|
|
11
|
+
null,
|
|
12
|
+
null,
|
|
13
|
+
null,
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
const values = Array.isArray(value) ? value : [value];
|
|
17
|
+
for (const v of values) {
|
|
18
|
+
if (typeof v === 'string' && HEADER_FORBIDDEN_CHAR.test(v)) {
|
|
19
|
+
throw new AccessioError(
|
|
20
|
+
`Invalid value for header "${name}": CR, LF and NUL are not allowed`,
|
|
21
|
+
ERR_BAD_OPTION,
|
|
22
|
+
null,
|
|
23
|
+
null,
|
|
24
|
+
null,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
1
30
|
const METHOD_KEYS = new Set<string>([
|
|
2
31
|
'common',
|
|
3
32
|
'delete',
|
|
@@ -47,6 +76,7 @@ export function removeContentType(headers: Record<string, string | string[]>): v
|
|
|
47
76
|
export function buildFetchHeaders(headers: Record<string, string | string[]>): Headers {
|
|
48
77
|
const fetchHeaders = new Headers();
|
|
49
78
|
for (const [key, value] of Object.entries(headers)) {
|
|
79
|
+
assertSafeHeader(key, value);
|
|
50
80
|
if (Array.isArray(value)) {
|
|
51
81
|
for (const v of value) {
|
|
52
82
|
fetchHeaders.append(key, v);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CacheProvider } from '../types';
|
|
2
|
+
|
|
3
|
+
class MemoryCache implements CacheProvider {
|
|
4
|
+
private cache = new Map<string, { value: any; expiry: number | null }>();
|
|
5
|
+
|
|
6
|
+
get(key: string) {
|
|
7
|
+
const item = this.cache.get(key);
|
|
8
|
+
if (!item) return null;
|
|
9
|
+
if (item.expiry && Date.now() > item.expiry) {
|
|
10
|
+
this.cache.delete(key);
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return item.value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
set(key: string, value: any, ttl?: number) {
|
|
17
|
+
const expiry = ttl ? Date.now() + ttl : null;
|
|
18
|
+
this.cache.set(key, { value, expiry });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
delete(key: string) {
|
|
22
|
+
this.cache.delete(key);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
clear() {
|
|
26
|
+
this.cache.clear();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const defaultMemoryCache = new MemoryCache();
|
|
@@ -21,10 +21,7 @@ export function createRateLimiter(
|
|
|
21
21
|
}
|
|
22
22
|
let active = 0;
|
|
23
23
|
let destroyed = false;
|
|
24
|
-
|
|
25
|
-
let tail = 0;
|
|
26
|
-
let pendingCount = 0;
|
|
27
|
-
const queue: Record<number, QueueItem> = {};
|
|
24
|
+
const queue: QueueItem[] = [];
|
|
28
25
|
|
|
29
26
|
function acquire(): Promise<void> {
|
|
30
27
|
if (destroyed) {
|
|
@@ -36,44 +33,34 @@ export function createRateLimiter(
|
|
|
36
33
|
return Promise.resolve();
|
|
37
34
|
}
|
|
38
35
|
|
|
39
|
-
if (
|
|
36
|
+
if (queue.length >= maxQueueSize) {
|
|
40
37
|
return Promise.reject(
|
|
41
38
|
new Error(`[Accessio] Rate limiter queue size exceeded maxQueueSize (${maxQueueSize})`),
|
|
42
39
|
);
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
return new Promise((resolve, reject) => {
|
|
46
|
-
queue
|
|
47
|
-
pendingCount++;
|
|
43
|
+
queue.push({ resolve, reject });
|
|
48
44
|
});
|
|
49
45
|
}
|
|
50
46
|
|
|
51
47
|
function release(): void {
|
|
52
48
|
if (destroyed) return;
|
|
53
|
-
|
|
54
49
|
if (active <= 0) return;
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const next = queue[head];
|
|
61
|
-
delete queue[head];
|
|
62
|
-
head++;
|
|
63
|
-
pendingCount--;
|
|
64
|
-
next?.resolve();
|
|
51
|
+
const next = queue.shift();
|
|
52
|
+
if (next) {
|
|
53
|
+
next.resolve();
|
|
54
|
+
return;
|
|
65
55
|
}
|
|
56
|
+
active--;
|
|
66
57
|
}
|
|
67
58
|
|
|
68
59
|
function destroy(): void {
|
|
69
60
|
destroyed = true;
|
|
70
61
|
const reason = new Error('[Accessio] Rate limiter destroyed — pending request cancelled');
|
|
71
|
-
while (
|
|
72
|
-
|
|
73
|
-
delete queue[head];
|
|
74
|
-
head++;
|
|
75
|
-
pendingCount--;
|
|
76
|
-
next?.reject(reason);
|
|
62
|
+
while (queue.length > 0) {
|
|
63
|
+
queue.shift()!.reject(reason);
|
|
77
64
|
}
|
|
78
65
|
}
|
|
79
66
|
|
|
@@ -82,7 +69,7 @@ export function createRateLimiter(
|
|
|
82
69
|
release,
|
|
83
70
|
destroy,
|
|
84
71
|
get pending() {
|
|
85
|
-
return
|
|
72
|
+
return queue.length;
|
|
86
73
|
},
|
|
87
74
|
get active() {
|
|
88
75
|
return active;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function toFormData(obj: any, form?: FormData, namespace?: string): FormData {
|
|
2
|
+
const fd = form || new FormData();
|
|
3
|
+
let formKey: string;
|
|
4
|
+
|
|
5
|
+
if (obj === null || obj === undefined) {
|
|
6
|
+
return fd;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (obj instanceof Date) {
|
|
10
|
+
fd.append(namespace || '', obj.toISOString());
|
|
11
|
+
} else if (typeof obj === 'object' && !(obj instanceof File) && !(obj instanceof Blob)) {
|
|
12
|
+
Object.keys(obj).forEach((key) => {
|
|
13
|
+
if (Array.isArray(obj)) {
|
|
14
|
+
formKey = namespace ? `${namespace}[${key}]` : key;
|
|
15
|
+
} else {
|
|
16
|
+
formKey = namespace ? `${namespace}.${key}` : key;
|
|
17
|
+
}
|
|
18
|
+
toFormData(obj[key], fd, formKey);
|
|
19
|
+
});
|
|
20
|
+
} else {
|
|
21
|
+
fd.append(namespace || '', obj);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return fd;
|
|
25
|
+
}
|
|
@@ -6,6 +6,7 @@ export default async function transformData(
|
|
|
6
6
|
data: unknown,
|
|
7
7
|
headers: Record<string, string | string[]>,
|
|
8
8
|
config?: AccessioRequestConfig,
|
|
9
|
+
direction: 'request' | 'response' = 'request',
|
|
9
10
|
): Promise<unknown> {
|
|
10
11
|
if (!transforms || !Array.isArray(transforms)) {
|
|
11
12
|
return data;
|
|
@@ -20,7 +21,7 @@ export default async function transformData(
|
|
|
20
21
|
} catch (err) {
|
|
21
22
|
throw AccessioError.from(
|
|
22
23
|
err instanceof Error ? err : new Error(String(err)),
|
|
23
|
-
AccessioError.ERR_BAD_REQUEST,
|
|
24
|
+
direction === 'response' ? AccessioError.ERR_BAD_RESPONSE : AccessioError.ERR_BAD_REQUEST,
|
|
24
25
|
config ?? null,
|
|
25
26
|
null,
|
|
26
27
|
null,
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import InterceptorManager from './interceptors/interceptorManager';
|
|
|
7
7
|
import { createRateLimiter } from './helpers/rateLimiter';
|
|
8
8
|
import { logRequest, logResponse, logError } from './helpers/debug';
|
|
9
9
|
import { ERR_CANCELED } from './constants/errorCodes';
|
|
10
|
-
import type { AccessioRequestConfig
|
|
10
|
+
import type { AccessioRequestConfig } from './types';
|
|
11
11
|
|
|
12
12
|
const PUBLIC_METHODS = [
|
|
13
13
|
'request',
|
|
@@ -22,6 +22,9 @@ const PUBLIC_METHODS = [
|
|
|
22
22
|
'postForm',
|
|
23
23
|
'putForm',
|
|
24
24
|
'patchForm',
|
|
25
|
+
'stream',
|
|
26
|
+
'autoPaginate',
|
|
27
|
+
'gql',
|
|
25
28
|
];
|
|
26
29
|
|
|
27
30
|
function createInstance(defaultConfig: AccessioRequestConfig) {
|
package/src/types.ts
CHANGED
|
@@ -43,6 +43,24 @@ export interface Interceptors {
|
|
|
43
43
|
response: InterceptorManager;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
export interface AccessioHooks {
|
|
47
|
+
onBeforeRequest?: (config: AccessioRequestConfig) => void | Promise<void>;
|
|
48
|
+
onRequestResponse?: (response: AccessioResponse) => void | Promise<void>;
|
|
49
|
+
onRequestError?: (error: AccessioError) => void | Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface CacheProvider {
|
|
53
|
+
get: (key: string) => Promise<any> | any;
|
|
54
|
+
set: (key: string, value: any, ttl?: number) => Promise<void> | void;
|
|
55
|
+
delete: (key: string) => Promise<void> | void;
|
|
56
|
+
clear: () => Promise<void> | void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface SchemaValidator<T = any> {
|
|
60
|
+
parse(data: unknown): T;
|
|
61
|
+
parseAsync?(data: unknown): Promise<T>;
|
|
62
|
+
}
|
|
63
|
+
|
|
46
64
|
export interface AccessioRequestConfig {
|
|
47
65
|
url?: string;
|
|
48
66
|
baseURL?: string;
|
|
@@ -67,8 +85,17 @@ export interface AccessioRequestConfig {
|
|
|
67
85
|
rateLimiter?: RateLimiter;
|
|
68
86
|
_builtUrl?: string;
|
|
69
87
|
maxContentLength?: number;
|
|
88
|
+
allowedProtocols?: string[] | null;
|
|
70
89
|
dispatcher?: unknown;
|
|
71
90
|
agent?: unknown;
|
|
91
|
+
dedupe?: boolean;
|
|
92
|
+
cache?: boolean | CacheProvider;
|
|
93
|
+
cacheTTL?: number;
|
|
94
|
+
onDownloadProgress?: (progressEvent: { loaded: number; total: number }) => void;
|
|
95
|
+
hooks?: AccessioHooks;
|
|
96
|
+
schema?: SchemaValidator;
|
|
97
|
+
fetch?: typeof fetch;
|
|
98
|
+
retryOn429?: boolean;
|
|
72
99
|
}
|
|
73
100
|
|
|
74
101
|
export interface AccessioResponse<T = unknown> {
|