accessio 1.1.2 → 1.2.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.
@@ -5,6 +5,7 @@ import settle from '../helpers/settle';
5
5
  import { flattenHeaders, removeContentType, buildFetchHeaders } from '../helpers/flattenHeaders';
6
6
  import { setBasicAuth } from '../helpers/auth';
7
7
  import fetchAdapter from './fetchAdapter';
8
+ import { defaultMemoryCache } from '../helpers/memoryCache';
8
9
  import type { AccessioRequestConfig, AccessioResponse, TransformFunction } from '../types';
9
10
 
10
11
  type HeadersConfig = Record<string, Record<string, string | string[]>>;
@@ -17,6 +18,8 @@ function buildTransformArray(
17
18
  return [transform];
18
19
  }
19
20
 
21
+ const activeRequests = new Map<string, Promise<AccessioResponse>>();
22
+
20
23
  export default async function dispatchRequest(
21
24
  config: AccessioRequestConfig,
22
25
  ): Promise<AccessioResponse> {
@@ -29,61 +32,119 @@ export default async function dispatchRequest(
29
32
  config.paramsSerializer,
30
33
  );
31
34
 
32
- const flatHeaders = flattenHeaders(config.headers as HeadersConfig | undefined, config.method);
33
-
34
- const requestTransforms = buildTransformArray(config.transformRequest);
35
+ if (config.hooks?.onBeforeRequest) {
36
+ await config.hooks.onBeforeRequest(config);
37
+ }
35
38
 
36
- const requestData = await transformData(requestTransforms, config.data, flatHeaders, config);
39
+ const isGet = (config.method || 'GET').toUpperCase() === 'GET';
40
+ const cacheKey = isGet ? `GET:${fullURL}` : '';
41
+
42
+ if (isGet && config.cache) {
43
+ const cacheProvider = typeof config.cache === 'object' ? config.cache : defaultMemoryCache;
44
+ const cached = await cacheProvider.get(cacheKey);
45
+ if (cached) {
46
+ if (config.hooks?.onRequestResponse) {
47
+ await config.hooks.onRequestResponse(cached);
48
+ }
49
+ return cached;
50
+ }
51
+ }
37
52
 
38
- if (
39
- requestData === null ||
40
- requestData === undefined ||
41
- (typeof FormData !== 'undefined' && requestData instanceof FormData)
42
- ) {
43
- removeContentType(flatHeaders);
53
+ if (isGet && config.dedupe) {
54
+ if (activeRequests.has(cacheKey)) {
55
+ return activeRequests.get(cacheKey)!;
56
+ }
44
57
  }
45
58
 
46
- setBasicAuth(config, flatHeaders);
59
+ const performRequest = async () => {
60
+ const flatHeaders = flattenHeaders(config.headers as HeadersConfig | undefined, config.method);
61
+ const requestTransforms = buildTransformArray(config.transformRequest);
62
+ const requestData = await transformData(requestTransforms, config.data, flatHeaders, config);
63
+
64
+ if (
65
+ requestData === null ||
66
+ requestData === undefined ||
67
+ (typeof FormData !== 'undefined' && requestData instanceof FormData)
68
+ ) {
69
+ removeContentType(flatHeaders);
70
+ }
71
+
72
+ setBasicAuth(config, flatHeaders);
73
+
74
+ const fetchOptions: RequestInit = {
75
+ method: (config.method || 'GET').toUpperCase(),
76
+ headers: buildFetchHeaders(flatHeaders),
77
+ };
78
+
79
+ const methodsWithBody = ['POST', 'PUT', 'PATCH', 'DELETE'];
80
+ if (
81
+ methodsWithBody.includes(fetchOptions.method!) &&
82
+ requestData !== undefined &&
83
+ requestData !== null
84
+ ) {
85
+ fetchOptions.body = requestData as BodyInit;
86
+ }
87
+
88
+ if (config.withCredentials) {
89
+ fetchOptions.credentials = 'include';
90
+ }
91
+
92
+ if (config.dispatcher) {
93
+ (fetchOptions as any).dispatcher = config.dispatcher;
94
+ }
95
+ if (config.agent) {
96
+ (fetchOptions as any).agent = config.agent;
97
+ }
98
+
99
+ const requestStartTime = Date.now();
100
+
101
+ const response = await fetchAdapter(config, fullURL, fetchOptions, requestStartTime);
102
+
103
+ const responseTransforms = buildTransformArray(config.transformResponse);
104
+
105
+ response.data = await transformData(
106
+ responseTransforms,
107
+ response.data,
108
+ response.headers,
109
+ config,
110
+ );
47
111
 
48
- const fetchOptions: RequestInit = {
49
- method: (config.method || 'GET').toUpperCase(),
50
- headers: buildFetchHeaders(flatHeaders),
112
+ return new Promise<AccessioResponse>((resolve, reject) => {
113
+ settle(
114
+ resolve as (value: AccessioResponse) => void,
115
+ reject as (reason: AccessioError) => void,
116
+ response,
117
+ config,
118
+ );
119
+ });
51
120
  };
52
121
 
53
- const methodsWithBody = ['POST', 'PUT', 'PATCH', 'DELETE'];
54
- if (
55
- methodsWithBody.includes(fetchOptions.method!) &&
56
- requestData !== undefined &&
57
- requestData !== null
58
- ) {
59
- fetchOptions.body = requestData as BodyInit;
60
- }
61
-
62
- if (config.withCredentials) {
63
- fetchOptions.credentials = 'include';
64
- }
122
+ const promise = performRequest();
65
123
 
66
- if (config.dispatcher) {
67
- (fetchOptions as any).dispatcher = config.dispatcher;
124
+ if (isGet && config.dedupe) {
125
+ activeRequests.set(cacheKey, promise);
126
+ promise.finally(() => {
127
+ activeRequests.delete(cacheKey);
128
+ });
68
129
  }
69
- if (config.agent) {
70
- (fetchOptions as any).agent = config.agent;
71
- }
72
-
73
- const requestStartTime = Date.now();
74
130
 
75
- const response = await fetchAdapter(config, fullURL, fetchOptions, requestStartTime);
131
+ try {
132
+ const response = await promise;
76
133
 
77
- const responseTransforms = buildTransformArray(config.transformResponse);
134
+ if (isGet && config.cache) {
135
+ const cacheProvider = typeof config.cache === 'object' ? config.cache : defaultMemoryCache;
136
+ await cacheProvider.set(cacheKey, response, config.cacheTTL);
137
+ }
78
138
 
79
- response.data = await transformData(responseTransforms, response.data, response.headers, config);
139
+ if (config.hooks?.onRequestResponse) {
140
+ await config.hooks.onRequestResponse(response);
141
+ }
80
142
 
81
- return new Promise<AccessioResponse>((resolve, reject) => {
82
- settle(
83
- resolve as (value: AccessioResponse) => void,
84
- reject as (reason: AccessioError) => void,
85
- response,
86
- config,
87
- );
88
- });
143
+ return response;
144
+ } catch (error) {
145
+ if (config.hooks?.onRequestError && error instanceof AccessioError) {
146
+ await config.hooks.onRequestError(error);
147
+ }
148
+ throw error;
149
+ }
89
150
  }
package/src/core/retry.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { ERR_CANCELED, ERR_NETWORK, ETIMEDOUT } from '../constants/errorCodes';
1
+ import { ERR_CANCELED, ERR_NETWORK } from '../constants/errorCodes';
2
2
  import type {
3
3
  AccessioRequestConfig,
4
- AccessioResponse,
5
4
  AccessioError,
6
5
  RetryConditionFunction,
7
6
  OnRetryFunction,
@@ -20,6 +19,10 @@ function defaultRetryCondition(error: any): boolean {
20
19
  return true;
21
20
  }
22
21
 
22
+ if (error.config?.retryOn429 && error.response && error.response.status === 429) {
23
+ return true;
24
+ }
25
+
23
26
  return false;
24
27
  }
25
28
 
@@ -62,7 +65,7 @@ async function retryRequest(
62
65
  ): Promise<any> {
63
66
  const maxRetries = config.retry ?? 0;
64
67
 
65
- if (maxRetries <= 0) {
68
+ if (maxRetries <= 0 && !config.retryOn429) {
66
69
  return dispatchFn(config);
67
70
  }
68
71
 
@@ -70,22 +73,39 @@ async function retryRequest(
70
73
  const retryCondition: RetryConditionFunction = config.retryCondition ?? defaultRetryCondition;
71
74
 
72
75
  let lastError: any;
76
+ const actualMaxRetries = Math.max(maxRetries, config.retryOn429 ? 3 : 0);
73
77
 
74
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
78
+ for (let attempt = 0; attempt <= actualMaxRetries; attempt++) {
75
79
  try {
76
80
  const response = await dispatchFn(config);
77
81
  return response;
78
82
  } catch (error) {
79
83
  lastError = error;
80
84
 
81
- const isLastAttempt = attempt >= maxRetries;
85
+ const isLastAttempt = attempt >= actualMaxRetries;
82
86
  const shouldRetry = !isLastAttempt && retryCondition(error as AccessioError);
83
87
 
84
88
  if (!shouldRetry) {
85
89
  throw error;
86
90
  }
87
91
 
88
- const delay = calculateDelay(attempt, retryDelay);
92
+ let delay = calculateDelay(attempt, retryDelay);
93
+
94
+ if (config.retryOn429 && (error as any).response?.status === 429) {
95
+ const headers = (error as any).response?.headers;
96
+ const retryAfterStr = headers?.['retry-after'] || headers?.['Retry-After'];
97
+ if (retryAfterStr) {
98
+ const parsed = parseInt(retryAfterStr, 10);
99
+ if (!isNaN(parsed)) {
100
+ delay = parsed * 1000;
101
+ } else {
102
+ const date = new Date(retryAfterStr);
103
+ if (!isNaN(date.getTime())) {
104
+ delay = Math.max(0, date.getTime() - Date.now());
105
+ }
106
+ }
107
+ }
108
+ }
89
109
 
90
110
  if (typeof config.onRetry === 'function') {
91
111
  (config.onRetry as OnRetryFunction)(attempt + 1, error as AccessioError, config);
@@ -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();
@@ -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
+ }
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, AccessioResponse } from './types';
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;
@@ -69,6 +87,14 @@ export interface AccessioRequestConfig {
69
87
  maxContentLength?: number;
70
88
  dispatcher?: unknown;
71
89
  agent?: unknown;
90
+ dedupe?: boolean;
91
+ cache?: boolean | CacheProvider;
92
+ cacheTTL?: number;
93
+ onDownloadProgress?: (progressEvent: { loaded: number; total: number }) => void;
94
+ hooks?: AccessioHooks;
95
+ schema?: SchemaValidator;
96
+ fetch?: typeof fetch;
97
+ retryOn429?: boolean;
72
98
  }
73
99
 
74
100
  export interface AccessioResponse<T = unknown> {