@peppierre/typesafe-http-iots
Version:
Type-safe HTTP client for Angular using io-ts and fp-ts
1 lines • 9.74 kB
Source Map (JSON)
{"version":3,"file":"peppierre-typesafe-http-iots.mjs","sources":["../../../projects/typesafe-http-iots/src/lib/typesafe-http.service.ts","../../../projects/typesafe-http-iots/src/lib/provide.util.ts","../../../projects/typesafe-http-iots/src/public-api.ts","../../../projects/typesafe-http-iots/src/peppierre-typesafe-http-iots.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { HttpClient } from '@angular/common/http';\nimport { throwError } from 'rxjs';\nimport { catchError, map } from 'rxjs/operators';\nimport * as iots from 'io-ts';\nimport {\n WrappedDelete,\n WrappedGet,\n WrappedHead,\n WrappedOptions,\n WrappedPatch,\n WrappedPost,\n WrappedPut,\n} from './types/wrap-method-types.type';\n\nconst IOTS_RESULT_TAG_LEFT = 'Left';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class TypesafeHttpService {\n constructor(private http: HttpClient) {}\n\n public get: WrappedGet = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.get<R>(url, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedGet;\n\n public post: WrappedPost = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, body, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.post<R>(url, body, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedPost;\n\n public put: WrappedPut = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, body, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.put<R>(url, body, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedPut;\n\n public patch: WrappedPatch = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, body, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.patch<R>(url, body, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedPatch;\n\n public delete: WrappedDelete = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.delete<R>(url, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedDelete;\n\n public head: WrappedHead = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.head<R>(url, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedHead;\n\n public options: WrappedOptions = (<R>(\n runtimeType: iots.Type<R, unknown, unknown>,\n ...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any\n ) => {\n const [url, options, ..._unsupportedParams] = [...args]; // eslint-disable-line @typescript-eslint/no-unused-vars\n return this.http.options<R>(url, options).pipe(\n map(generateHandler(runtimeType)),\n catchError(handleDecodeError)\n );\n }) as WrappedOptions;\n\n}\n\nfunction generateHandler<R>(\n runtimeType: iots.Type<R, unknown, unknown>\n) {\n return (response: unknown) => {\n const result = runtimeType.decode(response);\n if (result._tag === IOTS_RESULT_TAG_LEFT) {\n throw new TypeError(generateCustomTypeErrorMessage(\n runtimeType.name,\n response,\n result.left\n ));\n }\n return result.right;\n };\n}\n\nfunction generateCustomTypeErrorMessage(runtimeTypeName: string, response: unknown, errors: iots.Errors) {\n return `Type '${ runtimeTypeName }' decode mismatch.\\n` +\n '\\tUnable to decode following value:\\n' +\n `\\t${ JSON.stringify(response) }\\n\\n` +\n '\\tFollowing errors were found:\\n' +\n `${ errors.map(\n (error) => `${\n error.context\n .filter((context) => context.key !== '')\n .map((context) => `\\t\\t${ context.key }: ${ context.type.name } -> ${ context.actual }`)\n .join('\\n')\n }`\n ).join('\\n') }`;\n}\n\nfunction handleDecodeError(error: TypeError) {\n return throwError(() => error);\n}\n","import { Provider } from '@angular/core';\nimport { HttpClient } from '@angular/common/http';\nimport { TypesafeHttpService } from './typesafe-http.service';\n\nexport function provideTypesafeHttp(): Provider {\n return {\n provide: TypesafeHttpService,\n useClass: TypesafeHttpService,\n deps: [HttpClient],\n };\n}\n","/*\n * Public API Surface of typesafe-http-iots\n */\n\nexport * from './lib/typesafe-http.service';\nexport * from './lib/provide.util';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;;AAeA,MAAM,oBAAoB,GAAG,MAAM;MAKtB,mBAAmB,CAAA;AAC9B,IAAA,WAAA,CAAoB,IAAgB,EAAA;QAAhB,IAAA,CAAA,IAAI,GAAJ,IAAI;QAEjB,IAAA,CAAA,GAAG,IAAgB,CACxB,WAA2C,EAC3C,GAAG,IAAW;aACZ;AACF,YAAA,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,kBAAkB,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;YACxD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAI,GAAG,EAAE,OAAO,CAAC,CAAC,IAAI,CACxC,GAAG,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC,EACjC,UAAU,CAAC,iBAAiB,CAAC,CAC9B;AACH,QAAA,CAAC,CAAe;QAET,I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