@equinor/fusion-framework 1.1.0-alpha.1 → 1.1.0-alpha.5

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 (56) hide show
  1. package/dist/esm/index.js +8 -19
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/esm/modules.js +6 -0
  4. package/dist/esm/modules.js.map +1 -0
  5. package/dist/tsconfig.tsbuildinfo +1 -1
  6. package/dist/types/index.d.ts +13 -7
  7. package/dist/types/modules.d.ts +9 -0
  8. package/package.json +6 -11
  9. package/src/index.ts +29 -26
  10. package/src/modules.ts +16 -0
  11. package/tsconfig.json +12 -4
  12. package/README.md +0 -190
  13. package/dist/build/index.js +0 -13166
  14. package/dist/build/index.js.map +0 -1
  15. package/dist/build/src/__test__.d.ts +0 -1
  16. package/dist/build/src/index.d.ts +0 -8
  17. package/dist/build/src/services/http/http-client-msal.d.ts +0 -9
  18. package/dist/build/src/services/http/http-client.d.ts +0 -31
  19. package/dist/build/src/services/http/http-configurator.d.ts +0 -25
  20. package/dist/build/src/services/http/http-provider.d.ts +0 -7
  21. package/dist/build/src/services/http/index.d.ts +0 -4
  22. package/dist/build/src/services/index.d.ts +0 -22
  23. package/dist/build/src/util/process-operators.d.ts +0 -9
  24. package/dist/build/tsconfig.rollup.tsbuildinfo +0 -1
  25. package/dist/esm/__test__.js +0 -26
  26. package/dist/esm/__test__.js.map +0 -1
  27. package/dist/esm/services/http/http-client-msal.js +0 -11
  28. package/dist/esm/services/http/http-client-msal.js.map +0 -1
  29. package/dist/esm/services/http/http-client.js +0 -54
  30. package/dist/esm/services/http/http-client.js.map +0 -1
  31. package/dist/esm/services/http/http-configurator.js +0 -23
  32. package/dist/esm/services/http/http-configurator.js.map +0 -1
  33. package/dist/esm/services/http/http-provider.js +0 -14
  34. package/dist/esm/services/http/http-provider.js.map +0 -1
  35. package/dist/esm/services/http/index.js +0 -5
  36. package/dist/esm/services/http/index.js.map +0 -1
  37. package/dist/esm/services/index.js +0 -59
  38. package/dist/esm/services/index.js.map +0 -1
  39. package/dist/esm/util/process-operators.js +0 -25
  40. package/dist/esm/util/process-operators.js.map +0 -1
  41. package/dist/types/__test__.d.ts +0 -1
  42. package/dist/types/services/http/http-client-msal.d.ts +0 -9
  43. package/dist/types/services/http/http-client.d.ts +0 -31
  44. package/dist/types/services/http/http-configurator.d.ts +0 -25
  45. package/dist/types/services/http/http-provider.d.ts +0 -7
  46. package/dist/types/services/http/index.d.ts +0 -4
  47. package/dist/types/services/index.d.ts +0 -22
  48. package/dist/types/util/process-operators.d.ts +0 -9
  49. package/src/__test__.ts +0 -35
  50. package/src/services/http/http-client-msal.ts +0 -16
  51. package/src/services/http/http-client.ts +0 -91
  52. package/src/services/http/http-configurator.ts +0 -66
  53. package/src/services/http/http-provider.ts +0 -19
  54. package/src/services/http/index.ts +0 -4
  55. package/src/services/index.ts +0 -92
  56. package/src/util/process-operators.ts +0 -50
