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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +759 -36
  3. package/dist/esm/configurator.js +10 -1
  4. package/dist/esm/configurator.js.map +1 -1
  5. package/dist/esm/errors.js +18 -0
  6. package/dist/esm/errors.js.map +1 -1
  7. package/dist/esm/index.js +4 -0
  8. package/dist/esm/index.js.map +1 -1
  9. package/dist/esm/lib/client/client-msal.js +35 -0
  10. package/dist/esm/lib/client/client-msal.js.map +1 -1
  11. package/dist/esm/lib/client/client.js +150 -4
  12. package/dist/esm/lib/client/client.js.map +1 -1
  13. package/dist/esm/lib/operators/http-request-handler.js +16 -0
  14. package/dist/esm/lib/operators/http-request-handler.js.map +1 -1
  15. package/dist/esm/lib/operators/http-response-handler.js +6 -0
  16. package/dist/esm/lib/operators/http-response-handler.js.map +1 -1
  17. package/dist/esm/lib/operators/process-operators.js +49 -1
  18. package/dist/esm/lib/operators/process-operators.js.map +1 -1
  19. package/dist/esm/lib/operators/request-operator-header.js +7 -0
  20. package/dist/esm/lib/operators/request-operator-header.js.map +1 -1
  21. package/dist/esm/lib/selectors/blob-selector.js +12 -0
  22. package/dist/esm/lib/selectors/blob-selector.js.map +1 -1
  23. package/dist/esm/lib/selectors/json-selector.js +21 -0
  24. package/dist/esm/lib/selectors/json-selector.js.map +1 -1
  25. package/dist/esm/module.js +40 -0
  26. package/dist/esm/module.js.map +1 -1
  27. package/dist/esm/provider.js +65 -6
  28. package/dist/esm/provider.js.map +1 -1
  29. package/dist/esm/version.js +2 -1
  30. package/dist/esm/version.js.map +1 -1
  31. package/dist/tsconfig.tsbuildinfo +1 -1
  32. package/dist/types/configurator.d.ts +76 -0
  33. package/dist/types/errors.d.ts +18 -0
  34. package/dist/types/index.d.ts +4 -0
  35. package/dist/types/lib/client/client-msal.d.ts +43 -0
  36. package/dist/types/lib/client/client.d.ts +144 -0
  37. package/dist/types/lib/client/types.d.ts +150 -3
  38. package/dist/types/lib/operators/http-request-handler.d.ts +16 -0
  39. package/dist/types/lib/operators/http-response-handler.d.ts +6 -0
  40. package/dist/types/lib/operators/process-operators.d.ts +44 -0
  41. package/dist/types/lib/operators/request-operator-header.d.ts +7 -0
  42. package/dist/types/lib/operators/types.d.ts +57 -0
  43. package/dist/types/lib/selectors/blob-selector.d.ts +9 -2
  44. package/dist/types/lib/selectors/index.d.ts +1 -0
  45. package/dist/types/lib/selectors/json-selector.d.ts +16 -1
  46. package/dist/types/module.d.ts +35 -0
  47. package/dist/types/provider.d.ts +77 -0
  48. package/dist/types/version.d.ts +1 -1
  49. package/package.json +5 -5
  50. package/src/configurator.ts +38 -1
  51. package/src/errors.ts +11 -1
  52. package/src/lib/client/client-msal.ts +50 -5
  53. package/src/lib/client/client.ts +130 -5
  54. package/src/lib/client/types.ts +124 -67
  55. package/src/lib/operators/http-request-handler.ts +12 -4
  56. package/src/lib/operators/http-response-handler.ts +6 -0
  57. package/src/lib/operators/process-operators.ts +6 -1
  58. package/src/lib/operators/request-operator-header.ts +7 -0
  59. package/src/lib/operators/types.ts +22 -4
  60. package/src/lib/selectors/blob-selector.ts +2 -2
  61. package/src/lib/selectors/index.ts +2 -0
  62. package/src/lib/selectors/json-selector.ts +5 -1
  63. package/src/module.ts +55 -2
  64. package/src/provider.ts +76 -2
  65. package/src/version.ts +1 -1
