@chained/http
Advanced tools
Comparing version 1.0.2 to 1.0.3
/*! | ||
* | ||
* @chained/http 1.0.2 | ||
* @chained/http 1.0.3 | ||
* | ||
@@ -152,3 +152,3 @@ * Copyright 2021-present, City. | ||
if (options.params) Object.keys(options.params).forEach(key => { | ||
options.url = options.url.replace(':key', options.params[key]); | ||
options.url = options.url.replace(`:${key}`, options.params[key]); | ||
}); | ||
@@ -155,0 +155,0 @@ if (options.query) options.url = `${options.url}?${new URLSearchParams(options.query).toString()}`; |
/*! | ||
* | ||
* @chained/http 1.0.2 | ||
* @chained/http 1.0.3 | ||
* | ||
@@ -9,2 +9,2 @@ * Copyright 2021-present, City. | ||
*/ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@logically/coding-model")):"function"==typeof define&&define.amd?define(["exports","@logically/coding-model"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).ChainedHttp={},e.logicallyCodingModel)}(this,(function(e,r){"use strict";let t=function(e){return e.GET="GET",e.POST="POST",e.PUT="PUT",e.PATCH="PATCH",e.DELETE="DELETE",e}({}),o=function(e){return e.json="application/json",e.form="multipart/form-data ",e.urlencoded="application/x-www-form-urlencoded ",e}({}),n=function(e){return e.default="default",e["no-store"]="no-store",e.reload="reload",e["no-cache"]="no-cache",e["force-cache"]="force-cache",e["only-if-cached"]="only-if-cached",e}({}),i=function(e){return e.cors="cors",e["same-origin"]="same-origin",e["no-cors"]="no-cors",e}({}),s=function(e){return e["same-origin"]="same-origin",e.include="include",e.omit="omit",e}({}),a=function(e){return e.follow="follow",e.error="error",e.manual="manual",e}({}),c=function(e){return e["no-referrer-when-downgrade"]="no-referrer-when-downgrade",e["no-referrer"]="no-referrer",e.origin="origin",e["origin-when-cross-origin"]="origin-when-cross-origin",e["same-origin"]="same-origin",e["strict-origin"]="strict-origin",e["strict-origin-when-cross-origin"]="strict-origin-when-cross-origin",e["unsafe-url"]="unsafe-url",e}({}),u=function(e){return e.basic="basic",e.cors="cors",e.error="error",e.opaque="opaque",e.opaqueredirect="opaqueredirect",e}({}),d=function(e){return e.text="text",e.json="json",e.formData="formData",e.blob="blob",e.arrayBuffer="arrayBuffer",e}({});const l=e=>{const r=e.request;r.headers||(r.headers=new Headers),r.headers.get("Content-Type")||r?.body instanceof FormData||r.headers.set("Content-Type",o.json)},h=e=>{const r=e.request;r.parameters&&(r.method===t.GET?r.query=r.query?{...r.query,...r.parameters}:{...r.parameters}:r.body instanceof FormData?Object.keys(r.parameters).forEach((e=>r.body.set(e,r.parameters[e]))):r.body=r.body?{...r.body,...r.parameters}:{...r.parameters})},f=e=>{const r=e.request;r.method||(r.method=t.POST),r.params&&Object.keys(r.params).forEach((e=>{r.url=r.url.replace(":key",r.params[e])})),r.query&&(r.url=`${r.url}?${new URLSearchParams(r.query).toString()}`),r.body&&(r.body=r.headers?.get("Content-Type")===o.form||r.body instanceof FormData?r.body:r.headers?.get("Content-Type")===o.json?JSON.stringify(r.body):new URLSearchParams(r.body).toString())};class p{}class y extends p{name="CancelPlugin";controllerMap={};request=e=>{const r=new AbortController,{signal:t}=r;e.request.signal=t,this.controllerMap[e.request.id]=r};await=e=>{e.promise.cancel=()=>this.controllerMap[e.request.id].abort()};response=e=>{delete this.controllerMap[e.request.id]}}let g=0;const m=e=>{"function"==typeof e.cache&&(e.cache=e.cache(n)),"function"==typeof e.mode&&(e.mode=e.mode(i)),"function"==typeof e.credentials&&(e.credentials=e.credentials(s)),"function"==typeof e.redirect&&(e.redirect=e.redirect(a)),"function"==typeof e.referrerPolicy&&(e.referrerPolicy=e.referrerPolicy(c));const r=e;return r.id||(r.id=++g),r};class E{originChained=(()=>{const e=new r.Chained;e.use([l,h,f],{afterUse:!0});const t=new r.Chained,o=new r.Chained,n=r=>(e.use(r.request),t.use(r.await),o.use(r.response),{register:n});n(new y);const i=(...r)=>(e.use(...r),{use:i});return{request:e,await:t,response:o,interceptors:{request:{use:i},await:{use:(...e)=>(t.use(...e),{use:i})},response:{use:(...e)=>(o.use(...e),{use:i})}},plugin:{register:n}}})();interceptors=this.originChained.interceptors;plugin=this.originChained.plugin;constructor(e={}){this.config=e}async request(e){let r={};const t=(async()=>{this.config.baseURL&&(e.url=e.url?`${this.config.baseURL}${e.url}`:this.config.baseURL);let t={request:m(e)};const o=await this.originChained.request.trigger(t);if(!o)return!1;void 0!==o&&(t=o),r=t;const n=await this.core(t),i=await this.transform(n,e.bodyType||d.json);let s={...t,...i};const a=await this.originChained.response.trigger(s);return!!a&&(void 0!==a&&(s=a),s)})();return await this.originChained.await.trigger({...r,promise:t}),t}get(e,r={}){return this.request({...r,url:e,method:t.GET})}post(e,r={}){return this.request({...r,url:e,method:t.POST})}patch(e,r={}){return this.request({...r,url:e,method:t.PATCH})}put(e,r={}){return this.request({...r,url:e,method:t.PUT})}delete(e,r={}){return this.request({...r,url:e,method:t.DELETE})}}class T{constructor(e,r){if(this.prefix=e,this.http=r instanceof E?r:T.http,!this.http)throw new Error("No http request tool found!")}get=async e=>(await this.http.get(this.prefix,{query:e})).body;post=async e=>(await this.http.post(this.prefix,{body:e})).body;put=async(e,r)=>(await this.http.put(`${this.prefix}/${e}`,{body:r})).body;patch=async(e,r)=>(await this.http.patch(`${this.prefix}/${e}`,{body:r})).body;delete=async e=>(await this.http.delete(`${this.prefix}/${e}`)).body}e.HTTP=E,e.REQUEST_BODY_TYPE=o,e.REQUEST_OPTIONAL_CACHE=n,e.REQUEST_OPTIONAL_CREDENTIALS=s,e.REQUEST_OPTIONAL_MODE=i,e.REQUEST_OPTIONAL_REDIRECT=a,e.REQUEST_OPTIONAL_REFERRER_POLICY=c,e.REQUEST_TYPE=t,e.RESPONSE_BODY_TYPE=d,e.RESPONSE_TYPE=u,e.RESTful=T,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@logically/coding-model")):"function"==typeof define&&define.amd?define(["exports","@logically/coding-model"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).ChainedHttp={},e.logicallyCodingModel)}(this,(function(e,r){"use strict";let t=function(e){return e.GET="GET",e.POST="POST",e.PUT="PUT",e.PATCH="PATCH",e.DELETE="DELETE",e}({}),o=function(e){return e.json="application/json",e.form="multipart/form-data ",e.urlencoded="application/x-www-form-urlencoded ",e}({}),n=function(e){return e.default="default",e["no-store"]="no-store",e.reload="reload",e["no-cache"]="no-cache",e["force-cache"]="force-cache",e["only-if-cached"]="only-if-cached",e}({}),i=function(e){return e.cors="cors",e["same-origin"]="same-origin",e["no-cors"]="no-cors",e}({}),s=function(e){return e["same-origin"]="same-origin",e.include="include",e.omit="omit",e}({}),a=function(e){return e.follow="follow",e.error="error",e.manual="manual",e}({}),c=function(e){return e["no-referrer-when-downgrade"]="no-referrer-when-downgrade",e["no-referrer"]="no-referrer",e.origin="origin",e["origin-when-cross-origin"]="origin-when-cross-origin",e["same-origin"]="same-origin",e["strict-origin"]="strict-origin",e["strict-origin-when-cross-origin"]="strict-origin-when-cross-origin",e["unsafe-url"]="unsafe-url",e}({}),u=function(e){return e.basic="basic",e.cors="cors",e.error="error",e.opaque="opaque",e.opaqueredirect="opaqueredirect",e}({}),d=function(e){return e.text="text",e.json="json",e.formData="formData",e.blob="blob",e.arrayBuffer="arrayBuffer",e}({});const l=e=>{const r=e.request;r.headers||(r.headers=new Headers),r.headers.get("Content-Type")||r?.body instanceof FormData||r.headers.set("Content-Type",o.json)},h=e=>{const r=e.request;r.parameters&&(r.method===t.GET?r.query=r.query?{...r.query,...r.parameters}:{...r.parameters}:r.body instanceof FormData?Object.keys(r.parameters).forEach((e=>r.body.set(e,r.parameters[e]))):r.body=r.body?{...r.body,...r.parameters}:{...r.parameters})},f=e=>{const r=e.request;r.method||(r.method=t.POST),r.params&&Object.keys(r.params).forEach((e=>{r.url=r.url.replace(`:${e}`,r.params[e])})),r.query&&(r.url=`${r.url}?${new URLSearchParams(r.query).toString()}`),r.body&&(r.body=r.headers?.get("Content-Type")===o.form||r.body instanceof FormData?r.body:r.headers?.get("Content-Type")===o.json?JSON.stringify(r.body):new URLSearchParams(r.body).toString())};class p{}class y extends p{name="CancelPlugin";controllerMap={};request=e=>{const r=new AbortController,{signal:t}=r;e.request.signal=t,this.controllerMap[e.request.id]=r};await=e=>{e.promise.cancel=()=>this.controllerMap[e.request.id].abort()};response=e=>{delete this.controllerMap[e.request.id]}}let g=0;const m=e=>{"function"==typeof e.cache&&(e.cache=e.cache(n)),"function"==typeof e.mode&&(e.mode=e.mode(i)),"function"==typeof e.credentials&&(e.credentials=e.credentials(s)),"function"==typeof e.redirect&&(e.redirect=e.redirect(a)),"function"==typeof e.referrerPolicy&&(e.referrerPolicy=e.referrerPolicy(c));const r=e;return r.id||(r.id=++g),r};class E{originChained=(()=>{const e=new r.Chained;e.use([l,h,f],{afterUse:!0});const t=new r.Chained,o=new r.Chained,n=r=>(e.use(r.request),t.use(r.await),o.use(r.response),{register:n});n(new y);const i=(...r)=>(e.use(...r),{use:i});return{request:e,await:t,response:o,interceptors:{request:{use:i},await:{use:(...e)=>(t.use(...e),{use:i})},response:{use:(...e)=>(o.use(...e),{use:i})}},plugin:{register:n}}})();interceptors=this.originChained.interceptors;plugin=this.originChained.plugin;constructor(e={}){this.config=e}async request(e){let r={};const t=(async()=>{this.config.baseURL&&(e.url=e.url?`${this.config.baseURL}${e.url}`:this.config.baseURL);let t={request:m(e)};const o=await this.originChained.request.trigger(t);if(!o)return!1;void 0!==o&&(t=o),r=t;const n=await this.core(t),i=await this.transform(n,e.bodyType||d.json);let s={...t,...i};const a=await this.originChained.response.trigger(s);return!!a&&(void 0!==a&&(s=a),s)})();return await this.originChained.await.trigger({...r,promise:t}),t}get(e,r={}){return this.request({...r,url:e,method:t.GET})}post(e,r={}){return this.request({...r,url:e,method:t.POST})}patch(e,r={}){return this.request({...r,url:e,method:t.PATCH})}put(e,r={}){return this.request({...r,url:e,method:t.PUT})}delete(e,r={}){return this.request({...r,url:e,method:t.DELETE})}}class T{constructor(e,r){if(this.prefix=e,this.http=r instanceof E?r:T.http,!this.http)throw new Error("No http request tool found!")}get=async e=>(await this.http.get(this.prefix,{query:e})).body;post=async e=>(await this.http.post(this.prefix,{body:e})).body;put=async(e,r)=>(await this.http.put(`${this.prefix}/${e}`,{body:r})).body;patch=async(e,r)=>(await this.http.patch(`${this.prefix}/${e}`,{body:r})).body;delete=async e=>(await this.http.delete(`${this.prefix}/${e}`)).body}e.HTTP=E,e.REQUEST_BODY_TYPE=o,e.REQUEST_OPTIONAL_CACHE=n,e.REQUEST_OPTIONAL_CREDENTIALS=s,e.REQUEST_OPTIONAL_MODE=i,e.REQUEST_OPTIONAL_REDIRECT=a,e.REQUEST_OPTIONAL_REFERRER_POLICY=c,e.REQUEST_TYPE=t,e.RESPONSE_BODY_TYPE=d,e.RESPONSE_TYPE=u,e.RESTful=T,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -7,3 +7,3 @@ import { REQUEST_TYPE, REQUEST_BODY_TYPE } from '../../constants/request.js'; | ||
if (options.params) Object.keys(options.params).forEach(key => { | ||
options.url = options.url.replace(':key', options.params[key]); | ||
options.url = options.url.replace(`:${key}`, options.params[key]); | ||
}); | ||
@@ -10,0 +10,0 @@ if (options.query) options.url = `${options.url}?${new URLSearchParams(options.query).toString()}`; |
@@ -11,3 +11,3 @@ 'use strict'; | ||
if (options.params) Object.keys(options.params).forEach(key => { | ||
options.url = options.url.replace(':key', options.params[key]); | ||
options.url = options.url.replace(`:${key}`, options.params[key]); | ||
}); | ||
@@ -14,0 +14,0 @@ if (options.query) options.url = `${options.url}?${new URLSearchParams(options.query).toString()}`; |
{ | ||
"name": "@chained/http", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Http Chained.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
104485