UNPKG

@rxap/remote-method

Version:

This package provides abstractions for defining and executing remote methods in Angular applications. It includes features such as automatic refreshing, proxying, and error handling. It offers a structured way to manage remote calls and their dependencies

1 lines 15.8 kB
{"version":3,"file":"rxap-remote-method-http.mjs","sources":["../../../../../packages/angular/remote-method/http/src/lib/base-http.remote-method.ts","../../../../../packages/angular/remote-method/http/src/lib/http.remote-method.ts","../../../../../packages/angular/remote-method/http/src/lib/http-remote-method.loader.ts","../../../../../packages/angular/remote-method/http/src/index.ts","../../../../../packages/angular/remote-method/http/src/rxap-remote-method-http.ts"],"sourcesContent":["import {\n Inject,\n Injectable,\n Injector,\n Optional,\n} from '@angular/core';\nimport { HttpClient, HttpHeaders, HttpParams, HttpRequest } from '@angular/common/http';\nimport {\n BaseRemoteMethod,\n BaseRemoteMethodMetadata,\n REMOTE_METHOD_META_DATA,\n RxapRemoteMethodError,\n} from '@rxap/remote-method';\nimport { hasIndexSignature } from '@rxap/utilities';\n\nexport interface HttpRemoteMethodMetadata extends BaseRemoteMethodMetadata {\n url: string | (() => string);\n method: 'DELETE' | 'GET' | 'HEAD' | 'JSONP' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH';\n headers?: HttpHeaders;\n reportProgress?: boolean;\n params?: HttpParams;\n responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';\n withCredentials?: boolean;\n timeout?: number;\n retry?: number;\n}\n\nexport interface HttpRemoteMethodParameter<PathParams = any> {\n headers?: HttpHeaders;\n reportProgress?: boolean;\n params?: HttpParams;\n responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';\n withCredentials?: boolean;\n body?: any | null;\n setHeaders?: {\n [name: string]: string | string[];\n };\n setParams?: {\n [param: string]: string;\n };\n pathParams?: PathParams;\n}\n\n@Injectable()\nexport abstract class BaseHttpRemoteMethod<ReturnType = any,\n PathParams = any,\n Parameter extends object = HttpRemoteMethodParameter<PathParams>,\n MetaData extends HttpRemoteMethodMetadata = HttpRemoteMethodMetadata>\n extends BaseRemoteMethod<ReturnType, Parameter, MetaData> {\n\n public timeout = 60 * 1000;\n protected _httpRequest!: HttpRequest<ReturnType>;\n\n constructor(\n @Inject(HttpClient) public readonly http: HttpClient,\n @Inject(Injector) injector: Injector,\n @Optional() @Inject(REMOTE_METHOD_META_DATA) metaData: any | null = null,\n ) {\n super(injector, metaData);\n this.timeout = this.metadata.timeout ?? this.timeout;\n if (http === undefined) {\n throw new RxapRemoteMethodError(\n 'HttpClient is undefined. Ensure that the HttpClient is added to the deps property!',\n '',\n this.constructor.name,\n );\n }\n if (!(http instanceof HttpClient)) {\n throw new RxapRemoteMethodError(\n 'The property http is not an instance of HttpClinent. Check the deps property!',\n '',\n this.constructor.name,\n );\n }\n }\n\n public override init() {\n super.init();\n this._httpRequest = new HttpRequest<ReturnType>(\n this.metadata.method,\n this.getRequestUrl(),\n null,\n {\n headers: this.metadata.headers,\n reportProgress: this.metadata.reportProgress,\n params: this.metadata.params,\n responseType: this.metadata.responseType || 'json',\n withCredentials: this.metadata.withCredentials,\n },\n );\n }\n\n public updateRequest(parameters: Partial<HttpRemoteMethodParameter>): HttpRequest<ReturnType> {\n let url = this._httpRequest.url;\n\n if (parameters && parameters['pathParams']) {\n url = this.buildUrlWithParams(url, (parameters as any).pathParams);\n }\n\n return this._httpRequest.clone({\n withCredentials: this.metadata.withCredentials,\n ...parameters,\n url,\n });\n }\n\n public buildUrlWithParams(url: string, pathParams?: PathParams): string {\n if (pathParams) {\n\n if (!hasIndexSignature(pathParams)) {\n throw new RxapRemoteMethodError(\n `Path params for remote method '${ this.id }' has not an index signature`,\n '',\n this.constructor.name,\n );\n }\n\n const matches = url.match(/\\{[^}]+\\}/g);\n\n if (matches) {\n for (const match of matches) {\n const param = match.substr(1, match.length - 2);\n if (pathParams[param] === undefined) {\n throw new RxapRemoteMethodError(\n `Path params for remote method '${ this.id }' has not a defined value for '${ param }'`,\n '',\n this.constructor.name,\n );\n }\n url = url.replace(match, encodeURIComponent(pathParams[param]));\n }\n }\n }\n\n return url;\n }\n\n protected getRequestUrl(): string {\n if (typeof this.metadata.url === 'function') {\n return this.metadata.url();\n }\n return this.metadata.url;\n }\n\n}\n\nexport interface HttpRemoteMethodMetadata extends BaseRemoteMethodMetadata {\n url: string | (() => string);\n method: 'DELETE' | 'GET' | 'HEAD' | 'JSONP' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH';\n headers?: HttpHeaders;\n reportProgress?: boolean;\n params?: HttpParams;\n responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';\n withCredentials?: boolean;\n}\n\nexport interface HttpRemoteMethodParameter<PathParams = any> {\n headers?: HttpHeaders;\n reportProgress?: boolean;\n params?: HttpParams;\n responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';\n withCredentials?: boolean;\n body?: any | null;\n setHeaders?: {\n [name: string]: string | string[];\n };\n setParams?: {\n [param: string]: string;\n };\n pathParams?: PathParams;\n}\n","import { Injectable } from '@angular/core';\nimport { HttpEventType, HttpHeaderResponse, HttpProgressEvent, HttpResponse, HttpSentEvent } from '@angular/common/http';\nimport {\n filter,\n map,\n retry,\n tap,\n timeout,\n} from 'rxjs/operators';\nimport {\n BaseHttpRemoteMethod,\n HttpRemoteMethodMetadata,\n HttpRemoteMethodParameter,\n} from './base-http.remote-method';\nimport { firstValueFrom } from 'rxjs';\n\n@Injectable()\nexport class HttpRemoteMethod<ReturnType = any,\n PathParams = any,\n Parameter extends object = HttpRemoteMethodParameter<PathParams>,\n MetaData extends HttpRemoteMethodMetadata = HttpRemoteMethodMetadata>\n extends BaseHttpRemoteMethod<ReturnType, PathParams, Parameter, MetaData> {\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n public onSentEvent(event: HttpSentEvent): void {\n }\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n public onHeaderResponse(event: HttpHeaderResponse): void {\n }\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n public onResponse(event: HttpResponse<ReturnType>): void {\n }\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n public onProgressEvent(event: HttpProgressEvent): void {\n }\n\n // TODO : make abstract to force implementation to handel parameter transformation\n public createHttpRequestParameters(parameters?: Parameter): Partial<HttpRemoteMethodParameter> {\n return parameters ?? {} as any;\n }\n\n public transformer(response: any): ReturnType {\n return response;\n }\n\n protected _call(parameters?: Parameter): Promise<ReturnType> {\n return firstValueFrom(this.http.request<ReturnType>(this.updateRequest(this.createHttpRequestParameters(parameters)))\n .pipe(\n retry(this.metadata.retry ?? 0),\n tap(event => {\n switch (event.type) {\n\n case HttpEventType.Sent:\n this.onSentEvent(event);\n break;\n\n case HttpEventType.Response:\n this.onResponse(event);\n break;\n\n case HttpEventType.DownloadProgress:\n this.onProgressEvent(event);\n break;\n\n case HttpEventType.UploadProgress:\n this.onProgressEvent(event);\n break;\n\n case HttpEventType.ResponseHeader:\n this.onHeaderResponse(event);\n break;\n\n }\n }),\n filter((event: any) => event instanceof HttpResponse),\n map((event: HttpResponse<ReturnType>) => event.body),\n map(body => this.transformer(body)),\n timeout(this.timeout),\n ));\n }\n\n}\n","import {\n BaseRemoteMethod,\n BaseRemoteMethodMetadata,\n RemoteMethodLoader,\n RxapRemoteMethodError,\n} from '@rxap/remote-method';\nimport { HttpRemoteMethod } from './http.remote-method';\nimport {\n Inject,\n Injectable,\n InjectFlags,\n Injector,\n} from '@angular/core';\nimport { IdOrInstanceOrToken } from '@rxap/definition';\n\n@Injectable({ providedIn: 'root' })\nexport class HttpRemoteMethodLoader {\n\n // Instead of extanding the RemoteMethodLoader class the RemoteMethodLoader instance\n // will be injected, else there could be multiple instance of RemoteMethodLoader's. Then\n // the RefreshService will not trigger a refresh for HttpRemoteMethod's\n constructor(@Inject(RemoteMethodLoader) private readonly remoteMethodLoader: RemoteMethodLoader) {\n }\n\n public request$<ReturnType = any, Parameters = any, Metadata extends BaseRemoteMethodMetadata = BaseRemoteMethodMetadata>(\n remoteMethodIdOrInstanceOrToken: IdOrInstanceOrToken<BaseRemoteMethod<ReturnType, Parameters, Metadata>>,\n parameters: Parameters,\n metadata?: Partial<BaseRemoteMethodMetadata>,\n injector?: Injector,\n notFoundValue?: BaseRemoteMethod<ReturnType, Parameters, Metadata>,\n flags?: InjectFlags,\n ) {\n\n const remoteMethod = this.remoteMethodLoader.load(\n remoteMethodIdOrInstanceOrToken,\n metadata,\n injector,\n notFoundValue,\n flags,\n );\n\n if (!(remoteMethod instanceof HttpRemoteMethod)) {\n throw new RxapRemoteMethodError(`The remote method is not a HttpRemoteMethod`, '', 'HttpRemoteMethodLoader');\n }\n\n return remoteMethod.call(parameters);\n }\n\n}\n","// region \nexport * from './lib/base-http.remote-method';\nexport * from './lib/http-remote-method.loader';\nexport * from './lib/http.remote-method';\n// endregion\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;;;;AA4CM,MAAgB,oBAIpB,SAAQ,gBAAiD,CAAA;AAKzD,IAAA,WAAA,CACsC,IAAgB,EAClC,QAAkB,EACS,WAAuB,IAAI,EAAA;AAExE,QAAA,KAAK,CAAC,QAAQ,EAAE,QAAQ,CAAC;QAJW,IAAI,CAAA,IAAA,GAAJ,IAAI;AAJnC,QAAA,IAAA,CAAA,OAAO,GAAG,EAAE,GAAG,IAAI;AASxB,QAAA,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO;AACpD,QAAA,IAAI,IAAI,KAAK,SAAS,EAAE;AACtB,YAAA,MAAM,IAAI,qBAAqB,CAC7B,oFAAoF,EACpF,EAAE,EACF,IAAI,CAAC,WAAW,CAAC,IAAI,CACtB;;AAEH,QAAA,IAAI,EAAE,IAAI,YAAY,UAAU,CAAC,EAAE;AACjC,YAAA,MAAM,IAAI,qBAAqB,CAC7B,+EAA+E,EAC/E,EAAE,EACF,IAAI,CAAC,WAAW,CAAC,IAAI,CACtB;;;IAIW,IAAI,GAAA;QAClB,KAAK,CAAC,IAAI,EAAE;AACZ,QAAA,IAAI,CAAC,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