forked from ChanWahFung/nuxt-juejin-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (45 loc) · 1.08 KB
/
Copy pathindex.js
File metadata and controls
46 lines (45 loc) · 1.08 KB
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
45
46
const request = require('request')
const util = require('util')
const qs = require('querystring')
const requestPromise = util.promisify(request)
module.exports = function (options){
options.method = options.method.toUpperCase()
if(options.method === 'GET'){
if(options.params){
let params = qs.stringify(options.params)
options.url = `${options.url}?${params}`
}
}
if(
options.method === 'POST' ||
options.method === 'PUT' ||
options.method === 'DELETE'
){
if(options.body){
options.body = JSON.stringify(options.body)
}
if(!options.headers){
options.headers = {}
}
options.headers = {
'Content-Type': 'application/json',
...options.headers
}
}
return requestPromise(options)
.then(res=> ({
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers,
body: res.body
}))
.catch(err=> {
console.error(err)
return {
statusCode: -1,
statusMessage: err,
headers: res.headers,
body: { s: 0 },
}
})
}