Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ckpack/fetch-helper

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ckpack/fetch-helper - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

2

dist/index.es.js

@@ -1,1 +0,1 @@

function p(o=[]){return(e,t)=>o.reduce(async(n,s)=>s(await n,t),e)}var f=class{defaults;constructor(e){this.defaults=e}async request(e,t){return new Promise(async(n,s)=>{let i={...this.defaults,...t},r=await p(i?.interceptors?.request)(i),c;r.timeout&&(c=setTimeout(()=>{clearTimeout(c),s(new Error(`Timeout(${r.timeout}ms): Fetch ${e}`))},r.timeout));let a=await fetch(e,r);clearTimeout(c),n(p(i?.interceptors?.response)(a,r))})}};var m={mode:"same-origin",method:"GET",interceptors:{request:[],response:[]}};function u(o){let e=new f(o),t=e.request.bind(e);return t.create=n=>u({...o,...n}),t}var C=u(m);export{C as default};
function c(n=[]){return(e,t)=>n.reduce(async(r,s)=>s(await r,t),e)}var p=class{defaults;constructor(e){this.defaults=e}async request(e,t){return new Promise(async(r,s)=>{let u={...this.defaults,...t},o=await c(u?.interceptors?.request)(u),i;o.timeout&&(i=setTimeout(()=>{clearTimeout(i),s(new Error(`Timeout(${o.timeout}ms): Fetch ${e}`))},o.timeout));let f=await fetch(e,o);clearTimeout(i),r(await c(o?.interceptors?.response)(f,o))})}};function a(n){let e=new p(n),t=e.request.bind(e);return t.create=r=>a({...n,...r}),t}var y=a();export{y as default};

@@ -1,2 +0,2 @@

var fetchHelper=(()=>{var p=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var l=t=>p(t,"__esModule",{value:!0});var C=(t,e)=>{for(var o in e)p(t,o,{get:e[o],enumerable:!0})},y=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of d(e))!g.call(t,n)&&(o||n!=="default")&&p(t,n,{get:()=>e[n],enumerable:!(r=h(e,n))||r.enumerable});return t};var F=(t=>(e,o)=>t&&t.get(e)||(o=y(l({}),e,1),t&&t.set(e,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var R={};C(R,{default:()=>q});function f(t=[]){return(e,o)=>t.reduce(async(r,n)=>n(await r,o),e)}var u=class{defaults;constructor(e){this.defaults=e}async request(e,o){return new Promise(async(r,n)=>{let i={...this.defaults,...o},s=await f(i?.interceptors?.request)(i),c;s.timeout&&(c=setTimeout(()=>{clearTimeout(c),n(new Error(`Timeout(${s.timeout}ms): Fetch ${e}`))},s.timeout));let m=await fetch(e,s);clearTimeout(c),r(f(i?.interceptors?.response)(m,s))})}};var w={mode:"same-origin",method:"GET",interceptors:{request:[],response:[]}};function a(t){let e=new u(t),o=e.request.bind(e);return o.create=r=>a({...t,...r}),o}var q=a(w);return F(R);})();
var fetchHelper=(()=>{var c=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var y=t=>c(t,"__esModule",{value:!0});var F=(t,e)=>{for(var o in e)c(t,o,{get:e[o],enumerable:!0})},w=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of d(e))!l.call(t,n)&&(o||n!=="default")&&c(t,n,{get:()=>e[n],enumerable:!(r=h(e,n))||r.enumerable});return t};var g=(t=>(e,o)=>t&&t.get(e)||(o=w(y({}),e,1),t&&t.set(e,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var R={};F(R,{default:()=>C});function p(t=[]){return(e,o)=>t.reduce(async(r,n)=>n(await r,o),e)}var u=class{defaults;constructor(e){this.defaults=e}async request(e,o){return new Promise(async(r,n)=>{let a={...this.defaults,...o},s=await p(a?.interceptors?.request)(a),i;s.timeout&&(i=setTimeout(()=>{clearTimeout(i),n(new Error(`Timeout(${s.timeout}ms): Fetch ${e}`))},s.timeout));let m=await fetch(e,s);clearTimeout(i),r(await p(s?.interceptors?.response)(m,s))})}};function f(t){let e=new u(t),o=e.request.bind(e);return o.create=r=>f({...t,...r}),o}var C=f();return g(R);})();
fetchHelper = fetchHelper.default;

@@ -12,3 +12,3 @@ import type { RequestMiddleware, ResponseMiddleware } from './compose';

constructor(fetchConfig?: FetchConfig);
request(resource: RequestInfo, requestConfig?: FetchConfig): Promise<Response>;
request(fetchInput: RequestInfo, fetchInit?: FetchConfig): Promise<Response>;
}

@@ -7,18 +7,18 @@ import { compose } from './compose';

}
async request(resource, requestConfig) {
async request(fetchInput, fetchInit) {
return new Promise(async (resolve, reject) => {
const mergeConfig = { ...this.defaults, ...requestConfig };
const composeConfig = await compose(mergeConfig?.interceptors?.request)(mergeConfig);
const mergeFetchInit = { ...this.defaults, ...fetchInit };
const composeFetchInit = await compose(mergeFetchInit?.interceptors?.request)(mergeFetchInit);
let timeOut;
if (composeConfig.timeout) {
if (composeFetchInit.timeout) {
timeOut = setTimeout(() => {
clearTimeout(timeOut);
reject(new Error(`Timeout(${composeConfig.timeout}ms): Fetch ${resource}`));
}, composeConfig.timeout);
reject(new Error(`Timeout(${composeFetchInit.timeout}ms): Fetch ${fetchInput}`));
}, composeFetchInit.timeout);
}
const response = await fetch(resource, composeConfig);
const response = await fetch(fetchInput, composeFetchInit);
clearTimeout(timeOut);
resolve(compose(mergeConfig?.interceptors?.response)(response, composeConfig));
resolve(await compose(composeFetchInit?.interceptors?.response)(response, composeFetchInit));
});
}
}
import type { FetchConfig } from './fetch-helper';
declare function createInstance(defaultConfig?: FetchConfig): ((resource: RequestInfo, requestConfig?: FetchConfig | undefined) => Promise<Response>) & {
declare function createInstance(defaultConfig?: FetchConfig): ((fetchInput: RequestInfo, fetchInit?: FetchConfig | undefined) => Promise<Response>) & {
create?: typeof createInstance | undefined;
};
declare const _default: ((resource: RequestInfo, requestConfig?: FetchConfig | undefined) => Promise<Response>) & {
declare const _default: ((fetchInput: RequestInfo, fetchInit?: FetchConfig | undefined) => Promise<Response>) & {
create?: typeof createInstance | undefined;
};
export default _default;
import { FetchHelper } from './fetch-helper';
const defaultConfigt = {
mode: 'same-origin',
method: 'GET',
interceptors: {
request: [],
response: [],
},
};
function createInstance(defaultConfig) {

@@ -19,2 +11,2 @@ const context = new FetchHelper(defaultConfig);

}
export default createInstance(defaultConfigt);
export default createInstance();
{
"name": "@ckpack/fetch-helper",
"version": "0.0.4",
"version": "0.0.5",
"description": "Fetch simple wrapper helper",

@@ -5,0 +5,0 @@ "type": "module",

# fetch-helper
`fetch-helper`是对 [fetch](https://developer.mozilla.org/zh-CN/docs/Web/API/fetch) API的简单封装, 所以只支持在浏览器中使用,如果需要在node环境中使用,请添加`fetch-node`依赖。

@@ -9,3 +10,4 @@

### 参数
## 参数
> Promise<Response> fetch(input[, Fetchinit]);

@@ -17,4 +19,4 @@

+ `interceptors`: ``, 请求拦截器,该参数包含两个属性
- `request`: `(fetchConfig: FetchConfig) => FetchConfig []`, 请求触发前触发,你可以用来修改请求参数
- `response`: `(response: Response, fetchConfig: FetchConfig) => unknown []`, 请求触发后触发,你可以用来修改返回结果
+ `request`: `(fetchConfig: FetchConfig) => FetchConfig []`, 请求触发前触发,你可以用来修改请求参数
+ `response`: `(response: Response, fetchConfig: FetchConfig) => unknown []`, 请求触发后触发,你可以用来修改返回结果

@@ -25,2 +27,3 @@ 如下面是一个获取修改`headers`,并将返回结果转为`json`的例子

const fetchHelper = FetchHelper(`some url`, {
// fetch配置项所有可选的参数
interceptors: {

@@ -40,7 +43,7 @@ request: [(config) => {

#### 返回值
## 返回值
异步返回一个 [Response](https://developer.mozilla.org/zh-CN/docs/Web/API/Response) 对象。
#### 其他
## 其他

@@ -66,2 +69,1 @@ 你可以使用`create`方法创建一个自定义配置的新实例。

```

@@ -19,18 +19,18 @@ import type { RequestMiddleware, ResponseMiddleware } from './compose';

async request(resource: RequestInfo, requestConfig?: FetchConfig) :Promise<Response> {
async request(fetchInput: RequestInfo, fetchInit?: FetchConfig) :Promise<Response> {
return new Promise(async (resolve, reject) => {
const mergeConfig = { ...this.defaults, ...requestConfig };
const composeConfig = await compose(mergeConfig?.interceptors?.request)(mergeConfig);
const mergeFetchInit = { ...this.defaults, ...fetchInit };
const composeFetchInit = await compose(mergeFetchInit?.interceptors?.request)(mergeFetchInit);
let timeOut!: ReturnType<typeof setTimeout>;
if (composeConfig.timeout) {
if (composeFetchInit.timeout) {
timeOut = setTimeout(() => {
clearTimeout(timeOut);
reject(new Error(`Timeout(${composeConfig.timeout}ms): Fetch ${resource}`));
}, composeConfig.timeout);
reject(new Error(`Timeout(${composeFetchInit.timeout}ms): Fetch ${fetchInput}`));
}, composeFetchInit.timeout);
}
const response = await fetch(resource, composeConfig);
const response = await fetch(fetchInput, composeFetchInit);
clearTimeout(timeOut);
resolve(compose(mergeConfig?.interceptors?.response)(response, composeConfig));
resolve(await compose(composeFetchInit?.interceptors?.response)(response, composeFetchInit));
});
}
}
import type { FetchConfig } from './fetch-helper';
import { FetchHelper } from './fetch-helper';
const defaultConfigt: FetchConfig = {
mode: 'same-origin',
method: 'GET',
interceptors: {
request: [],
response: [],
},
};
function createInstance(defaultConfig?: FetchConfig) {

@@ -18,2 +9,3 @@ const context = new FetchHelper(defaultConfig);

} = context.request.bind(context);
instance.create = (createConfig?: FetchConfig) => createInstance({

@@ -26,2 +18,2 @@ ...defaultConfig,

export default createInstance(defaultConfigt);
export default createInstance();
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc