vigor-fetch 2.1.0 → 2.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.
package/README.md CHANGED
@@ -37,7 +37,7 @@ npm install vigor-fetch
37
37
  | Auto response parsing | ✅ | ❌ | ✅ | ✅ |
38
38
  | Fluent chaining API | ✅ | ❌ | ✅ | ❌ |
39
39
  | Concurrency control | ✅ | ❌ | ❌ | ❌ |
40
- | Lifecycle interceptors | ✅ | ❌ | partial | |
40
+ | Lifecycle interceptors | ✅ | ❌ | partial | |
41
41
  | Plugin system | ✅ | ❌ | ❌ | ❌ |
42
42
 
43
43
  ## Quick Start
@@ -195,6 +195,7 @@ vigor.all(tasks: VigorAllTask<T>[])
195
195
  |------|------|-------------|
196
196
  | concurrency | number | Max parallel tasks |
197
197
  | jitter | number | Delay randomness |
198
+ | onlySuccess | boolean | Filters Success |
198
199
 
199
200
 
200
201
  ---
package/dist/index.d.ts CHANGED
@@ -1,134 +1,166 @@
1
- declare abstract class VigorError extends Error {
1
+ declare const VIGOR_ERROR_MESSAGES: {
2
+ readonly TIMEOUT: ({ limit, attempt }: {
3
+ limit: number;
4
+ attempt: number;
5
+ }) => string;
6
+ readonly EXHAUSTED: ({ maxAttempts }: {
7
+ maxAttempts: number;
8
+ }) => string;
9
+ readonly INVALID_URL: ({ received }: {
10
+ received: unknown;
11
+ }) => string;
12
+ readonly INVALID_PROTOCOL: ({ expected, received }: {
13
+ expected: Array<string>;
14
+ received: unknown;
15
+ }) => string;
16
+ readonly FETCH_ERROR: ({ status, statusText, url }: {
17
+ status: number;
18
+ statusText: string;
19
+ url: string;
20
+ }) => string;
21
+ readonly PARSE_FAILED: ({ expected }: {
22
+ expected: unknown;
23
+ }) => string;
24
+ readonly INVALID_TYPE: ({ expected, received }: {
25
+ expected: unknown;
26
+ received: unknown;
27
+ }) => string;
28
+ readonly TARGET_MISSING: () => string;
29
+ readonly REQUEST_FAILED: ({ index, error }: {
30
+ index: number;
31
+ error: Error;
32
+ }) => string;
33
+ readonly UNKNOWN: () => string;
34
+ };
35
+ type VigorErrorCode = keyof typeof VIGOR_ERROR_MESSAGES;
36
+ type ErrorData<C extends VigorErrorCode> = Parameters<typeof VIGOR_ERROR_MESSAGES[C]> extends [infer A] ? A : undefined;
37
+ type VigorErrorOptions<C extends VigorErrorCode> = {
38
+ method?: string;
39
+ cause?: unknown;
40
+ context?: unknown;
41
+ type?: string;
42
+ data: ErrorData<C>;
43
+ };
44
+ declare abstract class VigorError<C extends VigorErrorCode> extends Error {
2
45
  readonly timestamp: Date;
3
46
  readonly method?: string;
47
+ readonly code: C;
4
48
  readonly cause?: unknown;
5
49
  readonly context?: unknown;
6
50
  readonly type?: string;
7
- readonly data?: unknown;
8
- constructor(message: string, options?: {
9
- method?: string;
10
- cause?: unknown;
11
- context?: unknown;
12
- type?: string;
13
- data?: unknown;
14
- });
51
+ readonly data: ErrorData<C>;
52
+ constructor(code: C, options: VigorErrorOptions<C>);
15
53
  }
16
- declare class VigorRetryError extends VigorError {
17
- constructor(message: string, options?: any);
54
+ declare class VigorRetryError<C extends "TIMEOUT" | "EXHAUSTED"> extends VigorError<C> {
55
+ constructor(code: C, options: VigorErrorOptions<C>);
18
56
  }
19
- declare class VigorParseError extends VigorError {
20
- constructor(message: string, options?: any);
57
+ declare class VigorParseError<C extends "PARSE_FAILED" | "INVALID_TYPE" | "TARGET_MISSING"> extends VigorError<C> {
58
+ constructor(code: C, options: VigorErrorOptions<C>);
21
59
  }
22
- declare class VigorFetchError extends VigorError {
23
- constructor(message: string, options?: any);
60
+ declare class VigorFetchError<C extends "FETCH_ERROR" | "INVALID_URL" | "INVALID_PROTOCOL"> extends VigorError<C> {
61
+ constructor(code: C, options: VigorErrorOptions<C>);
24
62
  }
25
- declare class VigorAllError extends VigorError {
26
- constructor(message: string, options?: any);
63
+ declare class VigorAllError<C extends "REQUEST_FAILED" | "TARGET_MISSING"> extends VigorError<C> {
64
+ constructor(code: C, options: VigorErrorOptions<C>);
27
65
  }
28
- declare abstract class VigorStatus<T, Self> {
66
+ declare const EMPTY: unique symbol;
67
+ declare abstract class VigorStatus<T> {
68
+ protected readonly _base: T;
29
69
  protected readonly _config: T;
30
- private readonly _ctor;
31
- private readonly _errorCtor?;
32
- constructor(_config: T, _ctor: (config: T) => Self, _errorCtor?: (() => new (message: string, data: any) => Error) | undefined);
33
- protected _create(config: T): Self;
34
- protected _next(config: Partial<T>): Self;
70
+ constructor(config: Partial<T>, _base: T);
35
71
  getConfig(): T;
36
- protected _pipeSub<C, R extends VigorStatus<any, any>>(value: C, Ctor: new (c: C) => R, fn: (r: R) => R, errorKey: string): C;
72
+ getBase(): T;
73
+ protected _next(config: Partial<T>): this;
74
+ protected _pipsub<C, R extends VigorStatus<C>>(config: C, fn: (r: R) => R, ctor: new (c: C) => R): C;
37
75
  }
76
+ type VigorIncludeSpread<T> = Array<(T | Array<T>)>;
38
77
  type VigorRetrySettingsConfig<T> = {
39
78
  count: number;
40
79
  limit: number;
41
80
  maxDelay: number;
42
- default?: T;
81
+ default?: T | typeof EMPTY;
43
82
  };
44
- declare class VigorRetrySettings<T> extends VigorStatus<VigorRetrySettingsConfig<T>, VigorRetrySettings<T>> {
45
- private _base;
83
+ declare class VigorRetrySettings<T> extends VigorStatus<VigorRetrySettingsConfig<T>> {
46
84
  constructor(config?: Partial<VigorRetrySettingsConfig<T>>);
47
- getBase(): VigorRetrySettingsConfig<T>;
48
- count(num: number): VigorRetrySettings<T>;
49
- limit(num: number): VigorRetrySettings<T>;
50
- maxDelay(num: number): VigorRetrySettings<T>;
51
- default(obj: T): VigorRetrySettings<T>;
85
+ count(num: number): this;
86
+ limit(num: number): this;
87
+ maxDelay(num: number): this;
88
+ default(obj: T): this;
52
89
  }
53
- type VigorRetryBackoffConfig = {
90
+ type VigorRetryBackoffConfig<T> = {
54
91
  initialDelay: number;
55
92
  baseDelay: number;
56
93
  factor: number;
57
94
  jitter: number;
58
95
  };
59
- declare class VigorRetryBackoff extends VigorStatus<VigorRetryBackoffConfig, VigorRetryBackoff> {
60
- private readonly _base;
61
- constructor(config?: Partial<VigorRetryBackoffConfig>);
62
- getBase(): VigorRetryBackoffConfig;
63
- initialDelay(num: number): VigorRetryBackoff;
64
- baseDelay(num: number): VigorRetryBackoff;
65
- factor(num: number): VigorRetryBackoff;
66
- jitter(num: number): VigorRetryBackoff;
96
+ declare class VigorRetryBackoff<T> extends VigorStatus<VigorRetryBackoffConfig<T>> {
97
+ constructor(config?: Partial<VigorRetryBackoffConfig<T>>);
98
+ initialDelay(num: number): this;
99
+ baseDelay(num: number): this;
100
+ factor(num: number): this;
101
+ jitter(num: number): this;
102
+ static randomJitter(num: number): number;
67
103
  }
68
104
  type VigorRetryBefore<T> = {
69
- setAttempt?: (attempt: number) => number;
70
- throwError?: (error: Error) => void;
71
- abort?: (error: Error) => void;
105
+ setAttempt: (attempt: number) => number;
106
+ throwError: (error: Error) => void;
107
+ abort: (error: Error) => void;
72
108
  };
73
109
  type VigorRetryAfter<T> = {
74
- setAttempt?: (attempt: number) => number;
75
- throwError?: (error: Error) => void;
76
- setResult?: (result: T) => T;
110
+ setAttempt: (attempt: number) => number;
111
+ throwError: (error: Error) => void;
112
+ setResult: (result: T) => T;
77
113
  };
78
114
  type VigorRetryRetryIf<T> = {
79
- throwError?: (error: Error) => void;
80
- proceedRetry?: () => boolean;
81
- cancelRetry?: (error?: Error) => boolean;
115
+ throwError: (error: Error) => void;
116
+ proceedRetry: () => boolean;
117
+ cancelRetry: (error?: Error) => boolean;
82
118
  };
83
119
  type VigorRetryOnRetry<T> = {
84
- setAttempt?: (attempt: number) => number;
85
- throwError?: (error: Error) => void;
86
- setDelay?: (delay: number) => number;
120
+ setAttempt: (attempt: number) => number;
121
+ throwError: (error: Error) => void;
122
+ setDelay: (delay: number) => number;
87
123
  };
88
124
  type VigorRetryOnError<T> = {
89
- setResult?: (result: T) => T;
90
- throwError?: (error: Error) => void;
125
+ setResult: (result: T) => T;
126
+ throwError: (error: Error) => void;
91
127
  };
92
- type VigorRetryBeforeFn<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryBefore<T>) => void | Promise<void>;
93
- type VigorRetryAfterFn<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryAfter<T>) => void | Promise<void>;
94
- type VigorRetryOnErrorFn<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryOnError<T>) => void | Promise<void>;
95
- type VigorRetryOnRetryFn<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryOnRetry<T>) => void | Promise<void>;
96
- type VigorRetryRetryIfFn<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryRetryIf<T>) => void | Promise<void>;
128
+ type VigorRetryFn<T, O> = (ctx: VigorRetryContext<T>, obj: O) => void | any | Promise<void | any>;
129
+ type VigorRetryBeforeFn<T> = VigorRetryFn<T, VigorRetryBefore<T>>;
130
+ type VigorRetryAfterFn<T> = VigorRetryFn<T, VigorRetryAfter<T>>;
131
+ type VigorRetryOnErrorFn<T> = VigorRetryFn<T, VigorRetryOnError<T>>;
132
+ type VigorRetryOnRetryFn<T> = VigorRetryFn<T, VigorRetryOnRetry<T>>;
133
+ type VigorRetryRetryIfFn<T> = VigorRetryFn<T, VigorRetryRetryIf<T>>;
97
134
  type VigorRetryOptionsTask<T> = {
98
135
  abort?: (error: Error) => void;
99
136
  signal?: AbortSignal;
100
137
  };
101
- type VigorRetryTask<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryOptionsTask<T>) => T | Promise<T>;
102
138
  type VigorRetryInterceptorsConfig<T> = {
103
- before: VigorRetryBeforeFn<T>[];
104
- after: VigorRetryAfterFn<T>[];
105
- onError: VigorRetryOnErrorFn<T>[];
106
- onRetry: VigorRetryOnRetryFn<T>[];
107
- retryIf: VigorRetryRetryIfFn<T>[];
108
- };
109
- declare class VigorRetryInterceptors<T> extends VigorStatus<VigorRetryInterceptorsConfig<T>, VigorRetryInterceptors<T>> {
110
- private readonly _base;
139
+ before: Array<VigorRetryBeforeFn<T>>;
140
+ after: Array<VigorRetryAfterFn<T>>;
141
+ onError: Array<VigorRetryOnErrorFn<T>>;
142
+ onRetry: Array<VigorRetryOnRetryFn<T>>;
143
+ retryIf: Array<VigorRetryRetryIfFn<T>>;
144
+ };
145
+ declare class VigorRetryInterceptors<T> extends VigorStatus<VigorRetryInterceptorsConfig<T>> {
111
146
  constructor(config?: Partial<VigorRetryInterceptorsConfig<T>>);
112
- getBase(): VigorRetryInterceptorsConfig<T>;
113
- before(...funcs: (VigorRetryBeforeFn<T> | VigorRetryBeforeFn<T>[])[]): VigorRetryInterceptors<T>;
114
- after(...funcs: (VigorRetryAfterFn<T> | VigorRetryAfterFn<T>[])[]): VigorRetryInterceptors<T>;
115
- onError(...funcs: (VigorRetryOnErrorFn<T> | VigorRetryOnErrorFn<T>[])[]): VigorRetryInterceptors<T>;
116
- onRetry(...funcs: (VigorRetryOnRetryFn<T> | VigorRetryOnRetryFn<T>[])[]): VigorRetryInterceptors<T>;
117
- retryIf(...funcs: (VigorRetryRetryIfFn<T> | VigorRetryRetryIfFn<T>[])[]): VigorRetryInterceptors<T>;
147
+ before(...funcs: VigorIncludeSpread<VigorRetryBeforeFn<T>>): this;
148
+ after(...funcs: VigorIncludeSpread<VigorRetryAfterFn<T>>): this;
149
+ onError(...funcs: VigorIncludeSpread<VigorRetryOnErrorFn<T>>): this;
150
+ onRetry(...funcs: VigorIncludeSpread<VigorRetryOnRetryFn<T>>): this;
151
+ retryIf(...funcs: VigorIncludeSpread<VigorRetryRetryIfFn<T>>): this;
118
152
  }
153
+ type VigorRetryTask<T> = (ctx: VigorRetryContext<T>, obj: VigorRetryOptionsTask<T>) => T | Promise<T>;
119
154
  type VigorRetryConfig<T> = {
120
155
  target: VigorRetryTask<T>;
121
156
  setting: VigorRetrySettingsConfig<T>;
122
- backoff: VigorRetryBackoffConfig;
157
+ backoff: VigorRetryBackoffConfig<T>;
123
158
  interceptors: VigorRetryInterceptorsConfig<T>;
159
+ controller: AbortController;
124
160
  };
125
- type VigorRetryContext<T> = {
126
- target: VigorRetryTask<T>;
127
- setting: VigorRetrySettingsConfig<T>;
128
- backoff: VigorRetryBackoffConfig;
129
- interceptors: VigorRetryInterceptorsConfig<T>;
161
+ type VigorRetryContext<T> = VigorRetryConfig<T> & {
130
162
  runtime: {
131
- result?: T;
163
+ result?: T | typeof EMPTY;
132
164
  attempt: number;
133
165
  controller: AbortController;
134
166
  abortPromise?: Promise<never>;
@@ -139,32 +171,38 @@ type VigorRetryContext<T> = {
139
171
  error?: unknown;
140
172
  };
141
173
  };
142
- declare class VigorRetry<T> extends VigorStatus<VigorRetryConfig<T>, VigorRetry<T>> {
143
- private readonly _base;
144
- private _controller;
145
- constructor(config?: VigorRetryConfig<T>);
146
- getBase(): VigorRetryConfig<T>;
147
- target<U>(func: VigorRetryTask<U>): VigorRetry<U>;
148
- createController(): (error: Error) => void;
149
- setting(func: (r: VigorRetrySettings<T>) => VigorRetrySettings<T>): VigorRetry<T>;
150
- backoff(func: (r: VigorRetryBackoff) => VigorRetryBackoff): VigorRetry<T>;
151
- interceptors(func: (r: VigorRetryInterceptors<T>) => VigorRetryInterceptors<T>): VigorRetry<T>;
174
+ declare class VigorRetry<T> extends VigorStatus<VigorRetryConfig<T>> {
175
+ constructor(config?: Partial<VigorRetryConfig<T>>);
176
+ private _transfer;
177
+ private _calculateDelay;
178
+ createController(): VigorRetryConfig<T>["controller"];
179
+ target<U>(func: VigorRetryConfig<U>["target"]): VigorRetry<U>;
180
+ setting(func: (r: VigorRetrySettings<T>) => VigorRetrySettings<T>): this;
181
+ backoff(func: (r: VigorRetryBackoff<T>) => VigorRetryBackoff<T>): this;
182
+ interceptors(func: (r: VigorRetryInterceptors<T>) => VigorRetryInterceptors<T>): this;
152
183
  request(): Promise<T>;
153
184
  }
154
- type VigorParseConfig<T> = {
185
+ type VigorParseConfig<T, O = false> = {
155
186
  target?: Response;
156
- original: boolean;
157
- type?: keyof Response;
187
+ original: O;
188
+ type?: (keyof Response) | undefined;
158
189
  result?: T;
159
190
  };
160
- declare class VigorParse<T extends any> extends VigorStatus<VigorParseConfig<T>, VigorParse<T>> {
161
- private _base;
162
- constructor(config?: Partial<VigorParseConfig<T>>);
163
- getBase(): VigorParseConfig<T>;
164
- target(response: Response): VigorParse<T>;
165
- original(bool: boolean): VigorParse<T>;
166
- type(str: keyof Response): VigorParse<T>;
167
- request(): Promise<T>;
191
+ declare class VigorParse<T, O extends boolean = false> extends VigorStatus<VigorParseConfig<T, O>> {
192
+ constructor(config?: Partial<VigorParseConfig<T, O> & {
193
+ original: O;
194
+ }>);
195
+ static stategy: {
196
+ key: RegExp;
197
+ parse: (res: Response) => Promise<any>;
198
+ type: string;
199
+ }[];
200
+ static supported: string[];
201
+ private _transfer;
202
+ target(res: Response): this;
203
+ original<B extends boolean>(bool: B): VigorParse<T, B>;
204
+ type<K extends keyof Response>(type: K): VigorParse<Response[K] extends (...args: any) => Promise<infer R> ? R : never, O>;
205
+ request<U = T>(): Promise<O extends true ? Response : U>;
168
206
  }
169
207
  type VigorFetchMethods = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE";
170
208
  type VigorFetchSettingsConfig<T> = {
@@ -177,12 +215,10 @@ type VigorFetchSettingsConfig<T> = {
177
215
  headers?: HeadersInit | Record<string, any>;
178
216
  body?: XMLHttpRequestBodyInit | object | null;
179
217
  options?: object;
180
- default?: T;
218
+ default?: T | typeof EMPTY;
181
219
  };
182
- declare class VigorFetchSettings<T> extends VigorStatus<VigorFetchSettingsConfig<T>, VigorFetchSettings<T>> {
183
- private _base;
220
+ declare class VigorFetchSettings<T> extends VigorStatus<VigorFetchSettingsConfig<T>> {
184
221
  constructor(config?: Partial<VigorFetchSettingsConfig<T>>);
185
- getBase(): VigorFetchSettingsConfig<T>;
186
222
  origin(str: string): VigorFetchSettings<T>;
187
223
  path(...strs: (string | string[])[]): VigorFetchSettings<T>;
188
224
  query(obj: object): VigorFetchSettings<T>;
@@ -209,24 +245,23 @@ type VigorFetchResult<T> = {
209
245
  setResult?: (result: T) => T;
210
246
  throwError?: (error: Error) => void;
211
247
  };
212
- type VigorFetchBeforeFn<T> = (ctx: VigorFetchContext<T>, obj: VigorFetchBefore<T>) => void | Promise<void>;
213
- type VigorFetchAfterFn<T> = (ctx: VigorFetchContext<T>, obj: VigorFetchAfter<T>) => void | Promise<void>;
214
- type VigorFetchOnErrorFn<T> = (ctx: VigorFetchContext<T>, obj: VigorFetchOnError<T>) => void | Promise<void>;
215
- type VigorFetchResultFn<T> = (ctx: VigorFetchContext<T>, obj: VigorFetchResult<T>) => void | Promise<void>;
248
+ type VigorFetchFn<T, O> = (ctx: VigorFetchContext<T>, obj: O) => void | any | Promise<void | any>;
249
+ type VigorFetchBeforeFn<T> = VigorFetchFn<T, VigorFetchBefore<T>>;
250
+ type VigorFetchAfterFn<T> = VigorFetchFn<T, VigorFetchAfter<T>>;
251
+ type VigorFetchOnErrorFn<T> = VigorFetchFn<T, VigorFetchOnError<T>>;
252
+ type VigorFetchResultFn<T> = VigorFetchFn<T, VigorFetchResult<T>>;
216
253
  type VigorFetchInterceptorsConfig<T> = {
217
254
  before: VigorFetchBeforeFn<T>[];
218
255
  after: VigorFetchAfterFn<T>[];
219
256
  onError: VigorFetchOnErrorFn<T>[];
220
257
  result: VigorFetchResultFn<T>[];
221
258
  };
222
- declare class VigorFetchInterceptors<T> extends VigorStatus<VigorFetchInterceptorsConfig<T>, VigorFetchInterceptors<T>> {
223
- private readonly _base;
259
+ declare class VigorFetchInterceptors<T> extends VigorStatus<VigorFetchInterceptorsConfig<T>> {
224
260
  constructor(config?: Partial<VigorFetchInterceptorsConfig<T>>);
225
- getBase(): VigorFetchInterceptorsConfig<T>;
226
- before(...funcs: (VigorFetchBeforeFn<T> | VigorFetchBeforeFn<T>[])[]): VigorFetchInterceptors<T>;
227
- after(...funcs: (VigorFetchAfterFn<T> | VigorFetchAfterFn<T>[])[]): VigorFetchInterceptors<T>;
228
- onError(...funcs: (VigorFetchOnErrorFn<T> | VigorFetchOnErrorFn<T>[])[]): VigorFetchInterceptors<T>;
229
- result(...funcs: (VigorFetchResultFn<T> | VigorFetchResultFn<T>[])[]): VigorFetchInterceptors<T>;
261
+ before(...funcs: VigorIncludeSpread<VigorFetchBeforeFn<T>>): VigorFetchInterceptors<T>;
262
+ after(...funcs: VigorIncludeSpread<VigorFetchAfterFn<T>>): VigorFetchInterceptors<T>;
263
+ onError(...funcs: VigorIncludeSpread<VigorFetchOnErrorFn<T>>): VigorFetchInterceptors<T>;
264
+ result(...funcs: VigorIncludeSpread<VigorFetchResultFn<T>>): VigorFetchInterceptors<T>;
230
265
  }
231
266
  type VigorFetchConfig<T> = {
232
267
  setting: VigorFetchSettingsConfig<T>;
@@ -234,11 +269,7 @@ type VigorFetchConfig<T> = {
234
269
  parseConfig: VigorParseConfig<T>;
235
270
  interceptors: VigorFetchInterceptorsConfig<T>;
236
271
  };
237
- type VigorFetchContext<T> = {
238
- setting: VigorFetchSettingsConfig<T>;
239
- retryConfig: VigorRetryConfig<T>;
240
- parseConfig: VigorParseConfig<T>;
241
- interceptors: VigorFetchInterceptorsConfig<T>;
272
+ type VigorFetchContext<T> = VigorFetchConfig<T> & {
242
273
  runtime: {
243
274
  retryEngine?: VigorRetry<T>;
244
275
  parseEngine?: VigorParse<T>;
@@ -247,100 +278,110 @@ type VigorFetchContext<T> = {
247
278
  baseOptions?: object;
248
279
  options?: object;
249
280
  response?: Response;
250
- result?: T;
281
+ result?: T | typeof EMPTY;
251
282
  error?: unknown;
252
283
  };
253
284
  };
254
- declare class VigorFetch<T extends any> extends VigorStatus<VigorFetchConfig<T>, VigorFetch<T>> {
255
- private readonly _base;
256
- constructor(config?: VigorFetchConfig<T>);
257
- getBase(): VigorFetchConfig<T>;
258
- origin(str: string): VigorFetch<T>;
259
- path(...strs: (string | string[])[]): VigorFetch<T>;
260
- query(obj: object): VigorFetch<T>;
261
- method(str: VigorFetchMethods): VigorFetch<T>;
262
- headers(obj: HeadersInit | Record<string, any>): VigorFetch<T>;
263
- body(obj: XMLHttpRequestBodyInit | object | null): VigorFetch<T>;
264
- options(obj: object): VigorFetch<T>;
265
- setting(func: (r: VigorFetchSettings<T>) => VigorFetchSettings<T>): VigorFetch<T>;
266
- retryConfig(func: (r: VigorRetry<T>) => VigorRetry<T>): VigorFetch<T>;
267
- parseConfig(func: (r: VigorParse<T>) => VigorParse<T>): VigorFetch<T>;
268
- buildUrl(origin: string, path: Array<string>, query: object): string;
269
- interceptors(func: (r: VigorFetchInterceptors<T>) => VigorFetchInterceptors<T>): VigorFetch<T>;
270
- request(): Promise<T>;
285
+ declare class VigorFetch<T> extends VigorStatus<VigorFetchConfig<T>> {
286
+ constructor(config?: Partial<VigorFetchConfig<T>>);
287
+ origin(str: string): this;
288
+ path(...strs: (string | string[])[]): this;
289
+ query(obj: object): this;
290
+ method(str: VigorFetchMethods): this;
291
+ headers(obj: HeadersInit | Record<string, any>): this;
292
+ body(obj: XMLHttpRequestBodyInit | object | null): this;
293
+ options(obj: object): this;
294
+ setting(func: (r: VigorFetchSettings<T>) => VigorFetchSettings<T>): this;
295
+ interceptors(func: (r: VigorFetchInterceptors<T>) => VigorFetchInterceptors<T>): this;
296
+ retryConfig(func: (r: VigorRetry<T>) => VigorRetry<T>): this;
297
+ parseConfig(func: (r: VigorParse<T>) => VigorParse<T>): this;
298
+ buildUrl(origin: string, path: string[], query: object): string;
299
+ request<U = T>(): Promise<U>;
271
300
  }
272
- type VigorAllSettingsConfig<T> = {
301
+ type VigorAllSettingsConfig = {
273
302
  concurrency: number;
274
303
  jitter: number;
304
+ onlySuccess: boolean;
275
305
  };
276
- declare class VigorAllSettings<T> extends VigorStatus<VigorAllSettingsConfig<T>, VigorAllSettings<T>> {
277
- private _base;
278
- constructor(config?: Partial<VigorAllSettingsConfig<T>>);
279
- getBase(): VigorAllSettingsConfig<T>;
280
- concurrency(num: number): VigorAllSettings<T>;
281
- jitter(num: number): VigorAllSettings<T>;
306
+ declare class VigorAllSettings extends VigorStatus<VigorAllSettingsConfig> {
307
+ constructor(config?: Partial<VigorAllSettingsConfig>);
308
+ concurrency(num: number): VigorAllSettings;
309
+ jitter(num: number): VigorAllSettings;
310
+ onlySuccess(bool: boolean): VigorAllSettings;
282
311
  }
283
- type VigorAllBefore<T> = {
312
+ type VigorAllBefore = {
284
313
  throwError?: (error: Error) => void;
285
314
  };
286
- type VigorAllAfter<T> = {
287
- setResult?: (result: T) => T;
315
+ type VigorAllAfter = {
316
+ setResult?: (result: any) => any;
288
317
  throwError?: (error: Error) => void;
289
318
  };
290
- type VigorAllOnError<T> = {
291
- setResult?: (result: T) => T;
319
+ type VigorAllOnError = {
320
+ setResult?: (result: any) => any;
292
321
  throwError?: (error: Error) => void;
293
322
  };
294
- type VigorAllResult<T> = {
295
- setResult?: (result: Array<T>) => Array<T>;
323
+ type VigorAllResult = {
324
+ setResult?: (result: Array<any>) => Array<any>;
296
325
  throwError?: (error: Error) => void;
297
326
  };
298
- type VigorAllBeforeFn<T> = (ctx: VigorAllContext<T>, obj: VigorAllBefore<T>) => void | Promise<void>;
299
- type VigorAllAfterFn<T> = (ctx: VigorAllContext<T>, obj: VigorAllAfter<T>) => void | Promise<void>;
300
- type VigorAllOnErrorFn<T> = (ctx: VigorAllContext<T>, obj: VigorAllOnError<T>) => void | Promise<void>;
301
- type VigorAllResultFn<T> = (ctx: VigorAllContext<T>, obj: VigorAllResult<T>) => void | Promise<void>;
302
- type VigorAllInterceptorsConfig<T> = {
303
- before: VigorAllBeforeFn<T>[];
304
- after: VigorAllAfterFn<T>[];
305
- onError: VigorAllOnErrorFn<T>[];
306
- result: VigorAllResultFn<T>[];
307
- };
308
- declare class VigorAllInterceptors<T> extends VigorStatus<VigorAllInterceptorsConfig<T>, VigorAllInterceptors<T>> {
309
- private readonly _base;
310
- constructor(config?: Partial<VigorAllInterceptorsConfig<T>>);
311
- getBase(): VigorAllInterceptorsConfig<T>;
312
- before(...funcs: (VigorAllBeforeFn<T> | VigorAllBeforeFn<T>[])[]): VigorAllInterceptors<T>;
313
- after(...funcs: (VigorAllAfterFn<T> | VigorAllAfterFn<T>[])[]): VigorAllInterceptors<T>;
314
- onError(...funcs: (VigorAllOnErrorFn<T> | VigorAllOnErrorFn<T>[])[]): VigorAllInterceptors<T>;
315
- result(...funcs: (VigorAllResultFn<T> | VigorAllResultFn<T>[])[]): VigorAllInterceptors<T>;
327
+ type VigorAllFn<O> = (ctx: VigorAllContext<any>, obj: O) => void | any | Promise<void | any>;
328
+ type VigorAllTaskFn<O> = (ctx: VigorAllTaskContext<any>, obj: O) => void | any | Promise<void | any>;
329
+ type VigorAllBeforeFn = VigorAllTaskFn<VigorAllBefore>;
330
+ type VigorAllAfterFn = VigorAllTaskFn<VigorAllAfter>;
331
+ type VigorAllOnErrorFn = VigorAllTaskFn<VigorAllOnError>;
332
+ type VigorAllResultFn = VigorAllFn<VigorAllResult>;
333
+ type VigorAllInterceptorsConfig = {
334
+ before: VigorAllBeforeFn[];
335
+ after: VigorAllAfterFn[];
336
+ onError: VigorAllOnErrorFn[];
337
+ result: VigorAllResultFn[];
338
+ };
339
+ declare class VigorAllInterceptors extends VigorStatus<VigorAllInterceptorsConfig> {
340
+ constructor(config?: Partial<VigorAllInterceptorsConfig>);
341
+ before(...funcs: (VigorAllBeforeFn | VigorAllBeforeFn[])[]): VigorAllInterceptors;
342
+ after(...funcs: (VigorAllAfterFn | VigorAllAfterFn[])[]): VigorAllInterceptors;
343
+ onError(...funcs: (VigorAllOnErrorFn | VigorAllOnErrorFn[])[]): VigorAllInterceptors;
344
+ result(...funcs: (VigorAllResultFn | VigorAllResultFn[])[]): VigorAllInterceptors;
316
345
  }
317
- type VigorAllOptionsTask<T> = {
346
+ type VigorAllOptionsTask = {
318
347
  abort?: (error: Error) => void;
319
348
  signal?: AbortSignal;
320
349
  };
321
- type VigorAllTask<T> = (ctx: VigorAllContext<T>, obj: VigorAllOptionsTask<T>) => T | Promise<T>;
322
- type VigorAllConfig<T> = {
323
- target: Array<VigorAllTask<T>>;
324
- setting: VigorAllSettingsConfig<T>;
325
- interceptors: VigorAllInterceptorsConfig<T>;
350
+ type VigorAllTask<R = any> = (ctx: VigorAllTaskContext<any>, obj: VigorAllOptionsTask) => R | Promise<R>;
351
+ type TaskReturn<T> = T extends VigorAllTask<infer R> ? R : never;
352
+ type MapTasks<T extends readonly VigorAllTask<any>[]> = {
353
+ [K in keyof T]: T[K] extends VigorAllTask<infer R> ? R : never;
354
+ };
355
+ type VigorAllConfig<Tasks extends readonly VigorAllTask<any>[]> = {
356
+ target: Tasks;
357
+ setting: VigorAllSettingsConfig;
358
+ interceptors: VigorAllInterceptorsConfig;
359
+ };
360
+ type VigorAllContext<Tasks extends readonly VigorAllTask<any>[]> = VigorAllConfig<Tasks> & {
361
+ runtime: {
362
+ tasks: {
363
+ [K in keyof Tasks]: Promise<TaskReturn<Tasks[K]>>;
364
+ };
365
+ result: {
366
+ [K in keyof Tasks]: TaskReturn<Tasks[K]> | Error;
367
+ };
368
+ };
326
369
  };
327
- type VigorAllContext<T> = {
328
- target?: Array<VigorAllTask<T>>;
329
- setting: VigorAllSettingsConfig<T>;
330
- interceptors: VigorAllInterceptorsConfig<T>;
370
+ type VigorAllTaskContext<Task extends VigorAllTask<any>> = {
371
+ target: Task;
331
372
  runtime: {
332
- tasks: Array<Promise<T>>;
333
- result: Array<T | Error>;
373
+ result: TaskReturn<Task> | typeof EMPTY;
374
+ error: unknown;
375
+ jitter: number;
334
376
  };
335
377
  };
336
- declare class VigorAll<T extends any> extends VigorStatus<VigorAllConfig<T>, VigorAll<T>> {
337
- private readonly _base;
338
- constructor(config?: VigorAllConfig<T>);
339
- getBase(): VigorAllConfig<T>;
340
- target(...funcs: (VigorAllTask<T> | VigorAllTask<T>[])[]): VigorAll<T>;
341
- setting(func: (r: VigorAllSettings<T>) => VigorAllSettings<T>): VigorAll<T>;
342
- interceptors(func: (r: VigorAllInterceptors<T>) => VigorAllInterceptors<T>): VigorAll<T>;
343
- request(): Promise<(T | Error)[]>;
378
+ declare class VigorAll<Tasks extends readonly VigorAllTask<any>[]> extends VigorStatus<VigorAllConfig<Tasks>> {
379
+ constructor(config?: Partial<VigorAllConfig<Tasks>>);
380
+ private _transfer;
381
+ target<T extends readonly VigorAllTask<any>[]>(...funcs: T): VigorAll<T>;
382
+ setting(func: (r: VigorAllSettings) => VigorAllSettings): this;
383
+ interceptors(func: (r: VigorAllInterceptors) => VigorAllInterceptors): this;
384
+ request(): Promise<MapTasks<Tasks>>;
344
385
  }
345
386
  type VigorRegistry = {
346
387
  VigorRetry: {
@@ -357,7 +398,7 @@ type VigorRegistry = {
357
398
  interceptors: typeof VigorFetchInterceptors;
358
399
  };
359
400
  VigorAll: {
360
- main: <T>() => VigorAll<T>;
401
+ main: () => VigorAll<any>;
361
402
  error: typeof VigorAllError;
362
403
  setting: typeof VigorAllSettings;
363
404
  interceptors: typeof VigorAllInterceptors;
@@ -374,8 +415,8 @@ declare class Vigor {
374
415
  private readonly registry;
375
416
  constructor(config?: Partial<VigorConfig>);
376
417
  fetch(origin: string): VigorFetch<unknown>;
377
- all<T>(tasks: (VigorAllTask<T> | VigorAllTask<T>[])[]): VigorAll<T>;
378
- parse(response: Response): VigorParse<unknown>;
418
+ all<T extends VigorAllTask<any>[] | readonly VigorAllTask<any>[]>(...args: T extends any ? (T[number] | T)[] : never): VigorAll<VigorAllTask<any>[]>;
419
+ parse(response: Response): VigorParse<unknown, false>;
379
420
  retry<T>(fn: VigorRetryTask<T>): VigorRetry<T>;
380
421
  use(plugin: (ctx: VigorRegistry, options?: object) => void, options?: object): Vigor;
381
422
  }