@ckpack/fetch-helper
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -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 l=(s,n)=>{for(var r in n)a(s,r,{get:n[r],enumerable:!0})},y=(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 f=s=>y(a({},"__esModule",{value:!0}),s);var q={};l(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 f(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:()=>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);})(); | ||
fetchHelper = fetchHelper.default; |
@@ -1,10 +0,10 @@ | ||
export declare type RequestParams = URLSearchParams | Record<string | number, any> | undefined; | ||
export declare type TransformRequest = (init: FetchHelperInit, ctx: FetchHelper) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export declare type TransformResponse = (response: Response, ctx: FetchHelper) => Promise<unknown>; | ||
declare const paramsSerializer: (params?: RequestParams) => string; | ||
export declare const mergeHeaders: (header?: HeadersInit, header2?: HeadersInit) => Headers; | ||
export type RequestParams = ConstructorParameters<typeof URLSearchParams>[number]; | ||
export type TransformRequest = (init: FetchHelperInit, ctx: FetchHelper) => Promise<FetchHelperInit> | FetchHelperInit; | ||
export type TransformResponse = (response: Response, ctx: FetchHelper) => Promise<unknown>; | ||
declare function paramsSerializer(params?: RequestParams): string; | ||
export declare function mergeHeaders(header?: HeadersInit, header2?: HeadersInit): Headers; | ||
export declare const WITHOUT_BODY_METHODS: readonly ["GET", "HEAD", "OPTIONS", "CONNECT", "TRACE"]; | ||
export declare const WITH_BODY_METHODS: readonly ["DELETE", "PATCH", "POST", "PUT"]; | ||
export declare 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 declare type FetchHelperInput = RequestInfo; | ||
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 interface FetchHelperInit extends RequestInit { | ||
@@ -11,0 +11,0 @@ baseURL?: string; |
@@ -1,3 +0,5 @@ | ||
const paramsSerializer = (params) => new URLSearchParams(params).toString(); | ||
export const mergeHeaders = (header, header2) => { | ||
function paramsSerializer(params) { | ||
return new URLSearchParams(params).toString(); | ||
} | ||
export function mergeHeaders(header, header2) { | ||
const base = new Headers(header); | ||
@@ -8,3 +10,3 @@ new Headers(header2).forEach((value, key) => { | ||
return base; | ||
}; | ||
} | ||
export const WITHOUT_BODY_METHODS = ['GET', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE']; | ||
@@ -11,0 +13,0 @@ export const WITH_BODY_METHODS = ['DELETE', 'PATCH', 'POST', 'PUT']; |
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 | 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 & { | ||
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 & { | ||
input: FetchHelperInput; | ||
}) => Promise<T | undefined>; | ||
export declare const createInstance: (defaultConfig?: FetchHelperInit) => (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T | undefined>) & { | ||
export declare function createInstance(defaultConfig?: FetchHelperInit): (<T = Response>(input: RequestInfo, init?: FetchHelperInit | undefined) => Promise<T | undefined>) & { | ||
create: typeof createInstance; | ||
@@ -10,0 +10,0 @@ request: InputBodyMethod; |
import { FetchHelper, WITHOUT_BODY_METHODS, WITH_BODY_METHODS } from './FetchHelper.js'; | ||
export * from './FetchHelper.js'; | ||
const defaultConfig = {}; | ||
export const createInstance = (defaultConfig) => { | ||
export function createInstance(defaultConfig) { | ||
const context = new FetchHelper(defaultConfig); | ||
@@ -21,4 +21,4 @@ const instance = context.request.bind(context); | ||
return instance; | ||
}; | ||
} | ||
const instance = createInstance(defaultConfig); | ||
export default instance; |
{ | ||
"name": "@ckpack/fetch-helper", | ||
"private": false, | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "一个轻量级的工具函数库", | ||
@@ -37,12 +37,12 @@ "sideEffects": false, | ||
"devDependencies": { | ||
"husky": "^8.0.1", | ||
"husky": "^8.0.3", | ||
"standard-version": "^9.5.0", | ||
"lint-staged": "^13.0.3", | ||
"eslint": "^8.23.0", | ||
"@antfu/eslint-config": "0.26.3", | ||
"lint-staged": "^13.2.0", | ||
"eslint": "^8.37.0", | ||
"@antfu/eslint-config": "0.38.4", | ||
"@types/node": "18.7.14", | ||
"typescript": "^4.8.2", | ||
"vitest": "0.23.1", | ||
"@vitest/coverage-c8": "0.23.1", | ||
"esbuild": "0.15.7" | ||
"typescript": "^5.0.3", | ||
"vitest": "0.29.8", | ||
"@vitest/coverage-c8": "0.29.8", | ||
"esbuild": "0.17.15" | ||
}, | ||
@@ -49,0 +49,0 @@ "engines": { |
@@ -1,8 +0,10 @@ | ||
export type RequestParams = URLSearchParams | Record<string | number, any> | undefined; | ||
export type RequestParams = ConstructorParameters<typeof URLSearchParams>[number]; | ||
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(); | ||
function paramsSerializer(params?: RequestParams) { | ||
return new URLSearchParams(params).toString(); | ||
} | ||
export const mergeHeaders = (header?: HeadersInit, header2?: HeadersInit) => { | ||
export function mergeHeaders(header?: HeadersInit, header2?: HeadersInit) { | ||
const base = new Headers(header); | ||
@@ -13,3 +15,3 @@ new Headers(header2).forEach((value, key) => { | ||
return base; | ||
}; | ||
} | ||
@@ -55,3 +57,5 @@ export const WITHOUT_BODY_METHODS = ['GET', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE'] as const; | ||
const queryString = (this.init.paramsSerializer || paramsSerializer)(this.init.params); | ||
if (queryString) { this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; } | ||
if (queryString) { | ||
this.input = `${inputURL.href}${inputURL.search ? '&' : '?'}${queryString}`; | ||
} | ||
else { this.input = inputURL.href; } | ||
@@ -62,7 +66,11 @@ } | ||
const response = await (this.init.adapter || fetch)(this.input, this.init); | ||
if (this.init.handlerSuccess) { this.init?.handlerSuccess(response); } | ||
if (this.init.handlerSuccess) { | ||
this.init?.handlerSuccess(response); | ||
} | ||
return mergeInit.transformResponse ? mergeInit.transformResponse(response, this) : response as any; | ||
} | ||
catch (error: any) { | ||
if (this.init.handlerError) { this.init?.handlerError(error); } | ||
if (this.init.handlerError) { | ||
this.init?.handlerError(error); | ||
} | ||
else { throw error; } | ||
@@ -69,0 +77,0 @@ } |
import type { FetchHelperInit, FetchHelperInput, RequestParams } from './FetchHelper.js'; | ||
import { FetchHelper, WITHOUT_BODY_METHODS, WITH_BODY_METHODS } from './FetchHelper.js'; | ||
export * from './FetchHelper.js'; | ||
@@ -7,9 +8,9 @@ | ||
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 & { | ||
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 & { | ||
input: FetchHelperInput | ||
}) => Promise<T | undefined>; | ||
export const createInstance = (defaultConfig?: FetchHelperInit) => { | ||
export function createInstance(defaultConfig?: FetchHelperInit) { | ||
const context = new FetchHelper(defaultConfig); | ||
@@ -49,5 +50,5 @@ | ||
return 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
26570
266