request/lib/helper.js

42 lines
849 B
JavaScript

/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2024/07/16 17:01:43
*/
import { parse } from 'node:querystring'
export function querystring(str) {
let query = {...parse(str)}
for (let k of Object.keys(query)) {
let val = query[k]
if (k.endsWith('[]')) {
let _k = k.slice(0, -2)
if (query.hasOwnProperty(_k)) {
let old = query[_k]
if (!Array.isArray(old)) {
old = [old]
}
query[_k] = old.concat(val)
} else {
query[_k] = val
}
delete query[k]
} else if (k.endsWith(']')) {
let idx = k.lastIndexOf('[')
let _pk = k.slice(0, idx)
let _k = k.slice(idx + 1, -1)
if (query[_pk]) {
query[_pk][_k] = val
} else {
query[_pk] = { [_k]: val }
}
delete query[k]
}
}
return query
}
对Http的request进一步封装, 提供常用的API
JavaScript 100%