@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
@@ -1,37 +1,113 @@
1
1
  import { HttpRequestHandler } from './lib/operators';
2
2
  import type { FetchRequest, IHttpClient } from './lib/client';
3
3
  import type { IHttpRequestHandler, IHttpResponseHandler } from './lib/operators';
4
+ /**
5
+ * Represents the options for constructing an `IHttpClient` instance.
6
+ *
7
+ * @template TInit - The type of the initial request object used by the `IHttpClient` instance.
8
+ * @property {IHttpRequestHandler<TInit>} requestHandler - The request handler to be used by the `IHttpClient` instance.
9
+ */
4
10
  interface HttpClientConstructorOptions<TInit extends FetchRequest> {
5
11
  requestHandler: IHttpRequestHandler<TInit>;
6
12
  }
13
+ /**
14
+ * Represents a constructor for an `IHttpClient` instance.
15
+ *
16
+ * @template TClient - The type of the `IHttpClient` instance to be constructed.
17
+ * @param uri - The base URI for the `IHttpClient` instance.
18
+ * @param options - The options for constructing the `IHttpClient` instance, including the request handler.
19
+ * @returns A new instance of the `TClient` type.
20
+ */
7
21
  interface HttpClientConstructor<TClient extends IHttpClient> {
8
22
  new (uri: string, options: HttpClientConstructorOptions<HttpClientRequestInitType<TClient>>): TClient;
9
23
  }
24
+ /**
25
+ * Represents the options for configuring an `IHttpClient` instance.
26
+ *
27
+ * @template TClient - The type of the `IHttpClient` instance to be configured.
28
+ */
10
29
  export interface HttpClientOptions<TClient extends IHttpClient = IHttpClient> {
30
+ /** The base URI for the `IHttpClient` instance. */
11
31
  baseUri?: string;
32
+ /** The default scopes to be used by the `IHttpClient` instance. */
12
33
  defaultScopes?: string[];
34
+ /** The constructor for the `TClient` type. */
13
35
  ctor?: HttpClientConstructor<TClient>;
36
+ /** A callback function that is called when a new `TClient` instance is created. */
14
37
  onCreate?: (client: TClient) => void;
38
+ /** The request handler to be used by the `IHttpClient` instance. */
15
39
  requestHandler?: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
40
+ /** The response handler to be used by the `IHttpClient` instance. */
16
41
  responseHandler?: IHttpResponseHandler<HttpClientRequestInitType<TClient>>;
17
42
  }
43
+ /**
44
+ * Utility type that extracts the request init type from an `IHttpClient` implementation.
45
+ * This is useful for ensuring type safety when configuring an `IHttpClient` instance.
46
+ *
47
+ * @template T - The type of the `IHttpClient` implementation.
48
+ * @returns The request init type for the `IHttpClient` implementation.
49
+ */
18
50
  export type HttpClientRequestInitType<T extends IHttpClient> = T extends IHttpClient<infer U> ? U : never;
51
+ /**
52
+ * Instance for configuring http client
53
+ * @template TClient base type of client that the provider will create
54
+ */
19
55
  export interface IHttpClientConfigurator<TClient extends IHttpClient = IHttpClient> {
20
56
  readonly clients: Record<string, HttpClientOptions<TClient>>;
21
57
  readonly defaultHttpClientCtor: HttpClientConstructor<TClient>;
22
58
  readonly defaultHttpRequestHandler: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
59
+ /**
60
+ * Configure a client with arguments
61
+ * @param name name of the client
62
+ * @param args option that are used cor creating a client
63
+ * @example
64
+ * ```ts
65
+ * configurator.http.configureClient('foo',{
66
+ * baseUri: 'https://foo.bar',
67
+ * defaultScopes: ['foobar/.default']
68
+ * });
69
+ * ```
70
+ */
23
71
  configureClient<T extends TClient>(name: string, args: HttpClientOptions<T>): IHttpClientConfigurator<TClient>;
72
+ /**
73
+ * Configure a simple client by name to an endpoint
74
+ * @param name name of the client
75
+ * @param uri base endpoint for the client
76
+ */
24
77
  configureClient(name: string, uri: string): IHttpClientConfigurator<TClient>;
78
+ /**
79
+ * Creates a client with callback configuration
80
+ * @param name name of the client
81
+ * @param onCreate callback when a client is created
82
+ * ```ts
83
+ * configurator.http.configureClient('foo',(client) => {
84
+ * client.requestHandler.add('logger', (request) => console.log(request));
85
+ * });
86
+ * ```
87
+ */
25
88
  configureClient<T extends TClient>(name: string, onCreate: (client: T) => void): HttpClientConfigurator<TClient>;
89
+ /**
90
+ * Check if there is a configuration for provided name
91
+ */
26
92
  hasClient(name: string): boolean;
27
93
  }
94
+ /** @inheritdoc */
28
95
  export declare class HttpClientConfigurator<TClient extends IHttpClient> implements IHttpClientConfigurator<TClient> {
29
96
  protected _clients: Record<string, HttpClientOptions<TClient>>;
97
+ /** Get a clone of all configured clients */
30
98
  get clients(): Record<string, HttpClientOptions<TClient>>;
99
+ /** default class for creation of http clients */
31
100
  readonly defaultHttpClientCtor: HttpClientConstructor<TClient>;
101
+ /** default request handler for http clients, applied on creation */
32
102
  readonly defaultHttpRequestHandler: HttpRequestHandler<HttpClientRequestInitType<TClient>>;
103
+ /**
104
+ * Create a instance of http configuration
105
+ * @param client defaultHttpRequestHandler
106
+ */
33
107
  constructor(client: HttpClientConstructor<TClient>);
108
+ /** @inheritdoc */
34
109
  hasClient(name: string): boolean;
110
+ /** @inheritdoc */
35
111
  configureClient<T extends TClient>(name: string, args: string | HttpClientOptions<T> | HttpClientOptions<T>['onCreate']): HttpClientConfigurator<TClient>;
36
112
  }
37
113
  export default HttpClientConfigurator;
@@ -1,11 +1,29 @@
1
+ /**
2
+ * Represents an error that occurs when handling an HTTP response.
3
+ * @template TResponse The type of the HTTP response.
4
+ */
1
5
  export declare class HttpResponseError<TResponse = Response> extends Error {
2
6
  readonly response: TResponse;
3
7
  static Name: string;
4
8
  constructor(message: string, response: TResponse, options?: ErrorOptions);
5
9
  }
