@equinor/fusion-framework-module-http
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -6,2 +6,13 @@ # Change Log | ||
# 0.3.0 (2022-06-14) | ||
### Features | ||
* **module-http:** expose response processor ([e4551c5](https://github.com/equinor/fusion-framework/commit/e4551c549654ef25f33eef72ebc2fcc02ab552a2)) | ||
## 0.2.2 (2022-06-13) | ||
@@ -8,0 +19,0 @@ |
@@ -15,6 +15,8 @@ import { firstValueFrom, of, Subject } from 'rxjs'; | ||
} | ||
export class HttpResponseHandler extends ProcessOperators { | ||
} | ||
export class HttpClient { | ||
uri; | ||
requestHandler; | ||
responseHandler = (x) => Promise.resolve(x); | ||
responseHandler; | ||
_request$ = new Subject(); | ||
@@ -31,3 +33,4 @@ _response$ = new Subject(); | ||
this.uri = uri; | ||
this.requestHandler = options?.requestHandler || new HttpRequestHandler(); | ||
this.requestHandler = options?.requestHandler ?? new HttpRequestHandler(); | ||
this.responseHandler = options?.responseHandler ?? new HttpResponseHandler(); | ||
this._init(); | ||
@@ -74,3 +77,3 @@ } | ||
_prepareResponse(response) { | ||
return this.responseHandler(response); | ||
return this.responseHandler.process(response); | ||
} | ||
@@ -77,0 +80,0 @@ _resolveUrl(path) { |
@@ -1,2 +0,2 @@ | ||
import { from } from 'rxjs'; | ||
import { from, of } from 'rxjs'; | ||
import { last, mergeScan } from 'rxjs/operators'; | ||
@@ -7,3 +7,3 @@ export class ProcessOperators { | ||
if (Object.keys(this._operators).includes(key)) | ||
throw Error(`Operator [${key}] allready defined`); | ||
throw Error(`Operator [${key}] already defined`); | ||
return this.set(key, operator); | ||
@@ -19,2 +19,6 @@ } | ||
process(request) { | ||
const operators = Object.values(this._operators); | ||
if (!operators.length) { | ||
return of(request); | ||
} | ||
return from(Object.values(this._operators)).pipe(mergeScan((value, operator) => Promise.resolve(operator(value)).then((x) => x ?? value), request, 1), last()); | ||
@@ -21,0 +25,0 @@ } |
@@ -13,6 +13,8 @@ import { Observable, ObservableInput, Subject } from 'rxjs'; | ||
} | ||
export declare type HttpClientCreateOptions<T extends FetchRequest = FetchRequest> = { | ||
requestHandler: HttpRequestHandler<T>; | ||
export declare class HttpResponseHandler<T = Response> extends ProcessOperators<T> { | ||
} | ||
export declare type HttpClientCreateOptions<TRequest extends FetchRequest = FetchRequest, TResponse = Response> = { | ||
requestHandler: HttpRequestHandler<TRequest>; | ||
responseHandler: HttpResponseHandler<TResponse>; | ||
}; | ||
export declare type HttpResponseHandler<T> = (response: Response) => Promise<T>; | ||
export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = Response> { | ||
@@ -39,3 +41,3 @@ uri: string; | ||
get response$(): Observable<TResponse>; | ||
constructor(uri: string, options?: HttpClientCreateOptions<TRequest>); | ||
constructor(uri: string, options?: Partial<HttpClientCreateOptions<TRequest, TResponse>>); | ||
protected _init(): void; | ||
@@ -49,4 +51,4 @@ fetch<T = TResponse>(path: string, args?: FetchRequestInit<T, TRequest, TResponse>): Observable<T>; | ||
protected _prepareRequest(init: TRequest): ObservableInput<TRequest>; | ||
protected _prepareResponse(response: Response): ObservableInput<TResponse>; | ||
protected _prepareResponse(response: TResponse): ObservableInput<TResponse>; | ||
protected _resolveUrl(path: string): string; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { HttpRequestHandler, FetchRequest, IHttpClient } from './client'; | ||
import { HttpRequestHandler, FetchRequest, IHttpClient, HttpResponseHandler } from './client'; | ||
interface HttpClientConstructorOptions<TInit extends FetchRequest> { | ||
@@ -14,2 +14,3 @@ requestHandler: HttpRequestHandler<TInit>; | ||
requestHandler?: HttpRequestHandler<HttpClientRequestInitType<TClient>>; | ||
responseHandler?: HttpResponseHandler<HttpClientRequestInitType<TClient>>; | ||
} | ||
@@ -16,0 +17,0 @@ declare type HttpClientRequestInitType<T extends IHttpClient> = T extends IHttpClient<infer U> ? U : never; |
{ | ||
"name": "@equinor/fusion-framework-module-http", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -37,3 +37,3 @@ "main": "./dist/esm/index.js", | ||
}, | ||
"gitHead": "e4d7cad7992443920ba9549d8b3ad8c579a26522" | ||
"gitHead": "b61d74513171d9b2ba84a6091966f6250f1a6aa0" | ||
} |
@@ -39,7 +39,13 @@ import { firstValueFrom, Observable, ObservableInput, of, Subject } from 'rxjs'; | ||
export type HttpClientCreateOptions<T extends FetchRequest = FetchRequest> = { | ||
requestHandler: HttpRequestHandler<T>; | ||
export class HttpResponseHandler<T = Response> extends ProcessOperators<T> {} | ||
export type HttpClientCreateOptions< | ||
TRequest extends FetchRequest = FetchRequest, | ||
TResponse = Response | ||
> = { | ||
requestHandler: HttpRequestHandler<TRequest>; | ||
responseHandler: HttpResponseHandler<TResponse>; | ||
}; | ||
export type HttpResponseHandler<T> = (response: Response) => Promise<T>; | ||
// export type HttpResponseHandler<T> = (response: Response) => Promise<T>; | ||
@@ -110,3 +116,3 @@ /** | ||
* const input = document.getElementById('input'); | ||
* input.addEventlistner('input', (e) => { | ||
* input.addEventlistener('input', (e) => { | ||
* try{ | ||
@@ -159,4 +165,3 @@ * // if a controller is defined, request might be ongoing | ||
readonly responseHandler: HttpResponseHandler<TResponse> = (x: Response) => | ||
Promise.resolve(x as unknown as TResponse); | ||
readonly responseHandler: HttpResponseHandler<TResponse>; | ||
@@ -179,4 +184,8 @@ /** stream of requests that are about to be executed */ | ||
constructor(public uri: string, options?: HttpClientCreateOptions<TRequest>) { | ||
this.requestHandler = options?.requestHandler || new HttpRequestHandler<TRequest>(); | ||
constructor( | ||
public uri: string, | ||
options?: Partial<HttpClientCreateOptions<TRequest, TResponse>> | ||
) { | ||
this.requestHandler = options?.requestHandler ?? new HttpRequestHandler<TRequest>(); | ||
this.responseHandler = options?.responseHandler ?? new HttpResponseHandler<TResponse>(); | ||
this._init(); | ||
@@ -248,3 +257,3 @@ } | ||
/** prepare response, allow extensions to modify response */ | ||
switchMap((x) => this._prepareResponse(x)), | ||
switchMap((x) => this._prepareResponse(x as unknown as TResponse)), | ||
/** push response to event buss */ | ||
@@ -264,4 +273,4 @@ tap((x) => this._response$.next(x)), | ||
protected _prepareResponse(response: Response): ObservableInput<TResponse> { | ||
return this.responseHandler(response); | ||
protected _prepareResponse(response: TResponse): ObservableInput<TResponse> { | ||
return this.responseHandler.process(response); | ||
} | ||
@@ -268,0 +277,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { HttpRequestHandler, FetchRequest, IHttpClient } from './client'; | ||
import { HttpRequestHandler, FetchRequest, IHttpClient, HttpResponseHandler } from './client'; | ||
@@ -20,2 +20,3 @@ interface HttpClientConstructorOptions<TInit extends FetchRequest> { | ||
requestHandler?: HttpRequestHandler<HttpClientRequestInitType<TClient>>; | ||
responseHandler?: HttpResponseHandler<HttpClientRequestInitType<TClient>>; | ||
} | ||
@@ -22,0 +23,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { from, Observable } from 'rxjs'; | ||
import { from, Observable, of } from 'rxjs'; | ||
import { last, mergeScan } from 'rxjs/operators'; | ||
@@ -18,3 +18,3 @@ | ||
if (Object.keys(this._operators).includes(key)) | ||
throw Error(`Operator [${key}] allready defined`); | ||
throw Error(`Operator [${key}] already defined`); | ||
return this.set(key, operator); | ||
@@ -42,2 +42,7 @@ } | ||
process(request: T): Observable<T> { | ||
const operators = Object.values(this._operators); | ||
/** if no operators registered, just return the observable value */ | ||
if (!operators.length) { | ||
return of(request); | ||
} | ||
return from(Object.values(this._operators)).pipe( | ||
@@ -44,0 +49,0 @@ mergeScan( |
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
112658
945