Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@chunpu/http
Advanced tools
Promise Based request / fetch / http For Real Project, Support multiple platforms
Promise Based request / fetch / http For Real Project, Support multiple platforms
npm i @chunpu/http
Inspired by axios
Send Http Request just like axios in 微信小程序
, 快应用
, jQuery
, or XMLHttpRequest
by default
Let's have the Same Experience with Request Data😜
import http from '@chunpu/http'
http.init({
baseURL: 'https://my.domain'
})
http.get('/data').then(({data}) => {
console.log(data)
})
const anotherHttp = http.create({
baseURL: 'https://my.domain'
})
.get(url, config)
.delete(url, config)
.head(url, config)
.options(url, config)
Careful! There is no such api like
.get(url, params)
.post(url, data, config)
.put(url, data, config)
.patch(url, data, config)
.request(config)
.request(url, config)
All config param is not required
data
data for request bodyheaders
request headersmethod
request http method, default GET
params
the url querystring objecttimeout
request timeout, 支持快应用和微信小程序withCredentials
whether use cors, default false
Respect the request headers['content-type']
setting, data will be transform by the content type, Plain Object data will be auto stringify
application/json
will JSON.stringify
the data objectapplication/x-www-form-urlencoded
will qs.stringify
the data objectdata also support FormData, Blob, String
data
response dataheaders
name: value
headers, all header names are lower casedstatus
status code, numberconfig
the request objectNot Respect the response headers['content-type']
value, will always try to JSON.parse
the data, because most server not respect the response mime
import http from '@chunpu/http'
http.init({
baseURL: 'https://my.domain',
wx: wx
})
http.get('/data').then(({data}) => {
console.log(data)
})
支持单个请求超时设置
请通过 npm 安装, 参见 npm 支持
import http from '@chunpu/http'
import fetch from '@system.fetch'
http.init({
baseURL: 'https://my.domain',
quickapp: fetch
})
支持单个请求超时设置
记得在 manifest.json
文件中加入权限
"features": [
{ "name": "system.network" },
{ "name": "system.fetch" }
]
const axios = require('axios')
import http from '@chunpu/http'
http.init({
baseURL: 'https://my.domain',
axios: axios
})
Please use http with axios mode
in Node.js platform
import http from '@chunpu/http'
http.init({
baseURL: 'https://my.domain',
jQuery: $
})
// support axios style
http.defaults.baseURL = 'https://my.domain'
http.defaults.timeout = 1000 * 20
// can also use http.init
http.init({
baseURL: 'https://my.domain',
timeout: 1000 * 20
})
Config default Post request
Content-Type
default is JSON
Always stringify Data to JSON
http.defaults.headers.post['Content-Type'] = 'application/json'
Always stringify Data to querystring
, which can really work not like axios...
http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
import http from '@chunpu/http'
http.init({
baseURL: 'https://my.domain'
})
http.interceptors.request.use(config => {
// Do something before request is sent
return config
})
http.interceptors.response.use(response => {
// Do something with response
return response
})
compatible with axios Cancellation
For easy understanding, cancelToken
equals deferred
equals source.token
const source = http.CancelToken.source()
http.get('/very/slow/api/1', {
cancelToken: source.token
}).catch(err => {
console.error(err) // error: cancel request
})
http.get('/very/slow/api/2', {
cancelToken: source.token
}).catch(err => {
console.error(err) // error: cancel request
})
setTimeout(() => {
source.cancel('cancel request') // will cancel all requests with this source
}, 1000)
Assume the my.domain
service always return data like this
{
code: 0,
message: 'ok',
data: {
key: 'value'
}
}
import http from '@chunpu/http'
http.init({
baseURL: 'https://my.domain'
})
http.interceptors.response.use(response => {
if (typeof response.data === 'object') {
// always spread the response data for directly usage
Object.assign(response, response.data)
}
return response
})
http.post('/user/1024', {
name: 'Tony'
}).then(({data, code, message}) => {
if (code === 0) {
return data
} else {
console.error('error', message)
}
})
import http from '@chunpu/http'
Vue.prototype.$http = http
// in vue component file
submit () {
this.$http.post('/user/1024', {name: 'Tony'}).then(({data}) => {
this.user = data
})
}
All Platform support timeout error for one request
http.get('/very/slow/api').catch(err => {
if (/timeout/i.test(err.message)) {
// this is timeout error
}
})
You can stringify query string by
import http from '@chunpu/http'
http.qs.stringify({
query: 'string'
})
// => 'query=string'
FAQs
Promise Based request / fetch / http For Real Project, Support multiple platforms
We found that @chunpu/http demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.