@ckpack/fetch-helper
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -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 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:()=>u,default:()=>O,mergeHeaders:()=>d});function T(s){return new URLSearchParams(s).toString()}function d(s,n){let r=new Headers(s);return new Headers(n).forEach((e,t)=>{r.set(t,e)}),r}var 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:d(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={};function u(s){let n=new i(s),r=n.request.bind(n);return r.default=s||{},r.create=e=>u({...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}var F=u(R),O=F;return y(q);})(); | ||
"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);})(); | ||
fetchHelper = fetchHelper.default; |
@@ -1,4 +0,4 @@ | ||
export type RequestParams = ConstructorParameters<typeof URLSearchParams>[number]; | ||
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 = (response: Response, ctx: FetchHelper) => Promise<unknown>; | ||
export type TransformResponse = <T = Response>(response: Response | undefined, error: Error | undefined, ctx: FetchHelper) => Promise<T>; | ||
declare function paramsSerializer(params?: RequestParams): string; | ||
@@ -27,4 +27,4 @@ export declare function mergeHeaders(header?: HeadersInit, header2?: HeadersInit): Headers; | ||
constructor(fetchConfig?: FetchHelperInit); | ||
request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T | undefined>; | ||
request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T>; | ||
} | ||
export {}; |
@@ -34,10 +34,12 @@ function paramsSerializer(params) { | ||
} | ||
let response; | ||
let error; | ||
try { | ||
const response = await (this.init.adapter || fetch)(this.input, this.init); | ||
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) { | ||
catch (e) { | ||
error = e; | ||
if (this.init.handlerError) { | ||
@@ -50,3 +52,7 @@ this.init?.handlerError(error); | ||
} | ||
finally { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, error, this) : response; | ||
} | ||
} | ||
} |
import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
export * from './FetchHelper.js'; | ||
export type WithoutBodyMethod = <T = Response>(input: FetchHelperInput, params?: RequestParams, 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 & { | ||
export type WithoutBodyMethod<T = Response> = (input: FetchHelperInput, params?: RequestParams, options?: FetchHelperInit) => Promise<T>; | ||
export type WithBodyMethod<T = Response> = (input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<T>; | ||
export type InputBodyMethod<T = Response> = (options: FetchHelperInit & { | ||
input: FetchHelperInput; | ||
}) => Promise<T | undefined>; | ||
export declare function createInstance(defaultConfig?: FetchHelperInit): (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T | undefined>) & { | ||
}) => Promise<T>; | ||
export declare function createInstance<T = Response>(defaultConfig?: FetchHelperInit): (<T_1 = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T_1>) & { | ||
create: typeof createInstance; | ||
request: InputBodyMethod; | ||
get: WithoutBodyMethod; | ||
head: WithoutBodyMethod; | ||
options: WithoutBodyMethod; | ||
connect: WithoutBodyMethod; | ||
trace: WithoutBodyMethod; | ||
delete: WithBodyMethod; | ||
patch: WithBodyMethod; | ||
post: WithBodyMethod; | ||
put: WithBodyMethod; | ||
request: InputBodyMethod<T>; | ||
get: WithoutBodyMethod<T>; | ||
head: WithoutBodyMethod<T>; | ||
options: WithoutBodyMethod<T>; | ||
connect: WithoutBodyMethod<T>; | ||
trace: WithoutBodyMethod<T>; | ||
delete: WithBodyMethod<T>; | ||
patch: WithBodyMethod<T>; | ||
post: WithBodyMethod<T>; | ||
put: WithBodyMethod<T>; | ||
default: FetchHelperInit; | ||
}; | ||
declare const instance: (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T | undefined>) & { | ||
declare const instance: (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T>) & { | ||
create: typeof createInstance; | ||
request: InputBodyMethod; | ||
get: WithoutBodyMethod; | ||
head: WithoutBodyMethod; | ||
options: WithoutBodyMethod; | ||
connect: WithoutBodyMethod; | ||
trace: WithoutBodyMethod; | ||
delete: WithBodyMethod; | ||
patch: WithBodyMethod; | ||
post: WithBodyMethod; | ||
put: WithBodyMethod; | ||
request: InputBodyMethod<Response>; | ||
get: WithoutBodyMethod<Response>; | ||
head: WithoutBodyMethod<Response>; | ||
options: WithoutBodyMethod<Response>; | ||
connect: WithoutBodyMethod<Response>; | ||
trace: WithoutBodyMethod<Response>; | ||
delete: WithBodyMethod<Response>; | ||
patch: WithBodyMethod<Response>; | ||
post: WithBodyMethod<Response>; | ||
put: WithBodyMethod<Response>; | ||
default: FetchHelperInit; | ||
}; | ||
export default instance; |
{ | ||
"name": "@ckpack/fetch-helper", | ||
"private": false, | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "一个轻量级的工具函数库", | ||
@@ -38,11 +38,11 @@ "sideEffects": false, | ||
"husky": "^8.0.3", | ||
"lint-staged": "^13.2.2", | ||
"standard-version": "^9.5.0", | ||
"lint-staged": "^13.2.0", | ||
"eslint": "^8.37.0", | ||
"@antfu/eslint-config": "0.38.4", | ||
"eslint": "^8.41.0", | ||
"@antfu/eslint-config": "0.39.1", | ||
"@types/node": "18.7.14", | ||
"typescript": "^5.0.3", | ||
"vitest": "0.29.8", | ||
"@vitest/coverage-c8": "0.29.8", | ||
"esbuild": "0.17.15" | ||
"typescript": "^5.0.4", | ||
"vitest": "0.31.1", | ||
"@vitest/coverage-c8": "0.31.1", | ||
"esbuild": "0.17.19" | ||
}, | ||
@@ -49,0 +49,0 @@ "engines": { |
@@ -1,7 +0,7 @@ | ||
export type RequestParams = ConstructorParameters<typeof URLSearchParams>[number]; | ||
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 = (response: Response, ctx: FetchHelper) => Promise<unknown>; | ||
export type TransformResponse = <T = Response>(response: Response | undefined, error: Error | undefined, ctx: FetchHelper) => Promise<T>; | ||
function paramsSerializer(params?: RequestParams) { | ||
return new URLSearchParams(params).toString(); | ||
return new URLSearchParams(params as ConstructorParameters<typeof URLSearchParams>[number]).toString(); | ||
} | ||
@@ -46,3 +46,3 @@ | ||
async request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit): Promise<T | undefined> { | ||
async request<T = Response>(input: FetchHelperInput, init?: FetchHelperInit) { | ||
this.input = input; | ||
@@ -59,20 +59,26 @@ const mergeInit: FetchHelperInit = { ...this.defaultInit, ...init, headers: mergeHeaders(this.defaultInit?.headers, init?.headers) }; | ||
this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
} else { | ||
this.input = inputURL.href; | ||
} | ||
else { this.input = inputURL.href; } | ||
} | ||
let response: Response | undefined; | ||
let error: Error | undefined; | ||
try { | ||
const response = await (this.init.adapter || fetch)(this.input, this.init); | ||
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; | ||
} | ||
catch (error: any) { | ||
} catch (e: any) { | ||
error = e; | ||
if (this.init.handlerError) { | ||
this.init?.handlerError(error); | ||
} else { | ||
throw error; | ||
} | ||
else { throw error; } | ||
} finally { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return mergeInit.transformResponse ? mergeInit.transformResponse<T>(response, error, this) : response as T; | ||
} | ||
} | ||
} |
@@ -8,9 +8,9 @@ import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
export type WithoutBodyMethod = <T = Response>(input: FetchHelperInput, params?: RequestParams, 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 & { | ||
export type WithoutBodyMethod<T = Response> = (input: FetchHelperInput, params?: RequestParams, options?: FetchHelperInit) => Promise<T>; | ||
export type WithBodyMethod<T = Response> = (input: FetchHelperInput, body?: BodyInit | Object, options?: FetchHelperInit) => Promise<T>; | ||
export type InputBodyMethod<T = Response> = (options: FetchHelperInit & { | ||
input: FetchHelperInput | ||
}) => Promise<T | undefined>; | ||
}) => Promise<T>; | ||
export function createInstance(defaultConfig?: FetchHelperInit) { | ||
export function createInstance<T = Response>(defaultConfig?: FetchHelperInit) { | ||
const context = new FetchHelper(defaultConfig); | ||
@@ -20,12 +20,12 @@ | ||
create: typeof createInstance | ||
request: InputBodyMethod | ||
get: WithoutBodyMethod | ||
head: WithoutBodyMethod | ||
options: WithoutBodyMethod | ||
connect: WithoutBodyMethod | ||
trace: WithoutBodyMethod | ||
delete: WithBodyMethod | ||
patch: WithBodyMethod | ||
post: WithBodyMethod | ||
put: WithBodyMethod | ||
request: InputBodyMethod<T> | ||
get: WithoutBodyMethod<T> | ||
head: WithoutBodyMethod<T> | ||
options: WithoutBodyMethod<T> | ||
connect: WithoutBodyMethod<T> | ||
trace: WithoutBodyMethod<T> | ||
delete: WithBodyMethod<T> | ||
patch: WithBodyMethod<T> | ||
post: WithBodyMethod<T> | ||
put: WithBodyMethod<T> | ||
default: FetchHelperInit | ||
@@ -36,3 +36,3 @@ }; | ||
instance.create = (createConfig?: FetchHelperInit) => createInstance({ | ||
instance.create = (createConfig?: FetchHelperInit) => createInstance<T>({ | ||
...defaultConfig, | ||
@@ -44,8 +44,8 @@ ...instance.default, | ||
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<T>(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[value] = (input, body: any, options) => instance<T>(input, { ...defaultConfig, body, method: value, ...options }); | ||
}); | ||
instance.request = options => instance(options.input, { ...defaultConfig, ...options }); | ||
instance.request = options => instance<T>(options.input, { ...defaultConfig, ...options }); | ||
@@ -52,0 +52,0 @@ return 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
27273
278