Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zhdd-request

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zhdd-request - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

7

index.js

@@ -1,6 +0,1 @@

import { createRequest, configDefaultConfig} from './lib/zhdd-request'
export {
createRequest,
configDefaultConfig
}
export * from './lib/zhdd-request'

198

lib/zhdd-request.js

@@ -5,32 +5,2 @@ import axios from 'axios'

/**
* 使用方法
* 1.初始配置
* configDefaultConfig(config,servicesUrl)
* config 初始配置,可设置baseUrl,timeout,headers
* servicesUrl 多个域名配置,可不传 不传时config.baseURL 为必传
* 2.设置当前请求超时 时间
* createRequest(null,timeout)
* 3.使用parameters 传参
* createRequest().setParameters({}).get('url')
* 4.使用add方法添加参数
* createRequest().add('key','value').get('url')
* 5.配置域名
* createRequest('SERVER_NAME').setParameters({}).get('url')
* 6.POST请求 默认 application/json
* createRequest().setParameters({}).post("url")
* 7.POST请求 请求格式为 multipart/form-data 传参一定要是个对象,不能是formData类型
* createRequest().setParameters({},'formdata').post("url")
* 8.POST请求 请求格式为 application/x-www-form-urlencoded
* createRequest().setParameters({},'urlencoded').post("url")
*/
/**
* axios请求框架封装

@@ -44,2 +14,3 @@ *

let servicesConfig = {}
let successFunction = null

@@ -51,3 +22,3 @@ /**

* */
function configDefaultConfig (config = {}, servicesUrl = []) {
const configDefaultConfig = (config = {}, servicesUrl = []) => {
// 多域名配置

@@ -66,11 +37,22 @@ if (servicesUrl && servicesUrl.length > 0) {

servicesConfig = config
Object.assign(servicesConfig, config)
}
/**
* 拦截器
* @param {Function} fun 回调函数
* @param {String} type 回调类型 默认为 SUCCESS
*/
const requestCallback = (fun, type = 'SUCCESS') => {
if (type === 'SUCCESS' && typeof fun === 'function') {
successFunction = fun
}
}
class Request {
constructor (servicesName = null, timeout) {
constructor(servicesName = null, timeout) {
// create axios
this.instance = axios.create({
baseURL: servicesName ? servicesBaseUrl[servicesName].servicesUrl : baseurl,
timeout: timeout ? servicesConfig.timeout : timeout,
timeout: timeout ? timeout : servicesConfig.timeout,
headers: servicesConfig.headers ? servicesConfig.headers : {}

@@ -85,5 +67,22 @@ })

this.paramData = {}
// axios 传参
this.axiosConfig = null
}
/**
* 直接用axios方法调用
* setAxios 调用后 setParameters 无效
* @param {Object} config axios传参
* {
* url: '',
* method: 'get',
* params | data
* }
*/
setAxios(config) {
return this.instanceRequest(config)
}
/**
* 设置请求参数对象

@@ -94,3 +93,3 @@ *

* */
setParameters (paramData, type = 'json') {
setParameters(paramData, type = 'json') {
this.paramData = paramData

@@ -106,4 +105,5 @@ this.type = type

* */
add (key, value) {
add(key, value) {
this.paramData[key] = value
return this
}

@@ -116,61 +116,67 @@

* @param method 方法名
* @return Promise
* @return 开始请求 Promise
* */
request (url, method) {
return new Promise((resolve, reject) => {
const data = this.type === 'formdata' ? new FormData() : {}
// config 配置
this.config.url = url
this.config.method = method
this.config.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
request(url, method) {
const data = this.type === 'formdata' ? new FormData() : {}
// config 配置
this.config.url = url
this.config.method = method
this.config.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
// 拼接请求参数
this.paramData && Object.keys(this.paramData).forEach(key => {
const reger = new RegExp('{' + key + '}')
if (reger.test(url)) {
this.config.url = url.replace(reger, this.paramData[key])
// 拼接请求参数
this.paramData && Object.keys(this.paramData).forEach(key => {
const reger = new RegExp('{' + key + '}')
if (reger.test(url)) {
this.config.url = url.replace(reger, this.paramData[key])
} else {
if (this.type === 'formdata') {
data.append(key, this.paramData[key])
} else {
if (this.type === 'formdata') {
data.append(key, this.paramData[key])
} else {
data[key] = this.paramData[key]
}
data[key] = this.paramData[key]
}
})
// params是添加到url的请求字符串中的
// data是添加到请求体(body)中的
if (method === 'GET') {
this.config.params = data
} else {
// post的三种 Content-Type
switch (this.type) {
case 'json':
this.config.data = data
this.config.headers['Content-Type'] = 'application/json'
break
case 'urlencoded':
this.config.data = qs.stringify(data)
this.config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
break
case 'formdata':
this.config.data = data
this.config.headers['Content-Type'] = 'multipart/form-data'
break
default:
break
}
}
})
// 默认返回json类型的数据格式
if (!this.config.dataType) {
this.config.dataType = 'json'
// params是添加到url的请求字符串中的
// data是添加到请求体(body)中的
if (method === 'GET') {
this.config.params = data
} else {
// post的三种 Content-Type
switch (this.type) {
case 'json':
this.config.data = data
this.config.headers['Content-Type'] = 'application/json'
break
case 'urlencoded':
this.config.data = qs.stringify(data)
this.config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
break
case 'formdata':
this.config.data = data
this.config.headers['Content-Type'] = 'multipart/form-data'
break
default:
break
}
}
// 开始请求
this.instance(this.config).then((response) => {
// 默认返回json类型的数据格式
if (!this.config.dataType) {
this.config.dataType = 'json'
}
return this.instanceRequest(this.config)
}
/**
* 开始请求
* @param {Object} config 请求参数
* @returns Promise
*/
instanceRequest(config) {
return new Promise((resolve, reject) => {
this.instance(config).then((response) => {
if (response.status === 200) {
// console.log('HTTP请求成功,请求地址:' + this.config.url + ',返回信息:', response.statusText)
successFunction && successFunction(response.data)
resolve(response.data)

@@ -194,3 +200,3 @@ } else {

* */
post (url) {
post(url) {
return this.request(url, 'POST')

@@ -205,3 +211,3 @@ }

* */
get (url) {
get(url) {
return this.request(url, 'GET')

@@ -216,3 +222,3 @@ }

* */
put (url) {
put(url) {
return this.request(url, 'PUT')

@@ -227,3 +233,3 @@ }

* */
delete (url) {
delete(url) {
return this.request(url, 'DELETE')

@@ -238,3 +244,3 @@ }

*/
download (url, openUrl = true) {
download(url, openUrl = true) {
return new Promise((resolve, reject) => {

@@ -257,4 +263,7 @@ try {

* 创建请求对象
* */
function createRequest (servicesName, timeout) {
* @param {String} servicesName 域名类型
* @param {Number} timeout 超时时间
* @returns
*/
const createRequest = (servicesName, timeout) => {
return new Request(servicesName, timeout)

@@ -265,3 +274,4 @@ }

createRequest,
configDefaultConfig
configDefaultConfig,
requestCallback
}
{
"name": "zhdd-request",
"version": "1.0.1",
"version": "1.0.2",
"private": false,

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,23 +0,26 @@

> # axios 请求封装
# axios 请求封装
> 研发人:朱琦
> 研发日期: 2022/1/10
> 示例框架: Vue 2.0
# 简介
zhdd-request 插件是基于 axios,qs 开发的请求封装,可以支持多域名请求
# 使用方式
## 安装
## 简介
zhdd-request 插件是基于 [axios](http://www.axios-js.com/),[qs](https://www.npmjs.com/package/qs) 开发的请求封装,可以支持多域名请求
## 使用方式
### 安装
```
npm i zhdd-request
```
## 引入
### 引入
* 1.初始化配置 vue main.js为例
```
import { configDefaultConfig } from 'zhdd-request'
import { configDefaultConfig, requestCallback } from 'zhdd-request'
/**
* 初始化axios
* */
* 初始化 配置
* @param {Object} config 初始配置
* @param {Object} servicesUrl 多个域名配置
* */
configDefaultConfig(

@@ -44,3 +47,11 @@ // 初始配置 可设置baseUrl timeout header

)
/**
* 拦截器
* @param {Function} fun 回调函数
* @param {String} type 回调类型 默认为 SUCCESS 即请求成功时
*/
requestCallback(fun, type)
```
> 若后续需要设置headers 可再次调用configDefaultConfig(config)

@@ -80,5 +91,22 @@ * 2.实际应用(以vue为例)

```
/**
* 实例1
* */
getList (params) {
return createRequest().setParameters(params).get('/event/listForJtqg')
}
/**
* 实例2
* 用setAxios请求
* 传参方法遵循 axios
* */
getList (params) {
return createRequest().setAxios({
url: '/event/listForJtqg',
method: 'get',
params
})
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc