@bdsoft/rabbit
v1.2.0
Published
rabbit封装的ajax访问插件
Downloads
33
Readme
注意:
代码同步来源地址:http://223.99.22.139:18091/jxcdchgp/sdk_js.git 将同步过来的代码复制到src下
rabbit 封装的 ajax 访问插件
Install
pnpm add @bdsoft/rabbit构建与打包
该包使用 Vite Library Mode 生成纯 ESM 产物:
pnpm --filter @bdsoft/rabbit run build构建结果为 dist/rabbit.js 和对应 Source Map。发布前可检查 npm 包内容:
cd packages/rabbit-sdk
npm pack --dry-run第三方 Token Header
Rabbit 可选择性地从当前页面 URL 捕获第三方 Token,并只为匹配数据服务 host 的请求动态添加 Header:
import * as rabbit from '@bdsoft/rabbit'
rabbit.configureThirdPartyToken({
host: 'http://data.example:8050',
enabled: true,
queryName: 'access_token',
headerName: 'Authorization',
valuePrefix: 'Bearer ',
storageType: 'sessionStorage',
storageKey: 'bd_third_party_access_token',
removeFromUrl: true
})配置默认关闭。启用后,URL 中的非空 Token 会写入 sessionStorage;也可以将 storageType 设置为 localStorage。请求发送时会重新读取存储,因此刷新页面后仍然有效。removeFromUrl: true 只在 Token 成功写入后通过 history.replaceState 清理 URL 参数。
匹配规则同时校验 origin 和路径边界。例如配置 http://data.example/api 时,会匹配 /api 及其子路径,但不会匹配 /api-v2。如果调用方已经显式传入同名 Header(不区分大小写),Rabbit 不会覆盖它。只有自动添加的 Token Header 跳过 Rabbit 原有的 Header 值 URI 编码,其他 Header 行为不变。
如服务端不要求前缀,可设置 valuePrefix: ''。退出登录时可清理当前 host 对应的存储值:
rabbit.clearThirdPartyToken('http://data.example:8050')自定义 Header 可能触发浏览器 CORS 预检;服务端需要在 Access-Control-Allow-Headers 中允许配置的 Header 名称。
Usage
import rabbit from '@bdsoft/rabbit'
// Make a request for a user with a given ID
rabbit
.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response)
})
.catch(function (error) {
// handle error
console.log(error)
})
.finally(function () {
// always executed
})
// Optionally the request above could also be done as
rabbit
.get('/user', {
params: {
ID: 12345,
},
})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
.finally(function () {
// always executed
})Performing a POST request
rabbit
.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone',
})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
rabbit
.post(
'/user',
{
firstName: 'Fred',
lastName: 'Flintstone',
},
{
params: {
ID: 12345,
},
}
)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})rabbit API
Requests can be made by passing the relevant config to rabbit.
rabbit(config)
// Send a POST request
rabbit.ajax({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone',
},
})// GET request for remote image
rabbit
.ajax({
method: 'get',
url: 'http://bit.ly/2mTM3nY',
responseType: 'stream',
})
.then(function (response) {
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
})For convenience aliases have been provided for all supported request methods.
rabbit.ajax(config)
rabbit.get(url[, config])
rabbit.delete(url[, config])
rabbit.head(url[, config])
rabbit.options(url[, config])
rabbit.post(url[, data[, config]])
rabbit.put(url[, data[, config]])
rabbit.patch(url[, data[, config]])
Request Config
These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not specified.
{
// `url` is the server URL that will be used for the request
url: '/user',
// `method` is the request method to be used when making the request
method: 'get', // default
// `headers` are custom headers to be sent
headers: {'X-Requested-With': 'XMLHttpRequest'},
// `params` are the URL parameters to be sent with the request
// Must be a plain object or a URLSearchParams object
params: {
ID: 12345
},
//contentType
contentType:"application/json; charset=UTF-8",// default
//async
async:ture,// default
// `data` is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no `transformRequest` is set, must be of one of the following types:
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream, Buffer
data: {
firstName: 'Fred'
},
// syntax alternative to send data into the body
// method post
// only the value is sent, not the key
data: 'Country=Brasil&City=Belo Horizonte',
// `timeout` specifies the number of milliseconds before the request times out.
// If the request takes longer than `timeout`, the request will be aborted.
timeout: 1000, // default is `0` (no timeout)
// `dataType` indicates the type of data that the server will respond with
// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
// browser only: 'blob'
dataType: 'json', // default
//Method executed after successful operation
success:function (res, status, errCode) {
},
//Method to be executed after running failure
error:function (res, status, errCode) {
},
//The method executed after the operation is completed
complete:function (xdr,status, errCode) {
},
}
License
更新记录
1 添加Data对象操作,路径是相对于整个服务的;与Resource的区别在与,Resource是定制出来的服务,相对于Resource目录的,Data比Resource调用时的相对路径要多写一级路径。
MIT