package/src/module.ts CHANGED
@@ -11,8 +11,23 @@ import type {
11
11
 
12
12
  import { MsalModule } from '@equinor/fusion-framework-module-msal';
13
13
 
14
+ /**
15
+ * Defines the type for the HTTP module, which includes:
16
+ * - The module name: 'http'
17
+ * - The type of the HTTP client provider, which is `IHttpClientProvider`
18
+ * - The type of the HTTP client configurator, which is `IHttpClientConfigurator`
19
+ */
14
20
  export type HttpModule = Module<'http', IHttpClientProvider, IHttpClientConfigurator>;
15
21
 
22
+ /**
23
+ * Defines the type for the HTTP module with MSAL authentication.
24
+ *
25
+ * This type represents the module configuration for the HTTP module, which includes:
26
+ * - The module name: 'http'
27
+ * - The type of the HTTP client provider, which is `IHttpClientProvider<HttpClientMsal>`
28
+ * - The type of the HTTP client configurator, which is `IHttpClientConfigurator<HttpClientMsal>`
29
+ * - The list of required modules, which includes the `MsalModule`
30
+ */
16
31
  export type HttpMsalModule = Module<
17
32
  'http',
18
33
  IHttpClientProvider<HttpClientMsal>,
@@ -21,11 +36,33 @@ export type HttpMsalModule = Module<
21
36
  >;
22
37
 
23
38
  /**
24
- * Configure http-client
39
+ * HTTP module with MSAL authentication.
25
40
  */
26
41
  export const module: HttpMsalModule = {
27
42
  name: 'http',
43
+ /**
44
+ * Configures the HTTP module with MSAL authentication.
45
+ *
46
+ * This function creates a new `HttpClientConfigurator` instance using the `HttpClientMsal` class.
47
+ * The `HttpClientConfigurator` is responsible for configuring the HTTP client with the necessary options,
48
+ * such as the base URL, request headers, and other settings.
49
+ *
50
+ * @returns A new `HttpClientConfigurator` instance configured for MSAL authentication.
51
+ */
28
52
  configure: () => new HttpClientConfigurator(HttpClientMsal),
53
+
54
+ /**
55
+ * Initializes the HTTP client provider with MSAL authentication.
56
+ *
57
+ * This function is responsible for setting up the default HTTP request handler
58
+ * to acquire an access token from MSAL and attach it to the request headers
59
+ * when the request includes scopes.
60
+ *
61
+ * @param config - The module configuration.
62
+ * @param hasModule - A function to check if a module is available.
63
+ * @param requireInstance - A function to get an instance of a module.
64
+ * @returns A promise that resolves to the HTTP client provider.
65
+ */
29
66
  initialize: async ({
30
67
  config,
31
68
  hasModule,
@@ -53,14 +90,27 @@ export const module: HttpMsalModule = {
53
90
  },
54
91
  };
55
92
 
93
+ /**
94
+ * Configures the HTTP module with MSAL authentication.
95
+ */
56
96
  export const configureHttp = <TRef = unknown>(
57
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
97
  configure: (config: ModuleConfigType<HttpMsalModule>, ref?: TRef) => void,
59
98
  ): IModuleConfigurator<HttpMsalModule, TRef> => ({
60
99
  module,
61
100
  configure,
62
101
  });
63
102
 
103
+ /**
104
+ * Configures the HTTP client with MSAL authentication.
105
+ *
106
+ * This function creates a module configurator that can be used to configure the HTTP module
107
+ * with MSAL authentication. The configurator takes a name and a set of HTTP client options,
108
+ * and returns a module configurator that can be used to configure the HTTP module.
109
+ *
110
+ * @param name - The name of the HTTP client configuration.
111
+ * @param args - The HTTP client options, including the MSAL configuration.
112
+ * @returns A module configurator that can be used to configure the HTTP module.
113
+ */
64
114
  export const configureHttpClient = <TRef = unknown>(
65
115
  name: string,
66
116
  args: HttpClientOptions<HttpClientMsal>,
@@ -71,6 +121,9 @@ export const configureHttpClient = <TRef = unknown>(
71
121
  },
72
122
  });
73
123
 
124
+ /**
125
+ * Declares a module named '@equinor/fusion-framework-module' that contains an interface named 'Modules' with a property 'http' of type 'HttpMsalModule'.
126
+ */
74
127
  declare module '@equinor/fusion-framework-module' {
75
128
  interface Modules {
76
129
  http: HttpMsalModule;
package/src/provider.ts CHANGED
@@ -15,12 +15,27 @@ export class ClientNotFoundException extends Error {
15
15
  }
16
16
 
17
17
  export interface IHttpClientProvider<TClient extends IHttpClient = IHttpClient> {
18
+ /**
19
+ * The default HTTP request handler used by the HttpClientProvider.
20
+ * This handler is responsible for executing HTTP requests using the configured HttpClient.
21
+ */
18
22
  readonly defaultHttpRequestHandler: IHttpRequestHandler<HttpClientRequestInitType<TClient>>;
19
- /** check if a client is configured */
23
+
24
+ /**
25
+ * Checks if a client is configured with the given key.
26
+ * @param key - The key of the client to check.
27
+ * @returns `true` if a client is configured with the given key, `false` otherwise.
28
+ */
20
29
  hasClient(key: string): boolean;
21
- /** create a new http client */
30
+
31
+ /**
32
+ * Creates a new HTTP client instance with the specified key.
33
+ * @param key - The key of the HTTP client to create.
34
+ * @returns The created HTTP client instance.
35
+ */
22
36
  createClient(key: string): TClient;
23
37
  createClient(key: HttpClientOptions<TClient>): TClient;
38
+
24
39
  /**
25
40
  * Class cast creation of custom client
26
41
  * @example
@@ -33,6 +48,11 @@ export interface IHttpClientProvider<TClient extends IHttpClient = IHttpClient>
33
48
  createCustomClient<T extends HttpClient>(key: string): T;
34
49
  }
35
50
 
51
+ /**
52
+ * Checks if a given string is a valid URL.
53
+ * @param url - The string to check for a valid URL.
54
+ * @returns `true` if the input string is a valid URL, `false` otherwise.
55
+ */
36
56
  const isURL = (url: string) => {
37
57
  const pattern = new RegExp(
38
58
  '^(https?:\\/\\/)?' + // protocol
@@ -46,19 +66,52 @@ const isURL = (url: string) => {
46
66
  return pattern.test(url);
47
67
  };
48
68
 
69
+ /**
70
+ * The `HttpClientProvider` class is responsible for managing HTTP client instances and their configuration.
71
+ * It provides methods to check if a client is configured, create new client instances, and create custom client instances.
72
+ */
49
73
  export class HttpClientProvider<TClient extends IHttpClient = IHttpClient>
50
74
  implements IHttpClientProvider<TClient>
51
75
  {
76
+ /**
77
+ * Gets the default HTTP request handler for the HTTP client provider.
78
+ * @returns The default HTTP request handler.
79
+ */
52
80
  get defaultHttpRequestHandler(): IHttpRequestHandler<HttpClientRequestInitType<TClient>> {
53
81
  return this.config.defaultHttpRequestHandler;
54
82
  }
55
83
 
56
84
  constructor(protected config: IHttpClientConfigurator<TClient>) {}
57
85
 
86
+ /**
87
+ * Checks if a client with the given key is configured in the `HttpClientProvider`.
88
+ * @param key - The key of the HTTP client to check.
89
+ * @returns `true` if a client with the given key is configured, `false` otherwise.
90
+ */
58
91
  public hasClient(key: string): boolean {
59
92
  return Object.keys(this.config.clients).includes(key);
60
93
  }
61
94
 
95
+ /**
96
+ * Creates a new HTTP client instance with the specified configuration.
97
+ *
98
+ * @param keyOrConfig - The key or configuration object for the HTTP client.
99
+ * @returns The created HTTP client instance.
100
+ *
101
+ * @remarks
102
+ * This method resolves the configuration for the HTTP client based on the provided `keyOrConfig` parameter.
103
+ * If a string is provided, it is treated as the key for a pre-configured client in the `HttpClientProvider`.
104
+ * If an `HttpClientOptions` object is provided, it is used as the configuration for the new client instance.
105
+ *
106
+ * The method sets up the HTTP client with the following options:
107
+ * - `baseUri`: The base URI for the HTTP client.
108
+ * - `defaultScopes`: The default scopes to be used for authentication.
109
+ * - `onCreate`: An optional callback function that is called when the client instance is created.
110
+ * - `ctor`: The constructor function for the HTTP client, defaulting to the configured `defaultHttpClientCtor`.
111
+ * - `requestHandler`: The HTTP request handler to be used by the client, defaulting to the `defaultHttpRequestHandler`.
112
+ *
113
+ * The created HTTP client instance is returned.
114
+ */
62
115
  public createClient(keyOrConfig: string | HttpClientOptions<TClient>): TClient {
63
116
  const config = this._resolveConfig(keyOrConfig);
64
117
  const {
@@ -75,10 +128,31 @@ export class HttpClientProvider<TClient extends IHttpClient = IHttpClient>
75
128
  return instance as TClient;
76
129
  }
77
130
 
131
+ /**
132
+ * Creates a new HTTP client instance with the specified configuration.
133
+ *
134
+ * @param key - The key of the pre-configured HTTP client to create.
135
+ * @returns The created HTTP client instance, cast to the specified type `T`.
136
+ *
137
+ * @remarks
138
+ * This method delegates to the `createClient` method, but casts the returned
139
+ * instance to the specified type `T`. This can be useful when you need to
140
+ * work with a specific HTTP client implementation, but the `HttpClientProvider`
141
+ * is configured to use a different implementation.
142
+ */
78
143
  public createCustomClient<T extends HttpClient>(key: string): T {
79
144
  return this.createClient(key) as unknown as T;
80
145
  }
81
146
 
147
+ /**
148
+ * Resolves the configuration for an HTTP client based on the provided `keyOrConfig` parameter.
149
+ *
150
+ * If a string is provided, it is treated as the key for a pre-configured client in the `HttpClientProvider`.
151
+ * If an `HttpClientOptions` object is provided, it is used as the configuration for the new client instance.
152
+ *
153
+ * @param keyOrConfig - The key or configuration object for the HTTP client.
154
+ * @returns The resolved HTTP client configuration.
155
+ */
82
156
  protected _resolveConfig(
83
157
  keyOrConfig: string | HttpClientOptions<TClient>,
84
158
  ): HttpClientOptions<TClient> {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '6.0.0';
2
+ export const version = '6.0.2';