-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmisc-override.js
48 lines (44 loc) · 1.43 KB
/
misc-override.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
var compose = require('../')
var qs = require('qs')
// process-wide override, unless extend was called after that
compose.Request.form = (form) => ({options, options: {headers}}) => {
headers['content-type'] = 'application/x-www-form-urlencoded'
// use qs instead of querystring for nested objects
return {options, body: qs.stringify(form)}
}
// process-wide override, unless extend was called after that
compose.Response.parse = () => ({options, res, res: {headers}, body, raw}) => {
var content = Object.keys(headers).find((name) => /content-type/i.test(name))
if (/application\/x-www-form-urlencoded/.test(headers[content])) {
// use qs instead of querystring for nested objects
raw = body
body = qs.parse(body)
}
return {options, res, body, raw}
}
var http = require('http')
;(async () => {
var server = await new Promise((resolve) => {
var server = http.createServer()
server.on('request', (req, res) => {
var body = ''
req.on('data', (chunk) => body += chunk)
req.on('end', () => {
res.writeHead(200, {'content-type': 'application/x-www-form-urlencoded'})
res.end(body)
})
})
server.listen(5000, () => resolve(server))
})
try {
var {body} = await compose.client({
url: 'http://localhost:5000',
form: {a: {b: {c: '(╯°□°)╯︵ ┻━┻'}}},
})
console.log(body)
server.close()
}
catch (err) {
console.error(err)
}
})()