UNPKG

@peppierre/typesafe-http-iots

Version:

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

101 lines (95 loc) 4.12 kB
import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import * as i1 from '@angular/common/http'; import { HttpEventType, HttpClient } from '@angular/common/http'; import { throwError } from 'rxjs'; import { tap, catchError } from 'rxjs/operators'; import * as iots from 'io-ts'; const IOTS_RESULT_TAG_LEFT = 'Left'; class TypesafeHttpService { /* eslint-disable @typescript-eslint/no-explicit-any */ constructor(http) { this.http = http; } delete(url, options) { return this.http.delete(url, options).pipe(tap(generateHandler(options)), catchError(handleDecodeError)); } get(url, options) { return this.http.get(url, options).pipe(tap(generateHandler(options)), catchError(handleDecodeError)); } head(url, options) { return this.http.head(url, options).pipe(tap(generateHandler(options)), catchError(handleDecodeError)); } options(url, options) { return this.http.options(url, options).pipe(tap(generateHandler(options)), catchError(handleDecodeError)); } patch(url, body, options) { return this.http.patch(url, body, options).pipe(tap(generateHandler(options)), catchError(handleDecodeError)); } post(url, body, options) { return this.http.post(url, body, options).pipe(tap(generateHandler(options)), catchError(handleDecodeError)); } put(url, body, options) { return this.http.put(url, body, options).pipe(tap(generateHandler(options)), 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(options = {}) { const { runtimeType, observe = 'body', responseType = 'json' } = options; return (response) => { if (responseType === 'json' && runtimeType instanceof iots.Type) { let resultValue = null; if (observe === 'body') { resultValue = response; } else if (observe === 'response') { resultValue = response.body; } else if (observe === 'events' && response.type === HttpEventType.Response) { resultValue = response.body; } if (resultValue === null) { return; } const result = runtimeType.decode(resultValue); if (result._tag === IOTS_RESULT_TAG_LEFT) { throw new TypeError(generateCustomTypeErrorMessage(runtimeType.name, response, result.left)); } } }; } 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