10
+ /**
11
+ * Represents an error that occurs when handling a JSON response in an HTTP request.
12
+ * Extends the base `HttpResponseError` class.
13
+ *
14
+ * @template TType - The type of the data associated with the error.
15
+ * @template TResponse - The type of the HTTP response.
16
+ */
6
17
  export declare class HttpJsonResponseError<TType = unknown, TResponse = Response> extends HttpResponseError<TResponse> {
7
18
  static Name: string;
8
19
  readonly data?: TType;
20
+ /**
21
+ * Creates a new instance of `HttpJsonResponseError`.
22
+ *
23
+ * @param message - The error message.
24
+ * @param response - The HTTP response associated with the error.
25
+ * @param options - Additional options for the error, including the associated data.
26
+ */
9
27
  constructor(message: string, response: TResponse, options?: ErrorOptions & {
10
28
  data?: TType;
11
29
  });
@@ -1,3 +1,7 @@
1
+ /**
2
+ * [[include:module-http/README.MD]]
3
+ * @module
4
+ */
1
5
  export * from './configurator';
2
6
  export * from './provider';
3
7
  export * from './module';
@@ -1,12 +1,55 @@
1
1
  import type { Observable } from 'rxjs';
2
2
  import type { FetchRequestInit, FetchRequest, FetchResponse } from '.';
3
3
  import { HttpClient } from './client';
4
+ /**
5
+ * Extends the `FetchRequest` type with an optional `scopes` property, which is an array of strings representing the scopes to be used for the request.
6
+ * This type is used to represent a request that requires authentication using the MSAL (Microsoft Authentication Library) library.
7
+ */
4
8
  type MsalFetchRequest = FetchRequest & {
5
9
  scopes?: string[];
6
10
  };
11
+ /**
12
+ * Extends the `FetchRequestInit` type with an optional `scopes` property, which is an array of strings representing the scopes to be used for the request.
13
+ * This type is used to represent a request that requires authentication using the MSAL (Microsoft Authentication Library) library.
14
+ *
15
+ * @template TReturn - The type of the response object. Defaults to `unknown`.
16
+ * @template TRequest - The type of the request object. Defaults to `FetchRequest`.
17
+ * @template TResponse - The type of the response object. Defaults to `Response`.
18
+ */
7
19
  type MsalFetchRequestInit<TReturn = unknown, TRequest = FetchRequest, TResponse = FetchResponse> = FetchRequestInit<TReturn, TRequest, TResponse> & Pick<MsalFetchRequest, 'scopes'>;
20
+ /**
21
+ * Extends the `HttpClient` class to provide MSAL (Microsoft Authentication Library) authentication support.
22
+ *
23
+ * The `HttpClientMsal` class is responsible for handling requests that require authentication using the MSAL library.
24
+ * It extends the `HttpClient` class and adds the following functionality:
25
+ *
26
+ * - `defaultScopes`: An array of strings representing the default scopes to be used for all requests, unless overridden in the request object.
27
+ * - `fetch$`: Overrides the `fetch$` method of the `HttpClient` class to add MSAL authentication support. It takes an optional `MsalFetchRequestInit` object, which can include the `scopes` property to specify the scopes to be used for the request.
28
+ * - If `scopes` is provided in the `MsalFetchRequestInit` object, it will be used in addition to the `defaultScopes`.
29
+ * - If `scopes` is not provided, only the `defaultScopes` will be used.
30
+ *
31
+ * @template TRequest - The type of the request object. Defaults to `MsalFetchRequest`.
32
+ * @template TResponse - The type of the response object. Defaults to `FetchResponse`.
33
+ */
8
34
  export declare class HttpClientMsal<TRequest extends MsalFetchRequest = MsalFetchRequest, TResponse extends FetchResponse = FetchResponse> extends HttpClient<TRequest, TResponse> {
35
+ /**
36
+ * An array of default scopes to be used for all requests, unless overridden in the request object.
37
+ * This property is used by the `HttpClientMsal` class to add MSAL authentication support to requests.
38
+ */
9
39
  defaultScopes: string[];
40
+ /**
41
+ * Fetches a resource from the specified path, with optional MSAL authentication scopes.
42
+ *
43
+ * This method extends the `HttpClient.fetch$` method to add support for MSAL authentication.
44
+ * If `init.scopes` is provided, it will be used in addition to the `defaultScopes` defined in the `HttpClientMsal` class.
45
+ * If `init.scopes` is not provided, only the `defaultScopes` will be used for the request.
46
+ *
47
+ * @overrides HttpClient.fetch$
48
+ *
49
+ * @param path - The path to the resource to fetch.
50
+ * @param init - An optional `MsalFetchRequestInit` object that can include the `scopes` property.
51
+ * @returns An `Observable` that emits the fetched resource.
52
+ */
10
53
  fetch$<T = TResponse>(path: string, init?: MsalFetchRequestInit<T, TRequest, TResponse>): Observable<T>;
11
54
  }
12
55
  export default HttpClientMsal;
@@ -1,34 +1,178 @@
1
1
  import { Subject } from 'rxjs';
2
2
  import type { Observable, ObservableInput } from 'rxjs';
3
3
  import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators';
4
- import type { FetchRequest, FetchRequestInit, FetchResponse, IHttpClient, JsonRequest, StreamResponse } from '.';
4
+ import type { BlobResult, FetchRequest, FetchRequestInit, FetchResponse, IHttpClient, JsonRequest, StreamResponse } from './types';
5
+ /**
6
+ * Configuration options for creating an `HttpClient` instance.
7
+ *
8
+ * @template TRequest - The type of the request object. Defaults to `FetchRequest`.
9
+ * @template TResponse - The type of the response object. Defaults to `Response`.
10
+ * @property {IHttpRequestHandler<TRequest>} requestHandler - A handler for customizing the request before it is sent.
11
+ * @property {IHttpResponseHandler<TResponse>} responseHandler - A handler for customizing the response after it is received.
12
+ */
5
13
  export type HttpClientCreateOptions<TRequest extends FetchRequest = FetchRequest, TResponse = Response> = {
6
14
  requestHandler: IHttpRequestHandler<TRequest>;
7
15
  responseHandler: IHttpResponseHandler<TResponse>;
8
16
  };
17
+ /** Base http client for executing requests */
9
18
  export declare class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse extends FetchResponse = FetchResponse> implements IHttpClient<TRequest, TResponse> {
10
19
  uri: string;
20
+ /**
21
+ * A request handler that can be used to customize the request before it is sent.
22
+ * This property is part of the `HttpClientCreateOptions` configuration object used to create an `HttpClient` instance.
23
+ */
11
24
  readonly requestHandler: IHttpRequestHandler<TRequest>;
25
+ /**
26
+ * A handler for customizing the response after it is received.
27
+ * This property is part of the `HttpClientCreateOptions` configuration object used to create an `HttpClient` instance.
28
+ */
12
29
  readonly responseHandler: IHttpResponseHandler<TResponse>;
30
+ /**
31
+ * A stream of requests that are about to be executed.
32
+ * This property is used internally by the `HttpClient` class to manage the lifecycle of requests.
33
+ */
13
34
  protected _request$: Subject<TRequest>;
35
+ /**
36
+ * A stream of responses that have been received.
37
+ * This property is used internally by the `HttpClient` class to manage the lifecycle of responses.
38
+ */
14
39
  protected _response$: Subject<TResponse>;
40
+ /**
41
+ * A stream that is used to signal the abortion of requests.
42
+ * This property is used internally by the `HttpClient` class to manage the lifecycle of requests.
43
+ */
15
44
  protected _abort$: Subject<void>;
45
+ /**
46
+ * A stream of requests that are about to be executed.
47
+ */
16
48
  get request$(): Observable<TRequest>;
49
+ /**
50
+ * A stream of responses that have been received.
51
+ */
17
52
  get response$(): Observable<TResponse>;
18
53
  constructor(uri: string, options?: Partial<HttpClientCreateOptions<TRequest, TResponse>>);
54
+ /**
55
+ * Internal method called by child classes to perform initialization logic in the constructor.
56
+ * This method is intended to be overridden by child classes to add their own initialization logic.
57
+ * @protected
58
+ * @virtual
59
+ */
19
60
  protected _init(): void;
61
+ /**
62
+ * Fetches data from the specified path and returns a stream response.
63
+ *
64
+ * @param path - The path to fetch data from.
65
+ * @param args - Optional request initialization options, including a custom selector function.
66
+ * @returns A stream response containing the fetched data.
67
+ */
20
68
  fetch$<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): StreamResponse<T>;
69
+ /**
70
+ * Fetches data from the specified path and returns a Promise containing the fetched data.
71
+ *
72
+ * @param path - The path to fetch data from.
73
+ * @param args - Optional request initialization options, including a custom selector function.
74
+ * @returns A Promise containing the fetched data.
75
+ */
21
76
  fetch<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
77
+ /** @deprecated */
22
78
  fetchAsync<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
79
+ /**
80
+ * Fetches data from the specified path and returns a stream response containing the data in JSON format.
81
+ *
82
+ * @param path - The path to fetch the data from.
83
+ * @param args - Optional request initialization options, including a custom selector function and request body.
84
+ * - `body`: The request body, which will be automatically serialized to JSON if it's an object.
85
+ * - `selector`: A custom selector function to transform the response data. If not provided, the `jsonSelector` function will be used.
86
+ * - `headers`: Additional headers to include in the request. The `Accept` and `Content-Type` headers will be automatically set to `application/json`.
87
+ * @returns A stream response containing the fetched data in JSON format.
88
+ */
23
89
  json$<T = unknown>(path: string, args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): StreamResponse<T>;
90
+ /**
91
+ * Fetches data from the specified path and returns a Promise containing the fetched data in JSON format.
92
+ *
93
+ * @param path - The path to fetch the data from.
94
+ * @param args - Optional request initialization options, including a custom selector function and request body.
95
+ * - `body`: The request body, which will be automatically serialized to JSON if it's an object.
96
+ * - `selector`: A custom selector function to transform the response data. If not provided, the `jsonSelector` function will be used.
97
+ * - `headers`: Additional headers to include in the request. The `Accept` and `Content-Type` headers will be automatically set to `application/json`.
98
+ * @returns A Promise containing the fetched data in JSON format.
99
+ */
24
100
  json<T = unknown>(path: string, args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): Promise<T>;
25
- blob$(path: string, args?: FetchRequestInit<Blob, TRequest, TResponse>): StreamResponse<Blob>;
26
- blob(path: string, args?: FetchRequestInit<Blob, TRequest, TResponse>): Promise<Blob>;
101
+ /**
102
+ * Fetches a blob resource from the specified path and returns a stream response.
103
+ *
104
+ * @param path - The path to the blob resource.
105
+ * @param args - Optional request initialization options, including a custom selector function.
106
+ * @returns A stream response containing the fetched blob data.
107
+ */
108
+ blob$<T = BlobResult>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): StreamResponse<T>;
109
+ /**
110
+ * Fetches a blob from the specified path and returns a Promise that resolves to the blob result.
111
+ *
112
+ * @param path - The path to fetch the blob from.
113
+ * @param args - Optional arguments for the fetch request, including request body, headers, and response type.
114
+ * @returns A Promise that resolves to the blob result.
115
+ */
116
+ blob<T = BlobResult>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
117
+ /** @deprecated */
27
118
  jsonAsync<T = unknown>(path: string, args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): Promise<T>;
119
+ /**
120
+ * Executes an HTTP request using the specified method and path.
121
+ *
122
+ * @param method - The HTTP method to use for the request, such as 'fetch', 'json', or 'blob'.
123
+ * @param path - The path to the resource to fetch.
124
+ * @param init - Optional request initialization options, including request body, headers, and response type.
125
+ * @returns The result of the HTTP request, which will be of the same type as the return value of the specified method.
126
+ */
28
127
  execute<T = TResponse, TMethod extends 'fetch' | 'fetch$' | 'json' | 'json$' = 'fetch'>(method: TMethod, path: string, init?: FetchRequestInit<T, TRequest, TResponse>): ReturnType<IHttpClient[TMethod]>;
128
+ /**
129
+ * Aborts any ongoing HTTP requests made by this `IHttpClient` instance.
130
+ * This will trigger the `takeUntil` operator in the `_fetch$` method,
131
+ * causing any in-flight requests to be cancelled.
132
+ */
29
133
  abort(): void;
134
+ /**
135
+ * Fetches data from the specified path and returns an Observable that emits the response.
136
+ *
137
+ * @param path - The path to fetch the data from.
138
+ * @param args - Optional arguments for the fetch request, including a response selector function, request body, headers, and response type.
139
+ * @returns {Observable<T>} An Observable that emits the response data.
140
+ *
141
+ * This method handles the following steps:
142
+ * 1. Resolves the full URL by combining the base URI and the provided path.
143
+ * 2. Prepares the request by passing it through the `requestHandler.process()` method.
144
+ * 3. Executes the fetch request using the prepared request.
145
+ * 4. Prepares the response by passing it through the `responseHandler.process()` method.
146
+ * 5. Applies the optional response selector function to transform the response data.
147
+ * 6. Cancels the request if the `_abort$` observable emits.
148
+ */
30
149
  protected _fetch$<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Observable<T>;
150
+ /**
151
+ * Prepares the request by passing it through the `requestHandler.process()` method.
152
+ * This method is an implementation detail of the `_fetch$()` method, and is not part of the public API.
153
+ * It takes a `TRequest` object as input, which represents the request data, and returns an `ObservableInput<TRequest>`,
154
+ * which can be used to further process the request before it is executed.
155
+ *
156
+ * @param init The request data to be processed.
157
+ * @returns An `ObservableInput<TRequest>` that represents the processed request.
158
+ */
31
159
  protected _prepareRequest(init: TRequest): ObservableInput<TRequest>;
160
+ /**
161
+ * Prepares the response by passing it through the `responseHandler.process()` method.
162
+ * This method is an implementation detail of the `_fetch$()` method, and is not part of the public API.
163
+ * It takes a `TResponse` object as input, which represents the response data, and returns an `ObservableInput<TResponse>`,
164
+ * which can be used to further process the response before it is returned.
165
+ *
166
+ * @param response The response data to be processed.
167
+ * @returns An `ObservableInput<TResponse>` that represents the processed response.
168
+ */
32
169
  protected _prepareResponse(response: TResponse): ObservableInput<TResponse>;
170
+ /**
171
+ * Resolves the full URL for a given path by combining it with the base URL.
172
+ * This is a protected method and is an implementation detail of the `HttpClient` class.
173
+ *
174
+ * @param path - The path to be resolved.
175
+ * @returns The full URL for the given path.
176
+ */
33
177
  protected _resolveUrl(path: string): string;
34
178
  }
@@ -1,36 +1,187 @@
1
1
  import type { ObservableInput, Observable } from 'rxjs';
2
2
  import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators/types';
3
+ /**
4
+ * Represents a stream of response data.
5
+ * @template T - The type of the response data.
6
+ */
3
7
  export type StreamResponse<T> = Observable<T>;
8
+ /**
9
+ * A function that takes a `Response` object and returns an `ObservableInput` of type `T`.
10
+ *
11
+ * @template TResult - The type of the data contained in the response.
12
+ * @template TResponse - The type of the response object.
13
+ * @param response - The `Response` object to be processed.
14
+ * @returns An `ObservableInput` of type `T`.
15
+ */
16
+ export type ResponseSelector<TResult = unknown, TResponse = Response> = (response: TResponse) => ObservableInput<TResult>;
17
+ /**
18
+ * Represents the parameters for a fetch request, including the URI and path.
19
+ * @property {string} uri - The URI of the request.
20
+ * @property {string} path - The path of the request.
21
+ */
4
22
  export type FetchRequest = RequestInit & {
5
23
  uri: string;
6
24
  path: string;
7
25
  };
26
+ /**
27
+ * Represents a request with a JSON body.
28
+ * @template TRequest - The base request type, which extends `FetchRequest`.
29
+ * @property {object | string | null} [body] - The request body, which can be an object, a string, or null.
30
+ */
8
31
  export type JsonRequest<TRequest extends FetchRequest = FetchRequest> = Omit<TRequest, 'body'> & {
9
32
  body?: object | string | null;
10
33
  };
34
+ /**
35
+ * Represents the result of a blob operation, including the filename (if available) and the blob itself.
36
+ * @property {string} [filename] - The filename of the blob, if available.
37
+ * @property {Blob} blob - The blob data.
38
+ */
39
+ export type BlobResult = {
40
+ filename?: string;
41
+ blob: Blob;
42
+ };
43
+ /**
44
+ * Represents the response from a fetch request, including the response object and a JSON parsing method.
45
+ * @template T - The type of the response data.
46
+ * @property {Response} - The original Response object.
47
+ * @property {() => Promise<T>} json - A method to parse the response body as JSON and return the data as type T.
48
+ */
11
49
  export type FetchResponse<T = unknown> = Response & {
12
50
  json(): Promise<T>;
13
51
  };
52
+ /**
53
+ * Represents the parameters for a fetch request, including the URI and path, as well as a selector function to transform the response.
54
+ * @template TReturn - The type of the transformed response data.
55
+ * @template TRequest - The type of the fetch request.
56
+ * @template TResponse - The type of the fetch response.
57
+ */
14
58
  export type FetchRequestInit<TReturn = unknown, TRequest = FetchRequest, TResponse = FetchResponse<TReturn>> = Omit<TRequest, 'uri' | 'path'> & {
15
- selector?: (response: TResponse) => ObservableInput<TReturn>;
59
+ /** response selector function */
60
+ selector?: ResponseSelector<TReturn, TResponse>;
16
61
  };
62
+ /**
63
+ * Represents the parameters for a fetch request, including the URI and path, as well as a selector function to transform the response.
64
+ *
65
+ * @template TReturn - The type of the transformed response data.
66
+ * @template TRequest - The type of the fetch request.
67
+ * @template TResponse - The type of the fetch response.
68
+ */
17
69
  export type ClientRequestInit<T extends IHttpClient, TReturn = unknown> = T extends IHttpClient<infer TRequest, infer TResponse> ? FetchRequestInit<TReturn, TRequest, TResponse> : never;
70
+ /**
71
+ * Represents the available execution methods for an HTTP client.
72
+ */
18
73
  export type ExecutionMethod = 'fetch' | 'fetch$' | 'json' | 'json$';
74
+ /**
75
+ * Represents the type of the parameters for the execution methods of an `IHttpClient` instance.
76
+ *
77
+ * @template TMethod - The execution method of the `IHttpClient` instance, e.g. 'fetch', 'json'.
78
+ * @template TClient - The type of the `IHttpClient` instance.
79
+ */
19
80
  export type ExecutionMethodParameters<TMethod extends ExecutionMethod = 'fetch', TClient extends IHttpClient = IHttpClient> = Parameters<TClient[TMethod]>;
81
+ /**
82
+ * Represents the type of the return value for the execution methods of an `IHttpClient` instance.
83
+ *
84
+ * @template TMethod - The execution method of the `IHttpClient` instance, e.g. 'fetch', 'json'.
85
+ * @template TClient - The type of the `IHttpClient` instance.
86
+ */
20
87
  export type ExecutionResponse<TMethod extends ExecutionMethod = 'fetch', TClient extends IHttpClient = IHttpClient> = ReturnType<TClient[TMethod]>;
88
+ /**
89
+ * @template TRequest request arguments @see {@link https://developer.mozilla.org/en-US/docs/Web/API/request|request}
90
+ * @template TResponse request arguments @see {@link https://developer.mozilla.org/en-US/docs/Web/API/response|response}
91
+ */
21
92
  export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = Response> {
22
93
  uri: string;
94
+ /**
95
+ * A pre-processor for requests made by the `IHttpClient` interface.
96
+ * This handler can be used to modify the request before it is sent, such as adding headers, authentication, or other transformations.
97
+ */
23
98
  readonly requestHandler: IHttpRequestHandler<TRequest>;
99
+ /**
100
+ * A post-processor for responses received by the `IHttpClient` interface.
101
+ * This handler can be used to transform the response data after it is received, such as parsing JSON, handling errors, or other transformations.
102
+ */
24
103
  readonly responseHandler: IHttpResponseHandler<TResponse>;
25
- request$: Observable<TRequest>;
26
- response$: Observable<TResponse>;
104
+ /**
105
+ * Observable stream of requests made by the `IHttpClient` interface.
106
+ * This stream can be used to observe and potentially modify the requests before they are sent.
107
+ */
108
+ readonly request$: Observable<TRequest>;
109
+ /**
110
+ * Observable stream of responses received by the `IHttpClient` interface.
111
+ * This stream can be used to observe and potentially handle the responses after they are received.
112
+ */
113
+ readonly response$: Observable<TResponse>;
114
+ /**
115
+ * Fetch a resource as an observable stream.
116
+ * This method simplifies the execution of a request and returns an observable stream of the response.
117
+ * The request will not be executed until the observable is subscribed to.
118
+ *
119
+ * @template T - The expected response type.
120
+ * @param path - The path to fetch the resource from.
121
+ * @param init - Optional request initialization options.
122
+ * @returns An observable stream of the response.
123
+ *
124
+ * @see {@link https://rxjs.dev/api/fetch/fromFetch|RxJS fromFetch}
125
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch|fetch}
126
+ */
27
127
  fetch$<T = TResponse>(path: string, init?: FetchRequestInit<T, TRequest, TResponse>): StreamResponse<T>;
128
+ /**
129
+ * Fetch a resource as a promise.
130
+ *
131
+ * @template T - The expected response type.
132
+ * @param path - The path to fetch the resource from.
133
+ * @param init - Optional request initialization options.
134
+ * @returns A promise that resolves to the fetched resource.
135
+ *
136
+ * @see {@link IHttpClient.fetch$}
137
+ */
28
138
  fetch<T = TResponse>(path: string, init?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
139
+ /** @deprecated use {@link IHttpClient.fetch} */
29
140
  fetchAsync<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>;
141
+ /**
142
+ * Fetches a resource as an observable stream.
143
+ * This method simplifies the execution of a request and returns an observable stream of the response.
144
+ * The request will not be executed until the observable is subscribed to.
145
+ *
146
+ * @template T - The expected response type.
147
+ * @param path - The path to fetch the resource from.
148
+ * @param init - Optional request initialization options, including the request body, headers, and response type.
149
+ * @returns An observable stream of the fetched resource.
150
+ *
151
+ * @see {@link IHttpClient.fetch$}
152
+ */
30
153
  json$<T = unknown>(path: string, init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): StreamResponse<T>;
154
+ /**
155
+ * Fetches a resource as a promise and returns the response as a JSON object.
156
+ *
157
+ * @template T - The expected response type.
158
+ * @param path - The path to fetch the resource from.
159
+ * @param init - Optional request initialization options, including the request body, headers, and response type.
160
+ * @returns A promise that resolves to the fetched resource as a JSON object.
161
+ *
162
+ * @see {@link IHttpClient.fetch} for fetching resources as an observable stream.
163
+ */
31
164
  json<T = unknown>(path: string, init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): Promise<T>;
165
+ /** @deprecated */
32
166
  jsonAsync<T = unknown>(path: string, args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): Promise<T>;
33
- blob(path: string, args?: FetchRequestInit<Blob, JsonRequest<TRequest>, TResponse>): Promise<Blob>;
34
- blob$(path: string, args?: FetchRequestInit<Blob, JsonRequest<TRequest>, TResponse>): StreamResponse<Blob>;
167
+ /**
168
+ * Fetches a blob resource from the specified path and returns a Promise that resolves to the fetched blob data.
169
+ *
170
+ * @param path - The path to fetch the blob from.
171
+ * @param args - Optional arguments for the fetch request, including the request body, headers, and response type.
172
+ * @returns A Promise that resolves to the fetched blob data.
173
+ */
174
+ blob<T = BlobResult>(path: string, args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): Promise<T>;
175
+ /**
176
+ * Fetches a blob from the specified path and returns a stream response.
177
+ *
178
+ * @param path - The path to fetch the blob from.
179
+ * @param args - Optional arguments for the fetch request, including the request body, headers, and response type.
180
+ * @returns A stream response containing the fetched blob.
181
+ */
182
+ blob$<T = BlobResult>(path: string, args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>): StreamResponse<T>;
183
+ /**
184
+ * Abort all ongoing requests for the current client.
185
+ */
35
186
  abort(): void;
36
187
  }
@@ -1,6 +1,22 @@
1
1
  import { ProcessOperators } from './process-operators';
2
2
  import type { FetchRequest } from '../client';
3
+ /**
4
+ * Extends the `ProcessOperators` class to handle HTTP requests.
5
+ *
6
+ * This class provides a method to set a header that will apply to all requests made by the `HttpClient`.
7
+ *
8
+ * @see {@link ProcessOperators}
9
+ *
10
+ * @template T - The type of the fetch request, which extends `FetchRequest`.
11
+ */
3
12
  export declare class HttpRequestHandler<T extends FetchRequest = FetchRequest> extends ProcessOperators<T> {
13
+ /**
14
+ * Sets a header that will apply to all requests made by the `HttpClient`.
15
+ *
16
+ * @param key - The name of the header to set.
17
+ * @param value - The value of the header to set.
18
+ * @returns The current `HttpRequestHandler` instance, allowing for method chaining.
19
+ */
4
20
  setHeader(key: string, value: string): HttpRequestHandler<T>;
5
21
  }
6
22
  export default HttpRequestHandler;
@@ -1,4 +1,10 @@
1
1
  import { ProcessOperators } from './process-operators';
2
+ /**
3
+ * The `HttpResponseHandler` class extends the `ProcessOperators` class and is responsible for handling HTTP responses.
4
+ * It provides a common interface for processing HTTP responses, allowing for consistent error handling and response transformation.
5
+ *
6
+ * @template T - The type of the HTTP response. Defaults to `Response`.
7
+ */
2
8
  export declare class HttpResponseHandler<T = Response> extends ProcessOperators<T> {
3
9
  }
4
10
  export default HttpResponseHandler;