@equinor/fusion-framework-module-http 6.0.0 → 6.0.2

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 (65) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +759 -36
  3. package/dist/esm/configurator.js +10 -1
  4. package/dist/esm/configurator.js.map +1 -1
  5. package/dist/esm/errors.js +18 -0
  6. package/dist/esm/errors.js.map +1 -1
  7. package/dist/esm/index.js +4 -0
  8. package/dist/esm/index.js.map +1 -1
  9. package/dist/esm/lib/client/client-msal.js +35 -0
  10. package/dist/esm/lib/client/client-msal.js.map +1 -1
  11. package/dist/esm/lib/client/client.js +150 -4
  12. package/dist/esm/lib/client/client.js.map +1 -1
  13. package/dist/esm/lib/operators/http-request-handler.js +16 -0
  14. package/dist/esm/lib/operators/http-request-handler.js.map +1 -1
  15. package/dist/esm/lib/operators/http-response-handler.js +6 -0
  16. package/dist/esm/lib/operators/http-response-handler.js.map +1 -1
  17. package/dist/esm/lib/operators/process-operators.js +49 -1
  18. package/dist/esm/lib/operators/process-operators.js.map +1 -1
  19. package/dist/esm/lib/operators/request-operator-header.js +7 -0
  20. package/dist/esm/lib/operators/request-operator-header.js.map +1 -1
  21. package/dist/esm/lib/selectors/blob-selector.js +12 -0
  22. package/dist/esm/lib/selectors/blob-selector.js.map +1 -1
  23. package/dist/esm/lib/selectors/json-selector.js +21 -0
  24. package/dist/esm/lib/selectors/json-selector.js.map +1 -1
  25. package/dist/esm/module.js +40 -0
  26. package/dist/esm/module.js.map +1 -1
  27. package/dist/esm/provider.js +65 -6
  28. package/dist/esm/provider.js.map +1 -1
  29. package/dist/esm/version.js +2 -1
  30. package/dist/esm/version.js.map +1 -1
  31. package/dist/tsconfig.tsbuildinfo +1 -1
  32. package/dist/types/configurator.d.ts +76 -0
  33. package/dist/types/errors.d.ts +18 -0
  34. package/dist/types/index.d.ts +4 -0
  35. package/dist/types/lib/client/client-msal.d.ts +43 -0
  36. package/dist/types/lib/client/client.d.ts +144 -0
  37. package/dist/types/lib/client/types.d.ts +150 -3
  38. package/dist/types/lib/operators/http-request-handler.d.ts +16 -0
  39. package/dist/types/lib/operators/http-response-handler.d.ts +6 -0
  40. package/dist/types/lib/operators/process-operators.d.ts +44 -0
  41. package/dist/types/lib/operators/request-operator-header.d.ts +7 -0
  42. package/dist/types/lib/operators/types.d.ts +57 -0
  43. package/dist/types/lib/selectors/blob-selector.d.ts +9 -2
  44. package/dist/types/lib/selectors/index.d.ts +1 -0
  45. package/dist/types/lib/selectors/json-selector.d.ts +16 -1
  46. package/dist/types/module.d.ts +35 -0
  47. package/dist/types/provider.d.ts +77 -0
  48. package/dist/types/version.d.ts +1 -1
  49. package/package.json +5 -5
  50. package/src/configurator.ts +38 -1
  51. package/src/errors.ts +11 -1
  52. package/src/lib/client/client-msal.ts +50 -5
  53. package/src/lib/client/client.ts +130 -5
  54. package/src/lib/client/types.ts +124 -67
  55. package/src/lib/operators/http-request-handler.ts +12 -4
  56. package/src/lib/operators/http-response-handler.ts +6 -0
  57. package/src/lib/operators/process-operators.ts +6 -1
  58. package/src/lib/operators/request-operator-header.ts +7 -0
  59. package/src/lib/operators/types.ts +22 -4
  60. package/src/lib/selectors/blob-selector.ts +2 -2
  61. package/src/lib/selectors/index.ts +2 -0
  62. package/src/lib/selectors/json-selector.ts +5 -1
  63. package/src/module.ts +55 -2
  64. package/src/provider.ts +76 -2
  65. package/src/version.ts +1 -1
@@ -16,8 +16,17 @@ import type {
16
16
  JsonRequest,
17
17
  StreamResponse,
18
18
  } from './types';
