-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
44 lines (38 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* global uni */
'use strict'
const isUploadFile = require('./helper/isUploadFile')
const format = require('./helper/format')
/**
* 参数配置参考:
* > axios https://www.npmjs.com/package/axios#request-config
* > uniapp request https://uniapp.dcloud.io/api/request/request
* > uniapp upload https://uniapp.dcloud.io/api/request/network-file
* @param {object} config
*/
function uniappAdapter(config = {}) {
return new Promise(function dispatchUniApp(resolve, reject) {
const uniConfig = format(config, resolve, reject)
let requestTask = null
if (config.cancelToken) {
// Handle cancellation
config.cancelToken.promise.then(function onCanceled(cancel) {
if (!requestTask) {
return
}
requestTask.abort()
reject(cancel)
// Clean up request
requestTask = null
})
}
// Send the request
if (isUploadFile(config)) {
requestTask = uni.uploadFile(uniConfig)
} else {
requestTask = uni.request(uniConfig)
}
})
}
module.exports = uniappAdapter
// Allow use of default import syntax in TypeScript
module.exports.default = uniappAdapter