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