@naturalcycles/js-lib 14.148.0 → 14.149.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.
@@ -14,36 +14,36 @@ export declare class Fetcher {
14
14
  onBeforeRetry(hook: FetcherBeforeRetryHook): this;
15
15
  cfg: FetcherNormalizedCfg;
16
16
  static create(cfg?: FetcherCfg & FetcherOptions): Fetcher;
17
- get: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>;
18
- post: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>;
19
- put: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>;
20
- patch: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>;
21
- delete: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>;
22
- getText: (url: string, opt?: FetcherOptions<string>) => Promise<string>;
23
- postText: (url: string, opt?: FetcherOptions<string>) => Promise<string>;
24
- putText: (url: string, opt?: FetcherOptions<string>) => Promise<string>;
25
- patchText: (url: string, opt?: FetcherOptions<string>) => Promise<string>;
26
- deleteText: (url: string, opt?: FetcherOptions<string>) => Promise<string>;
27
- getVoid: (url: string, opt?: FetcherOptions<void>) => Promise<void>;
28
- postVoid: (url: string, opt?: FetcherOptions<void>) => Promise<void>;
29
- putVoid: (url: string, opt?: FetcherOptions<void>) => Promise<void>;
30
- patchVoid: (url: string, opt?: FetcherOptions<void>) => Promise<void>;
31
- deleteVoid: (url: string, opt?: FetcherOptions<void>) => Promise<void>;
32
- headVoid: (url: string, opt?: FetcherOptions<void>) => Promise<void>;
17
+ get: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>;
18
+ post: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>;
19
+ put: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>;
20
+ patch: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>;
21
+ delete: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>;
22
+ getText: (url: string, opt?: FetcherOptions) => Promise<string>;
23
+ postText: (url: string, opt?: FetcherOptions) => Promise<string>;
24
+ putText: (url: string, opt?: FetcherOptions) => Promise<string>;
25
+ patchText: (url: string, opt?: FetcherOptions) => Promise<string>;
26
+ deleteText: (url: string, opt?: FetcherOptions) => Promise<string>;
27
+ getVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
28
+ postVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
29
+ putVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
30
+ patchVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
31
+ deleteVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
32
+ headVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
33
33
  /**
34
34
  * Returns raw fetchResponse.body, which is a ReadableStream<Uint8Array>
35
35
  *
36
36
  * More on streams and Node interop:
37
37
  * https://css-tricks.com/web-streams-everywhere-and-fetch-for-node-js/
38
38
  */
39
- getReadableStream(url: string, opt?: FetcherOptions<ReadableStream<Uint8Array>>): Promise<ReadableStream<Uint8Array>>;
40
- fetch<T = unknown>(opt: FetcherOptions<T>): Promise<T>;
39
+ getReadableStream(url: string, opt?: FetcherOptions): Promise<ReadableStream<Uint8Array>>;
40
+ fetch<T = unknown>(opt: FetcherOptions): Promise<T>;
41
41
  /**
42
42
  * Returns FetcherResponse.
43
43
  * Never throws, returns `err` property in the response instead.
44
44
  * Use this method instead of `throwHttpErrors: false` or try-catching.
45
45
  */
46
- doFetch<T = unknown>(opt: FetcherOptions<T>): Promise<FetcherResponse<T>>;
46
+ doFetch<T = unknown>(opt: FetcherOptions): Promise<FetcherResponse<T>>;
47
47
  private onOkResponse;
48
48
  /**
49
49
  * This method exists to be able to easily mock it.
@@ -173,12 +173,6 @@ class Fetcher {
173
173
  for (const hook of this.cfg.hooks.afterResponse || []) {
174
174
  await hook(res);
175
175
  }
176
- if (req.paginate && res.ok) {
177
- const proceeed = await req.paginate(res, opt);
178
- if (proceeed) {
179
- return await this.doFetch(opt);
180
- }
181
- }
182
176
  return res;
183
177
  }
184
178
  async onOkResponse(res, timeout) {
@@ -6,7 +6,7 @@ export interface FetcherNormalizedCfg extends Required<FetcherCfg>, Omit<Fetcher
6
6
  logger: CommonLogger;
7
7
  searchParams: Record<string, any>;
8
8
  }
9
- export type FetcherBeforeRequestHook = <BODY = unknown>(req: FetcherRequest<BODY>) => Promisable<void>;
9
+ export type FetcherBeforeRequestHook = (req: FetcherRequest) => Promisable<void>;
10
10
  export type FetcherAfterResponseHook = <BODY = unknown>(res: FetcherResponse<BODY>) => Promisable<void>;
11
11
  export type FetcherBeforeRetryHook = <BODY = unknown>(res: FetcherResponse<BODY>) => Promisable<void>;
12
12
  export interface FetcherCfg {
@@ -77,7 +77,7 @@ export interface FetcherRetryOptions {
77
77
  timeoutMax: number;
78
78
  timeoutMultiplier: number;
79
79
  }
80
- export interface FetcherRequest<BODY = unknown> extends Omit<FetcherOptions<BODY>, 'method' | 'headers' | 'baseUrl' | 'url'> {
80
+ export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers' | 'baseUrl' | 'url'> {
81
81
  /**
82
82
  * inputUrl is only the part that was passed in the request,
83
83
  * without baseUrl or searchParams.
@@ -97,7 +97,7 @@ export interface FetcherRequest<BODY = unknown> extends Omit<FetcherOptions<BODY
97
97
  retry5xx: boolean;
98
98
  started: UnixTimestampMillisNumber;
99
99
  }
100
- export interface FetcherOptions<BODY = unknown> {
100
+ export interface FetcherOptions {
101
101
  method?: HttpMethod;
102
102
  /**
103
103
  * If defined - this `url` will override the original given `url`.
@@ -148,21 +148,6 @@ export interface FetcherOptions<BODY = unknown> {
148
148
  */
149
149
  retry5xx?: boolean;
150
150
  jsonReviver?: Reviver;
151
- /**
152
- * Allows to walk over multiple pages of results.
153
- * Paginate take a function.
154
- * Function has access to FetcherResponse and FetcherOptions
155
- * and has to make a decision to continue pagination or not.
156
- *
157
- * Return false to stop pagination.
158
- * Return true to continue pagination.
159
- * Feel free to mutate/modify opt (FetcherOptions), for example:
160
- *
161
- * opt.searchParams!['page']++
162
- *
163
- * @experimental
164
- */
165
- paginate?: (res: FetcherSuccessResponse<BODY>, opt: FetcherOptions<BODY>) => Promisable<boolean>;
166
151
  }
167
152
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
168
153
  method: HttpMethod;
@@ -158,12 +158,6 @@ export class Fetcher {
158
158
  for (const hook of this.cfg.hooks.afterResponse || []) {
159
159
  await hook(res);
160
160
  }
161
- if (req.paginate && res.ok) {
162
- const proceeed = await req.paginate(res, opt);
163
- if (proceeed) {
164
- return await this.doFetch(opt);
165
- }
166
- }
167
161
  return res;
168
162
  }
169
163
  async onOkResponse(res, timeout) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.148.0",
3
+ "version": "14.149.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -10,9 +10,7 @@ export interface FetcherNormalizedCfg
10
10
  searchParams: Record<string, any>
11
11
  }
12
12
 
13
- export type FetcherBeforeRequestHook = <BODY = unknown>(
14
- req: FetcherRequest<BODY>,
15
- ) => Promisable<void>
13
+ export type FetcherBeforeRequestHook = (req: FetcherRequest) => Promisable<void>
16
14
  export type FetcherAfterResponseHook = <BODY = unknown>(
17
15
  res: FetcherResponse<BODY>,
18
16
  ) => Promisable<void>
@@ -96,8 +94,8 @@ export interface FetcherRetryOptions {
96
94
  timeoutMultiplier: number
97
95
  }
98
96
 