@@ -1,25 +0,0 @@
1
- import { BehaviorSubject, from } from 'rxjs';
2
- import { withLatestFrom, concatMap, last } from 'rxjs/operators';
3
- export class ProcessOperators {
4
- _operators = {};
5
- add(key, operator) {
6
- if (Object.keys(this._operators).includes(key))
7
- throw Error(`Operator [${key}] allready defined`);
8
- return this.set(key, operator);
9
- }
10
- set(key, operator) {
11
- this._operators[key] = operator;
12
- return this;
13
- }
14
- get(key) {
15
- return this._operators[key];
16
- }
17
- process(request) {
18
- const request$ = new BehaviorSubject(request);
19
- from(Object.values(this._operators))
20
- .pipe(withLatestFrom(request$), concatMap(async ([operator, value]) => (await operator(value)) || value))
21
- .subscribe(request$);
22
- return request$.pipe(last());
23
- }
24
- }
25
- //# sourceMappingURL=process-operators.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"process-operators.js","sourceRoot":"","sources":["../../../src/util/process-operators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,IAAI,EAAc,MAAM,MAAM,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAQjE,MAAM,OAAO,gBAAgB;IACf,UAAU,GAAuC,EAAE,CAAC;IAK9D,GAAG,CAAC,GAAW,EAAE,QAA4B;QACzC,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC;YAC1C,MAAM,KAAK,CAAC,aAAa,GAAG,oBAAoB,CAAC,CAAC;QACtD,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACnC,CAAC;IAKD,GAAG,CAAC,GAAW,EAAE,QAA4B;QACzC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC;QAChC,OAAO,IAAI,CAAC;IAChB,CAAC;IAKD,GAAG,CAAC,GAAW;QACX,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAKD,OAAO,CAAC,OAAU;QACd,MAAM,QAAQ,GAAG,IAAI,eAAe,CAAI,OAAO,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;aAC/B,IAAI,CACD,cAAc,CAAC,QAAQ,CAAC,EACxB,SAAS,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,QAAQ,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,CAAC,CAC3E;aACA,SAAS,CAAC,QAAQ,CAAC,CAAC;QACzB,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACjC,CAAC;CACJ"}
@@ -1 +0,0 @@
1
- export {};
@@ -1,9 +0,0 @@
1
- import { HttpClient, HttpRequestInit } from './http-client';
2
- declare type RequestInitMsal = HttpRequestInit & {
3
- scopes?: string[];
4
- };
5
- export declare class HttpClientMsal extends HttpClient<RequestInitMsal> {
6
- defaultScope: string[];
7
- fetch(init: Omit<RequestInitMsal, 'uri'> | string): ReturnType<HttpClient['fetch']>;
8
- }
9
- export default HttpClientMsal;
@@ -1,31 +0,0 @@
1
- import { Observable, ObservableInput, Subject } from 'rxjs';
2
- import { ProcessOperators } from '../../util/process-operators';
3
- export declare type HttpRequestInit = RequestInit & {
4
- uri: string;
5
- path: string;
6
- };
7
- export declare class HttpRequestHandler<T extends HttpRequestInit = HttpRequestInit> extends ProcessOperators<T> {
8
- setHeader(name: string, value: string): HttpRequestHandler<T>;
9
- }
10
- export declare type HttpClientCreateOptions<T extends HttpRequestInit = HttpRequestInit> = {
11
- requestHandler: HttpRequestHandler<T>;
12
- };
13
- export declare type HttpResponseHandler<T> = (response: Response) => Promise<T>;
14
- export declare class HttpClient<TRequest extends HttpRequestInit = HttpRequestInit, TResponse = Response> {
15
- uri: string;
16
- readonly requestHandler: HttpRequestHandler<TRequest>;
17
- readonly responseHandler: HttpResponseHandler<TResponse>;
18
- protected _request$: Subject<TRequest>;
19
- protected _response$: Subject<TResponse>;
20
- protected _abort$: Subject<void>;
21
- get request$(): Observable<TRequest>;
22
- get response$(): Observable<TResponse>;
23
- constructor(uri: string, options?: HttpClientCreateOptions<TRequest>);
24
- protected _init(): void;
25
- protected _prepareRequest(init: TRequest): ObservableInput<TRequest>;
26
- protected _prepareResponse(response: Response): ObservableInput<TResponse>;
27
- protected _resolveUrl(path: string): string;
28
- fetch(init: Omit<TRequest, 'uri'> | string): Observable<TResponse>;
29
- fetchAsync(init: Omit<TRequest, 'uri'> | string): Promise<TResponse>;
30
- abort(): void;
31
- }
@@ -1,25 +0,0 @@
1
- import { HttpClient, HttpRequestHandler, HttpRequestInit } from './http-client';
2
- interface HttpClientConstructorOptions<TInit extends HttpRequestInit> {
3
- requestHandler: HttpRequestHandler<TInit>;
4
- }
5
- interface HttpClientConstructor<TClient extends HttpClient> {
6
- new (uri: string, options: HttpClientConstructorOptions<HttpClientRequestInitType<TClient>>): TClient;
7
- }
8
- interface HttpClientOptions<TClient extends HttpClient> {
9
- defaultUri?: string;
10
- ctor?: HttpClientConstructor<TClient>;
11
- onCreate?: (client: TClient) => void;
12
- requestHandler?: HttpRequestHandler<HttpClientRequestInitType<TClient>>;
13
- }
14
- declare type HttpClientRequestInitType<T extends HttpClient> = T extends HttpClient<infer U> ? U : never;
15
- export declare class HttpClientConfigurator<TClient extends HttpClient = HttpClient> {
16
- protected _clients: Record<string, HttpClientOptions<TClient>>;
17
- get clients(): Record<string, HttpClientOptions<TClient>>;
18
- readonly defaultHttpClientCtor: HttpClientConstructor<TClient>;
19
- readonly defaulHttpRequestHandler: HttpRequestHandler<HttpClientRequestInitType<TClient>>;
20
- constructor(client: HttpClientConstructor<TClient>);
21
- configureClient(name: string, uri: string): HttpClientConfigurator<TClient>;
22
- configureClient<T extends TClient>(name: string, args: HttpClientOptions<T>): HttpClientConfigurator<TClient>;
23
- configureClient<T extends TClient>(name: string, onCreate: (client: T) => void): HttpClientConfigurator<TClient>;
24
- }
25
- export default HttpClientConfigurator;
@@ -1,7 +0,0 @@
1
- import { HttpClient } from './http-client';
2
- import { HttpClientConfigurator } from './http-configurator';
3
- export declare class HttpClientProvider<TClient extends HttpClient = HttpClient> {
4
- protected config: HttpClientConfigurator<TClient>;
5
- constructor(config: HttpClientConfigurator<TClient>);
6
- createClient<T = TClient>(name: string): T;
7
- }
@@ -1,4 +0,0 @@
1
- export { HttpClient } from './http-client';
2
- export { HttpClientMsal } from './http-client-msal';
3
- export { HttpClientConfigurator } from './http-configurator';
4
- export { HttpClientProvider } from './http-provider';
@@ -1,22 +0,0 @@
1
- import { HttpClientProvider, HttpClientConfigurator, HttpClientMsal } from './http';
2
- interface AuthClient {
3
- acquireToken(req: {
4
- scopes: string[];
5
- }): Promise<{
6
- accessToken: string;
7
- } | void>;
8
- login(): void;
9
- }
10
- export declare type Services = {
11
- auth: AuthClient;
12
- http: HttpClientProvider<HttpClientMsal>;
13
- };
14
- export declare type ServiceInitiator = (config: ServiceConfig) => void | Promise<void>;
15
- interface ServiceConfig {
16
- auth: {
17
- client?: AuthClient;
18
- };
19
- http: HttpClientConfigurator<HttpClientMsal>;
20
- }
21
- export declare const createServices: (services?: Services | undefined) => Promise<(cb: ServiceInitiator) => Promise<Services>>;
22
- export default createServices;
@@ -1,9 +0,0 @@
1
- import { Observable } from 'rxjs';
2
- export declare type ProcessOperator<T, R = T> = (request: T) => R | void | Promise<R | void>;
3
- export declare class ProcessOperators<T> {
4
- protected _operators: Record<string, ProcessOperator<T>>;
5
- add(key: string, operator: ProcessOperator<T>): ProcessOperators<T>;
6
- set(key: string, operator: ProcessOperator<T>): ProcessOperators<T>;
7
- get(key: string): ProcessOperator<T>;
8
- process(request: T): Observable<T>;
9
- }
package/src/__test__.ts DELETED
@@ -1,35 +0,0 @@
1
- // TODO - DELETE ME!
2
- import createInstance, { AuthClient, createAuthClient } from '.';
3
-
4
- async () => {
5
- const testing = await createInstance((root) => {
6
- root.auth.client = createAuthClient('ddasdas', 'dsadasdas', '/msal/auth') as AuthClient;
7
-
8
- root.http.configureClient('portal', 'https://somewhere.com');
9
-
10
- root.http.configureClient('test', (client) => {
11
- client.uri = 'https://somewhere.com';
12
- client.defaultScope = ['https://somewhere.com/read'];
13
- client.requestHandler.add('test', (request) => {
14
- const headers = new Headers(request.headers);
15
- headers.append('x-test', 'testing');
16
- return { ...request, headers };
17
- });
18
- });
19
- });
20
- const client = testing.http.createClient('portal');
21
- const response = await client.fetchAsync('api/v2/f-u');
22
- if (!response.ok) {
23
- // do something;
24
- throw Error('invalid something');
25
- }
26
- // if(response.headers.has('content-type', 'application/json')){
27
- // return await response.json();
28
- // }
29
-
30
- testing.http.createClient;
31
-
32
- testing.registerApp('myapp', (app) => {
33
- app.http.configureClient('app-client', 'https://my-app.com');
34
- });
35
- };
@@ -1,16 +0,0 @@
1
- import { HttpClient, HttpRequestInit } from './http-client';
2
-
3
- type RequestInitMsal = HttpRequestInit & { scopes?: string[] };
4
-
5
- // TODO - add description
6
- export class HttpClientMsal extends HttpClient<RequestInitMsal> {
7
- public defaultScope: string[] = [];
8
-
9
- fetch(init: Omit<RequestInitMsal, 'uri'> | string): ReturnType<HttpClient['fetch']> {
10
- init = typeof init === 'string' ? { path: init } : init;
11
- Object.assign(init, { scopes: this.defaultScope.concat(init.scopes || []) });
12
- return super.fetch(init);
13
- }
14
- }
15
-
16
- export default HttpClientMsal;
@@ -1,91 +0,0 @@
1
- import { firstValueFrom, Observable, ObservableInput, of, Subject } from 'rxjs';
2
- import { switchMap, takeUntil, tap } from 'rxjs/operators';
3
- import { fromFetch } from 'rxjs/fetch';
4
-
5
- import { ProcessOperators } from '../../util/process-operators';
6
-
7
- export type HttpRequestInit = RequestInit & { uri: string; path: string };
8
-
9
- export class HttpRequestHandler<
10
- T extends HttpRequestInit = HttpRequestInit
11
- > extends ProcessOperators<T> {
12
- setHeader(name: string, value: string): HttpRequestHandler<T> {
13
- return this.set('header-' + name, (request) => {
14
- const headers = new Headers(request.headers);
15
- headers.append(name, value);
16
- return { ...request, headers };
17
- }) as unknown as HttpRequestHandler<T>;
18
- }
19
- }
20
-
21
- export type HttpClientCreateOptions<T extends HttpRequestInit = HttpRequestInit> = {
22
- requestHandler: HttpRequestHandler<T>;
23
- };
24
-
25
- export type HttpResponseHandler<T> = (response: Response) => Promise<T>;
26
-
27
- export class HttpClient<TRequest extends HttpRequestInit = HttpRequestInit, TResponse = Response> {
28
- readonly requestHandler: HttpRequestHandler<TRequest>;
29
-
30
- readonly responseHandler: HttpResponseHandler<TResponse> = (x: Response) =>
31
- Promise.resolve(x as unknown as TResponse);
32
-
33
- protected _request$ = new Subject<TRequest>();
34
- protected _response$ = new Subject<TResponse>();
35
- protected _abort$ = new Subject<void>();
36
-
37
- public get request$(): Observable<TRequest> {
38
- return this._request$.asObservable();
39
- }
40
-
41
- public get response$(): Observable<TResponse> {
42
- return this._response$.asObservable();
43
- }
44
-
45
- constructor(public uri: string, options?: HttpClientCreateOptions<TRequest>) {
46
- this.requestHandler = options?.requestHandler || new HttpRequestHandler<TRequest>();
47
- this._init();
48
- }
49
-
50
- protected _init(): void {
51
- // called by children for constructor setup
52
- }
53
-
54
- protected _prepareRequest(init: TRequest): ObservableInput<TRequest> {
55
- return this.requestHandler.process(init);
56
- }
57
-
58
- protected _prepareResponse(response: Response): ObservableInput<TResponse> {
59
- return this.responseHandler(response);
60
- }
61
-
62
- protected _resolveUrl(path: string): string {
63
- return [this.uri, path].join('/');
64
- }
65
-
66
- public fetch(init: Omit<TRequest, 'uri'> | string): Observable<TResponse> {
67
- const options = typeof init === 'string' ? { path: init } : init;
68
- return of({ ...options, uri: this._resolveUrl(options.path) } as TRequest).pipe(
69
- switchMap((x) => this._prepareRequest(x)),
70
- tap((x) => this._request$.next(x)),
71
- switchMap(({ uri, path: _path, ...args }) => fromFetch(uri, args)),
72
- switchMap((x) => this._prepareResponse(x)),
73
- tap((x) => this._response$.next(x)),
74
- takeUntil(this._abort$)
75
- );
76
- }
77
-
78
- /**
79
- * Fetch a resource as an promise
80
- */
81
- public fetchAsync(init: Omit<TRequest, 'uri'> | string): Promise<TResponse> {
82
- return firstValueFrom(this.fetch(init));
83
- }
84
-
85
- /**
86
- * Abort all ongoing request for current client
87
- */
88
- public abort(): void {
89
- this._abort$.next();
90
- }
91
- }
@@ -1,66 +0,0 @@
1
- import { HttpClient, HttpRequestHandler, HttpRequestInit } from './http-client';
2
-
3
- interface HttpClientConstructorOptions<TInit extends HttpRequestInit> {
4
- requestHandler: HttpRequestHandler<TInit>;
5
- }
6
-
7
- interface HttpClientConstructor<TClient extends HttpClient> {
8
- new (
9
- uri: string,
10
- options: HttpClientConstructorOptions<HttpClientRequestInitType<TClient>>
11
- ): TClient;
12
- }
13
-
14
- interface HttpClientOptions<TClient extends HttpClient> {
15
- defaultUri?: string;
16
- ctor?: HttpClientConstructor<TClient>;
17
- onCreate?: (client: TClient) => void;
18
- requestHandler?: HttpRequestHandler<HttpClientRequestInitType<TClient>>;
19
- }
20
-
21
- type HttpClientRequestInitType<T extends HttpClient> = T extends HttpClient<infer U> ? U : never;
22
-
23
- export class HttpClientConfigurator<TClient extends HttpClient = HttpClient> {
24
- protected _clients: Record<string, HttpClientOptions<TClient>> = {};
25
-
26
- public get clients(): Record<string, HttpClientOptions<TClient>> {
27
- return { ...this._clients };
28
- }
29
-
30
- readonly defaultHttpClientCtor: HttpClientConstructor<TClient>;
31
-
32
- readonly defaulHttpRequestHandler = new HttpRequestHandler<
33
- HttpClientRequestInitType<TClient>
34
- >();
35
-
36
- constructor(client: HttpClientConstructor<TClient>) {
37
- this.defaultHttpClientCtor = client;
38
- }
39
-
40
- configureClient(name: string, uri: string): HttpClientConfigurator<TClient>;
41
-
42
- configureClient<T extends TClient>(
43
- name: string,
44
- args: HttpClientOptions<T>
45
- ): HttpClientConfigurator<TClient>;
46
-
47
- configureClient<T extends TClient>(
48
- name: string,
49
- onCreate: (client: T) => void
50
- ): HttpClientConfigurator<TClient>;
51
-
52
- configureClient<T extends TClient>(
53
- name: string,
54
- args: string | HttpClientOptions<T> | HttpClientOptions<T>['onCreate']
55
- ): HttpClientConfigurator<TClient> {
56
- const argFn = typeof args === 'string' ? (x: T) => (x.uri = String(args)) : args;
57
- const options = typeof argFn === 'function' ? { onCreate: argFn } : argFn;
58
- this._clients[name] = {
59
- ...this._clients[name],
60
- ...(options as unknown as HttpClientOptions<TClient>),
61
- };
62
- return this;
63
- }
64
- }
65
-
66
- export default HttpClientConfigurator;
@@ -1,19 +0,0 @@
1
- import { HttpClient } from './http-client';
2
- import { HttpClientConfigurator } from './http-configurator';
3
-
4
- export class HttpClientProvider<TClient extends HttpClient = HttpClient> {
5
- constructor(protected config: HttpClientConfigurator<TClient>) {}
6
-
7
- createClient<T = TClient>(name: string): T {
8
- const {
9
- defaultUri,
10
- onCreate,
11
- ctor = this.config.defaultHttpClientCtor,
12
- requestHandler = this.config.defaulHttpRequestHandler,
13
- } = this.config.clients[name];
14
- const options = { requestHandler };
15
- const instance = new ctor(defaultUri || '', options) as TClient;
16
- onCreate && onCreate(instance as TClient);
17
- return instance as unknown as T;
18
- }
19
- }
@@ -1,4 +0,0 @@
1
- export { HttpClient } from './http-client';
2
- export { HttpClientMsal } from './http-client-msal';
3
- export { HttpClientConfigurator } from './http-configurator';
4
- export { HttpClientProvider } from './http-provider';
@@ -1,92 +0,0 @@
1
- import { HttpClientProvider, HttpClientConfigurator, HttpClientMsal } from './http';
2
-
3
- // TODO make provider
4
- interface AuthClient {
5
- acquireToken(req: { scopes: string[] }): Promise<{ accessToken: string } | void>;
6
- login(): void;
7
- }
8
-
9
- export type Services = {
10
- auth: AuthClient;
11
- http: HttpClientProvider<HttpClientMsal>;
12
- };
13
-
14
- export type ServiceInitiator = (config: ServiceConfig) => void | Promise<void>;
15
-
16
- interface ServiceConfig {
17
- auth: {
18
- client?: AuthClient;
19
- };
20
- http: HttpClientConfigurator<HttpClientMsal>;
21
- }
22
-
23
- type ServiceConfigurator = (
24
- config: Partial<ServiceConfig>,
25
- services?: Services
26
- ) => (services: Partial<Services>) => Partial<Services> | Promise<Partial<Services>>;
27
-
28
- const configureAuth: ServiceConfigurator = (
29
- config: Partial<ServiceConfig>,
30
- services?: Services
31
- ) => {
32
- config.auth = { client: services?.auth };
33
- return (_services: Partial<Services>) => {
34
- const client = config.auth?.client;
35
- if (!client) {
36
- throw Error('Missing auth Client');
37
- }
38
- return {
39
- auth: {
40
- client,
41
- acquireToken(args: { scopes: string[] }) {
42
- return client.acquireToken(args);
43
- },
44
- login() {
45
- client.login();
46
- },
47
- },
48
- };
49
- };
50
- };
51
-
52
- const configureHttp: ServiceConfigurator = (config: Partial<ServiceConfig>) => {
53
- config.http = new HttpClientConfigurator(HttpClientMsal);
54
- return (services: Partial<Services>) => {
55
- if (!config.http) {
56
- throw Error('Missing config for HTTP');
57
- }
58
- config.http.defaulHttpRequestHandler.add('msal', async (request) => {
59
- const { scopes = [] } = request;
60
- const token = await services.auth?.acquireToken({ scopes });
61
- if (token) {
62
- const headers = new Headers(request.headers);
63
- headers.append('Authorization', `Bearer ${token.accessToken}`);
64
- return { ...request, headers };
65
- }
66
- });
67
- return {
68
- http: new HttpClientProvider(config.http),
69
- };
70
- };
71
- };
72
-
73
- export const createServices = async (
74
- services?: Services
75
- ): Promise<(cb: ServiceInitiator) => Promise<Services>> => {
76
- const config = {};
77
- const build = {
78
- auth: configureAuth(config, services),
79
- http: configureHttp(config, services),
80
- };
81
- return async (init: (config: ServiceConfig) => void): Promise<Services> => {
82
- await Promise.resolve(init(config as ServiceConfig));
83
- const services = await Object.keys(build).reduce(async (acc, key) => {
84
- const obj = await acc;
85
- const provider = await Promise.resolve(build[key as keyof typeof build](obj));
86
- return Object.assign(obj, provider);
87
- }, Promise.resolve({}));
88
- return services as Services;
89
- };
90
- };
91
-
92
- export default createServices;
@@ -1,50 +0,0 @@
1
- import { BehaviorSubject, from, Observable } from 'rxjs';
2
- import { withLatestFrom, concatMap, last } from 'rxjs/operators';
3
-
4
- export type ProcessOperator<T, R = T> = (request: T) => R | void | Promise<R | void>;
5
-
6
- /**
7
- * Container for sync/async operators.
8
- * Pipes each operator sequential
9
- */
10
- export class ProcessOperators<T> {
11
- protected _operators: Record<string, ProcessOperator<T>> = {};
12
-
13
- /**
14
- * Add a new operator (throw error if allready defined)
15
- */
16
- add(key: string, operator: ProcessOperator<T>): ProcessOperators<T> {
17
- if (Object.keys(this._operators).includes(key))
18
- throw Error(`Operator [${key}] allready defined`);
19
- return this.set(key, operator);
20
- }
21
-
22
- /**
23
- * Add or sets a operator
24
- */
25
- set(key: string, operator: ProcessOperator<T>): ProcessOperators<T> {
26
- this._operators[key] = operator;
27
- return this;
28
- }
29
-
30
- /**
31
- * Get a operator, will return undefined on invalid key.
32
- */
33
- get(key: string): ProcessOperator<T> {
34
- return this._operators[key];
35
- }
36
-
37
- /**
38
- * Process registered processors.
39
- */
40
- process(request: T): Observable<T> {
41
- const request$ = new BehaviorSubject<T>(request);
42
- from(Object.values(this._operators))
43
- .pipe(
44
- withLatestFrom(request$),
45
- concatMap(async ([operator, value]) => (await operator(value)) || value)
46
- )
47
- .subscribe(request$);
48
- return request$.pipe(last());
49
- }
50
- }