@equinor/fusion-framework-module-http 5.2.3 → 6.0.1

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 +162 -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 +153 -6
  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 +28 -3
  22. package/dist/esm/lib/selectors/blob-selector.js.map +1 -1
  23. package/dist/esm/lib/selectors/json-selector.js +24 -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 +147 -3
  37. package/dist/types/lib/client/types.d.ts +156 -5
  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 -1
  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 +161 -12
  54. package/src/lib/client/types.ts +149 -71
  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 +26 -4
  61. package/src/lib/selectors/index.ts +2 -0
  62. package/src/lib/selectors/json-selector.ts +29 -9
  63. package/src/module.ts +55 -2
  64. package/src/provider.ts +76 -2
  65. package/src/version.ts +1 -1
@@ -8,15 +8,25 @@ import { blobSelector, jsonSelector } from '../selectors';
8
8
  import type { Observable, ObservableInput } from 'rxjs';
9
9
  import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators';
10
10
  import type {
11
+ BlobResult,
11
12
  FetchRequest,
12
13
  FetchRequestInit,
13
14
  FetchResponse,
14
15
  IHttpClient,
15
16
  JsonRequest,
16
17
  StreamResponse,
17
- } from '.';
18
+ } from './types';
19
+
18
20
  import { HttpResponseError } from '../../errors';
19
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
+ */
20
30
  export type HttpClientCreateOptions<
21
31
  TRequest extends FetchRequest = FetchRequest,
22
32
  TResponse = Response,
@@ -31,22 +41,46 @@ export class HttpClient<
31
41
  TResponse extends FetchResponse = FetchResponse,
32
42
  > implements IHttpClient<TRequest, TResponse>
33
43
  {
34
- 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>;
35
49
 
36
- 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>;
37
55
 
38
- /** 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
+ */
39
60
  protected _request$ = new Subject<TRequest>();
40
61
 
41
- /** 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
+ */
42
66
  protected _response$ = new Subject<TResponse>();
43
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
+ */
44
72
  protected _abort$ = new Subject<void>();
45
73
 
74
+ /**
75
+ * A stream of requests that are about to be executed.
76
+ */
46
77
  public get request$(): Observable<TRequest> {
47
78
  return this._request$.asObservable();
48
79
  }
49
80
 
81
+ /**
82
+ * A stream of responses that have been received.
83
+ */
50
84
  public get response$(): Observable<TResponse> {
51
85
  return this._response$.asObservable();
52
86
  }
@@ -60,11 +94,23 @@ export class HttpClient<
60
94
  this._init();
61
95
  }
62
96
 
63
- /** 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
+ */
64
103
  protected _init(): void {
65
104
  // called by children for constructor setup
66
105
  }
67
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
+ */
68
114
  public fetch$<T = TResponse>(
69
115
  path: string,
70
116
  args?: FetchRequestInit<T, TRequest, TResponse>,
@@ -72,6 +118,13 @@ export class HttpClient<
72
118
  return this._fetch$(path, args);
73
119
  }
74
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
+ */
75
128
  public fetch<T = TResponse>(
76
129
  path: string,
77
130
  args?: FetchRequestInit<T, TRequest, TResponse>,
@@ -87,6 +140,16 @@ export class HttpClient<
87
140
  return this.fetch(path, args);
88
141
  }
89
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
+ */
90
153
  public json$<T = unknown>(
91
154
  path: string,
92
155
  args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
@@ -104,6 +167,16 @@ export class HttpClient<
104
167
  } as FetchRequestInit<T, TRequest, TResponse>);
105
168
  }
106
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
+ */
107
180
  public json<T = unknown>(
108
181
  path: string,
109
182
  args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
@@ -111,18 +184,41 @@ export class HttpClient<
111
184
  return firstValueFrom(this.json$<T>(path, args));
112
185
  }
113
186
 
114
- public blob$(
187
+ /**
188
+ * Fetches a blob resource from the specified path and returns a stream response.
189
+ *
190
+ * @param path - The path to the blob resource.
191
+ * @param args - Optional request initialization options, including a custom selector function.
192
+ * @returns A stream response containing the fetched blob data.
193
+ */
194
+ public blob$<T = BlobResult>(
115
195
  path: string,
116
- args?: FetchRequestInit<Blob, TRequest, TResponse>,
117
- ): StreamResponse<Blob> {
196
+ args?: FetchRequestInit<T, TRequest, TResponse>,
197
+ ): StreamResponse<T> {
198
+ // Get the selector value from the provided args, or use the default blobSelector
118
199
  const selector = args?.selector ?? blobSelector;
119
- return this.fetch$(path, {
200
+
201
+ // Create the FetchRequestInit object with the provided args and the selector
202
+ const init = {
120
203
  ...args,
121
204
  selector,
122
- } as FetchRequestInit<Blob, TRequest, TResponse>);
205
+ } as FetchRequestInit<T, TRequest, TResponse>;
206
+
207
+ // Call the fetch$ method with the provided path and the constructed init object
208
+ return this.fetch$(path, init);
123
209
  }
124
210
 
125
- public blob(path: string, args?: FetchRequestInit<Blob, TRequest, TResponse>): Promise<Blob> {
211
+ /**
212
+ * Fetches a blob from the specified path and returns a Promise that resolves to the blob result.
213
+ *
214
+ * @param path - The path to fetch the blob from.
215
+ * @param args - Optional arguments for the fetch request, including request body, headers, and response type.
216
+ * @returns A Promise that resolves to the blob result.
217
+ */
218
+ public blob<T = BlobResult>(
219
+ path: string,
220
+ args?: FetchRequestInit<T, TRequest, TResponse>,
221
+ ): Promise<T> {
126
222
  return firstValueFrom(this.blob$(path, args));
127
223
  }
128
224
 
@@ -134,6 +230,14 @@ export class HttpClient<
134
230
  return this.json(path, args);
135
231
  }
136
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
+ */
137
241
  public execute<T = TResponse, TMethod extends 'fetch' | 'fetch$' | 'json' | 'json$' = 'fetch'>(
138
242
  method: TMethod,
139
243
  path: string,
@@ -142,10 +246,30 @@ export class HttpClient<
142
246
  return this[method](path, init) as ReturnType<IHttpClient[TMethod]>;
143
247
  }
144
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
+ */
145
254
  public abort(): void {
146
255
  this._abort$.next();
147
256
  }
148
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
+ */
149
273
  protected _fetch$<T = TResponse>(
150
274
  path: string,
151
275
  args?: FetchRequestInit<T, TRequest, TResponse>,
@@ -189,14 +313,39 @@ export class HttpClient<
189
313
  return response$ as unknown as Observable<T>;
190
314
  }
191
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
+ */
192
325
  protected _prepareRequest(init: TRequest): ObservableInput<TRequest> {
193
326
  return this.requestHandler.process(init);
194
327
  }
195
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
+ */
196
338
  protected _prepareResponse(response: TResponse): ObservableInput<TResponse> {
197
339
  return this.responseHandler.process(response);
198
340
  }
199
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
+ */
200
349
  protected _resolveUrl(path: string): string {
201
350
  const baseUrl = this.uri || window.location.origin;
202
351
  return new URL(path, baseUrl).href;
@@ -1,41 +1,109 @@
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
  };
14
40
 
41
+ /**
42
+ * Represents the result of a blob operation, including the filename (if available) and the blob itself.
43
+ * @property {string} [filename] - The filename of the blob, if available.
44
+ * @property {Blob} blob - The blob data.
45
+ */
46
+ export type BlobResult = { filename?: string; blob: Blob };
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
+ */
15
54
  export type FetchResponse<T = unknown> = Response & {
16
55
  json(): Promise<T>;
17
56
  };
18
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
+ */
19
64
  export type FetchRequestInit<
20
65
  TReturn = unknown,
21
66
  TRequest = FetchRequest,
22
67
  TResponse = FetchResponse<TReturn>,
23
68
  > = Omit<TRequest, 'uri' | 'path'> & {
24
- selector?: (response: TResponse) => ObservableInput<TReturn>;
69
+ /** response selector function */
70
+ selector?: ResponseSelector<TReturn, TResponse>;
25
71
  };
26
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
+ */
27
80
  export type ClientRequestInit<T extends IHttpClient, TReturn = unknown> =
28
81
  T extends IHttpClient<infer TRequest, infer TResponse>
29
82
  ? FetchRequestInit<TReturn, TRequest, TResponse>
30
83
  : never;
31
84
 
85
+ /**
86
+ * Represents the available execution methods for an HTTP client.
87
+ */
32
88
  export type ExecutionMethod = 'fetch' | 'fetch$' | 'json' | 'json$';
33
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
+ */
34
96
  export type ExecutionMethodParameters<
35
97
  TMethod extends ExecutionMethod = 'fetch',
36
98
  TClient extends IHttpClient = IHttpClient,
37
99
  > = Parameters<TClient[TMethod]>;
38
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
+ */
39
107
  export type ExecutionResponse<
40
108
  TMethod extends ExecutionMethod = 'fetch',
41
109
  TClient extends IHttpClient = IHttpClient,
@@ -47,50 +115,42 @@ export type ExecutionResponse<
47
115
  */
48
116
  export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = Response> {
49
117
  uri: string;
50
- /** 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
+ */
51
122
  readonly requestHandler: IHttpRequestHandler<TRequest>;
52
- /** 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
+ */
53
128
  readonly responseHandler: IHttpResponseHandler<TResponse>;
54
129
 
55
- /** Observable stream of request */
56
- 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>;
57
135
 
58
- /** Observable stream of responses */
59
- 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>;
60
141
 
61
142
  /**
62
- * Observable request.
63
- * Simplifies execution of request and
64
- * 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.
65
151
  *
66
- * @see {@link https://rxjs.dev/api/fetch/fromFetch|RXJS}
152
+ * @see {@link https://rxjs.dev/api/fetch/fromFetch|RxJS fromFetch}
67
153
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch|fetch}
68
- * @example
69
- * ```ts
70
- * // Observer changes of a input field
71
- * const client = modules.http.createClient('my-client');
72
- * const input$ = fromEvent(document.getElementById('input'), 'input');
73
- * input$.pipe(
74
- * // only call after no key input in .5s
75
- * debounceTime(500),
76
- * // extract value from event
77
- * map(x => x.currentTarget.value),
78
- * // only search when text longer than 2 characters
79
- * filter(x => x.length >=3),
80
- * // query api with input value
81
- * switchMap(x => client.fetch(`api/foo?q=${x}`).pipe(
82
- * // retry 2 times
83
- * retry(2)
84
- * // cancel request if new input
85
- * takeUntil(input$)
86
- * )),
87
- * // extract data from response
88
- * switchMap(x => x.json()),
89
- * // process error
90
- * catchError(x => of({error: e.message}))
91
- * // write result to pre element
92
- * ).subscribe(console.log);
93
- * ```
94
154
  */
95
155
  fetch$<T = TResponse>(
96
156
  path: string,
@@ -98,46 +158,50 @@ export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResp
98
158
  ): StreamResponse<T>;
99
159
 
100
160
  /**
101
- * Fetch a resource as an promise
102
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch|fetch}
103
- * @example
104
- * ```ts
105
- * let controller: AbortController;
106
- * const client = window.Fusion.createClient('my-client');
107
- * const input = document.getElementById('input');
108
- * input.addEventlistener('input', (e) => {
109
- * try{
110
- * // if a controller is defined, request might be ongoing
111
- * controller && controller.abort();
112
- * // create a new abort controller
113
- * controller = new AbortController();
114
- * // query api with
115
- * const response = await client.fetch({
116
- * path: `api/foo?q=${e.currentTarget.value}`,
117
- * signal: controller.signal,
118
- * });
119
- * const json = await response.json();
120
- * result.innerText = JSON.stringify(json, null, 2)
121
- * } catch(err){
122
- * result.innerText = 'an error occurred'
123
- * } finally{
124
- * delete controller;
125
- * }
126
- * });
127
- * ```
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$}
128
169
  */
129
170
  fetch<T = TResponse>(path: string, init?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
130
171
 
172
+ /** @deprecated use {@link IHttpClient.fetch} */
131
173
  fetchAsync<T = TResponse>(
132
174
  path: string,
133
175
  args?: FetchRequestInit<T, TRequest, TResponse>,
134
176
  ): Promise<T>;
135
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
+ */
136
190
  json$<T = unknown>(
137
191
  path: string,
138
192
  init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
139
193
  ): StreamResponse<T>;
140
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
+ */
141
205
  json<T = unknown>(
142
206
  path: string,
143
207
  init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
@@ -149,18 +213,32 @@ export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResp
149
213
  args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
150
214
  ): Promise<T>;
151
215
 
152
- blob(
216
+ /**
217
+ * Fetches a blob resource from the specified path and returns a Promise that resolves to the fetched blob data.
218
+ *
219
+ * @param path - The path to fetch the blob from.
220
+ * @param args - Optional arguments for the fetch request, including the request body, headers, and response type.
221
+ * @returns A Promise that resolves to the fetched blob data.
222
+ */
223
+ blob<T = BlobResult>(
153
224
  path: string,
154
- args?: FetchRequestInit<Blob, JsonRequest<TRequest>, TResponse>,
155
- ): Promise<Blob>;
225
+ args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
226
+ ): Promise<T>;
156
227
 
157
- blob$(
228
+ /**
229
+ * Fetches a blob from the specified path and returns a stream response.
230
+ *
231
+ * @param path - The path to fetch the blob from.
232
+ * @param args - Optional arguments for the fetch request, including the request body, headers, and response type.
233
+ * @returns A stream response containing the fetched blob.
234
+ */
235
+ blob$<T = BlobResult>(
158
236
  path: string,
159
- args?: FetchRequestInit<Blob, JsonRequest<TRequest>, TResponse>,
160
- ): StreamResponse<Blob>;
237
+ args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
238
+ ): StreamResponse<T>;
161
239
 
162
240
  /**
163
- * Abort all ongoing request for current client
241
+ * Abort all ongoing requests for the current client.
164
242
  */
165
243
  abort(): void;
166
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) => {