1.0.17 • Published 2 years ago

@fim3/super-request v1.0.17

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

安装

npm install @fim3/super-request -S

更新

1.0.17版本

  • 增加自定义拦截器customResponse
  • 增加接口T2 T3自动转化

基本使用

import Vue from 'Vue'

import {
  SuperRequest,
  Cache
} from '@fim3/superRequest'

// 无参数初始化
Vue.protoType.$http = SuperRequest
// OR
// 携带参数初始化
Vue.protoType.$http = SuperRequest.create({
  baseUrl: '/g/fim3/xxx',
  debug: true,
  ...
})

API

参数类型默认值说明
cachebooleanfalse是否开启缓存
forceUpdatebooleanfalse是否强制更新缓存
cancelTolenPromise/取消请求
dictionarystringdictionary是否启动字典缓存
camelCasebooleantrue是否启动驼峰处理
encodeParamsbooleanfalse是否开启请求参数转义
targetEncodeArray/是否指定特定的值进行转义
customResponseFunctionnull自定义拦截器
custom401ResponseFunctionnull401拦截器

缓存

// 主动使用缓存
$http.get('/users', { cache: true }); 
// 自定义缓存
import { Cache } from 'super-request';
const cache1 = new Cache()
$http.get('/users', { cache: cache1 });
// 强制更新缓存
$http.get('/users', { cache: cache1, forceUpdate: true });


// post字典也可以进行缓存
$http.post('/name',{
  template_id: 1,
  dictionary: 9902
},{
  cache: true
})
.then(res=> {
  console.log('res',res);
})
// 是否启用驼峰处理
$http.post('/name',{
  template_id: 1,
  dictionary: 9902
},{
  camelCase: false
})
.then(res=> {
  // 返回的数据会保留a_b的格式
  console.log('res',res);
})

// 开启转义
$http.post('/name',{
  template_id: 1,
  dictionary: 9902
},{
  encodeParams: true
})
.then(res=> {
  // script和sql注入会被转义
  console.log('res',res);
})
// 开启转义后会有sql注入和script注入自动转义
// sql注入正则
private regSql = /select|update|delete|truncate|join|union|exec|insert|drop|count|'|"|=|;|>|</i
// 非法<script></script>标签
private regScript = /<script[^>]*>[\s\S]*?<\/[^>]*script>/gi

// e.g以下输入开启 encodeParams: true后会被自动转义
// <script><script>
// select * from
// insert
// 关键词命中后会自动 encode

// 开启特定值转义
$http.post('/name',{
  dictionary: "!@#$%^&*",
  val: {
    a: '<script>',
    b: 'query'
  }
},{
  encodeParams: true,
  targetEncode: ['dictionary', 'a']
})
.then(res=> {
  // dictionary和 a 会被转义
  console.log('res',res);
})
// 自定义拦截器用法
// customResponse
// 携带参数初始化
Vue.protoType.$http = SuperRequest.create({
  baseUrl: '/g/fim3/xxx',
  debug: true,
  customResponse: (response)=> {
    console.log(response)
  }
})
1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.4

2 years ago

1.0.0

3 years ago