99
- export interface FetcherRequest<BODY = unknown>
100
- extends Omit<FetcherOptions<BODY>, 'method' | 'headers' | 'baseUrl' | 'url'> {
97
+ export interface FetcherRequest
98
+ extends Omit<FetcherOptions, 'method' | 'headers' | 'baseUrl' | 'url'> {
101
99
  /**
102
100
  * inputUrl is only the part that was passed in the request,
103
101
  * without baseUrl or searchParams.
@@ -118,7 +116,7 @@ export interface FetcherRequest<BODY = unknown>
118
116
  started: UnixTimestampMillisNumber
119
117
  }
120
118
 
121
- export interface FetcherOptions<BODY = unknown> {
119
+ export interface FetcherOptions {
122
120
  method?: HttpMethod
123
121
 
124
122
  /**
@@ -183,22 +181,6 @@ export interface FetcherOptions<BODY = unknown> {
183
181
  retry5xx?: boolean
184
182
 
185
183
  jsonReviver?: Reviver
186
-
187
- /**
188
- * Allows to walk over multiple pages of results.
189
- * Paginate take a function.
190
- * Function has access to FetcherResponse and FetcherOptions
191
- * and has to make a decision to continue pagination or not.
192
- *
193
- * Return false to stop pagination.
194
- * Return true to continue pagination.
195
- * Feel free to mutate/modify opt (FetcherOptions), for example:
196
- *
197
- * opt.searchParams!['page']++
198
- *
199
- * @experimental
200
- */
201
- paginate?: (res: FetcherSuccessResponse<BODY>, opt: FetcherOptions<BODY>) => Promisable<boolean>
202
184
  }
203
185
 
204
186
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
@@ -54,10 +54,7 @@ export class Fetcher {
54
54
  const m = method.toLowerCase()
55
55
 
56
56
  // mode=void
57
- ;(this as any)[`${m}Void`] = async (
58
- url: string,
59
- opt?: FetcherOptions<void>,
60
- ): Promise<void> => {
57
+ ;(this as any)[`${m}Void`] = async (url: string, opt?: FetcherOptions): Promise<void> => {
61
58
  return await this.fetch<void>({
62
59
  url,
63
60
  method,
@@ -67,10 +64,7 @@ export class Fetcher {
67
64
  }
68
65
 
69
66
  if (method === 'HEAD') return // mode=text
70
- ;(this as any)[`${m}Text`] = async (
71
- url: string,
72
- opt?: FetcherOptions<string>,
73
- ): Promise<string> => {
67
+ ;(this as any)[`${m}Text`] = async (url: string, opt?: FetcherOptions): Promise<string> => {
74
68
  return await this.fetch<string>({
75
69
  url,
76
70
  method,
@@ -80,7 +74,7 @@ export class Fetcher {
80
74
  }
81
75
 
82
76
  // Default mode=json, but overridable
83
- ;(this as any)[m] = async <T = unknown>(url: string, opt?: FetcherOptions<T>): Promise<T> => {
77
+ ;(this as any)[m] = async <T = unknown>(url: string, opt?: FetcherOptions): Promise<T> => {
84
78
  return await this.fetch<T>({
85
79
  url,
86
80
  method,
@@ -117,26 +111,26 @@ export class Fetcher {
117
111
 
118
112
  // These methods are generated dynamically in the constructor
119
113
  // These default methods use mode=json
120
- get!: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>
121
- post!: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>
122
- put!: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>
123
- patch!: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>
124
- delete!: <T = unknown>(url: string, opt?: FetcherOptions<T>) => Promise<T>
114
+ get!: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>
115
+ post!: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>
116
+ put!: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>
117
+ patch!: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>
118
+ delete!: <T = unknown>(url: string, opt?: FetcherOptions) => Promise<T>
125
119
 
126
120
  // mode=text
127
- getText!: (url: string, opt?: FetcherOptions<string>) => Promise<string>
128
- postText!: (url: string, opt?: FetcherOptions<string>) => Promise<string>
129
- putText!: (url: string, opt?: FetcherOptions<string>) => Promise<string>
130
- patchText!: (url: string, opt?: FetcherOptions<string>) => Promise<string>
131
- deleteText!: (url: string, opt?: FetcherOptions<string>) => Promise<string>
121
+ getText!: (url: string, opt?: FetcherOptions) => Promise<string>
122
+ postText!: (url: string, opt?: FetcherOptions) => Promise<string>
123
+ putText!: (url: string, opt?: FetcherOptions) => Promise<string>
124
+ patchText!: (url: string, opt?: FetcherOptions) => Promise<string>
125
+ deleteText!: (url: string, opt?: FetcherOptions) => Promise<string>
132
126
 
133
127
  // mode=void (no body fetching/parsing)
134
- getVoid!: (url: string, opt?: FetcherOptions<void>) => Promise<void>
135
- postVoid!: (url: string, opt?: FetcherOptions<void>) => Promise<void>
136
- putVoid!: (url: string, opt?: FetcherOptions<void>) => Promise<void>
137
- patchVoid!: (url: string, opt?: FetcherOptions<void>) => Promise<void>
138
- deleteVoid!: (url: string, opt?: FetcherOptions<void>) => Promise<void>
139
- headVoid!: (url: string, opt?: FetcherOptions<void>) => Promise<void>
128
+ getVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
129
+ postVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
130
+ putVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
131
+ patchVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
132
+ deleteVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
133
+ headVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
140
134
 
141
135
  // mode=readableStream
142
136
  /**
@@ -145,10 +139,7 @@ export class Fetcher {
145
139
  * More on streams and Node interop:
146
140
  * https://css-tricks.com/web-streams-everywhere-and-fetch-for-node-js/
147
141
  */
148
- async getReadableStream(
149
- url: string,
150
- opt?: FetcherOptions<ReadableStream<Uint8Array>>,
151
- ): Promise<ReadableStream<Uint8Array>> {
142
+ async getReadableStream(url: string, opt?: FetcherOptions): Promise<ReadableStream<Uint8Array>> {
152
143
  return await this.fetch({
153
144
  url,
154
145
  mode: 'readableStream',
@@ -156,7 +147,7 @@ export class Fetcher {
156
147
  })
157
148
  }
158
149
 
159
- async fetch<T = unknown>(opt: FetcherOptions<T>): Promise<T> {
150
+ async fetch<T = unknown>(opt: FetcherOptions): Promise<T> {
160
151
  const res = await this.doFetch<T>(opt)
161
152
  if (res.err) {
162
153
  if (res.req.throwHttpErrors) throw res.err
@@ -170,7 +161,7 @@ export class Fetcher {
170
161
  * Never throws, returns `err` property in the response instead.
171
162
  * Use this method instead of `throwHttpErrors: false` or try-catching.
172
163
  */
173
- async doFetch<T = unknown>(opt: FetcherOptions<T>): Promise<FetcherResponse<T>> {
164
+ async doFetch<T = unknown>(opt: FetcherOptions): Promise<FetcherResponse<T>> {
174
165
  const req = this.normalizeOptions(opt)
175
166
  const { logger } = this.cfg
176
167
  const {
@@ -248,13 +239,6 @@ export class Fetcher {
248
239
  await hook(res)
249
240
  }
250
241
 
251
- if (req.paginate && res.ok) {
252
- const proceeed = await req.paginate(res, opt)
253
- if (proceeed) {
254
- return await this.doFetch(opt)
255
- }
256
- }
257
-
258
242
  return res
259
243
  }
260
244
 
@@ -560,7 +544,7 @@ export class Fetcher {
560
544
  return norm
561
545
  }
562
546
 
563
- private normalizeOptions<BODY>(opt: FetcherOptions<BODY>): FetcherRequest<BODY> {
547
+ private normalizeOptions(opt: FetcherOptions): FetcherRequest {
564
548
  const {
565
549
  timeoutSeconds,
566
550
  throwHttpErrors,
@@ -572,7 +556,7 @@ export class Fetcher {
572
556
  jsonReviver,
573
557
  } = this.cfg
574
558
 
575
- const req: FetcherRequest<BODY> = {
559
+ const req: FetcherRequest = {
576
560
  started: Date.now(),
577
561
  mode,
578
562
  timeoutSeconds,