UNPKG

@peppierre/typesafe-http-iots

Version:

Type-safe HTTP client for Angular using io-ts and fp-ts

98 lines (92 loc) 5.02 kB
import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import { throwError } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import * as i1 from '@angular/common/http'; import { HttpClient } from '@angular/common/http'; const IOTS_RESULT_TAG_LEFT = 'Left'; class TypesafeHttpService { constructor(http) { this.http = http; this.get = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.get(url, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); this.post = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, body, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.post(url, body, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); this.put = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, body, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.put(url, body, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); this.patch = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, body, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.patch(url, body, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); this.delete = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.delete(url, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); this.head = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.head(url, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); this.options = ((runtimeType, ...args // eslint-disable-line @typescript-eslint/no-explicit-any ) => { const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars return this.http.options(url, options).pipe(map(generateHandler(runtimeType)), catchError(handleDecodeError)); }); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: TypesafeHttpService, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: TypesafeHttpService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: TypesafeHttpService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [{ type: i1.HttpClient }] }); function generateHandler(runtimeType) { return (response) => { const result = runtimeType.decode(response); if (result._tag === IOTS_RESULT_TAG_LEFT) { throw new TypeError(generateCustomTypeErrorMessage(runtimeType.name, response, result.left)); } return result.right; }; } function generateCustomTypeErrorMessage(runtimeTypeName, response, errors) { return `Type '${runtimeTypeName}' decode mismatch.\n` + '\tUnable to decode following value:\n' + `\t${JSON.stringify(response)}\n\n` + '\tFollowing errors were found:\n' + `${errors.map((error) => `${error.context .filter((context) => context.key !== '') .map((context) => `\t\t${context.key}: ${context.type.name} -> ${context.actual}`) .join('\n')}`).join('\n')}`; } function handleDecodeError(error) { return throwError(() => error); } function provideTypesafeHttp() { return { provide: TypesafeHttpService, useClass: TypesafeHttpService, deps: [HttpClient], }; } /* * Public API Surface of typesafe-http-iots */ /** * Generated bundle index. Do not edit. */ export { TypesafeHttpService, provideTypesafeHttp }; //# sourceMappingURL=peppierre-typesafe-http-iots.mjs.map