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.1.1 to 0.1.2

4

package.json
{
"name": "@ckpack/fetch-helper",
"private": false,
"version": "0.1.1",
"version": "0.1.2",
"description": "一个轻量级的工具函数库",
"sideEffects": false,
"repository": "https://github.com/ckpack/lib",
"repository": "https://github.com/ckpack/fetch-helper",
"author": "chenkai",

@@ -9,0 +9,0 @@ "license": "MIT",

# @ckpack/fetch-helper
<h4 align="center">
<a href="/README-EN.md">Chinese</a>
<a href="/README-ZH.md">中文</a>
|

@@ -27,3 +27,3 @@ <a href="/README.md">ENGLISH</a>

```js
import fetchHelper from '@ckpack/fetch-helper';
import fetchHelper from '@ckpack/fetch-helper'

@@ -36,3 +36,3 @@ // equal to fetch('http://jsonplaceholder.typicode.com/comments?id=1')

},
});
})
```

@@ -48,3 +48,3 @@

baseURL: 'http://jsonplaceholder.typicode.com',
});
})
```

@@ -85,3 +85,3 @@ ### fetchHelper.default

```js
import fetchHelper from '@ckpack/fetch-helper';
import fetchHelper from '@ckpack/fetch-helper'

@@ -91,10 +91,10 @@ const instance = fetchHelper.create({

baseURL: 'http://jsonplaceholder.typicode.com',
});
})
// fetch('http://jsonplaceholder.typicode.com/comments?id=1')
instance(`/comments`, {
instance('/comments', {
params: {
id: 1,
},
});
})

@@ -104,11 +104,11 @@ // Permanently modify the configuration via the default property

instance.default.baseURL = 'http://localhost:3000'
instance(`/comments`, {
instance('/comments', {
params: {
id: 1,
},
});
})
// Temporarily modify the configuration by parameters
// fetch('http://localhost:3000/comments?id=1')
await instance(`/comments`, {
await instance('/comments', {
baseURL: 'http://localhost:3000',

@@ -118,3 +118,3 @@ params: {

},
});
})
```

@@ -132,3 +132,3 @@

body: JSON.stringify({ firstName: 'Fred', lastName: 'Flintstone' }),
});
})

@@ -140,3 +140,3 @@ // or

},
});
})
```

@@ -153,5 +153,5 @@ ### set query string params

page: 2,
ids: [1,2,3] // ids=1,2,3
ids: [1, 2, 3] // ids=1,2,3
},
});
})
```

@@ -172,3 +172,3 @@ ### paramsSerializer

paramsSerializer: (params) => Qs.stringify(params, {arrayFormat: 'brackets'},
});
})
```

@@ -182,5 +182,5 @@

baseURL: 'http://jsonplaceholder.typicode.com',
transformRequest(init){
const { body } = init;
if(typeof body === 'object' && !(body instanceof FormData || body instanceof URLSearchParams)) {
transformRequest(init) {
const { body } = init
if (typeof body === 'object' && !(body instanceof FormData || body instanceof URLSearchParams)) {
const headers = new Headers(init.headers)

@@ -191,5 +191,5 @@ headers.set('Content-type', 'application/json')

}
return init;
return init
},
});
})

@@ -201,7 +201,7 @@ const res = await resuest.post('/posts', { firstName: 'Fred', lastName: 'Flintstone' })

```js
const fetchHelper = fetchHelper(`http://jsonplaceholder.typicode.com/comments`, {
const fetchHelper = fetchHelper('http://jsonplaceholder.typicode.com/comments', {
transformResponse(response) {
return response.json();
return response.json()
},
});
})
// The request result will be converted to json

@@ -212,7 +212,7 @@ ```

// If TypeScript is used, generic types can be specified
const fetchHelper = fetchHelper<{id: number}[]>(`http://jsonplaceholder.typicode.com/comments`, {
const fetchHelper = fetchHelper<{ id: number }[]>('http://jsonplaceholder.typicode.com/comments', {
transformResponse(response) {
return response.json();
return response.json()
},
});
})
// fetchHelper[0].id

@@ -226,14 +226,14 @@ ```

transformRequest(config) {
if(config.timeout){
const controller = new AbortController();
config.signal = controller.signal;
setTimeout(()=> controller.abort('timeout'), config.timeout)
if (config.timeout) {
const controller = new AbortController()
config.signal = controller.signal
setTimeout(() => controller.abort('timeout'), config.timeout)
}
return config;
return config
},
});
})
await instance('http://jsonplaceholder.typicode.com/comments', {
timeout: 6000,
});
})
// automatically cancel the request after six seconds

@@ -245,3 +245,3 @@ ```

```js
const fetchResponse = await fetchHelper(`http://jsonplaceholder.typicode.com/comments`, {
const fetchResponse = await fetchHelper('http://jsonplaceholder.typicode.com/comments', {
params: {

@@ -251,6 +251,6 @@ limit: 1,

},
adapter(input){
return new Response(`${input}`);
adapter(input) {
return new Response(`${input}`)
},
});
})

@@ -257,0 +257,0 @@ console.log(await fetchResponse.text())

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