@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.
- package/CHANGELOG.md +162 -0
- package/README.md +759 -36
- package/dist/esm/configurator.js +10 -1
- package/dist/esm/configurator.js.map +1 -1
- package/dist/esm/errors.js +18 -0
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/client/client-msal.js +35 -0
- package/dist/esm/lib/client/client-msal.js.map +1 -1
- package/dist/esm/lib/client/client.js +153 -6
- package/dist/esm/lib/client/client.js.map +1 -1
- package/dist/esm/lib/operators/http-request-handler.js +16 -0
- package/dist/esm/lib/operators/http-request-handler.js.map +1 -1
- package/dist/esm/lib/operators/http-response-handler.js +6 -0
- package/dist/esm/lib/operators/http-response-handler.js.map +1 -1
- package/dist/esm/lib/operators/process-operators.js +49 -1
- package/dist/esm/lib/operators/process-operators.js.map +1 -1
- package/dist/esm/lib/operators/request-operator-header.js +7 -0
- package/dist/esm/lib/operators/request-operator-header.js.map +1 -1
- package/dist/esm/lib/selectors/blob-selector.js +28 -3
- package/dist/esm/lib/selectors/blob-selector.js.map +1 -1
- package/dist/esm/lib/selectors/json-selector.js +24 -0
- package/dist/esm/lib/selectors/json-selector.js.map +1 -1
- package/dist/esm/module.js +40 -0
- package/dist/esm/module.js.map +1 -1
- package/dist/esm/provider.js +65 -6
- package/dist/esm/provider.js.map +1 -1
- package/dist/esm/version.js +2 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/configurator.d.ts +76 -0
- package/dist/types/errors.d.ts +18 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/lib/client/client-msal.d.ts +43 -0
- package/dist/types/lib/client/client.d.ts +147 -3
- package/dist/types/lib/client/types.d.ts +156 -5
- package/dist/types/lib/operators/http-request-handler.d.ts +16 -0
- package/dist/types/lib/operators/http-response-handler.d.ts +6 -0
- package/dist/types/lib/operators/process-operators.d.ts +44 -0
- package/dist/types/lib/operators/request-operator-header.d.ts +7 -0
- package/dist/types/lib/operators/types.d.ts +57 -0
- package/dist/types/lib/selectors/blob-selector.d.ts +9 -1
- package/dist/types/lib/selectors/index.d.ts +1 -0
- package/dist/types/lib/selectors/json-selector.d.ts +16 -1
- package/dist/types/module.d.ts +35 -0
- package/dist/types/provider.d.ts +77 -0
- package/dist/types/version.d.ts +1 -1
- package/package.json +5 -5
- package/src/configurator.ts +38 -1
- package/src/errors.ts +11 -1
- package/src/lib/client/client-msal.ts +50 -5
- package/src/lib/client/client.ts +161 -12
- package/src/lib/client/types.ts +149 -71
- package/src/lib/operators/http-request-handler.ts +12 -4
- package/src/lib/operators/http-response-handler.ts +6 -0
- package/src/lib/operators/process-operators.ts +6 -1
- package/src/lib/operators/request-operator-header.ts +7 -0
- package/src/lib/operators/types.ts +22 -4
- package/src/lib/selectors/blob-selector.ts +26 -4
- package/src/lib/selectors/index.ts +2 -0
- package/src/lib/selectors/json-selector.ts +29 -9
- package/src/module.ts +55 -2
- package/src/provider.ts +76 -2
- package/src/version.ts +1 -1
|
@@ -1,11 +1,55 @@
|
|
|
1
1
|
import type { Observable } from 'rxjs';
|
|
2
2
|
import { IProcessOperators, ProcessOperator } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* ProcessOperators class manages a collection of process operators
|
|
5
|
+
* and provides methods to add, set, get, and process these operators.
|
|
6
|
+
* It implements the IProcessOperators interface for type T.
|
|
7
|
+
*
|
|
8
|
+
* @template T The type of data that the process operators work with
|
|
9
|
+
*/
|
|
3
10
|
export declare class ProcessOperators<T> implements IProcessOperators<T> {
|
|
11
|
+
/**
|
|
12
|
+
* A record of process operators keyed by a string.
|
|
13
|
+
* This property is used to store and manage the collection of process operators
|
|
14
|
+
* that are used by the `ProcessOperators` class.
|
|
15
|
+
*/
|
|
4
16
|
protected _operators: Record<string, ProcessOperator<T>>;
|
|
17
|
+
/**
|
|
18
|
+
* Accessor for the collection of process operators.
|
|
19
|
+
* @returns The record of process operators.
|
|
20
|
+
*/
|
|
5
21
|
get operators(): Record<string, ProcessOperator<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Constructs a new instance of the ProcessOperators class.
|
|
24
|
+
* @param operators - An optional object containing process operators.
|
|
25
|
+
* It can be either an instance of IProcessOperators<T> or a record of string keys and ProcessOperator<T> values.
|
|
26
|
+
*/
|
|
6
27
|
constructor(operators?: IProcessOperators<T> | Record<string, ProcessOperator<T>>);
|
|
28
|
+
/**
|
|
29
|
+
* Adds a new operator to the collection.
|
|
30
|
+
* @param key The key under which the operator is stored.
|
|
31
|
+
* @param operator The operator to be added.
|
|
32
|
+
* @returns The instance of ProcessOperators for chaining.
|
|
33
|
+
* @throws Error if an operator with the same key already exists.
|
|
34
|
+
*/
|
|
7
35
|
add(key: string, operator: ProcessOperator<T>): ProcessOperators<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Sets or updates an operator in the collection.
|
|
38
|
+
* @param key The key under which the operator is stored.
|
|
39
|
+
* @param operator The operator to be set.
|
|
40
|
+
* @returns The instance of ProcessOperators for chaining.
|
|
41
|
+
*/
|
|
8
42
|
set(key: string, operator: ProcessOperator<T>): ProcessOperators<T>;
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves an operator from the collection by its key.
|
|
45
|
+
* @param key The key of the operator to retrieve.
|
|
46
|
+
* @returns The retrieved operator.
|
|
47
|
+
*/
|
|
9
48
|
get(key: string): ProcessOperator<T>;
|
|
49
|
+
/**
|
|
50
|
+
* Processes an input request through the chain of operators.
|
|
51
|
+
* @param request The request to be processed.
|
|
52
|
+
* @returns An Observable of the processed request.
|
|
53
|
+
*/
|
|
10
54
|
process(request: T): Observable<T>;
|
|
11
55
|
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { FetchRequest } from '../client';
|
|
2
2
|
import type { ProcessOperator } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a process operator that adds a header to the request.
|
|
5
|
+
*
|
|
6
|
+
* @param key - The header key to add.
|
|
7
|
+
* @param value - The header value to add.
|
|
8
|
+
* @returns A process operator that adds the specified header to the request.
|
|
9
|
+
*/
|
|
3
10
|
export declare const requestOperatorHeader: <T extends FetchRequest = FetchRequest>(key: string, value: string) => ProcessOperator<T>;
|
|
4
11
|
export default requestOperatorHeader;
|
|
@@ -1,15 +1,72 @@
|
|
|
1
1
|
import type { Observable } from 'rxjs';
|
|
2
2
|
import type { FetchRequest } from '../client';
|
|
3
|
+
/**
|
|
4
|
+
* A process operator that takes a request of type `T` and returns a transformed request of type `R`, or `void`, or a Promise that resolves to `R` or `void`.
|
|
5
|
+
*
|
|
6
|
+
* Process operators are used to transform or modify requests in a sequential pipeline before they are processed by an `IHttpRequestHandler`.
|
|
7
|
+
*
|
|
8
|
+
* @template T The type of the input request.
|
|
9
|
+
* @template R The type of the output request. Defaults to `T` if not specified.
|
|
10
|
+
* @param request The input request to be processed.
|
|
11
|
+
* @returns The transformed request, `void`, or a Promise that resolves to the transformed request or `void`.
|
|
12
|
+
*/
|
|
3
13
|
export type ProcessOperator<T, R = T> = (request: T) => R | void | Promise<R | void>;
|
|
14
|
+
/**
|
|
15
|
+
* Represents a collection of process operators.
|
|
16
|
+
* @template T The type of the request being processed.
|
|
17
|
+
*/
|
|
4
18
|
export interface IProcessOperators<T> {
|
|
19
|
+
/**
|
|
20
|
+
* Gets the operators registered in the collection.
|
|
21
|
+
*/
|
|
5
22
|
get operators(): Record<string, ProcessOperator<T>>;
|
|
23
|
+
/**
|
|
24
|
+
* Adds a new operator to the collection.
|
|
25
|
+
* @param key The key to identify the operator.
|
|
26
|
+
* @param operator The process operator to add.
|
|
27
|
+
* @returns The updated collection of process operators.
|
|
28
|
+
* @throws An error if the operator is already defined.
|
|
29
|
+
*/
|
|
6
30
|
add(key: string, operator: ProcessOperator<T>): IProcessOperators<T>;
|
|
31
|
+
/**
|
|
32
|
+
* Adds or sets a process operator in the collection.
|
|
33
|
+
* @param key The key to identify the operator.
|
|
34
|
+
* @param operator The process operator to add or set.
|
|
35
|
+
* @returns The updated collection of process operators.
|
|
36
|
+
*/
|
|
7
37
|
set(key: string, operator: ProcessOperator<T>): IProcessOperators<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Gets a process operator from the collection.
|
|
40
|
+
* @param key The key of the operator to retrieve.
|
|
41
|
+
* @returns The process operator associated with the key, or undefined if the key is invalid.
|
|
42
|
+
*/
|
|
8
43
|
get(key: string): ProcessOperator<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Processes the registered process operators.
|
|
46
|
+
* @param request The request to process.
|
|
47
|
+
* @returns An observable that emits the processed request.
|
|
48
|
+
*/
|
|
9
49
|
process(request: T): Observable<T>;
|
|
10
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Represents an HTTP request handler that extends the `IProcessOperators` interface.
|
|
53
|
+
* This interface provides methods to manage and process HTTP request operators.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The type of the request being processed. Defaults to `FetchRequest`.
|
|
56
|
+
*/
|
|
11
57
|
export interface IHttpRequestHandler<T extends FetchRequest = FetchRequest> extends IProcessOperators<T> {
|
|
58
|
+
/**
|
|
59
|
+
* Set header that will apply on all requests done by consumer @see {HttpClient}
|
|
60
|
+
* @param key - name of header
|
|
61
|
+
* @param value - header value
|
|
62
|
+
*/
|
|
12
63
|
setHeader(key: string, value: string): IHttpRequestHandler<T>;
|
|
13
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Represents an HTTP response handler that extends the `IProcessOperators` interface.
|
|
67
|
+
* This interface provides methods to manage and process HTTP response operators.
|
|
68
|
+
*
|
|
69
|
+
* @template T - The type of the response being processed. Defaults to `Response`.
|
|
70
|
+
*/
|
|
14
71
|
export interface IHttpResponseHandler<T = Response> extends IProcessOperators<T> {
|
|
15
72
|
}
|
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ResponseSelector } from '../client/types';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts a blob and filename from a successful HTTP response.
|
|
4
|
+
*
|
|
5
|
+
* @param response - The HTTP response to extract the blob and filename from.
|
|
6
|
+
* @returns A promise that resolves to an object containing the extracted blob and filename.
|
|
7
|
+
* @throws {Error} If the response is not successful or if there is an error parsing the response.
|
|
8
|
+
*/
|
|
9
|
+
export declare const blobSelector: ResponseSelector;
|
|
2
10
|
export default blobSelector;
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ResponseSelector } from '../client/types';
|
|
2
|
+
/**
|
|
3
|
+
* Asynchronously parses the JSON data from a given HTTP response.
|
|
4
|
+
*
|
|
5
|
+
* If the response has a status code of 204 (No Content), this function will resolve with `undefined`.
|
|
6
|
+
*
|
|
7
|
+
* If the response is not successful (i.e. `response.ok` is `false`), this function will throw an `HttpJsonResponseError` with the response details and the parsed data (if any).
|
|
8
|
+
*
|
|
9
|
+
* If there is an error parsing the JSON data, this function will throw an `HttpJsonResponseError` with the parsing error and the original response.
|
|
10
|
+
*
|
|
11
|
+
* @template TType - The expected type of the parsed JSON data.
|
|
12
|
+
* @template TResponse - The type of the HTTP response object.
|
|
13
|
+
* @param response - The HTTP response to parse.
|
|
14
|
+
* @returns A promise that resolves with the parsed JSON data, or rejects with an `HttpJsonResponseError`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const jsonSelector: ResponseSelector;
|
|
2
17
|
export default jsonSelector;
|
package/dist/types/module.d.ts
CHANGED
|
@@ -3,13 +3,48 @@ import { IHttpClientConfigurator, HttpClientOptions } from './configurator';
|
|
|
3
3
|
import { IHttpClientProvider } from './provider';
|
|
4
4
|
import type { Module, ModuleConfigType, IModuleConfigurator } from '@equinor/fusion-framework-module';
|
|
5
5
|
import { MsalModule } from '@equinor/fusion-framework-module-msal';
|
|
6
|
+
/**
|
|
7
|
+
* Defines the type for the HTTP module, which includes:
|
|
8
|
+
* - The module name: 'http'
|
|
9
|
+
* - The type of the HTTP client provider, which is `IHttpClientProvider`
|
|
10
|
+
* - The type of the HTTP client configurator, which is `IHttpClientConfigurator`
|
|
11
|
+
*/
|
|
6
12
|
export type HttpModule = Module<'http', IHttpClientProvider, IHttpClientConfigurator>;
|
|
13
|
+
/**
|
|
14
|
+
* Defines the type for the HTTP module with MSAL authentication.
|
|
15
|
+
*
|
|
16
|
+
* This type represents the module configuration for the HTTP module, which includes:
|
|
17
|
+
* - The module name: 'http'
|
|
18
|
+
* - The type of the HTTP client provider, which is `IHttpClientProvider<HttpClientMsal>`
|
|
19
|
+
* - The type of the HTTP client configurator, which is `IHttpClientConfigurator<HttpClientMsal>`
|
|
20
|
+
* - The list of required modules, which includes the `MsalModule`
|
|
21
|
+
*/
|
|
7
22
|
export type HttpMsalModule = Module<'http', IHttpClientProvider<HttpClientMsal>, IHttpClientConfigurator<HttpClientMsal>, [
|
|
8
23
|
MsalModule
|
|
9
24
|
]>;
|
|
25
|
+
/**
|
|
26
|
+
* HTTP module with MSAL authentication.
|
|
27
|
+
*/
|
|
10
28
|
export declare const module: HttpMsalModule;
|
|
29
|
+
/**
|
|
30
|
+
* Configures the HTTP module with MSAL authentication.
|
|
31
|
+
*/
|
|
11
32
|
export declare const configureHttp: <TRef = unknown>(configure: (config: ModuleConfigType<HttpMsalModule>, ref?: TRef) => void) => IModuleConfigurator<HttpMsalModule, TRef>;
|
|
33
|
+
/**
|
|
34
|
+
* Configures the HTTP client with MSAL authentication.
|
|
35
|
+
*
|
|
36
|
+
* This function creates a module configurator that can be used to configure the HTTP module
|
|
37
|
+
* with MSAL authentication. The configurator takes a name and a set of HTTP client options,
|
|
38
|
+
* and returns a module configurator that can be used to configure the HTTP module.
|
|
39
|
+
*
|
|
40
|
+
* @param name - The name of the HTTP client configuration.
|
|
41
|
+
* @param args - The HTTP client options, including the MSAL configuration.
|
|
42
|
+
* @returns A module configurator that can be used to configure the HTTP module.
|
|
43
|
+
*/
|
|
12
44
|
export declare const configureHttpClient: <TRef = unknown>(name: string, args: HttpClientOptions<HttpClientMsal>) => IModuleConfigurator<HttpMsalModule, TRef>;
|
|
45
|
+
/**
|
|
46
|
+
* Declares a module named '@equinor/fusion-framework-module' that contains an interface named 'Modules' with a property 'http' of type 'HttpMsalModule'.
|
|
47
|
+
*/
|
|
13
48
|
declare module '@equinor/fusion-framework-module' {
|
|
14
49
|
interface Modules {
|
|
15
50
|
http: HttpMsalModule;
|
package/dist/types/provider.d.ts
CHANGED
|
@@ -6,18 +6,95 @@ export declare class ClientNotFoundException extends Error {
|
|
|
6
6
|
constructor(message: string);
|
|
7
7
|
}
|
|
8
8
|
export interface IHttpClientProvider<TClient extends IHttpClient = IHttpClient> {
|
|
9
|
+
/**
|
|
10
|
+
* The default HTTP request handler used by the HttpClientProvider.
|
|
11
|
+
* This handler is responsible for executing HTTP requests using the configured HttpClient.
|
|
12
|
+
*/
|
|
9
13
|
readonly defaultHttpRequestHandler: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if a client is configured with the given key.
|
|
16
|
+
* @param key - The key of the client to check.
|
|
17
|
+
* @returns `true` if a client is configured with the given key, `false` otherwise.
|
|
18
|
+
*/
|
|
10
19
|
hasClient(key: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new HTTP client instance with the specified key.
|
|
22
|
+
* @param key - The key of the HTTP client to create.
|
|
23
|
+
* @returns The created HTTP client instance.
|
|
24
|
+
*/
|
|
11
25
|
createClient(key: string): TClient;
|
|
12
26
|
createClient(key: HttpClientOptions<TClient>): TClient;
|
|
27
|
+
/**
|
|
28
|
+
* Class cast creation of custom client
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* config.http.configureClient('foobar', (client) => {
|
|
32
|
+
* client.ctor = MyClient;
|
|
33
|
+
* client.uri = 'https://foobar.com';
|
|
34
|
+
* });
|
|
35
|
+
*/
|
|
13
36
|
createCustomClient<T extends HttpClient>(key: string): T;
|
|
14
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The `HttpClientProvider` class is responsible for managing HTTP client instances and their configuration.
|
|
40
|
+
* It provides methods to check if a client is configured, create new client instances, and create custom client instances.
|
|
41
|
+
*/
|
|
15
42
|
export declare class HttpClientProvider<TClient extends IHttpClient = IHttpClient> implements IHttpClientProvider<TClient> {
|
|
16
43
|
protected config: IHttpClientConfigurator<TClient>;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the default HTTP request handler for the HTTP client provider.
|
|
46
|
+
* @returns The default HTTP request handler.
|
|
47
|
+
*/
|
|
17
48
|
get defaultHttpRequestHandler(): IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
|
|
18
49
|
constructor(config: IHttpClientConfigurator<TClient>);
|
|
50
|
+
/**
|
|
51
|
+
* Checks if a client with the given key is configured in the `HttpClientProvider`.
|
|
52
|
+
* @param key - The key of the HTTP client to check.
|
|
53
|
+
* @returns `true` if a client with the given key is configured, `false` otherwise.
|
|
54
|
+
*/
|
|
19
55
|
hasClient(key: string): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new HTTP client instance with the specified configuration.
|
|
58
|
+
*
|
|
59
|
+
* @param keyOrConfig - The key or configuration object for the HTTP client.
|
|
60
|
+
* @returns The created HTTP client instance.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* This method resolves the configuration for the HTTP client based on the provided `keyOrConfig` parameter.
|
|
64
|
+
* If a string is provided, it is treated as the key for a pre-configured client in the `HttpClientProvider`.
|
|
65
|
+
* If an `HttpClientOptions` object is provided, it is used as the configuration for the new client instance.
|
|
66
|
+
*
|
|
67
|
+
* The method sets up the HTTP client with the following options:
|
|
68
|
+
* - `baseUri`: The base URI for the HTTP client.
|
|
69
|
+
* - `defaultScopes`: The default scopes to be used for authentication.
|
|
70
|
+
* - `onCreate`: An optional callback function that is called when the client instance is created.
|
|
71
|
+
* - `ctor`: The constructor function for the HTTP client, defaulting to the configured `defaultHttpClientCtor`.
|
|
72
|
+
* - `requestHandler`: The HTTP request handler to be used by the client, defaulting to the `defaultHttpRequestHandler`.
|
|
73
|
+
*
|
|
74
|
+
* The created HTTP client instance is returned.
|
|
75
|
+
*/
|
|
20
76
|
createClient(keyOrConfig: string | HttpClientOptions<TClient>): TClient;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new HTTP client instance with the specified configuration.
|
|
79
|
+
*
|
|
80
|
+
* @param key - The key of the pre-configured HTTP client to create.
|
|
81
|
+
* @returns The created HTTP client instance, cast to the specified type `T`.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* This method delegates to the `createClient` method, but casts the returned
|
|
85
|
+
* instance to the specified type `T`. This can be useful when you need to
|
|
86
|
+
* work with a specific HTTP client implementation, but the `HttpClientProvider`
|
|
87
|
+
* is configured to use a different implementation.
|
|
88
|
+
*/
|
|
21
89
|
createCustomClient<T extends HttpClient>(key: string): T;
|
|
90
|
+
/**
|
|
91
|
+
* Resolves the configuration for an HTTP client based on the provided `keyOrConfig` parameter.
|
|
92
|
+
*
|
|
93
|
+
* If a string is provided, it is treated as the key for a pre-configured client in the `HttpClientProvider`.
|
|
94
|
+
* If an `HttpClientOptions` object is provided, it is used as the configuration for the new client instance.
|
|
95
|
+
*
|
|
96
|
+
* @param keyOrConfig - The key or configuration object for the HTTP client.
|
|
97
|
+
* @returns The resolved HTTP client configuration.
|
|
98
|
+
*/
|
|
22
99
|
protected _resolveConfig(keyOrConfig: string | HttpClientOptions<TClient>): HttpClientOptions<TClient>;
|
|
23
100
|
}
|
package/dist/types/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "
|
|
1
|
+
export declare const version = "6.0.1";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@equinor/fusion-framework-module-http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/esm/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -55,12 +55,12 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"rxjs": "^7.8.1",
|
|
58
|
-
"@equinor/fusion-framework-module": "^4.3.
|
|
59
|
-
"@equinor/fusion-framework-module-msal": "^3.1.
|
|
58
|
+
"@equinor/fusion-framework-module": "^4.3.2",
|
|
59
|
+
"@equinor/fusion-framework-module-msal": "^3.1.2"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"typescript": "^5.
|
|
63
|
-
"vitest": "^
|
|
62
|
+
"typescript": "^5.5.3",
|
|
63
|
+
"vitest": "^2.0.1"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"build": "tsc -b",
|
package/src/configurator.ts
CHANGED
|
@@ -3,10 +3,24 @@ import { HttpRequestHandler } from './lib/operators';
|
|
|
3
3
|
import type { FetchRequest, IHttpClient } from './lib/client';
|
|
4
4
|
import type { IHttpRequestHandler, IHttpResponseHandler } from './lib/operators';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents the options for constructing an `IHttpClient` instance.
|
|
8
|
+
*
|
|
9
|
+
* @template TInit - The type of the initial request object used by the `IHttpClient` instance.
|
|
10
|
+
* @property {IHttpRequestHandler<TInit>} requestHandler - The request handler to be used by the `IHttpClient` instance.
|
|
11
|
+
*/
|
|
6
12
|
interface HttpClientConstructorOptions<TInit extends FetchRequest> {
|
|
7
13
|
requestHandler: IHttpRequestHandler<TInit>;
|
|
8
14
|
}
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Represents a constructor for an `IHttpClient` instance.
|
|
18
|
+
*
|
|
19
|
+
* @template TClient - The type of the `IHttpClient` instance to be constructed.
|
|
20
|
+
* @param uri - The base URI for the `IHttpClient` instance.
|
|
21
|
+
* @param options - The options for constructing the `IHttpClient` instance, including the request handler.
|
|
22
|
+
* @returns A new instance of the `TClient` type.
|
|
23
|
+
*/
|
|
10
24
|
interface HttpClientConstructor<TClient extends IHttpClient> {
|
|
11
25
|
new (
|
|
12
26
|
uri: string,
|
|
@@ -14,15 +28,38 @@ interface HttpClientConstructor<TClient extends IHttpClient> {
|
|
|
14
28
|
): TClient;
|
|
15
29
|
}
|
|
16
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Represents the options for configuring an `IHttpClient` instance.
|
|
33
|
+
*
|
|
34
|
+
* @template TClient - The type of the `IHttpClient` instance to be configured.
|
|
35
|
+
*/
|
|
17
36
|
export interface HttpClientOptions<TClient extends IHttpClient = IHttpClient> {
|
|
37
|
+
/** The base URI for the `IHttpClient` instance. */
|
|
18
38
|
baseUri?: string;
|
|
39
|
+
|
|
40
|
+
/** The default scopes to be used by the `IHttpClient` instance. */
|
|
19
41
|
defaultScopes?: string[];
|
|
42
|
+
|
|
43
|
+
/** The constructor for the `TClient` type. */
|
|
20
44
|
ctor?: HttpClientConstructor<TClient>;
|
|
45
|
+
|
|
46
|
+
/** A callback function that is called when a new `TClient` instance is created. */
|
|
21
47
|
onCreate?: (client: TClient) => void;
|
|
48
|
+
|
|
49
|
+
/** The request handler to be used by the `IHttpClient` instance. */
|
|
22
50
|
requestHandler?: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
|
|
51
|
+
|
|
52
|
+
/** The response handler to be used by the `IHttpClient` instance. */
|
|
23
53
|
responseHandler?: IHttpResponseHandler<HttpClientRequestInitType<TClient>>;
|
|
24
54
|
}
|
|
25
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Utility type that extracts the request init type from an `IHttpClient` implementation.
|
|
58
|
+
* This is useful for ensuring type safety when configuring an `IHttpClient` instance.
|
|
59
|
+
*
|
|
60
|
+
* @template T - The type of the `IHttpClient` implementation.
|
|
61
|
+
* @returns The request init type for the `IHttpClient` implementation.
|
|
62
|
+
*/
|
|
26
63
|
export type HttpClientRequestInitType<T extends IHttpClient> =
|
|
27
64
|
T extends IHttpClient<infer U> ? U : never;
|
|
28
65
|
|
|
@@ -117,7 +154,7 @@ export class HttpClientConfigurator<TClient extends IHttpClient>
|
|
|
117
154
|
name: string,
|
|
118
155
|
args: string | HttpClientOptions<T> | HttpClientOptions<T>['onCreate'],
|
|
119
156
|
): HttpClientConfigurator<TClient> {
|
|
120
|
-
const argFn = typeof args === 'string' ? (
|
|
157
|
+
const argFn = typeof args === 'string' ? ({ baseUri: args } as HttpClientOptions<T>) : args;
|
|
121
158
|
const options = typeof argFn === 'function' ? { onCreate: argFn } : argFn;
|
|
122
159
|
this._clients[name] = {
|
|
123
160
|
...this._clients[name],
|
package/src/errors.ts
CHANGED
|
@@ -15,7 +15,9 @@ export class HttpResponseError<TResponse = Response> extends Error {
|
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Represents an error that occurs when handling a JSON response in an HTTP request.
|
|
18
|
-
*
|
|
18
|
+
* Extends the base `HttpResponseError` class.
|
|
19
|
+
*
|
|
20
|
+
* @template TType - The type of the data associated with the error.
|
|
19
21
|
* @template TResponse - The type of the HTTP response.
|
|
20
22
|
*/
|
|
21
23
|
export class HttpJsonResponseError<
|
|
@@ -24,6 +26,14 @@ export class HttpJsonResponseError<
|
|
|
24
26
|
> extends HttpResponseError<TResponse> {
|
|
25
27
|
static Name = 'HttpJsonResponseError';
|
|
26
28
|
public readonly data?: TType;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new instance of `HttpJsonResponseError`.
|
|
32
|
+
*
|
|
33
|
+
* @param message - The error message.
|
|
34
|
+
* @param response - The HTTP response associated with the error.
|
|
35
|
+
* @param options - Additional options for the error, including the associated data.
|
|
36
|
+
*/
|
|
27
37
|
constructor(message: string, response: TResponse, options?: ErrorOptions & { data?: TType }) {
|
|
28
38
|
super(message, response, options);
|
|
29
39
|
this.name = HttpJsonResponseError.Name;
|
|
@@ -2,30 +2,75 @@ import type { Observable } from 'rxjs';
|
|
|
2
2
|
import type { FetchRequestInit, FetchRequest, FetchResponse } from '.';
|
|
3
3
|
import { HttpClient } from './client';
|
|
4
4
|
|
|
5
|
-
/**
|
|
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
|
+
*/
|
|
6
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
|
+
*/
|
|
7
19
|
type MsalFetchRequestInit<
|
|
8
20
|
TReturn = unknown,
|
|
9
21
|
TRequest = FetchRequest,
|
|
10
22
|
TResponse = FetchResponse,
|
|
11
23
|
> = FetchRequestInit<TReturn, TRequest, TResponse> & Pick<MsalFetchRequest, 'scopes'>;
|
|
12
24
|
|
|
13
|
-
/**
|
|
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
|
+
*/
|
|
14
39
|
export class HttpClientMsal<
|
|
15
40
|
TRequest extends MsalFetchRequest = MsalFetchRequest,
|
|
16
41
|
TResponse extends FetchResponse = FetchResponse,
|
|
17
42
|
> extends HttpClient<TRequest, TResponse> {
|
|
18
|
-
/**
|
|
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
|
+
*/
|
|
19
47
|
public defaultScopes: string[] = [];
|
|
20
48
|
|
|
21
|
-
/**
|
|
22
|
-
|
|
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
|
+
* @param path - The path to the resource to fetch.
|
|
59
|
+
* @param init - An optional `MsalFetchRequestInit` object that can include the `scopes` property.
|
|
60
|
+
* @returns An `Observable` that emits the fetched resource.
|
|
61
|
+
*/
|
|
62
|
+
public fetch$<T = TResponse>(
|
|
23
63
|
path: string,
|
|
24
64
|
init?: MsalFetchRequestInit<T, TRequest, TResponse>,
|
|
25
65
|
): Observable<T> {
|
|
66
|
+
/**
|
|
67
|
+
* Merges the default scopes defined in the `HttpClientMsal` class with the scopes provided in the `init` parameter, if any.
|
|
68
|
+
* This ensures that the request includes the necessary scopes for MSAL authentication.
|
|
69
|
+
*/
|
|
26
70
|
const args = Object.assign(init || {}, {
|
|
27
71
|
scopes: this.defaultScopes.concat(init?.scopes || []),
|
|
28
72
|
}) as FetchRequestInit<T, TRequest, TResponse>;
|
|
73
|
+
|
|
29
74
|
return super._fetch$(path, args);
|
|
30
75
|
}
|
|
31
76
|
}
|