UNPKG

5.45 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const operators_1=require("rxjs/operators");require("reflect-metadata");const axios_1=require("axios"),rxjs_1=require("rxjs"),pathParamMetadataKey=Symbol("__pathParam__"),queryMetadataKey=Symbol("__queryParam__"),classMetadataKey=Symbol("__class__"),bodyMetadataKey=Symbol("__body__"),pathParamPropertyMetadataKey=Symbol("__pathParamProperty__"),mapperMetadataKey=Symbol("__mapper__"),headersMetadataKey=Symbol("__headers__"),beforeMetadataKey=Symbol("__headers__"),exceptionHandlerMetadataKey=Symbol("__handlerError__");function addInterceptors(...e){e.forEach(e=>exports.interceptors.unshift(new e))}function Client(e){return t=>Reflect.defineMetadata(classMetadataKey,e,t)}function Get(e,t=400){return request("get",e,t)}function Post(e,t=400){return request("post",e,t)}function Put(e,t=400){return request("put",e,t)}function Patch(e,t=400){return request("patch",e,t)}function Delete(e,t=400){return request("delete",e,t)}function request(e,t="",a){return(r,n,o)=>{o.value=((...o)=>{const s=Reflect.getMetadata(classMetadataKey,r.constructor),c=Reflect.getMetadata(pathParamMetadataKey,r,n)||[],d=Reflect.getMetadata(queryMetadataKey,r,n)||[],p=Reflect.getMetadata(bodyMetadataKey,r,n)||[],u=Reflect.getMetadata(mapperMetadataKey,r,n)||null,l=Reflect.getMetadata(headersMetadataKey,r,n)||null,i=Reflect.getMetadata(beforeMetadataKey,r,n)||null,f=Reflect.getMetadata(exceptionHandlerMetadataKey,r,n)||null;"/"==t.charAt(0)&&(t=t.substr(1,t.length));const y=new HeadersHttp;let h=String();const m=o;let M=String(t);M=UtilsHttp.buildPathParams(c,m,M);const x=UtilsHttp.buildQueryParams(d,m);"object"==typeof s?(h=s.url,UtilsHttp.prepareHeaders(s.headers,y)):h=s,"/"!==h.charAt(h.length-1)&&(h=h.concat("/")),h=h.concat(M).concat("?"===x?"":x);const P="get"!==e?UtilsHttp.prepareBody(p,m):{};l&&Object.keys(l).forEach(e=>y.set(e,l[e]));let _={url:h,body:P,headers:y.getHeaders(),method:e};return _=i?i(_):_,exports.interceptors.forEach(e=>_=e.intercep(_)),rxjs_1.from(axios_1.default.request({method:_.method,data:_.body,headers:_.headers,url:_.url,responseType:"json"})).pipe(operators_1.catchError(({Error:e})=>mapBodyAndControlError(e,f,a)),operators_1.map(({data:e})=>u?u(e):e))})}}function mapBodyAndControlError(e,t,a){const{config:r}=e,{data:n}=r;throw t?t(e):n&&n.message&&n.error?new HttpRequestException(n.error,e.response.status,n.message):new HttpRequestException(JSON.stringify(n),e.response.status,String())}function PathParam(e){return(t,a,r)=>{const n=Reflect.getOwnMetadata(pathParamMetadataKey,t,a)||[];n.unshift({indexArgument:r,paramValue:e}),Reflect.defineMetadata(pathParamMetadataKey,n,t,a)}}function Query(e){return(t,a,r)=>{const n=Reflect.getOwnMetadata(queryMetadataKey,t,a)||[];n.unshift({indexArgument:r,paramValue:e}),Reflect.defineMetadata(queryMetadataKey,n,t,a)}}function Body(e,t,a){const r=Reflect.getOwnMetadata(bodyMetadataKey,e,t)||[];r.unshift(a),Reflect.defineMetadata(bodyMetadataKey,r,e,t)}function PathParamProperty(e=!0){return(e,t)=>Reflect.defineMetadata(pathParamPropertyMetadataKey,{name:t},e,t)}function Mapper(e){return(t,a)=>Reflect.defineMetadata(mapperMetadataKey,e,t,a)}function Headers(e){return(t,a)=>Reflect.defineMetadata(headersMetadataKey,e,t,a)}function Before(e){return(t,a)=>Reflect.defineMetadata(beforeMetadataKey,e,t,a)}function HandlerError(e){return(t,a)=>Reflect.defineMetadata(exceptionHandlerMetadataKey,e,t,a)}exports.interceptors=[],exports.addInterceptors=addInterceptors,exports.Client=Client,exports.Get=Get,exports.Post=Post,exports.Put=Put,exports.Patch=Patch,exports.Delete=Delete,exports.PathParam=PathParam,exports.Query=Query,exports.Body=Body,exports.PathParamProperty=PathParamProperty,exports.Mapper=Mapper,exports.Headers=Headers,exports.Before=Before,exports.HandlerError=HandlerError;class UtilsHttp{static prepareHeaders(e={},t){Object.keys(e).forEach(a=>t.has(e[a])?null:t.set(a,e[a])),t.has("Content-Type")||t.set("Content-Type","application/json")}static buildQueryParams(e,t){let a="?";const r=String();return(e=e.filter(e=>t[e.indexArgument])).forEach((n,o)=>{if("object"==typeof t[n.indexArgument]){const e=Object.keys(t[n.indexArgument])||[];let o=0;for(const s in t[n.indexArgument])a=a.concat(`${s}=${t[n.indexArgument][s]}${o===e.length-1?r:"&"}`),o++}else{if(!n.paramValue)return;a=(a=a.length>1?a.concat("&"):a).concat(`${n.paramValue}=${t[n.indexArgument]}${o===e.length-1?r:"&"}`)}}),a}static buildPathParams(e,t,a){a=a.replace(/\s/g,"").trim();return e.filter(e=>e.paramValue).forEach(e=>{if(!e.paramValue)return;const r="{".concat(e.paramValue.toString()).concat("}");a.includes(r)&&(a=a.replace(r,t[e.indexArgument]))}),e.filter(e=>!e.paramValue).map(e=>a+=`/${t[e.indexArgument]}`),t.filter(e=>e&&"object"==typeof e).forEach(e=>Object.keys(e).forEach(t=>{const r=Reflect.getMetadata(pathParamPropertyMetadataKey,e,t);r&&(a=a.replace(`{${r.name}}`,e[r.name]))})),a}static prepareBody(e=[],t=[]){let a={};return e.filter(e=>"object"==typeof t[e]&&t[e]).forEach(e=>a=Object.assign({},a,t[e])),a}}class HttpRequestException{constructor(e,t,a){this.error=e,this.statusCode=t,this.message=a}}exports.HttpRequestException=HttpRequestException;class HeadersHttp{constructor(){this.headers=new Map}has(e){return this.headers.has(e)}set(e,t){return this.headers.set(e,t),this}getHeaders(){const e=Object();return Array.from(this.headers.keys()).forEach(t=>e[t]=this.headers.get(t)),e}}
\No newline at end of file