19
+
19
20
  import { HttpResponseError } from '../../errors';
20
21
 
22
+ /**
23
+ * Configuration options for creating an `HttpClient` instance.
24
+ *
25
+ * @template TRequest - The type of the request object. Defaults to `FetchRequest`.
26
+ * @template TResponse - The type of the response object. Defaults to `Response`.
27
+ * @property {IHttpRequestHandler<TRequest>} requestHandler - A handler for customizing the request before it is sent.
28
+ * @property {IHttpResponseHandler<TResponse>} responseHandler - A handler for customizing the response after it is received.
29
+ */
21
30
  export type HttpClientCreateOptions<
22
31
  TRequest extends FetchRequest = FetchRequest,
23
32
  TResponse = Response,
@@ -32,22 +41,46 @@ export class HttpClient<
32
41
  TResponse extends FetchResponse = FetchResponse,
33
42
  > implements IHttpClient<TRequest, TResponse>
34
43
  {
35
- readonly requestHandler: IHttpRequestHandler<TRequest>;
44
+ /**
45
+ * A request handler that can be used to customize the request before it is sent.
46
+ * This property is part of the `HttpClientCreateOptions` configuration object used to create an `HttpClient` instance.
47
+ */
48
+ public readonly requestHandler: IHttpRequestHandler<TRequest>;
36
49
 
37
- readonly responseHandler: IHttpResponseHandler<TResponse>;
50
+ /**
51
+ * A handler for customizing the response after it is received.
52
+ * This property is part of the `HttpClientCreateOptions` configuration object used to create an `HttpClient` instance.
53
+ */
54
+ public readonly responseHandler: IHttpResponseHandler<TResponse>;
38
55
 
39
- /** stream of requests that are about to be executed */
56
+ /**
57
+ * A stream of requests that are about to be executed.
58
+ * This property is used internally by the `HttpClient` class to manage the lifecycle of requests.
59
+ */
40
60
  protected _request$ = new Subject<TRequest>();
41
61
 
42
- /** stream of request responses */
62
+ /**
63
+ * A stream of responses that have been received.
64
+ * This property is used internally by the `HttpClient` class to manage the lifecycle of responses.
65
+ */
43
66
  protected _response$ = new Subject<TResponse>();
44
67
 
68
+ /**
69
+ * A stream that is used to signal the abortion of requests.
70
+ * This property is used internally by the `HttpClient` class to manage the lifecycle of requests.
71
+ */
45
72
  protected _abort$ = new Subject<void>();
46
73
 
74
+ /**
75
+ * A stream of requests that are about to be executed.
76
+ */
47
77
  public get request$(): Observable<TRequest> {
48
78
  return this._request$.asObservable();
49
79
  }
50
80
 
81
+ /**
82
+ * A stream of responses that have been received.
83
+ */
51
84
  public get response$(): Observable<TResponse> {
52
85
  return this._response$.asObservable();
53
86
  }
@@ -61,11 +94,23 @@ export class HttpClient<
61
94
  this._init();
62
95
  }
63
96
 
64
- /** internal abstract method, for overriding instead of overriding constructor */
97
+ /**
98
+ * Internal method called by child classes to perform initialization logic in the constructor.
99
+ * This method is intended to be overridden by child classes to add their own initialization logic.
100
+ * @protected
101
+ * @virtual
102
+ */
65
103
  protected _init(): void {
66
104
  // called by children for constructor setup
67
105
  }
68
106
 
107
+ /**
108
+ * Fetches data from the specified path and returns a stream response.
109
+ *
110
+ * @param path - The path to fetch data from.
111
+ * @param args - Optional request initialization options, including a custom selector function.
112
+ * @returns A stream response containing the fetched data.
113
+ */
69
114
  public fetch$<T = TResponse>(
70
115
  path: string,
71
116
  args?: FetchRequestInit<T, TRequest, TResponse>,
@@ -73,6 +118,13 @@ export class HttpClient<
73
118
  return this._fetch$(path, args);
74
119
  }
75
120
 
121
+ /**
122
+ * Fetches data from the specified path and returns a Promise containing the fetched data.
123
+ *
124
+ * @param path - The path to fetch data from.
125
+ * @param args - Optional request initialization options, including a custom selector function.
126
+ * @returns A Promise containing the fetched data.
127
+ */
76
128
  public fetch<T = TResponse>(
77
129
  path: string,
78
130
  args?: FetchRequestInit<T, TRequest, TResponse>,
@@ -88,6 +140,16 @@ export class HttpClient<
88
140
  return this.fetch(path, args);
89
141
  }
90
142
 
143
+ /**
144
+ * Fetches data from the specified path and returns a stream response containing the data in JSON format.
145
+ *
146
+ * @param path - The path to fetch the data from.
147
+ * @param args - Optional request initialization options, including a custom selector function and request body.
148
+ * - `body`: The request body, which will be automatically serialized to JSON if it's an object.
149
+ * - `selector`: A custom selector function to transform the response data. If not provided, the `jsonSelector` function will be used.
150
+ * - `headers`: Additional headers to include in the request. The `Accept` and `Content-Type` headers will be automatically set to `application/json`.
151
+ * @returns A stream response containing the fetched data in JSON format.
152
+ */
91
153
  public json$<T = unknown>(
92
154
  path: string,
93
155
  args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
@@ -105,6 +167,16 @@ export class HttpClient<
105
167
  } as FetchRequestInit<T, TRequest, TResponse>);
106
168
  }
107
169
 
170
+ /**
171
+ * Fetches data from the specified path and returns a Promise containing the fetched data in JSON format.
172
+ *
173
+ * @param path - The path to fetch the data from.
174
+ * @param args - Optional request initialization options, including a custom selector function and request body.
175
+ * - `body`: The request body, which will be automatically serialized to JSON if it's an object.
176
+ * - `selector`: A custom selector function to transform the response data. If not provided, the `jsonSelector` function will be used.
177
+ * - `headers`: Additional headers to include in the request. The `Accept` and `Content-Type` headers will be automatically set to `application/json`.
178
+ * @returns A Promise containing the fetched data in JSON format.
179
+ */
108
180
  public json<T = unknown>(
109
181
  path: string,
110
182
  args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
@@ -158,6 +230,14 @@ export class HttpClient<
158
230
  return this.json(path, args);
159
231
  }
160
232
 
233
+ /**
234
+ * Executes an HTTP request using the specified method and path.
235
+ *
236
+ * @param method - The HTTP method to use for the request, such as 'fetch', 'json', or 'blob'.
237
+ * @param path - The path to the resource to fetch.
238
+ * @param init - Optional request initialization options, including request body, headers, and response type.
239
+ * @returns The result of the HTTP request, which will be of the same type as the return value of the specified method.
240
+ */
161
241
  public execute<T = TResponse, TMethod extends 'fetch' | 'fetch$' | 'json' | 'json$' = 'fetch'>(
162
242
  method: TMethod,
163
243
  path: string,
@@ -166,10 +246,30 @@ export class HttpClient<
166
246
  return this[method](path, init) as ReturnType<IHttpClient[TMethod]>;
167
247
  }
168
248
 
249
+ /**
250
+ * Aborts any ongoing HTTP requests made by this `IHttpClient` instance.
251
+ * This will trigger the `takeUntil` operator in the `_fetch$` method,
252
+ * causing any in-flight requests to be cancelled.
253
+ */
169
254
  public abort(): void {
170
255
  this._abort$.next();
171
256
  }
172
257
 
258
+ /**
259
+ * Fetches data from the specified path and returns an Observable that emits the response.
260
+ *
261
+ * @param path - The path to fetch the data from.
262
+ * @param args - Optional arguments for the fetch request, including a response selector function, request body, headers, and response type.
263
+ * @returns {Observable<T>} An Observable that emits the response data.
264
+ *
265
+ * This method handles the following steps:
266
+ * 1. Resolves the full URL by combining the base URI and the provided path.
267
+ * 2. Prepares the request by passing it through the `requestHandler.process()` method.
268
+ * 3. Executes the fetch request using the prepared request.
269
+ * 4. Prepares the response by passing it through the `responseHandler.process()` method.
270
+ * 5. Applies the optional response selector function to transform the response data.
271
+ * 6. Cancels the request if the `_abort$` observable emits.
272
+ */
173
273
  protected _fetch$<T = TResponse>(
174
274
  path: string,
175
275
  args?: FetchRequestInit<T, TRequest, TResponse>,
@@ -213,14 +313,39 @@ export class HttpClient<
213
313
  return response$ as unknown as Observable<T>;
214
314
  }
