@equinor/fusion-framework-module-http
Advanced tools
Comparing version 2.0.9 to 2.0.10
@@ -6,2 +6,11 @@ # Change Log | ||
## 2.0.10 (2022-10-03) | ||
### Bug Fixes | ||
* **module-http:** allow typing of fetch request ([de08783](https://github.com/equinor/fusion-framework/commit/de0878342d82249ffc7e1212230d0aa0e14d32cb)) | ||
## [2.0.9](https://github.com/equinor/fusion-framework/compare/@equinor/fusion-framework-module-http@2.0.8...@equinor/fusion-framework-module-http@2.0.9) (2022-09-29) | ||
@@ -8,0 +17,0 @@ |
import { HttpRequestHandler } from './lib/operators'; | ||
export class HttpClientConfigurator { | ||
_clients = {}; | ||
get clients() { | ||
return { ...this._clients }; | ||
} | ||
defaultHttpClientCtor; | ||
defaultHttpRequestHandler = new HttpRequestHandler(); | ||
constructor(client) { | ||
this._clients = {}; | ||
this.defaultHttpRequestHandler = new HttpRequestHandler(); | ||
this.defaultHttpClientCtor = client; | ||
} | ||
get clients() { | ||
return Object.assign({}, this._clients); | ||
} | ||
hasClient(name) { | ||
@@ -18,6 +17,3 @@ return Object.keys(this._clients).includes(name); | ||
const options = typeof argFn === 'function' ? { onCreate: argFn } : argFn; | ||
this._clients[name] = { | ||
...this._clients[name], | ||
...options, | ||
}; | ||
this._clients[name] = Object.assign(Object.assign({}, this._clients[name]), options); | ||
return this; | ||
@@ -24,0 +20,0 @@ } |
import { HttpClient } from './client'; | ||
export class HttpClientMsal extends HttpClient { | ||
defaultScopes = []; | ||
constructor() { | ||
super(...arguments); | ||
this.defaultScopes = []; | ||
} | ||
fetch$(path, init) { | ||
const args = Object.assign(init || {}, { | ||
scopes: this.defaultScopes.concat(init?.scopes || []), | ||
scopes: this.defaultScopes.concat((init === null || init === void 0 ? void 0 : init.scopes) || []), | ||
}); | ||
@@ -8,0 +11,0 @@ return super._fetch$(path, args); |
@@ -0,1 +1,12 @@ | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
import { firstValueFrom, of, Subject } from 'rxjs'; | ||
@@ -7,8 +18,12 @@ import { switchMap, takeUntil, tap } from 'rxjs/operators'; | ||
export class HttpClient { | ||
uri; | ||
requestHandler; | ||
responseHandler; | ||
_request$ = new Subject(); | ||
_response$ = new Subject(); | ||
_abort$ = new Subject(); | ||
constructor(uri, options) { | ||
var _a, _b; | ||
this.uri = uri; | ||
this._request$ = new Subject(); | ||
this._response$ = new Subject(); | ||
this._abort$ = new Subject(); | ||
this.requestHandler = (_a = options === null || options === void 0 ? void 0 : options.requestHandler) !== null && _a !== void 0 ? _a : new HttpRequestHandler(); | ||
this.responseHandler = (_b = options === null || options === void 0 ? void 0 : options.responseHandler) !== null && _b !== void 0 ? _b : new HttpResponseHandler(); | ||
this._init(); | ||
} | ||
get request$() { | ||
@@ -20,8 +35,2 @@ return this._request$.asObservable(); | ||
} | ||
constructor(uri, options) { | ||
this.uri = uri; | ||
this.requestHandler = options?.requestHandler ?? new HttpRequestHandler(); | ||
this.responseHandler = options?.responseHandler ?? new HttpResponseHandler(); | ||
this._init(); | ||
} | ||
_init() { | ||
@@ -39,11 +48,9 @@ } | ||
json$(path, args) { | ||
const body = typeof args?.body === 'object' ? JSON.stringify(args?.body) : args?.body; | ||
const selector = args?.selector ?? jsonSelector; | ||
const header = new Headers(args?.headers); | ||
var _a; | ||
const body = typeof (args === null || args === void 0 ? void 0 : args.body) === 'object' ? JSON.stringify(args === null || args === void 0 ? void 0 : args.body) : args === null || args === void 0 ? void 0 : args.body; | ||
const selector = (_a = args === null || args === void 0 ? void 0 : args.selector) !== null && _a !== void 0 ? _a : jsonSelector; | ||
const header = new Headers(args === null || args === void 0 ? void 0 : args.headers); | ||
header.append('Content-Type', 'application/json'); | ||
return this.fetch$(path, { | ||
...args, | ||
body, | ||
selector, | ||
}); | ||
return this.fetch$(path, Object.assign(Object.assign({}, args), { body, | ||
selector })); | ||
} | ||
@@ -63,7 +70,7 @@ json(path, args) { | ||
_fetch$(path, args) { | ||
const { selector, ...options } = args || {}; | ||
const response$ = of({ | ||
...options, | ||
uri: this._resolveUrl(path), | ||
}).pipe(switchMap((x) => this._prepareRequest(x)), tap((x) => this._request$.next(x)), switchMap(({ uri, path: _path, ...init }) => fromFetch(uri, init)), switchMap((x) => this._prepareResponse(x)), tap((x) => this._response$.next(x)), switchMap((x) => (selector ? selector(x) : Promise.resolve(x))), takeUntil(this._abort$)); | ||
const _a = args || {}, { selector } = _a, options = __rest(_a, ["selector"]); | ||
const response$ = of(Object.assign(Object.assign({}, options), { uri: this._resolveUrl(path) })).pipe(switchMap((x) => this._prepareRequest(x)), tap((x) => this._request$.next(x)), switchMap((_a) => { | ||
var { uri, path: _path } = _a, init = __rest(_a, ["uri", "path"]); | ||
return fromFetch(uri, init); | ||
}), switchMap((x) => this._prepareResponse(x)), tap((x) => this._response$.next(x)), switchMap((x) => (selector ? selector(x) : Promise.resolve(x))), takeUntil(this._abort$)); | ||
return response$; | ||
@@ -70,0 +77,0 @@ } |
import { from, of } from 'rxjs'; | ||
import { last, mergeScan } from 'rxjs/operators'; | ||
export class ProcessOperators { | ||
_operators = {}; | ||
constructor() { | ||
this._operators = {}; | ||
} | ||
add(key, operator) { | ||
@@ -22,5 +24,5 @@ if (Object.keys(this._operators).includes(key)) | ||
} | ||
return from(Object.values(this._operators)).pipe(mergeScan((value, operator) => Promise.resolve(operator(value)).then((x) => x ?? value), request, 1), last()); | ||
return from(Object.values(this._operators)).pipe(mergeScan((value, operator) => Promise.resolve(operator(value)).then((x) => x !== null && x !== void 0 ? x : value), request, 1), last()); | ||
} | ||
} | ||
//# sourceMappingURL=process-operators.js.map |
export const requestOperatorHeader = (key, value) => (request) => { | ||
const headers = new Headers(request.headers); | ||
headers.append(key, value); | ||
return { ...request, headers }; | ||
return Object.assign(Object.assign({}, request), { headers }); | ||
}; | ||
export default requestOperatorHeader; | ||
//# sourceMappingURL=request-operator-header.js.map |
@@ -16,9 +16,8 @@ export class ClientNotFoundException extends Error { | ||
export class HttpClientProvider { | ||
config; | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
get defaultHttpRequestHandler() { | ||
return this.config.defaultHttpRequestHandler; | ||
} | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
hasClient(key) { | ||
@@ -25,0 +24,0 @@ return Object.keys(this.config.clients).includes(key); |
import type { Observable } from 'rxjs'; | ||
import type { FetchRequestInit, FetchRequest } from '.'; | ||
import type { FetchRequestInit, FetchRequest, FetchResponse } from '.'; | ||
import { HttpClient } from './client'; | ||
@@ -7,4 +7,4 @@ declare type MsalFetchRequest = FetchRequest & { | ||
}; | ||
declare type MsalFetchRequestInit<TReturn = unknown, TRequest = FetchRequest, TResponse = Response> = FetchRequestInit<TReturn, TRequest, TResponse> & Pick<MsalFetchRequest, 'scopes'>; | ||
export declare class HttpClientMsal<TRequest extends MsalFetchRequest = MsalFetchRequest, TResponse = Response> extends HttpClient<TRequest, TResponse> { | ||
declare type MsalFetchRequestInit<TReturn = unknown, TRequest = FetchRequest, TResponse = FetchResponse> = FetchRequestInit<TReturn, TRequest, TResponse> & Pick<MsalFetchRequest, 'scopes'>; | ||
export declare class HttpClientMsal<TRequest extends MsalFetchRequest = MsalFetchRequest, TResponse = FetchResponse> extends HttpClient<TRequest, TResponse> { | ||
defaultScopes: string[]; | ||
@@ -11,0 +11,0 @@ fetch$<T = TResponse>(path: string, init?: MsalFetchRequestInit<T, TRequest, TResponse>): Observable<T>; |
import { Subject } from 'rxjs'; | ||
import type { Observable, ObservableInput } from 'rxjs'; | ||
import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators'; | ||
import type { FetchRequest, FetchRequestInit, IHttpClient, StreamResponse } from '.'; | ||
import type { FetchRequest, FetchRequestInit, FetchResponse, IHttpClient, StreamResponse } from '.'; | ||
export declare type HttpClientCreateOptions<TRequest extends FetchRequest = FetchRequest, TResponse = Response> = { | ||
@@ -9,3 +9,3 @@ requestHandler: IHttpRequestHandler<TRequest>; | ||
}; | ||
export declare class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = Response> implements IHttpClient<TRequest, TResponse> { | ||
export declare class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = FetchResponse> implements IHttpClient<TRequest, TResponse> { | ||
uri: string; | ||
@@ -24,4 +24,4 @@ readonly requestHandler: IHttpRequestHandler<TRequest>; | ||
fetchAsync<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>; | ||
json$<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): StreamResponse<T>; | ||
json<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>; | ||
json$<T = unknown>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): StreamResponse<T>; | ||
json<T = unknown>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>; | ||
jsonAsync<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Promise<T>; | ||
@@ -28,0 +28,0 @@ execute<T = TResponse, TMethod extends 'fetch' | 'fetch$' | 'json' | 'json$' = 'fetch'>(method: TMethod, path: string, init?: FetchRequestInit<T, TRequest, TResponse>): ReturnType<IHttpClient[TMethod]>; |
@@ -8,3 +8,6 @@ import type { ObservableInput, Observable } from 'rxjs'; | ||
}; | ||
export declare type FetchRequestInit<TReturn = unknown, TRequest = FetchRequest, TResponse = Response> = Omit<TRequest, 'uri' | 'path'> & { | ||
export declare type FetchResponse<T = unknown> = Response & { | ||
json(): Promise<T>; | ||
}; | ||
export declare type FetchRequestInit<TReturn = unknown, TRequest = FetchRequest, TResponse = FetchResponse<TReturn>> = Omit<TRequest, 'uri' | 'path'> & { | ||
selector?: (response: TResponse) => ObservableInput<TReturn>; | ||
@@ -11,0 +14,0 @@ }; |
{ | ||
"name": "@equinor/fusion-framework-module-http", | ||
"version": "2.0.9", | ||
"version": "2.0.10", | ||
"description": "", | ||
@@ -51,3 +51,3 @@ "main": "dist/esm/index.js", | ||
}, | ||
"gitHead": "81198e7772ed6e1f0c318fc37c5b4f2a9fe72ada" | ||
"gitHead": "8b7efac3ab5b1db6531a89b884714dd90cb482e8" | ||
} |
import type { Observable } from 'rxjs'; | ||
import type { FetchRequestInit, FetchRequest } from '.'; | ||
import type { FetchRequestInit, FetchRequest, FetchResponse } from '.'; | ||
import { HttpClient } from './client'; | ||
@@ -10,3 +10,3 @@ | ||
TRequest = FetchRequest, | ||
TResponse = Response | ||
TResponse = FetchResponse | ||
> = FetchRequestInit<TReturn, TRequest, TResponse> & Pick<MsalFetchRequest, 'scopes'>; | ||
@@ -17,3 +17,3 @@ | ||
TRequest extends MsalFetchRequest = MsalFetchRequest, | ||
TResponse = Response | ||
TResponse = FetchResponse | ||
> extends HttpClient<TRequest, TResponse> { | ||
@@ -20,0 +20,0 @@ /** Scope that will be applied to all request if no scope is provided in request object */ |
@@ -10,3 +10,3 @@ import { firstValueFrom, of, Subject } from 'rxjs'; | ||
import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators'; | ||
import type { FetchRequest, FetchRequestInit, IHttpClient, StreamResponse } from '.'; | ||
import type { FetchRequest, FetchRequestInit, FetchResponse, IHttpClient, StreamResponse } from '.'; | ||
@@ -22,3 +22,3 @@ export type HttpClientCreateOptions< | ||
/** Base http client for executing requests */ | ||
export class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = Response> | ||
export class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = FetchResponse> | ||
implements IHttpClient<TRequest, TResponse> | ||
@@ -82,3 +82,3 @@ { | ||
public json$<T = TResponse>( | ||
public json$<T = unknown>( | ||
path: string, | ||
@@ -98,3 +98,3 @@ args?: FetchRequestInit<T, TRequest, TResponse> | ||
public json<T = TResponse>( | ||
public json<T = unknown>( | ||
path: string, | ||
@@ -101,0 +101,0 @@ args?: FetchRequestInit<T, TRequest, TResponse> |
@@ -11,6 +11,10 @@ import type { ObservableInput, Observable } from 'rxjs'; | ||
export type FetchResponse<T = unknown> = Response & { | ||
json(): Promise<T>; | ||
}; | ||
export type FetchRequestInit< | ||
TReturn = unknown, | ||
TRequest = FetchRequest, | ||
TResponse = Response | ||
TResponse = FetchResponse<TReturn> | ||
> = Omit<TRequest, 'uri' | 'path'> & { | ||
@@ -17,0 +21,0 @@ selector?: (response: TResponse) => ObservableInput<TReturn>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136549
1166