-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoauth.js
47 lines (36 loc) · 960 Bytes
/
oauth.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
var _oauth = require('./lib/oauth')
var url = require('url')
module.exports = (oauth) => ({options, options: {headers}, body}) => {
var header = Object.keys(options.headers)
.find((name) => name.toLowerCase() === 'content-type')
var form
if (/^application\/x-www-form-urlencoded/.test(headers[header])) {
form = body
}
var uri = url.parse(
`${options.protocol}//${options.hostname}${options.path}`
+ (options.port ? `:${options.port}` : '')
)
var result = _oauth({
method: options.method,
url: uri,
query: uri.query,
form,
body,
oauth,
})
if (result instanceof Error) {
throw result
}
var transport = oauth.transport_method || 'header'
if (transport === 'header') {
options.headers.Authorization = result
}
else if (transport === 'query') {
options.path += result
}
else if (transport === 'body') {
body = (body || '') + result
}
return {options, body}
}