lissa 1.0.4 → 1.0.6

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.
Files changed (2) hide show
  1. package/lib/index.d.ts +151 -111
  2. package/package.json +1 -1
package/lib/index.d.ts CHANGED
@@ -6,14 +6,21 @@ import { RequestInit, HeadersInit, BodyInit, Headers } from 'undici-types';
6
6
 
7
7
  export type JsonPrimitive = null | string | number | boolean;
8
8
 
9
- export type JsonObject = {
10
- [key: string]: Json;
11
- };
9
+ export type JsonObject = Record<string, Json>;
12
10
 
13
11
  export type JsonArray = Json[];
14
12
 
15
13
  export type Json = JsonPrimitive | JsonObject | JsonArray;
16
14
 
15
+
16
+ export type JsonStringifyablePrimitive = null | undefined | string | number | boolean;
17
+
18
+ export type JsonStringifyableObject = Record<string, JsonStringifyable>;
19
+
20
+ export type JsonStringifyableArray = JsonStringifyable[];
21
+
22
+ export type JsonStringifyable = JsonStringifyablePrimitive | JsonStringifyableObject | JsonStringifyableArray | { toJSON(): JsonStringifyable };
23
+
17
24
  /*
18
25
  * Param typing (like json but it supports dates if paramsSerializer is set to extended mode)
19
26
  */
@@ -49,7 +56,7 @@ export type LissaOptionsInit = Omit<RequestInit, 'method' | 'body'> & {
49
56
  timeout?: number;
50
57
  onUploadProgress?: (uploaded: number, total: number) => void;
51
58
  onDownloadProgress?: (downloaded: number, total: number) => void;
52
- body?: BodyInit | JsonObject;
59
+ body?: BodyInit | JsonStringifyableObject;
53
60
  };
54
61
 
55
62
  export type LissaOptions = Omit<LissaOptionsInit, 'headers'> & {
@@ -78,224 +85,284 @@ export type FetchArguments = {
78
85
  options: Omit<RequestInit, 'headers'> & { headers: Headers },
79
86
  };
80
87
 
81
- export type LissaResult = {
88
+ export type ResultData = null | string | Json | File | ReadableStream | Blob;
89
+
90
+ export type LissaResult<D = ResultData> = {
82
91
  options: LissaOptions;
83
92
  request: FetchArguments;
84
93
  response: Response;
85
94
  headers: Headers;
86
95
  status: number;
87
- data: null | string | Json | File | ReadableStream | Blob;
96
+ data: D;
88
97
  };
89
98
 
90
- export type GeneralErrorResponse = Error & {
99
+ export interface TypeError extends Error {
100
+ name: 'TypeError';
101
+ }
102
+
103
+ export interface TimeoutError extends DOMException {
104
+ name: 'TimeoutError';
105
+ }
106
+
107
+ export interface AbortError extends DOMException {
108
+ name: 'AbortError';
109
+ }
110
+
111
+ export type GeneralErrorResponse = (TimeoutError | AbortError) & {
91
112
  options: LissaOptions;
92
113
  request: FetchArguments;
93
114
  };
94
115
 
95
- export type ResultValue = LissaResult | Exclude<any, undefined>;
116
+
117
+ /**
118
+ * ConnectionError class for instance checking
119
+ *
120
+ * Will get thrown if a network-level error occurs (e.g. DNS resolution, connection lost)
121
+ */
122
+ export declare class ConnectionError extends Error {
123
+ name: 'ConnectionError';
124
+ options: LissaOptions;
125
+ request: FetchArguments;
126
+ }
127
+
128
+ /**
129
+ * ResponseError class for instance checking
130
+ *
131
+ * Will get thrown when the server responds with a status code that indicates a failure (non-2xx status)
132
+ */
133
+ export declare class ResponseError extends Error {
134
+ name: 'ResponseError';
135
+ options: LissaOptions;
136
+ request: FetchArguments;
137
+ response: Response;
138
+ headers: Headers;
139
+ status: number;
140
+ data: null | string | Json | ReadableStream;
141
+ }
142
+
143
+ export type ExpectedError = GeneralErrorResponse | ConnectionError | ResponseError;
144
+ export type ThrownError = TypeError | ExpectedError;
145
+
146
+ export type ResultValue<D> = LissaResult<D>;
96
147
 
97
148
  /*
98
149
  * Interfaces
99
150
  */
100
151
 
101
- export declare class LissaRequest extends Promise<ResultValue> {
152
+ export declare class LissaRequest<RT = ResultValue<ResultData>> extends Promise<RT> {
102
153
  readonly options: LissaOptions;
103
154
 
104
155
  /**
105
156
  * Set a base URL for the request
106
157
  */
107
- baseURL(baseURL: string): LissaRequest;
158
+ baseURL(baseURL: string): LissaRequest<RT>;
108
159
 
109
160
  /**
110
161
  * Set the request URL
111
162
  */
112
- url(url: string): LissaRequest;
163
+ url(url: string): LissaRequest<RT>;
113
164
 
114
165
  /**
115
166
  * Set the HTTP method (GET, POST, etc.)
116
167
  */
117
- method(method: HttpMethod): LissaRequest;
168
+ method(method: HttpMethod): LissaRequest<RT>;
118
169
 
119
170
  /**
120
171
  * Add or override request headers
121
172
  */
122
- headers(headers: HeadersInit): LissaRequest;
173
+ headers(headers: HeadersInit): LissaRequest<RT>;
123
174
 
124
175
  /**
125
176
  * Provide basic authentication credentials
126
177
  *
127
178
  * It sets the "Authorization" header to "Basic base64(username:password)"
128
179
  */
129
- authenticate(username: string, password: string): LissaRequest;
180
+ authenticate(username: string, password: string): LissaRequest<RT>;
130
181
 
131
182
  /**
132
183
  * Add or override query string parameters
133
184
  *
134
185
  * Check the paramsSerializer option to control the serialization of the params
135
186
  */
136
- params(params: Params): LissaRequest;
187
+ params(params: Params): LissaRequest<RT>;
137
188
 
138
189
  /**
139
190
  * Attach or merge a request body
140
191
  *
141
192
  * The body gets json stringified if it is a plain object
142
193
  */
143
- body(body: BodyInit | JsonObject): LissaRequest;
194
+ body(body: BodyInit | JsonStringifyableObject): LissaRequest<RT>;
144
195
 
145
196
  /**
146
197
  * Set request timeout in milliseconds
147
198
  *
148
199
  * Attaches an AbortSignal.timeout(...) signal to the request
149
200
  */
150
- timeout(timeout: number): LissaRequest;
201
+ timeout(timeout: number): LissaRequest<RT>;
151
202
 
152
203
  /**
153
204
  * Attach an AbortSignal to cancel the request
154
205
  */
155
- signal(signal: AbortSignal): LissaRequest;
206
+ signal(signal: AbortSignal): LissaRequest<RT>;
156
207
 
157
208
  /**
158
209
  * Change the expected response type
159
210
  */
160
- responseType(responseType: 'json' | 'text' | 'file' | 'raw'): LissaRequest;
211
+ responseType(responseType: 'json' | 'text' | 'file' | 'raw'): LissaRequest<RT>;
161
212
 
162
213
  /**
163
214
  * Add an upload progress listener
164
215
  */
165
- onUploadProgress(onProgress: (uploaded: number, total: number) => void): LissaRequest;
216
+ onUploadProgress(onProgress: (uploaded: number, total: number) => void): LissaRequest<RT>;
166
217
 
167
218
  /**
168
219
  * Add a download progress listener
169
220
  */
170
- onDownloadProgress(onProgress: (downloaded: number, total: number) => void): LissaRequest;
221
+ onDownloadProgress(onProgress: (downloaded: number, total: number) => void): LissaRequest<RT>;
171
222
 
172
223
  readonly status: 'pending' | 'fulfilled' | 'rejected';
173
- readonly value: void | ResultValue;
174
- readonly reason: void | Error;
224
+ readonly value: void | RT;
225
+ readonly reason: void | ThrownError;
175
226
 
176
227
  on(
177
- event: 'resolve' | 'reject' | 'settle',
178
- listener: (arg: ResultValue | Error | {
179
- status: 'fulfilled' | 'rejected',
180
- value: void | ResultValue,
181
- reason: void | Error,
228
+ event: 'resolve',
229
+ listener: (arg: RT) => void,
230
+ ): LissaRequest<RT>;
231
+
232
+ on(
233
+ event: 'reject',
234
+ listener: (arg: ThrownError) => void,
235
+ ): LissaRequest<RT>;
236
+
237
+ on(
238
+ event: 'settle',
239
+ listener: (arg: {
240
+ status: 'fulfilled',
241
+ value: RT,
242
+ reason: void,
243
+ } | {
244
+ status: 'rejected',
245
+ value: void,
246
+ reason: ThrownError,
182
247
  }) => void,
183
- ): LissaRequest;
248
+ ): LissaRequest<RT>;
184
249
 
185
250
  off(
186
251
  event: 'resolve' | 'reject' | 'settle',
187
- listener: (arg: ResultValue | Error | {
252
+ listener: (arg: RT | ThrownError | {
188
253
  status: 'fulfilled' | 'rejected',
189
- value: void | ResultValue,
190
- reason: void | Error,
254
+ value: void | RT,
255
+ reason: void | ThrownError,
191
256
  }) => void,
192
- ): LissaRequest;
257
+ ): LissaRequest<RT>;
193
258
  }
194
259
 
195
- interface MakeRequest {
260
+ interface MakeRequest<RT> {
196
261
 
197
262
  /**
198
263
  * Perform a GET request
199
264
  */
200
- get(
265
+ get<D = RT extends LissaResult<any> ? ResultData : RT>(
201
266
  url?: string,
202
267
  options?: Omit<LissaOptionsInit, 'method' | 'url'>
203
- ): LissaRequest;
268
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
204
269
 
205
270
  /**
206
271
  * Perform a POST request with optional body
207
272
  */
208
- post(
273
+ post<D = RT extends LissaResult<any> ? ResultData : RT>(
209
274
  url?: string,
210
- body?: BodyInit | JsonObject,
275
+ body?: BodyInit | JsonStringifyableObject,
211
276
  options?: Omit<LissaOptionsInit, 'method' | 'url' | 'body'>
212
- ): LissaRequest;
277
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
213
278
 
214
279
  /**
215
280
  * Perform a PUT request with optional body
216
281
  */
217
- put(
282
+ put<D = RT extends LissaResult<any> ? ResultData : RT>(
218
283
  url?: string,
219
- body?: BodyInit | JsonObject,
284
+ body?: BodyInit | JsonStringifyableObject,
220
285
  options?: Omit<LissaOptionsInit, 'method' | 'url' | 'body'>
221
- ): LissaRequest;
286
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
222
287
 
223
288
  /**
224
289
  * Perform a PATCH request with optional body
225
290
  */
226
- patch(
291
+ patch<D = RT extends LissaResult<any> ? ResultData : RT>(
227
292
  url?: string,
228
- body?: BodyInit | JsonObject,
293
+ body?: BodyInit | JsonStringifyableObject,
229
294
  options?: Omit<LissaOptionsInit, 'method' | 'url' | 'body'>
230
- ): LissaRequest;
295
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
231
296
 
232
297
  /**
233
298
  * Perform a DELETE request
234
299
  */
235
- delete(
300
+ delete<D = RT extends LissaResult<any> ? ResultData : RT>(
236
301
  url?: string,
237
302
  options?: Omit<LissaOptionsInit, 'method' | 'url'>
238
- ): LissaRequest;
303
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
239
304
 
240
305
  /**
241
306
  * Perform a general fetch request.
242
307
  *
243
308
  * Specify url, method, body, headers and more in the given options object
244
309
  */
245
- request(options?: LissaOptionsInit): LissaRequest;
310
+ request<D = RT extends LissaResult<any> ? ResultData : RT>(
311
+ options?: LissaOptionsInit
312
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
246
313
 
247
314
  /**
248
315
  * Upload a file
249
316
  */
250
- upload(
317
+ upload<D = RT extends LissaResult<any> ? ResultData : RT>(
251
318
  file: File,
252
319
  url?: string,
253
320
  onProgress?: (uploaded: number, total: number) => void,
254
321
  options?: Omit<LissaOptionsInit, 'url'>,
255
- ): LissaRequest;
256
- upload(
322
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
323
+ upload<D = RT extends LissaResult<any> ? ResultData : RT>(
257
324
  file: File,
258
325
  url?: string,
259
326
  options?: Omit<LissaOptionsInit, 'url'>,
260
327
  onProgress?: (uploaded: number, total: number) => void,
261
- ): LissaRequest;
262
- upload(
328
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
329
+ upload<D = RT extends LissaResult<any> ? ResultData : RT>(
263
330
  file: File,
264
331
  onProgress?: (uploaded: number, total: number) => void,
265
332
  options?: LissaOptionsInit,
266
- ): LissaRequest;
267
- upload(
333
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
334
+ upload<D = RT extends LissaResult<any> ? ResultData : RT>(
268
335
  file: File,
269
336
  options?: LissaOptionsInit,
270
337
  onProgress?: (uploaded: number, total: number) => void,
271
- ): LissaRequest;
338
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
272
339
 
273
340
  /**
274
341
  * Download a file
275
342
  */
276
- download(
343
+ download<D = RT extends LissaResult<any> ? File : RT>(
277
344
  url?: string,
278
345
  onProgress?: (downloaded: number, total: number) => void,
279
346
  options?: Omit<LissaOptionsInit, 'url'>,
280
- ): LissaRequest;
281
- download(
347
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
348
+ download<D = RT extends LissaResult<any> ? File : RT>(
282
349
  url?: string,
283
350
  options?: Omit<LissaOptionsInit, 'url'>,
284
351
  onProgress?: (downloaded: number, total: number) => void,
285
- ): LissaRequest;
286
- download(
352
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
353
+ download<D = RT extends LissaResult<any> ? File : RT>(
287
354
  onProgress?: (downloaded: number, total: number) => void,
288
355
  options?: LissaOptionsInit,
289
- ): LissaRequest;
290
- download(
356
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
357
+ download<D = RT extends LissaResult<any> ? File : RT>(
291
358
  options?: LissaOptionsInit,
292
359
  onProgress?: (downloaded: number, total: number) => void,
293
- ): LissaRequest;
360
+ ): LissaRequest<RT extends LissaResult<any> ? LissaResult<D> : D>;
294
361
  }
295
362
 
296
- export type Plugin = (lissa: Lissa) => void;
363
+ export type Plugin<RT> = (lissa: Lissa<RT>) => void;
297
364
 
298
- export interface Lissa extends MakeRequest {
365
+ export interface Lissa<RT = ResultValue<ResultData>> extends MakeRequest<RT> {
299
366
  /**
300
367
  * Modify the base options directly.
301
368
  *
@@ -310,21 +377,21 @@ export interface Lissa extends MakeRequest {
310
377
  * @example
311
378
  * lissa.use(Lissa.retry());
312
379
  */
313
- use(plugin: Plugin): Lissa;
380
+ use(plugin: Plugin<RT>): Lissa<RT>;
314
381
 
315
382
  /**
316
383
  * Add a beforeRequest hook into the request cycle.
317
384
  *
318
385
  * Modify the given options as argument or return a new options object.
319
386
  */
320
- beforeRequest(hook: (options: LissaOptions) => void | LissaOptions | Promise<void | LissaOptions>): Lissa;
387
+ beforeRequest(hook: (options: LissaOptions) => void | LissaOptions | Promise<void | LissaOptions>): Lissa<RT>;
321
388
 
322
389
  /**
323
390
  * Add a beforeFetch hook into the request cycle.
324
391
  *
325
392
  * Modify the actual fetch arguments or return new arguments.
326
393
  */
327
- beforeFetch(hook: (request: FetchArguments) => void | FetchArguments | Promise<void | FetchArguments>): Lissa;
394
+ beforeFetch(hook: (request: FetchArguments) => void | FetchArguments | Promise<void | FetchArguments>): Lissa<RT>;
328
395
 
329
396
  /**
330
397
  * Add an onResponse hook into the request cycle.
@@ -333,7 +400,7 @@ export interface Lissa extends MakeRequest {
333
400
  * stop looping over existing hooks and instantly returns this value (if it
334
401
  * is an instance of Error it will get thrown).
335
402
  */
336
- onResponse(hook: (result: LissaResult) => void | Exclude<any, undefined> | Promise<void | Exclude<any, undefined>>): Lissa;
403
+ onResponse<NRT = RT>(hook: (result: RT) => void | NRT | Error | Promise<void | NRT | Error>): Lissa<NRT>;
337
404
 
338
405
  /**
339
406
  * Add an onError hook into the request cycle.
@@ -342,40 +409,40 @@ export interface Lissa extends MakeRequest {
342
409
  * over existing hooks and instantly returns this value (if it is an instance
343
410
  * of Error it will get thrown).
344
411
  */
345
- onError(hook: (error: ResponseError | ConnectionError | GeneralErrorResponse) => void | Exclude<any, undefined> | Promise<void | Exclude<any, undefined>>): Lissa;
412
+ onError<NRT = RT>(hook: (error: ExpectedError) => void | NRT | Error | Promise<void | NRT | Error>): Lissa<NRT>;
346
413
 
347
414
  /**
348
415
  * Copy the current instance with all its options and hooks.
349
416
  */
350
- extend(options: DefaultOptionsInit): Lissa;
417
+ extend(options: DefaultOptionsInit): Lissa<RT>;
351
418
 
352
419
  /**
353
420
  * Provide basic authentication credentials
354
421
  *
355
422
  * It sets the "Authorization" header to "Basic base64(username:password)"
356
423
  */
357
- authenticate(username: string, password: string): Lissa;
424
+ authenticate(username: string, password: string): Lissa<RT>;
358
425
  }
359
426
 
360
- declare const LissaLib: MakeRequest & NamedExports & {
427
+ declare const LissaLib: MakeRequest<ResultValue<ResultData>> & NamedExports & {
361
428
  /**
362
429
  * Perform a general fetch request.
363
430
  *
364
431
  * Specify method, body, headers and more in the given options object
365
432
  */
366
- (
433
+ <D = ResultData>(
367
434
  url: string,
368
435
  options?: Omit<LissaOptionsInit, 'url'>
369
- ): LissaRequest;
436
+ ): LissaRequest<ResultValue<D>>;
370
437
 
371
438
  /**
372
439
  * Create a Lissa instance with the given base options.
373
440
  */
374
- create(options: DefaultOptionsInit): Lissa;
441
+ create(options: DefaultOptionsInit): Lissa<ResultValue<ResultData>>;
375
442
  create(
376
443
  baseURL?: string,
377
444
  options?: Omit<DefaultOptionsInit, 'baseURL'>
378
- ): Lissa;
445
+ ): Lissa<ResultValue<ResultData>>;
379
446
  };
380
447
 
381
448
  export default LissaLib;
@@ -385,39 +452,12 @@ export default LissaLib;
385
452
  */
386
453
  export declare const defaults: DefaultOptions;
387
454
 
388
-
389
- /**
390
- * ConnectionError class for instance checking
391
- *
392
- * Will get thrown if a network-level error occurs (e.g. DNS resolution, connection lost)
393
- */
394
- export declare class ConnectionError extends Error {
395
- name: 'ConnectionError';
396
- options: LissaOptions;
397
- request: FetchArguments;
398
- }
399
-
400
- /**
401
- * ResponseError class for instance checking
402
- *
403
- * Will get thrown when the server responds with a status code that indicates a failure (non-2xx status)
404
- */
405
- export declare class ResponseError extends Error {
406
- name: 'ResponseError';
407
- options: LissaOptions;
408
- request: FetchArguments;
409
- response: Response;
410
- headers: Headers;
411
- status: number;
412
- data: null | string | Json | ReadableStream;
413
- }
414
-
415
455
  /**
416
456
  * Retry plugin
417
457
  *
418
458
  * Retry requests on connection errors or server errors
419
459
  */
420
- export declare const retry: (options?: RetryOptions) => Plugin;
460
+ export declare const retry: (options?: RetryOptions) => Plugin<ResultValue<ResultData>>;
421
461
 
422
462
  export type CustomRetryError = {
423
463
  /** custom retry type have to be returned by shouldRetry hook */
@@ -442,7 +482,7 @@ export type RetryOptions = CustomRetryError & {
442
482
  */
443
483
  shouldRetry?: (
444
484
  errorType: void | 'ConnectionError' | 'GatewayError' | '429' | 'ServerError',
445
- error: ResponseError | ConnectionError | GeneralErrorResponse,
485
+ error: ExpectedError,
446
486
  ) => void | false | string;
447
487
 
448
488
  /**
@@ -452,7 +492,7 @@ export type RetryOptions = CustomRetryError & {
452
492
  */
453
493
  beforeRetry?: (
454
494
  retry: { attempt: number, delay: number },
455
- error: ResponseError | ConnectionError | GeneralErrorResponse,
495
+ error: ExpectedError,
456
496
  ) => void | { attempt: number, delay: number };
457
497
 
458
498
  /**
@@ -461,7 +501,7 @@ export type RetryOptions = CustomRetryError & {
461
501
  */
462
502
  onRetry?: (
463
503
  retry: { attempt: number, delay: number },
464
- error: ResponseError | ConnectionError | GeneralErrorResponse,
504
+ error: ExpectedError,
465
505
  ) => void;
466
506
 
467
507
  /**
@@ -470,7 +510,7 @@ export type RetryOptions = CustomRetryError & {
470
510
  */
471
511
  onSuccess?: (
472
512
  retry: { attempt: number, delay: number },
473
- res: ResultValue,
513
+ res: ResultValue<ResultData>,
474
514
  ) => void;
475
515
  };
476
516
 
@@ -479,7 +519,7 @@ export type RetryOptions = CustomRetryError & {
479
519
  *
480
520
  * Aborts leading or trailing requests to the same endpoint (depends on configured strategy [default is leading])
481
521
  */
482
- export declare const dedupe: (options?: DedupeOptions) => Plugin;
522
+ export declare const dedupe: (options?: DedupeOptions) => Plugin<ResultValue<ResultData>>;
483
523
 
484
524
  export type DedupeOptions = {
485
525
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lissa",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "A lightweight, extensible HTTP client for modern JavaScript applications with fluent api syntax, hooks, plugins and more.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",