-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.module.js
64 lines (60 loc) · 1.67 KB
/
http.module.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: hands;
const _info = {
name: 'http.module',
version: '1.1',
updated_at: '2023-12-04 17:45:00',
author: 'raoyc',
description: 'a module for http request',
repo_file_url: 'https://github.com/ycrao/scripts-for-scriptable/blob/main/app/http.module.js',
raw_file_url: 'https://raw.githubusercontent.com/ycrao/scripts-for-scriptable/main/app/http.module.js'
}
const defaultHeaders = {
"Accept": "*/*",
"Content-Type": "application/json"
}
function req(method, url, params, headers) {
let request = new Request("")
request.allowInsecureRequest = true
method = method.toLowerCase()
request.method = method
request.headers = {
...defaultHeaders,
...headers,
}
request.url = url
if (method === 'get') {
let qStr = qs(params)
request.url = url.endsWith('?') ? url + qStr : url + '?' + qStr
} else if (['post', 'put', 'patch'].includes(method) && typeof params === 'object') {
request.body = JSON.stringify(params)
}
return request
}
function qs(params) {
let qs = ''
if (Object.prototype.toString.call(params) === '[object Object]') {
qs = Object.keys(params)
.map(key => `${key}=${encodeURIComponent(params[key])}`)
.join('&')
}
return qs
}
function postForm(url, formParam) {
const request = new Request("")
request.allowInsecureRequest = true
request.method = "post"
request.headers = {
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded"
}
request.url = url
request.body = qs(formParam)
return request
}
module.exports = {
req,
qs,
postForm,
}