@ckpack/fetch-helper
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -1,2 +0,2 @@ | ||
"use strict";var fetchHelper=(()=>{var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var l=(r,s)=>{for(var t in s)a(r,t,{get:s[t],enumerable:!0})},f=(r,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of H(s))!I.call(r,n)&&n!==t&&a(r,n,{get:()=>s[n],enumerable:!(e=m(s,n))||e.enumerable});return r};var y=r=>f(a({},"__esModule",{value:!0}),r);var E={};l(E,{FetchHelper:()=>p,WITHOUT_BODY_METHODS:()=>h,WITH_BODY_METHODS:()=>c,createInstance:()=>u,default:()=>q,mergeHeaders:()=>T});function R(r){return new URLSearchParams(r).toString()}function T(r,s){let t=new Headers(r);return new Headers(s).forEach((e,n)=>{t.set(n,e)}),t}var h=["GET","HEAD","OPTIONS","CONNECT","TRACE"],c=["DELETE","PATCH","POST","PUT"],p=class{defaultInit;input;init;constructor(s){this.defaultInit=s||{}}async request(s,t){this.input=s;let e={...this.defaultInit,...t,headers:T(this.defaultInit?.headers,t?.headers)};if(this.init=e.transformRequest?await e.transformRequest(e,this):e,typeof this.input=="string"){let o=new URL(this.input,this.init.baseURL),d=(this.init.paramsSerializer||R)(this.init.params);d?this.input=`${o.href}${o.search?"&":"?"}${d}`:this.input=o.href}let n,i;try{n=await(this.init.adapter||fetch)(this.input,this.init),this.init.handlerSuccess&&this.init?.handlerSuccess(n)}catch(o){if(i=o,this.init.handlerError)this.init?.handlerError(i);else throw i}finally{return e.transformResponse?e.transformResponse(n,i,this):n}}};var F={};function u(r){let s=new p(r),t=s.request.bind(s);return t.default=r||{},t.create=e=>u({...r,...t.default,...e}),h.map(e=>e.toLowerCase()).forEach(e=>{t[e]=(n,i,o)=>t(n,{...r,params:i,method:e,...o})}),c.map(e=>e.toLowerCase()).forEach(e=>{t[e]=(n,i,o)=>t(n,{...r,body:i,method:e,...o})}),t.request=e=>t(e.input,{...r,...e}),t}var O=u(F),q=O;return y(E);})(); | ||
"use strict";var fetchHelper=(()=>{var c=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var I=(s,r)=>{for(var e in r)c(s,e,{get:r[e],enumerable:!0})},f=(s,r,e,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of H(r))!l.call(s,o)&&o!==e&&c(s,o,{get:()=>r[o],enumerable:!(t=m(r,o))||t.enumerable});return s};var y=s=>f(c({},"__esModule",{value:!0}),s);var E={};I(E,{FetchHelper:()=>p,WITHOUT_BODY_METHODS:()=>h,WITH_BODY_METHODS:()=>i,createInstance:()=>u,default:()=>O,mergeHeaders:()=>T});function R(s){return new URLSearchParams(s).toString()}function T(s,r){let e=new Headers(s);return new Headers(r).forEach((t,o)=>{e.set(o,t)}),e}var h=["GET","HEAD","OPTIONS","CONNECT","TRACE"],i=["DELETE","PATCH","POST","PUT"],p=class{defaultInit;constructor(r){this.defaultInit=r||{}}async request(r,e){let t={...this.defaultInit,...e,headers:T(this.defaultInit?.headers,e?.headers)};if(e=t.transformRequest?await t.transformRequest({init:t,input:r,ctx:this}):t,typeof r=="string"){let n=new URL(r,e.baseURL),d=(e.paramsSerializer||R)(e.params);d?r=`${n.href}${n.search?"&":"?"}${d}`:r=n.href}let o,a;try{o=await(e.adapter||fetch)(r,e),e.handlerSuccess&&e?.handlerSuccess(o)}catch(n){if(a=n,e.handlerError)e?.handlerError(a);else throw a}finally{return t.transformResponse?t.transformResponse({response:o,error:a,init:e,input:r,ctx:this}):o}}};var F={};function u(s){let r=new p(s),e=r.request.bind(r);return e.default=s||{},e.create=t=>u({...s,...e.default,...t}),h.map(t=>t.toLowerCase()).forEach(t=>{e[t]=(o,a,n)=>e(o,{...s,params:a,method:t,...n})}),i.map(t=>t.toLowerCase()).forEach(t=>{e[t]=(o,a,n)=>e(o,{...s,body:a,method:t,...n})}),e.request=t=>e(t.input,{...s,...t}),e}var x=u(F),O=x;return y(E);})(); | ||
fetchHelper = fetchHelper.default; |
export type RequestParams = ConstructorParameters<typeof URLSearchParams>[number] | number | number[][] | Record<string | number, number>; | ||
export type TransformRequest = (init: FetchHelperInit, ctx: FetchHelper) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export type TransformResponse = <T = Response>(response: Response | undefined, error: Error | undefined, ctx: FetchHelper) => Promise<T>; | ||
export type TransformRequest = (p: { | ||
init: FetchHelperInit; | ||
input: FetchHelperInput; | ||
ctx: FetchHelper; | ||
}) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export type TransformResponse = <T = Response>(p: { | ||
response: Response | undefined; | ||
error: Error | undefined; | ||
init: FetchHelperInit; | ||
input: FetchHelperInput; | ||
ctx: FetchHelper; | ||
}) => Promise<T>; | ||
declare function paramsSerializer(params?: RequestParams): string; | ||
@@ -24,4 +34,2 @@ export declare function mergeHeaders(header?: HeadersInit, header2?: HeadersInit): Headers; | ||
defaultInit?: FetchHelperInit; | ||
input?: RequestInfo; | ||
init?: FetchHelperInit; | ||
constructor(fetchConfig?: FetchHelperInit); | ||
@@ -28,0 +36,0 @@ request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T>; |
@@ -15,4 +15,2 @@ function paramsSerializer(params) { | ||
defaultInit; | ||
input; | ||
init; | ||
constructor(fetchConfig) { | ||
@@ -22,13 +20,12 @@ this.defaultInit = fetchConfig || {}; | ||
async request(input, init) { | ||
this.input = input; | ||
const mergeInit = { ...this.defaultInit, ...init, headers: mergeHeaders(this.defaultInit?.headers, init?.headers) }; | ||
this.init = mergeInit.transformRequest ? await mergeInit.transformRequest(mergeInit, this) : mergeInit; | ||
if (typeof this.input === 'string') { | ||
const inputURL = new URL(this.input, this.init.baseURL); | ||
const queryString = (this.init.paramsSerializer || paramsSerializer)(this.init.params); | ||
init = mergeInit.transformRequest ? await mergeInit.transformRequest({ init: mergeInit, input, ctx: this }) : mergeInit; | ||
if (typeof input === 'string') { | ||
const inputURL = new URL(input, init.baseURL); | ||
const queryString = (init.paramsSerializer || paramsSerializer)(init.params); | ||
if (queryString) { | ||
this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
} | ||
else { | ||
this.input = inputURL.href; | ||
input = inputURL.href; | ||
} | ||
@@ -39,5 +36,5 @@ } | ||
try { | ||
response = await (this.init.adapter || fetch)(this.input, this.init); | ||
if (this.init.handlerSuccess) { | ||
this.init?.handlerSuccess(response); | ||
response = await (init.adapter || fetch)(input, init); | ||
if (init.handlerSuccess) { | ||
init?.handlerSuccess(response); | ||
} | ||
@@ -47,4 +44,4 @@ } | ||
error = e; | ||
if (this.init.handlerError) { | ||
this.init?.handlerError(error); | ||
if (init.handlerError) { | ||
init?.handlerError(error); | ||
} | ||
@@ -57,5 +54,5 @@ else { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, error, this) : response; | ||
return mergeInit.transformResponse ? mergeInit.transformResponse({ response, error, init, input, ctx: this }) : response; | ||
} | ||
} | ||
} |
import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
export * from './FetchHelper.js'; | ||
export type WithoutBodyMethod<T = Response> = <U = T>(input: FetchHelperInput, params?: RequestParams, options?: FetchHelperInit) => Promise<U>; | ||
export type WithBodyMethod<T = Response> = <U = T>(input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<U>; | ||
export type WithBodyMethod<T = Response> = <U = T>(input: FetchHelperInput, body?: BodyInit | object, options?: FetchHelperInit) => Promise<U>; | ||
export type InputBodyMethod<T = Response> = <U = T>(options: FetchHelperInit & { | ||
@@ -6,0 +6,0 @@ input: FetchHelperInput; |
{ | ||
"name": "@ckpack/fetch-helper", | ||
"private": false, | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "一个轻量级的工具函数库", | ||
@@ -6,0 +6,0 @@ "sideEffects": false, |
export type RequestParams = ConstructorParameters<typeof URLSearchParams>[number] | number | number[][] | Record<string | number, number>; | ||
export type TransformRequest = (init: FetchHelperInit, ctx: FetchHelper) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export type TransformResponse = <T = Response>(response: Response | undefined, error: Error | undefined, ctx: FetchHelper) => Promise<T>; | ||
export type TransformRequest = (p: { init: FetchHelperInit; input: FetchHelperInput; ctx: FetchHelper }) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export type TransformResponse = <T = Response>(p: { response: Response | undefined; error: Error | undefined; init: FetchHelperInit; input: FetchHelperInput; ctx: FetchHelper }) => Promise<T>; | ||
@@ -39,4 +39,2 @@ function paramsSerializer(params?: RequestParams) { | ||
defaultInit?: FetchHelperInit; | ||
input?: RequestInfo; | ||
init?: FetchHelperInit; | ||
@@ -48,15 +46,14 @@ constructor(fetchConfig?: FetchHelperInit) { | ||
async request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit) { | ||
this.input = input; | ||
const mergeInit: FetchHelperInit = { ...this.defaultInit, ...init, headers: mergeHeaders(this.defaultInit?.headers, init?.headers) }; | ||
this.init = mergeInit.transformRequest ? await mergeInit.transformRequest(mergeInit, this) : mergeInit; | ||
init = mergeInit.transformRequest ? await mergeInit.transformRequest({ init: mergeInit, input, ctx: this }) : mergeInit; | ||
if (typeof this.input === 'string') { | ||
const inputURL = new URL(this.input, this.init.baseURL); | ||
if (typeof input === 'string') { | ||
const inputURL = new URL(input, init.baseURL); | ||
const queryString = (this.init.paramsSerializer || paramsSerializer)(this.init.params); | ||
const queryString = (init.paramsSerializer || paramsSerializer)(init.params); | ||
if (queryString) { | ||
this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
} else { | ||
this.input = inputURL.href; | ||
input = inputURL.href; | ||
} | ||
@@ -68,10 +65,10 @@ } | ||
try { | ||
response = await (this.init.adapter || fetch)(this.input, this.init); | ||
if (this.init.handlerSuccess) { | ||
this.init?.handlerSuccess(response); | ||
response = await (init.adapter || fetch)(input, init); | ||
if (init.handlerSuccess) { | ||
init?.handlerSuccess(response); | ||
} | ||
} catch (e: any) { | ||
error = e; | ||
if (this.init.handlerError) { | ||
this.init?.handlerError(error); | ||
if (init.handlerError) { | ||
init?.handlerError(error); | ||
} else { | ||
@@ -82,5 +79,5 @@ throw error; | ||
// eslint-disable-next-line no-unsafe-finally | ||
return mergeInit.transformResponse ? mergeInit.transformResponse<T>(response, error, this) : response as T; | ||
return mergeInit.transformResponse ? mergeInit.transformResponse<T>({ response, error, init, input, ctx: this }) : response as T; | ||
} | ||
} | ||
} |
@@ -9,3 +9,3 @@ import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
export type WithoutBodyMethod<T = Response> = <U = T> (input: FetchHelperInput, params?: RequestParams, options?: FetchHelperInit) => Promise<U>; | ||
export type WithBodyMethod<T = Response> = <U = T> (input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<U>; | ||
export type WithBodyMethod<T = Response> = <U = T> (input: FetchHelperInput, body?: BodyInit | object, options?: FetchHelperInit) => Promise<U>; | ||
export type InputBodyMethod<T = Response> = <U = T> (options: FetchHelperInit & { | ||
@@ -12,0 +12,0 @@ input: FetchHelperInput |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26956