@ckpack/fetch-helper
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"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()) |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
4
25168