@equinor/fusion-framework-module-http 8.1.0-next.0 → 8.1.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 (53) hide show
  1. package/dist/esm/version.js +1 -1
  2. package/dist/esm/version.js.map +1 -1
  3. package/dist/tsconfig.tsbuildinfo +1 -1
  4. package/dist/types/version.d.ts +1 -1
  5. package/package.json +6 -3
  6. package/CHANGELOG.md +0 -1321
  7. package/docs/client-configuration.md +0 -175
  8. package/docs/observable-patterns.md +0 -103
  9. package/docs/selectors-and-handlers.md +0 -112
  10. package/docs/server-sent-events.md +0 -125
  11. package/docs/testing.md +0 -78
  12. package/src/configurator.ts +0 -249
  13. package/src/errors/ClientNotFoundException.ts +0 -9
  14. package/src/errors/HttpJsonResponseError.ts +0 -32
  15. package/src/errors/HttpResponseError.ts +0 -21
  16. package/src/errors/ServerSentEventResponseError.ts +0 -30
  17. package/src/errors/index.ts +0 -4
  18. package/src/index.ts +0 -15
  19. package/src/lib/client/client-msal.ts +0 -80
  20. package/src/lib/client/client.ts +0 -496
  21. package/src/lib/client/index.ts +0 -4
  22. package/src/lib/client/types.ts +0 -244
  23. package/src/lib/index.ts +0 -3
  24. package/src/lib/operators/HttpMiddlewareHandler.ts +0 -58
  25. package/src/lib/operators/HttpRequestHandler.ts +0 -29
  26. package/src/lib/operators/HttpResponseHandler.ts +0 -11
  27. package/src/lib/operators/ProcessOperators.ts +0 -113
  28. package/src/lib/operators/capitalize-request-method-operator.ts +0 -26
  29. package/src/lib/operators/fetch-request.schemas.ts +0 -104
  30. package/src/lib/operators/index.ts +0 -9
  31. package/src/lib/operators/request-operator-header.ts +0 -19
  32. package/src/lib/operators/request-validation-operator.ts +0 -51
  33. package/src/lib/operators/sse-map.operator.ts +0 -45
  34. package/src/lib/operators/types.ts +0 -174
  35. package/src/lib/selectors/blob-selector.ts +0 -42
  36. package/src/lib/selectors/create-sse-selector.ts +0 -279
  37. package/src/lib/selectors/index.ts +0 -11
  38. package/src/lib/selectors/json-selector.ts +0 -52
  39. package/src/mock/create-open-api-mock-middleware.ts +0 -40
  40. package/src/mock/create-router-middleware.ts +0 -158
  41. package/src/mock/index.ts +0 -26
  42. package/src/mock/resolve-open-api-mock-response.ts +0 -36
  43. package/src/module.ts +0 -149
  44. package/src/provider.ts +0 -225
  45. package/src/version.ts +0 -2
  46. package/tests/HttpClient.test.ts +0 -173
  47. package/tests/HttpMiddlewareHandler.test.ts +0 -58
  48. package/tests/mock/adapters.test.ts +0 -62
  49. package/tests/mock/router-middleware.test.ts +0 -135
  50. package/tests/operators.test.ts +0 -137
  51. package/tests/sse.selector.test.ts +0 -162
  52. package/tsconfig.json +0 -18
  53. package/vitest.config.ts +0 -12
@@ -1,249 +0,0 @@
1
- import {
2
- capitalizeRequestMethodOperator,
3
- requestValidationOperator,
4
- HttpMiddlewareHandler,
5
- HttpRequestHandler,
6
- } from './lib/operators';
7
-
8
- import type { FetchRequest, IHttpClient } from './lib/client';
9
- import type {
10
- HttpMiddleware,
11
- IHttpMiddlewareHandler,
12
- IHttpRequestHandler,
13
- IHttpResponseHandler,
14
- } from './lib/operators';
15
-
16
- /**
17
- * Represents the options for constructing an `IHttpClient` instance.
18
- *
19
- * @template TInit - The type of the initial request object used by the `IHttpClient` instance.
20
- * @template TResponse - The type of the response object used by the `IHttpClient` instance.
21
- * @property {IHttpRequestHandler<TInit>} requestHandler - The request handler to be used by the `IHttpClient` instance.
22
- * @property {IHttpResponseHandler<TResponse>} [responseHandler] - The response handler to be used by the `IHttpClient` instance.
23
- */
24
- interface HttpClientConstructorOptions<TInit extends FetchRequest, TResponse = Response> {
25
- requestHandler: IHttpRequestHandler<TInit>;
26
- responseHandler?: IHttpResponseHandler<TResponse>;
27
- }
28
-
29
- /**
30
- * Represents a constructor for an `IHttpClient` instance.
31
- *
32
- * @template TClient - The type of the `IHttpClient` instance to be constructed.
33
- * @param uri - The base URI for the `IHttpClient` instance.
34
- * @param options - The options for constructing the `IHttpClient` instance, including the request handler.
35
- * @returns A new instance of the `TClient` type.
36
- */
37
- interface HttpClientConstructor<TClient extends IHttpClient> {
38
- new (
39
- uri: string,
40
- options: HttpClientConstructorOptions<
41
- HttpClientRequestInitType<TClient>,
42
- HttpClientResponseType<TClient>
43
- >,
44
- ): TClient;
45
- }
46
-
47
- /**
48
- * Configures how the provider creates a named `IHttpClient` instance.
49
- *
50
- * Use these options to define a base URL, MSAL scopes, shared request or response handlers,
51
- * a custom client constructor, or per-instance setup through `onCreate`.
52
- *
53
- * @template TClient - The client type created from this configuration.
54
- */
55
- export interface HttpClientOptions<TClient extends IHttpClient = IHttpClient> {
56
- /** The base URI for the `IHttpClient` instance. */
57
- baseUri?: string;
58
-
59
- /** The default scopes to be used by the `IHttpClient` instance. */
60
- defaultScopes?: string[];
61
-
62
- /** The constructor for the `TClient` type. */
63
- ctor?: HttpClientConstructor<TClient>;
64
-
65
- /** A callback function that is called when a new `TClient` instance is created. */
66
- onCreate?: (client: TClient) => void;
67
-
68
- /** The request handler to be used by the `IHttpClient` instance. */
69
- requestHandler?: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
70
-
71
- /** The response handler to be used by the `IHttpClient` instance. */
72
- responseHandler?: IHttpResponseHandler<HttpClientResponseType<TClient>>;
73
-
74
- /**
75
- * Middleware wrapping the network call, overriding {@link HttpClientConfigurator.addMiddleware}
76
- * for this client only. Rarely needed — most middleware belongs on the configurator so it
77
- * applies to every client, and still runs the same way against a mocked client in tests.
78
- */
79
- middlewareHandler?: IHttpMiddlewareHandler;
80
- }
81
-
82
- /**
83
- * Utility type that extracts the request init type from an `IHttpClient` implementation.
84
- * This is useful for ensuring type safety when configuring an `IHttpClient` instance.
85
- *
86
- * @template T - The type of the `IHttpClient` implementation.
87
- * @returns The request init type for the `IHttpClient` implementation.
88
- */
89
- export type HttpClientRequestInitType<T extends IHttpClient> =
90
- T extends IHttpClient<infer U> ? U : never;
91
-
92
- /**
93
- * Utility type that extracts the response type from an `IHttpClient` implementation.
94
- * This is useful for ensuring type safety when configuring response handlers for an `IHttpClient` instance.
95
- *
96
- * @template T - The type of the `IHttpClient` implementation.
97
- * @returns The response type for the `IHttpClient` implementation.
98
- */
99
- export type HttpClientResponseType<T extends IHttpClient> =
100
- T extends IHttpClient<infer _TRequest, infer TResponse> ? TResponse : never;
101
-
102
- /**
103
- * Registers and looks up named HTTP client configurations for the HTTP module.
104
- *
105
- * Each named configuration can later be turned into a fresh client instance by the provider.
106
- *
107
- * @template TClient - The base client type the provider creates.
108
- */
109
- export interface IHttpClientConfigurator<TClient extends IHttpClient = IHttpClient> {
110
- readonly clients: Record<string, HttpClientOptions<TClient>>;
111
- readonly defaultHttpClientCtor: HttpClientConstructor<TClient>;
112
- readonly defaultHttpRequestHandler: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
113
- readonly defaultHttpMiddlewareHandler: IHttpMiddlewareHandler;
114
-
115
- /**
116
- * Registers middleware wrapping the network call for every client this configurator builds —
117
- * retries, caching, telemetry, circuit breaking. Register it wherever the app configures its
118
- * real HTTP clients; because it wraps `_performFetch` rather than replacing it, the same
119
- * registration still runs, unaltered, against a mocked client in tests.
120
- * @param middleware - The middleware to register.
121
- * @returns The configurator so registrations can be chained.
122
- * @example
123
- * ```ts
124
- * configurator.http.addMiddleware(async (uri, init, next) => {
125
- * const response = await next(uri, init);
126
- * return response.ok ? response : next(uri, init);
127
- * });
128
- * ```
129
- */
130
- addMiddleware(middleware: HttpMiddleware): IHttpClientConfigurator<TClient>;
131
-
132
- /**
133
- * Registers or updates a named client configuration.
134
- * @param name - The client key used later with `createClient(name)`.
135
- * @param args - The configuration used when creating a client instance.
136
- * @returns The configurator so registrations can be chained.
137
- * @example
138
- * ```ts
139
- * configurator.http.configureClient('catalog', {
140
- * baseUri: 'https://api.example.com',
141
- * defaultScopes: ['api://catalog-api/.default'],
142
- * });
143
- * ```
144
- */
145
- configureClient<T extends TClient>(
146
- name: string,
147
- args: HttpClientOptions<T>,
148
- ): IHttpClientConfigurator<TClient>;
149
-
150
- /**
151
- * Registers a named client with only a base URI.
152
- * @param name - The client key used later with `createClient(name)`.
153
- * @param uri - The base endpoint for the client.
154
- * @returns The configurator so registrations can be chained.
155
- */
156
- configureClient(name: string, uri: string): IHttpClientConfigurator<TClient>;
157
-
158
- /**
159
- * Registers a named client using only an `onCreate` callback.
160
- * @param name - The client key used later with `createClient(name)`.
161
- * @param onCreate - The callback that runs for every created client instance.
162
- * @returns The configurator so registrations can be chained.
163
- * @example
164
- * ```ts
165
- * configurator.http.configureClient('catalog', (client) => {
166
- * client.requestHandler.add('logger', (request) => console.log(request));
167
- * });
168
- * ```
169
- */
170
- configureClient<T extends TClient>(
171
- name: string,
172
- onCreate: (client: T) => void,
173
- ): HttpClientConfigurator<TClient>;
174
-
175
- /**
176
- * Checks whether a named client configuration exists.
177
- * @param name - The client key to check.
178
- * @returns `true` when a configuration exists for the key.
179
- */
180
- hasClient(name: string): boolean;
181
- }
182
-
183
- /** @inheritdoc */
184
- export class HttpClientConfigurator<TClient extends IHttpClient>
185
- implements IHttpClientConfigurator<TClient>
186
- {
187
- /** Named client configurations keyed by client name. */
188
- protected _clients: Record<string, HttpClientOptions<TClient>> = {};
189
-
190
- /**
191
- * Gets a shallow clone of all named client configurations.
192
- *
193
- * @returns A shallow clone of the named client configurations.
194
- */
195
- public get clients(): Record<string, HttpClientOptions<TClient>> {
196
- return { ...this._clients };
197
- }
198
-
199
- /** Default constructor used when a client configuration does not provide `ctor`. */
200
- readonly defaultHttpClientCtor: HttpClientConstructor<TClient>;
201
-
202
- /** Default request handler pipeline cloned into each created client instance. */
203
- readonly defaultHttpRequestHandler = new HttpRequestHandler<HttpClientRequestInitType<TClient>>({
204
- // convert all request methods to uppercase
205
- 'capitalize-method': capitalizeRequestMethodOperator(),
206
- // validate the request object
207
- 'request-validation': requestValidationOperator(),
208
- });
209
-
210
- /** Default middleware chain cloned into each created client instance. */
211
- readonly defaultHttpMiddlewareHandler: IHttpMiddlewareHandler = new HttpMiddlewareHandler();
212
-
213
- /**
214
- * Creates a configurator with the default client constructor.
215
- * @param client - The default client constructor used when `ctor` is not configured per client.
216
- */
217
- constructor(client: HttpClientConstructor<TClient>) {
218
- this.defaultHttpClientCtor = client;
219
- }
220
-
221
- /** @inheritdoc */
222
- hasClient(name: string): boolean {
223
- return Object.keys(this._clients).includes(name);
224
- }
225
-
226
- /** @inheritdoc */
227
- addMiddleware(middleware: HttpMiddleware): HttpClientConfigurator<TClient> {
228
- this.defaultHttpMiddlewareHandler.use(middleware);
229
- return this;
230
- }
231
-
232
- /** @inheritdoc */
233
- configureClient<T extends TClient>(
234
- name: string,
235
- args: string | HttpClientOptions<T> | HttpClientOptions<T>['onCreate'],
236
- ): HttpClientConfigurator<TClient> {
237
- const argFn = typeof args === 'string' ? ({ baseUri: args } as HttpClientOptions<T>) : args;
238
- const options = typeof argFn === 'function' ? { onCreate: argFn } : argFn;
239
- // `options` is `HttpClientOptions<T>` (this call's generic), but `_clients` is keyed by the
240
- // configurator's own `TClient` — callers are expected to keep the two in sync.
241
- this._clients[name] = {
242
- ...this._clients[name],
243
- ...(options as unknown as HttpClientOptions<TClient>),
244
- };
245
- return this;
246
- }
247
- }
248
-
249
- export default HttpClientConfigurator;
@@ -1,9 +0,0 @@
1
- /**
2
- * Thrown when `createClient(name)` is called with an unknown client key.
3
- *
4
- * This is only used when the provided string is neither a registered client name
5
- * nor an absolute `http:` or `https:` URL.
6
- */
7
- export class ClientNotFoundException extends Error {}
8
-
9
- export default ClientNotFoundException;
@@ -1,32 +0,0 @@
1
- import { HttpResponseError } from './HttpResponseError.js';
2
-
3
- /**
4
- * Represents an error that occurs when handling a JSON response in an HTTP request.
5
- * Extends the base `HttpResponseError` class.
6
- *
7
- * @template TType - The type of the data associated with the error.
8
- * @template TResponse - The type of the HTTP response.
9
- */
10
- export class HttpJsonResponseError<
11
- TType = unknown,
12
- TResponse = Response,
13
- > extends HttpResponseError<TResponse> {
14
- static Name = 'HttpJsonResponseError';
15
- /** The parsed JSON data associated with the error response, if any. */
16
- public readonly data?: TType;
17
-
18
- /**
19
- * Creates a new instance of `HttpJsonResponseError`.
20
- *
21
- * @param message - The error message.
22
- * @param response - The HTTP response associated with the error.
23
- * @param options - Additional options for the error, including the associated data.
24
- */
25
- constructor(message: string, response: TResponse, options?: ErrorOptions & { data?: TType }) {
26
- super(message, response, options);
27
- this.name = HttpJsonResponseError.Name;
28
- this.data = options?.data;
29
- }
30
- }
31
-
32
- export default HttpJsonResponseError;
@@ -1,21 +0,0 @@
1
- /**
2
- * Represents an error that occurs when handling an HTTP response.
3
- * @template TResponse The type of the HTTP response.
4
- */
5
- export class HttpResponseError<TResponse = Response> extends Error {
6
- static Name = 'HttpResponseError';
7
- /**
8
- * @param message - The error message.
9
- * @param response - The HTTP response associated with the error.
10
- * @param options - Additional error options.
11
- */
12
- constructor(
13
- message: string,
14
- public readonly response: TResponse,
15
- options?: ErrorOptions,
16
- ) {
17
- super(message, options);
18
- }
19
- }
20
-
21
- export default HttpResponseError;
@@ -1,30 +0,0 @@
1
- import { HttpResponseError } from './HttpResponseError.js';
2
-
3
- /**
4
- * Represents an error that occurs when handling a server-sent event (SSE) HTTP response.
5
- *
6
- * @template TType - The type of additional data associated with the error.
7
- * @template TResponse - The type of the HTTP response object.
8
- *
9
- * @extends HttpResponseError<TResponse>
10
- */
11
- export class ServerSentEventResponseError<
12
- TType = unknown,
13
- TResponse = Response,
14
- > extends HttpResponseError<TResponse> {
15
- static Name = 'ServerSentEventResponseError';
16
-
17
- /**
18
- * Creates a new instance of the error.
19
- *
20
- * @param message - The error message describing the cause of the error.
21
- * @param response - The HTTP response associated with the error.
22
- * @param options - Optional error options, which may include additional data of type `TType`.
23
- */
24
- constructor(message: string, response: TResponse, options?: ErrorOptions & { data?: TType }) {
25
- super(message, response, options);
26
- this.name = ServerSentEventResponseError.Name;
27
- }
28
- }
29
-
30
- export default ServerSentEventResponseError;
@@ -1,4 +0,0 @@
1
- export * from './HttpResponseError.js';
2
- export * from './HttpJsonResponseError.js';
3
- export * from './ServerSentEventResponseError.js';
4
- export * from './ClientNotFoundException.js';
package/src/index.ts DELETED
@@ -1,15 +0,0 @@
1
- /**
2
- * [[include:module-http/README.MD]]
3
- * @module
4
- */
5
-
6
- export * from './configurator';
7
- export * from './provider';
8
- export * from './module';
9
-
10
- export * as Errors from './errors/index.js';
11
- export * from './errors/index.js';
12
-
13
- export type { IHttpClient, FetchResponse } from './lib/client';
14
-
15
- export { default } from './module';
@@ -1,80 +0,0 @@
1
- import type { Observable } from 'rxjs';
2
- import type { FetchRequestInit, FetchRequest, FetchResponse } from '.';
3
- import { HttpClient } from './client';
4
-
5
- /**
6
- * Extends the `FetchRequest` type with an optional `scopes` property, which is an array of strings representing the scopes to be used for the request.
7
- * This type is used to represent a request that requires authentication using the MSAL (Microsoft Authentication Library) library.
8
- */
9
- type MsalFetchRequest = FetchRequest & { scopes?: string[] };
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
- */
19
- type MsalFetchRequestInit<
20
- TReturn = unknown,
21
- TRequest = FetchRequest,
22
- TResponse = FetchResponse,
23
- > = FetchRequestInit<TReturn, TRequest, TResponse> & Pick<MsalFetchRequest, 'scopes'>;
24
-
25
- /**
26
- * Extends the `HttpClient` class to provide MSAL (Microsoft Authentication Library) authentication support.
27
- *
28
- * The `HttpClientMsal` class is responsible for handling requests that require authentication using the MSAL library.
29
- * It extends the `HttpClient` class and adds the following functionality:
30
- *
31
- * - `defaultScopes`: An array of strings representing the default scopes to be used for all requests, unless overridden in the request object.
32
- * - `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.
33
- * - If `scopes` is provided in the `MsalFetchRequestInit` object, it will be used in addition to the `defaultScopes`.
34
- * - If `scopes` is not provided, only the `defaultScopes` will be used.
35
- *
36
- * @template TRequest - The type of the request object. Defaults to `MsalFetchRequest`.
37
- * @template TResponse - The type of the response object. Defaults to `FetchResponse`.
38
- */
39
- export class HttpClientMsal<
40
- TRequest extends MsalFetchRequest = MsalFetchRequest,
41
- TResponse extends FetchResponse = FetchResponse,
42
- > extends HttpClient<TRequest, TResponse> {
43
- /**
44
- * An array of default scopes to be used for all requests, unless overridden in the request object.
45
- * This property is used by the `HttpClientMsal` class to add MSAL authentication support to requests.
46
- */
47
- public defaultScopes: string[] = [];
48
-
49
- /**
50
- * Fetches a resource from the specified path, with optional MSAL authentication scopes.
51
- *
52
- * This method extends the `HttpClient.fetch$` method to add support for MSAL authentication.
53
- * If `init.scopes` is provided, it will be used in addition to the `defaultScopes` defined in the `HttpClientMsal` class.
54
- * If `init.scopes` is not provided, only the `defaultScopes` will be used for the request.
55
- *
56
- * @overrides HttpClient.fetch$
57
- *
58
- * @template T - The expected response type.
59
- * @param path - The path to the resource to fetch.
60
- * @param init - An optional `MsalFetchRequestInit` object that can include the `scopes` property.
61
- * @returns An `Observable` that emits the fetched resource.
62
- */
63
- public fetch$<T = TResponse>(
64
- path: string,
65
- init?: MsalFetchRequestInit<T, TRequest, TResponse>,
66
- ): Observable<T> {
67
- /**
68
- * Merges the default scopes defined in the `HttpClientMsal` class with the scopes provided in the `init` parameter, if any.
69
- * This ensures that the request includes the necessary scopes for MSAL authentication.
70
- */
71
- const args = {
72
- ...init,
73
- scopes: this.defaultScopes.concat(init?.scopes || []),
74
- } as FetchRequestInit<T, TRequest, TResponse>;
75
-
76
- return super._fetch$(path, args);
77
- }
78
- }
79
-
80
- export default HttpClientMsal;