215
315
 
316
+ /**
317
+ * Prepares the request by passing it through the `requestHandler.process()` method.
318
+ * This method is an implementation detail of the `_fetch$()` method, and is not part of the public API.
319
+ * It takes a `TRequest` object as input, which represents the request data, and returns an `ObservableInput<TRequest>`,
320
+ * which can be used to further process the request before it is executed.
321
+ *
322
+ * @param init The request data to be processed.
323
+ * @returns An `ObservableInput<TRequest>` that represents the processed request.
324
+ */
216
325
  protected _prepareRequest(init: TRequest): ObservableInput<TRequest> {
217
326
  return this.requestHandler.process(init);
218
327
  }
219
328
 
329
+ /**
330
+ * Prepares the response by passing it through the `responseHandler.process()` method.
331
+ * This method is an implementation detail of the `_fetch$()` method, and is not part of the public API.
332
+ * It takes a `TResponse` object as input, which represents the response data, and returns an `ObservableInput<TResponse>`,
333
+ * which can be used to further process the response before it is returned.
334
+ *
335
+ * @param response The response data to be processed.
336
+ * @returns An `ObservableInput<TResponse>` that represents the processed response.
337
+ */
220
338
  protected _prepareResponse(response: TResponse): ObservableInput<TResponse> {
221
339
  return this.responseHandler.process(response);
222
340
  }
223
341
 
342
+ /**
343
+ * Resolves the full URL for a given path by combining it with the base URL.
344
+ * This is a protected method and is an implementation detail of the `HttpClient` class.
345
+ *
346
+ * @param path - The path to be resolved.
347
+ * @returns The full URL for the given path.
348
+ */
224
349
  protected _resolveUrl(path: string): string {
225
350
  const baseUrl = this.uri || window.location.origin;
226
351
  return new URL(path, baseUrl).href;
@@ -1,13 +1,39 @@
1
1
  import type { ObservableInput, Observable } from 'rxjs';
2
2
  import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators/types';
3
3
 
4
+ /**
5
+ * Represents a stream of response data.
6
+ * @template T - The type of the response data.
7
+ */
4
8
  export type StreamResponse<T> = Observable<T>;
5
9
 
10
+ /**
11
+ * A function that takes a `Response` object and returns an `ObservableInput` of type `T`.
12
+ *
13
+ * @template TResult - The type of the data contained in the response.
14
+ * @template TResponse - The type of the response object.
15
+ * @param response - The `Response` object to be processed.
16
+ * @returns An `ObservableInput` of type `T`.
17
+ */
18
+ export type ResponseSelector<TResult = unknown, TResponse = Response> = (
19
+ response: TResponse,
20
+ ) => ObservableInput<TResult>;
21
+
22
+ /**
23
+ * Represents the parameters for a fetch request, including the URI and path.
24
+ * @property {string} uri - The URI of the request.
25
+ * @property {string} path - The path of the request.
26
+ */
6
27
  export type FetchRequest = RequestInit & {
7
28
  uri: string;
8
29
  path: string;
9
30
  };
10
31
 
32
+ /**
33
+ * Represents a request with a JSON body.
34
+ * @template TRequest - The base request type, which extends `FetchRequest`.
35
+ * @property {object | string | null} [body] - The request body, which can be an object, a string, or null.
36
+ */
11
37
  export type JsonRequest<TRequest extends FetchRequest = FetchRequest> = Omit<TRequest, 'body'> & {
12
38
  body?: object | string | null;
13
39
  };
@@ -19,30 +45,65 @@ export type JsonRequest<TRequest extends FetchRequest = FetchRequest> = Omit<TRe
19
45
  */
20
46
  export type BlobResult = { filename?: string; blob: Blob };
21
47
 
48
+ /**
49
+ * Represents the response from a fetch request, including the response object and a JSON parsing method.
50
+ * @template T - The type of the response data.
51
+ * @property {Response} - The original Response object.
52
+ * @property {() => Promise<T>} json - A method to parse the response body as JSON and return the data as type T.
53
+ */
22
54
  export type FetchResponse<T = unknown> = Response & {
23
55
  json(): Promise<T>;
24
56
  };
25
57
 
58
+ /**
59
+ * Represents the parameters for a fetch request, including the URI and path, as well as a selector function to transform the response.
60
+ * @template TReturn - The type of the transformed response data.
61
+ * @template TRequest - The type of the fetch request.
62
+ * @template TResponse - The type of the fetch response.
63
+ */
26
64
  export type FetchRequestInit<
27
65
  TReturn = unknown,
28
66
  TRequest = FetchRequest,
29
67
  TResponse = FetchResponse<TReturn>,
30
68
  > = Omit<TRequest, 'uri' | 'path'> & {
31
- selector?: (response: TResponse) => ObservableInput<TReturn>;
69
+ /** response selector function */
70
+ selector?: ResponseSelector<TReturn, TResponse>;
32
71
  };
33
72
 
73
+ /**
74
+ * Represents the parameters for a fetch request, including the URI and path, as well as a selector function to transform the response.
75
+ *
76
+ * @template TReturn - The type of the transformed response data.
77
+ * @template TRequest - The type of the fetch request.
78
+ * @template TResponse - The type of the fetch response.
79
+ */
34
80
  export type ClientRequestInit<T extends IHttpClient, TReturn = unknown> =
35
81
  T extends IHttpClient<infer TRequest, infer TResponse>
36
82
  ? FetchRequestInit<TReturn, TRequest, TResponse>
37
83
  : never;
38
84
 
85
+ /**
86
+ * Represents the available execution methods for an HTTP client.
87
+ */
39
88
  export type ExecutionMethod = 'fetch' | 'fetch$' | 'json' | 'json$';
40
89
 
90
+ /**
91
+ * Represents the type of the parameters for the execution methods of an `IHttpClient` instance.
92
+ *
93
+ * @template TMethod - The execution method of the `IHttpClient` instance, e.g. 'fetch', 'json'.
94
+ * @template TClient - The type of the `IHttpClient` instance.
95
+ */
41
96
  export type ExecutionMethodParameters<
42
97
  TMethod extends ExecutionMethod = 'fetch',
43
98
  TClient extends IHttpClient = IHttpClient,
44
99
  > = Parameters<TClient[TMethod]>;
45
100
 
101
+ /**
102
+ * Represents the type of the return value for the execution methods of an `IHttpClient` instance.
103
+ *
104
+ * @template TMethod - The execution method of the `IHttpClient` instance, e.g. 'fetch', 'json'.
105
+ * @template TClient - The type of the `IHttpClient` instance.
106
+ */
46
107
  export type ExecutionResponse<
47
108
  TMethod extends ExecutionMethod = 'fetch',
48
109
  TClient extends IHttpClient = IHttpClient,
@@ -54,50 +115,42 @@ export type ExecutionResponse<
54
115
  */
55
116
  export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = Response> {
56
117
  uri: string;
57
- /** pre-processor of requests */
118
+ /**
119
+ * A pre-processor for requests made by the `IHttpClient` interface.
120
+ * This handler can be used to modify the request before it is sent, such as adding headers, authentication, or other transformations.
121
+ */
58
122
  readonly requestHandler: IHttpRequestHandler<TRequest>;
59
- /** post-processor of requests */
123
+
124
+ /**
125
+ * A post-processor for responses received by the `IHttpClient` interface.
126
+ * This handler can be used to transform the response data after it is received, such as parsing JSON, handling errors, or other transformations.
127
+ */
60
128
  readonly responseHandler: IHttpResponseHandler<TResponse>;
61
129
 
62
- /** Observable stream of request */
63
- request$: Observable<TRequest>;
130
+ /**
131
+ * Observable stream of requests made by the `IHttpClient` interface.
132
+ * This stream can be used to observe and potentially modify the requests before they are sent.
133
+ */
134
+ readonly request$: Observable<TRequest>;
64
135
 
65
- /** Observable stream of responses */
66
- response$: Observable<TResponse>;
136
+ /**
137
+ * Observable stream of responses received by the `IHttpClient` interface.
138
+ * This stream can be used to observe and potentially handle the responses after they are received.
139
+ */
140
+ readonly response$: Observable<TResponse>;
67
141
 
68
142
  /**
69
- * Observable request.
70
- * Simplifies execution of request and
71
- * note: request will not be executed until subscribe!
143
+ * Fetch a resource as an observable stream.
144
+ * This method simplifies the execution of a request and returns an observable stream of the response.
145
+ * The request will not be executed until the observable is subscribed to.
146
+ *
147
+ * @template T - The expected response type.
148
+ * @param path - The path to fetch the resource from.
149
+ * @param init - Optional request initialization options.
150
+ * @returns An observable stream of the response.
72
151
  *
73
- * @see {@link https://rxjs.dev/api/fetch/fromFetch|RXJS}
152
+ * @see {@link https://rxjs.dev/api/fetch/fromFetch|RxJS fromFetch}
74
153
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch|fetch}
75
- * @example
76
- * ```ts
77
- * // Observer changes of a input field
78
- * const client = modules.http.createClient('my-client');
79
- * const input$ = fromEvent(document.getElementById('input'), 'input');
80
- * input$.pipe(
81
- * // only call after no key input in .5s
82
- * debounceTime(500),
83
- * // extract value from event
84
- * map(x => x.currentTarget.value),
85
- * // only search when text longer than 2 characters
86
- * filter(x => x.length >=3),
87
- * // query api with input value
88
- * switchMap(x => client.fetch(`api/foo?q=${x}`).pipe(
89
- * // retry 2 times
90
- * retry(2)
91
- * // cancel request if new input
92
- * takeUntil(input$)
93
- * )),
94
- * // extract data from response
95
- * switchMap(x => x.json()),
96
- * // process error
97
- * catchError(x => of({error: e.message}))
98
- * // write result to pre element
99
- * ).subscribe(console.log);
100
- * ```
101
154
  */
102
155
  fetch$<T = TResponse>(
103
156
  path: string,
@@ -105,46 +158,50 @@ export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResp
105
158
  ): StreamResponse<T>;
106
159
 
107
160
  /**
108
- * Fetch a resource as an promise
109
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch|fetch}
110
- * @example
111
- * ```ts
112
- * let controller: AbortController;
113
- * const client = window.Fusion.createClient('my-client');
114
- * const input = document.getElementById('input');
115
- * input.addEventlistener('input', (e) => {
116
- * try{
117
- * // if a controller is defined, request might be ongoing
118
- * controller && controller.abort();
119
- * // create a new abort controller
120
- * controller = new AbortController();
121
- * // query api with
122
- * const response = await client.fetch({
123
- * path: `api/foo?q=${e.currentTarget.value}`,
124
- * signal: controller.signal,
125
- * });
126
- * const json = await response.json();
127
- * result.innerText = JSON.stringify(json, null, 2)
128
- * } catch(err){
129
- * result.innerText = 'an error occurred'
130
- * } finally{
131
- * delete controller;
132
- * }
133
- * });
134
- * ```
161
+ * Fetch a resource as a promise.
162
+ *
163
+ * @template T - The expected response type.
164
+ * @param path - The path to fetch the resource from.
165
+ * @param init - Optional request initialization options.
166
+ * @returns A promise that resolves to the fetched resource.
167
+ *
168
+ * @see {@link IHttpClient.fetch$}
135
169
  */
136
170
  fetch<T = TResponse>(path: string, init?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
137
171
 
172
+ /** @deprecated use {@link IHttpClient.fetch} */
138
173
  fetchAsync<T = TResponse>(
139
174
  path: string,
140
175
  args?: FetchRequestInit<T, TRequest, TResponse>,
141
176
  ): Promise<T>;
142
177
 
178
+ /**
179
+ * Fetches a resource as an observable stream.
180
+ * This method simplifies the execution of a request and returns an observable stream of the response.
181
+ * The request will not be executed until the observable is subscribed to.
182
+ *
183
+ * @template T - The expected response type.
184
+ * @param path - The path to fetch the resource from.
185
+ * @param init - Optional request initialization options, including the request body, headers, and response type.
186
+ * @returns An observable stream of the fetched resource.
187
+ *
188
+ * @see {@link IHttpClient.fetch$}
189
+ */
143
190
  json$<T = unknown>(
144
191
  path: string,
145
192
  init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
146
193
  ): StreamResponse<T>;
147
194
 
195
+ /**
196
+ * Fetches a resource as a promise and returns the response as a JSON object.
197
+ *
198
+ * @template T - The expected response type.
199
+ * @param path - The path to fetch the resource from.
200
+ * @param init - Optional request initialization options, including the request body, headers, and response type.
201
+ * @returns A promise that resolves to the fetched resource as a JSON object.
202
+ *
203
+ * @see {@link IHttpClient.fetch} for fetching resources as an observable stream.
204
+ */
148
205
  json<T = unknown>(
149
206
  path: string,
150
207
  init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
@@ -157,9 +214,9 @@ export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResp
157
214
  ): Promise<T>;
158
215
 
159
216
  /**
160
- * Fetches a blob resource from the specified path.
217
+ * Fetches a blob resource from the specified path and returns a Promise that resolves to the fetched blob data.
161
218
  *
162
- * @param path - The path to the blob resource.
219
+ * @param path - The path to fetch the blob from.
163
220
  * @param args - Optional arguments for the fetch request, including the request body, headers, and response type.
164
221
  * @returns A Promise that resolves to the fetched blob data.
165
222
  */
@@ -181,7 +238,7 @@ export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResp
181
238
  ): StreamResponse<T>;
182
239
 
183
240
  /**
184
- * Abort all ongoing request for current client
241
+ * Abort all ongoing requests for the current client.
185
242
  */
186
243
  abort(): void;
187
244
  }
@@ -4,13 +4,21 @@ import { requestOperatorHeader } from './request-operator-header';
4
4
  import type { FetchRequest } from '../client';
5
5
 
6
6
  /**
7
- * Extends @see {ProcessOperators} for pre-processing requests.
7
+ * Extends the `ProcessOperators` class to handle HTTP requests.
8
+ *
9
+ * This class provides a method to set a header that will apply to all requests made by the `HttpClient`.
10
+ *
11
+ * @see {@link ProcessOperators}
12
+ *
13
+ * @template T - The type of the fetch request, which extends `FetchRequest`.
8
14
  */
9
15
  export class HttpRequestHandler<T extends FetchRequest = FetchRequest> extends ProcessOperators<T> {
10
16
  /**
11
- * Set header that will apply on all requests done by consumer @see {HttpClient}
12
- * @param key - name of header
13
- * @param value - header value
17
+ * Sets a header that will apply to all requests made by the `HttpClient`.
18
+ *
19
+ * @param key - The name of the header to set.
20
+ * @param value - The value of the header to set.
21
+ * @returns The current `HttpRequestHandler` instance, allowing for method chaining.
14
22
  */
15
23
  setHeader(key: string, value: string): HttpRequestHandler<T> {
16
24
  const operator = requestOperatorHeader<T>(key, value);
@@ -1,5 +1,11 @@
1
1
  import { ProcessOperators } from './process-operators';
2
2
 
3
+ /**
4
+ * The `HttpResponseHandler` class extends the `ProcessOperators` class and is responsible for handling HTTP responses.
5
+ * It provides a common interface for processing HTTP responses, allowing for consistent error handling and response transformation.
6
+ *
7
+ * @template T - The type of the HTTP response. Defaults to `Response`.
8
+ */
3
9
  export class HttpResponseHandler<T = Response> extends ProcessOperators<T> {}
4
10
 
5
11
  export default HttpResponseHandler;
@@ -6,15 +6,20 @@ import { IProcessOperators, ProcessOperator } from './types';
6
6
  /**
7
7
  * ProcessOperators class manages a collection of process operators
8
8
  * and provides methods to add, set, get, and process these operators.
9
+ * It implements the IProcessOperators interface for type T.
10
+ *
11
+ * @template T The type of data that the process operators work with
9
12
  */
10
13
  export class ProcessOperators<T> implements IProcessOperators<T> {
11
14
  /**
12
15
  * A record of process operators keyed by a string.
16
+ * This property is used to store and manage the collection of process operators
17
+ * that are used by the `ProcessOperators` class.
13
18
  */
14
19
  protected _operators: Record<string, ProcessOperator<T>>;
15
20
 
16
21
  /**
17
- * Accessor for the operators.
22
+ * Accessor for the collection of process operators.
18
23
  * @returns The record of process operators.
19
24
  */
20
25
  get operators(): Record<string, ProcessOperator<T>> {
@@ -1,6 +1,13 @@
1
1
  import type { FetchRequest } from '../client';
2
2
  import type { ProcessOperator } from './types';
3
3
 
4
+ /**
5
+ * Creates a process operator that adds a header to the request.
6
+ *
7
+ * @param key - The header key to add.
8
+ * @param value - The header value to add.
9
+ * @returns A process operator that adds the specified header to the request.
10
+ */
4
11
  export const requestOperatorHeader =
5
12
  <T extends FetchRequest = FetchRequest>(key: string, value: string): ProcessOperator<T> =>
6
13
  (request) => {
@@ -1,12 +1,18 @@
1
1
  import type { Observable } from 'rxjs';
2
2
  import type { FetchRequest } from '../client';
3
3
 
4
- export type ProcessOperator<T, R = T> = (request: T) => R | void | Promise<R | void>;
5
-
6
4
  /**
7
- * Container for sync/async operators.
8
- * Pipes each operator sequential
5
+ * A process operator that takes a request of type `T` and returns a transformed request of type `R`, or `void`, or a Promise that resolves to `R` or `void`.
6
+ *
7
+ * Process operators are used to transform or modify requests in a sequential pipeline before they are processed by an `IHttpRequestHandler`.
8
+ *
9
+ * @template T The type of the input request.
10
+ * @template R The type of the output request. Defaults to `T` if not specified.
11
+ * @param request The input request to be processed.
12
+ * @returns The transformed request, `void`, or a Promise that resolves to the transformed request or `void`.
9
13
  */
14
+ export type ProcessOperator<T, R = T> = (request: T) => R | void | Promise<R | void>;
15
+
10
16
  /**
11
17
  * Represents a collection of process operators.
12
18
  * @template T The type of the request being processed.
@@ -49,6 +55,12 @@ export interface IProcessOperators<T> {
49
55
  process(request: T): Observable<T>;
50
56
  }
51
57
 
58
+ /**
59
+ * Represents an HTTP request handler that extends the `IProcessOperators` interface.
60
+ * This interface provides methods to manage and process HTTP request operators.
61
+ *
62
+ * @template T - The type of the request being processed. Defaults to `FetchRequest`.
63
+ */
52
64
  export interface IHttpRequestHandler<T extends FetchRequest = FetchRequest>
53
65
  extends IProcessOperators<T> {
54
66
  /**
@@ -59,4 +71,10 @@ export interface IHttpRequestHandler<T extends FetchRequest = FetchRequest>
59
71
  setHeader(key: string, value: string): IHttpRequestHandler<T>;
60
72
  }
61
73
 
74
+ /**
75
+ * Represents an HTTP response handler that extends the `IProcessOperators` interface.
76
+ * This interface provides methods to manage and process HTTP response operators.
77
+ *
78
+ * @template T - The type of the response being processed. Defaults to `Response`.
79
+ */
62
80
  export interface IHttpResponseHandler<T = Response> extends IProcessOperators<T> {}
@@ -1,4 +1,4 @@
1
- import { type BlobResult } from '../client/types';
1
+ import type { ResponseSelector, BlobResult } from '../client/types';
2
2
 
3
3
  /**
4
4
  * Extracts a blob and filename from a successful HTTP response.
@@ -7,7 +7,7 @@ import { type BlobResult } from '../client/types';
7
7
  * @returns A promise that resolves to an object containing the extracted blob and filename.
8
8
  * @throws {Error} If the response is not successful or if there is an error parsing the response.
9
9
  */
10
- export const blobSelector = async <TResponse extends Response = Response>(
10
+ export const blobSelector: ResponseSelector = async <TResponse extends Response = Response>(
11
11
  response: TResponse,
12
12
  ): Promise<BlobResult> => {
13
13
  if (!response.ok) {
@@ -1,2 +1,4 @@
1
1
  export { jsonSelector } from './json-selector';
2
2
  export { blobSelector } from './blob-selector';
3
+
4
+ export type { ResponseSelector } from '../client/types';
@@ -1,3 +1,4 @@
1
+ import type { ResponseSelector } from '../client/types';
1
2
  import { HttpJsonResponseError } from '../../errors';
2
3
 
3
4
  /**
@@ -14,7 +15,10 @@ import { HttpJsonResponseError } from '../../errors';
14
15
  * @param response - The HTTP response to parse.
15
16
  * @returns A promise that resolves with the parsed JSON data, or rejects with an `HttpJsonResponseError`.
16
17
  */
17
- export const jsonSelector = async <TType = unknown, TResponse extends Response = Response>(
18
+ export const jsonSelector: ResponseSelector = async <
19
+ TType = unknown,
20
+ TResponse extends Response = Response,
21
+ >(
18
22
  response: TResponse,
19
23
  ): Promise<TType> => {
20
24
  /** Status code 204 indicates no content in the response */