@ckpack/fetch-helper
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -1,2 +0,2 @@ | ||
"use strict";var fetchHelper=(()=>{var a=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var y=(r,s)=>{for(var t in s)a(r,t,{get:s[t],enumerable:!0})},T=(r,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of I(s))!m.call(r,n)&&n!==t&&a(r,n,{get:()=>s[n],enumerable:!(e=H(s,n))||e.enumerable});return r};var l=r=>T(a({},"__esModule",{value:!0}),r);var q={};y(q,{FetchHelper:()=>p,WITHOUT_BODY_METHODS:()=>h,WITH_BODY_METHODS:()=>c,createInstance:()=>u,default:()=>O,mergeHeaders:()=>d});var R=r=>new URLSearchParams(r).toString(),d=(r,s)=>{let t=new Headers(r);return new Headers(s).forEach((e,n)=>{t.set(n,e)}),t},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:d(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),i=(this.init.paramsSerializer||R)(this.init.params);i?this.input=`${o.href}${o.search?"&":"?"}${i}`:this.input=o.href}let n=await(this.init.adapter||fetch)(this.input,this.init);return e.transformResponse?e.transformResponse(n,this):n}};var f={},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,o,i)=>t(n,{...r,params:o,method:e,...i})}),c.map(e=>e.toLowerCase()).forEach(e=>{t[e]=(n,o,i)=>t(n,{...r,body:o,method:e,...i})}),t.request=e=>t(e.input,{...r,...e}),t},F=u(f),O=F;return l(q);})(); | ||
"use strict";var fetchHelper=(()=>{var a=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var f=(s,n)=>{for(var r in n)a(s,r,{get:n[r],enumerable:!0})},l=(s,n,r,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let t of I(n))!m.call(s,t)&&t!==r&&a(s,t,{get:()=>n[t],enumerable:!(e=H(n,t))||e.enumerable});return s};var y=s=>l(a({},"__esModule",{value:!0}),s);var q={};f(q,{FetchHelper:()=>i,WITHOUT_BODY_METHODS:()=>h,WITH_BODY_METHODS:()=>c,createInstance:()=>d,default:()=>O,mergeHeaders:()=>u});var T=s=>new URLSearchParams(s).toString(),u=(s,n)=>{let r=new Headers(s);return new Headers(n).forEach((e,t)=>{r.set(t,e)}),r},h=["GET","HEAD","OPTIONS","CONNECT","TRACE"],c=["DELETE","PATCH","POST","PUT"],i=class{defaultInit;input;init;constructor(n){this.defaultInit=n||{}}async request(n,r){this.input=n;let e={...this.defaultInit,...r,headers:u(this.defaultInit?.headers,r?.headers)};if(this.init=e.transformRequest?await e.transformRequest(e,this):e,typeof this.input=="string"){let t=new URL(this.input,this.init.baseURL),o=(this.init.paramsSerializer||T)(this.init.params);o?this.input=`${t.href}${t.search?"&":"?"}${o}`:this.input=t.href}try{let t=await(this.init.adapter||fetch)(this.input,this.init);return this.init.handlerSuccess&&this.init?.handlerSuccess(t),e.transformResponse?e.transformResponse(t,this):t}catch(t){if(this.init.handlerError)this.init?.handlerError(t);else throw t}}};var R={},d=s=>{let n=new i(s),r=n.request.bind(n);return r.default=s||{},r.create=e=>d({...s,...r.default,...e}),h.map(e=>e.toLowerCase()).forEach(e=>{r[e]=(t,o,p)=>r(t,{...s,params:o,method:e,...p})}),c.map(e=>e.toLowerCase()).forEach(e=>{r[e]=(t,o,p)=>r(t,{...s,body:o,method:e,...p})}),r.request=e=>r(e.input,{...s,...e}),r},F=d(R),O=F;return y(q);})(); | ||
fetchHelper = fetchHelper.default; |
@@ -18,2 +18,4 @@ export declare type RequestParams = URLSearchParams | Record<string | number, any> | undefined; | ||
adapter?: (input?: RequestInfo | URL, init?: RequestInit) => Response; | ||
handlerError?: (error: any) => undefined; | ||
handlerSuccess?: (response: Response) => undefined; | ||
[index: string]: any; | ||
@@ -26,4 +28,4 @@ } | ||
constructor(fetchConfig?: FetchHelperInit); | ||
request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T>; | ||
request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T | undefined>; | ||
} | ||
export {}; |
@@ -25,10 +25,25 @@ const paramsSerializer = (params) => new URLSearchParams(params).toString(); | ||
const queryString = (this.init.paramsSerializer || paramsSerializer)(this.init.params); | ||
if (queryString) | ||
if (queryString) { | ||
this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
else | ||
} | ||
else { | ||
this.input = inputURL.href; | ||
} | ||
} | ||
const response = await (this.init.adapter || fetch)(this.input, this.init); | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, this) : response; | ||
try { | ||
const response = await (this.init.adapter || fetch)(this.input, this.init); | ||
if (this.init.handlerSuccess) { | ||
this.init?.handlerSuccess(response); | ||
} | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, this) : response; | ||
} | ||
catch (error) { | ||
if (this.init.handlerError) { | ||
this.init?.handlerError(error); | ||
} | ||
else { | ||
throw error; | ||
} | ||
} | ||
} | ||
} |
import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
export * from './FetchHelper.js'; | ||
export declare type WithoutBodyMethod = <T = Response>(input: FetchHelperInput, params?: RequestParams | Object, options?: FetchHelperInit) => Promise<T>; | ||
export declare type WithBodyMethod = <T = Response>(input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<T>; | ||
export declare type WithoutBodyMethod = <T = Response>(input: FetchHelperInput, params?: RequestParams | Object, options?: FetchHelperInit) => Promise<T | undefined>; | ||
export declare type WithBodyMethod = <T = Response>(input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<T | undefined>; | ||
export declare type InputBodyMethod = <T = Response>(options: FetchHelperInit & { | ||
input: FetchHelperInput; | ||
}) => Promise<T>; | ||
export declare const createInstance: (defaultConfig?: FetchHelperInit) => (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T>) & { | ||
}) => Promise<T | undefined>; | ||
export declare const createInstance: (defaultConfig?: FetchHelperInit) => (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T | undefined>) & { | ||
create: typeof createInstance; | ||
@@ -22,3 +22,3 @@ request: InputBodyMethod; | ||
}; | ||
declare const instance: (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T>) & { | ||
declare const instance: (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T | undefined>) & { | ||
create: typeof createInstance; | ||
@@ -25,0 +25,0 @@ request: InputBodyMethod; |
{ | ||
"name": "@ckpack/fetch-helper", | ||
"private": false, | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "一个轻量级的工具函数库", | ||
@@ -6,0 +6,0 @@ "sideEffects": false, |
@@ -1,21 +0,21 @@ | ||
export type RequestParams = URLSearchParams | Record<string | number, any> | undefined | ||
export type TransformRequest = (init: FetchHelperInit, ctx: FetchHelper) => Promise<FetchHelperInit> | FetchHelperInit | ||
export type TransformResponse = (response: Response, ctx: FetchHelper) => Promise<unknown> | ||
export type RequestParams = URLSearchParams | Record<string | number, any> | undefined; | ||
export type TransformRequest = (init: FetchHelperInit, ctx: FetchHelper) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export type TransformResponse = (response: Response, ctx: FetchHelper) => Promise<unknown>; | ||
const paramsSerializer = (params?: RequestParams) => new URLSearchParams(params).toString() | ||
const paramsSerializer = (params?: RequestParams) => new URLSearchParams(params).toString(); | ||
export const mergeHeaders = (header?: HeadersInit, header2?: HeadersInit) => { | ||
const base = new Headers(header) | ||
const base = new Headers(header); | ||
new Headers(header2).forEach((value, key) => { | ||
base.set(key, value) | ||
}) | ||
return base | ||
} | ||
base.set(key, value); | ||
}); | ||
return base; | ||
}; | ||
export const WITHOUT_BODY_METHODS = ['GET', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE'] as const | ||
export const WITH_BODY_METHODS = ['DELETE', 'PATCH', 'POST', 'PUT'] as const | ||
export const WITHOUT_BODY_METHODS = ['GET', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE'] as const; | ||
export const WITH_BODY_METHODS = ['DELETE', 'PATCH', 'POST', 'PUT'] as const; | ||
export type RequestMethod = typeof WITHOUT_BODY_METHODS[number] | typeof WITH_BODY_METHODS[number] | Lowercase<typeof WITHOUT_BODY_METHODS[number]> | Lowercase<typeof WITH_BODY_METHODS[number]> | ||
export type RequestMethod = typeof WITHOUT_BODY_METHODS[number] | typeof WITH_BODY_METHODS[number] | Lowercase<typeof WITHOUT_BODY_METHODS[number]> | Lowercase<typeof WITH_BODY_METHODS[number]>; | ||
export type FetchHelperInput = RequestInfo | ||
export type FetchHelperInput = RequestInfo; | ||
@@ -30,2 +30,4 @@ export interface FetchHelperInit extends RequestInit { | ||
adapter?: (input?: RequestInfo | URL, init?: RequestInit) => Response | ||
handlerError?: (error: any) => undefined | ||
handlerSuccess?: (response: Response) => undefined | ||
[index: string]: any | ||
@@ -35,30 +37,34 @@ } | ||
export class FetchHelper { | ||
defaultInit?: FetchHelperInit | ||
input?: RequestInfo | ||
init?: FetchHelperInit | ||
defaultInit?: FetchHelperInit; | ||
input?: RequestInfo; | ||
init?: FetchHelperInit; | ||
constructor(fetchConfig?: FetchHelperInit) { | ||
this.defaultInit = fetchConfig || {} | ||
this.defaultInit = fetchConfig || {}; | ||
} | ||
async request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T> { | ||
this.input = input | ||
const mergeInit: FetchHelperInit = { ...this.defaultInit, ...init, headers: mergeHeaders(this.defaultInit?.headers, init?.headers) } | ||
async request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T | undefined> { | ||
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 | ||
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 inputURL = new URL(this.input, this.init.baseURL); | ||
const queryString = (this.init.paramsSerializer || paramsSerializer)(this.init.params) | ||
if (queryString) | ||
this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}` | ||
const queryString = (this.init.paramsSerializer || paramsSerializer)(this.init.params); | ||
if (queryString) { this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; } | ||
else { this.input = inputURL.href; } | ||
} | ||
else | ||
this.input = inputURL.href | ||
try { | ||
const response = await (this.init.adapter || fetch)(this.input, this.init); | ||
if (this.init.handlerSuccess) { this.init?.handlerSuccess(response); } | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, this) : response as any; | ||
} | ||
const response = await (this.init.adapter || fetch)(this.input, this.init) | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, this) : response as any | ||
catch (error: any) { | ||
if (this.init.handlerError) { this.init?.handlerError(error); } | ||
else { throw error; } | ||
} | ||
} | ||
} |
@@ -1,15 +0,15 @@ | ||
import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js' | ||
import { FetchHelper, WITHOUT_BODY_METHODS, WITH_BODY_METHODS } from './FetchHelper.js' | ||
export * from './FetchHelper.js' | ||
import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
import { FetchHelper, WITHOUT_BODY_METHODS, WITH_BODY_METHODS } from './FetchHelper.js'; | ||
export * from './FetchHelper.js'; | ||
const defaultConfig: FetchHelperInit = {} | ||
const defaultConfig: FetchHelperInit = {}; | ||
export type WithoutBodyMethod = <T=Response>(input: FetchHelperInput, params?: RequestParams | Object, options?: FetchHelperInit) => Promise<T> | ||
export type WithBodyMethod = <T=Response>(input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<T> | ||
export type WithoutBodyMethod = <T=Response>(input: FetchHelperInput, params?: RequestParams | Object, options?: FetchHelperInit) => Promise<T | undefined>; | ||
export type WithBodyMethod = <T=Response>(input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<T | undefined>; | ||
export type InputBodyMethod = <T=Response>(options: FetchHelperInit & { | ||
input: FetchHelperInput | ||
}) => Promise<T> | ||
}) => Promise<T | undefined>; | ||
export const createInstance = (defaultConfig?: FetchHelperInit) => { | ||
const context = new FetchHelper(defaultConfig) | ||
const context = new FetchHelper(defaultConfig); | ||
@@ -29,5 +29,5 @@ const instance = context.request.bind(context) as typeof context.request & { | ||
default: FetchHelperInit | ||
} | ||
}; | ||
instance.default = defaultConfig || {} | ||
instance.default = defaultConfig || {}; | ||
@@ -38,16 +38,16 @@ instance.create = (createConfig?: FetchHelperInit) => createInstance({ | ||
...createConfig, | ||
}) | ||
}); | ||
WITHOUT_BODY_METHODS.map(val => val.toLowerCase() as Lowercase<typeof val>).forEach((value) => { | ||
instance[value] = (input, params, options) => instance(input, { ...defaultConfig, params, method: value, ...options }) | ||
}) | ||
instance[value] = (input, params, options) => instance(input, { ...defaultConfig, params, method: value, ...options }); | ||
}); | ||
WITH_BODY_METHODS.map(val => val.toLowerCase() as Lowercase<typeof val>).forEach((value) => { | ||
instance[value] = (input, body: any, options) => instance(input, { ...defaultConfig, body, method: value, ...options }) | ||
}) | ||
instance.request = options => instance(options.input, { ...defaultConfig, ...options }) | ||
instance[value] = (input, body: any, options) => instance(input, { ...defaultConfig, body, method: value, ...options }); | ||
}); | ||
instance.request = options => instance(options.input, { ...defaultConfig, ...options }); | ||
return instance | ||
} | ||
return instance; | ||
}; | ||
const instance = createInstance(defaultConfig) | ||
export default instance | ||
const instance = createInstance(defaultConfig); | ||
export default instance; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26529
255