|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2018 Includable. |
|
3
|
|
|
* Created by Thomas Schoffelen. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
const axios = require('axios') |
|
7
|
|
|
const netrc = require('netrc') |
|
8
|
|
|
const FormData = require('form-data') |
|
9
|
|
|
const queryString = require('query-string') |
|
10
|
|
|
const pkg = require('../../package.json') |
|
11
|
|
|
const output = require('../output') |
|
12
|
|
|
|
|
13
|
|
|
const API = axios.create({ |
|
14
|
|
|
maxRedirects: 0, |
|
15
|
|
|
baseURL: 'https://dashboard.includable.com/dashboard/v1', |
|
16
|
|
|
headers: { |
|
17
|
|
|
'Accept': 'application/json', |
|
18
|
|
|
'User-Agent': 'IncludableCLI ' + pkg.name + '/' + pkg.version, |
|
19
|
|
|
'X-Client': pkg.name + '/' + pkg.version, |
|
20
|
|
|
'X-Client-Version': pkg.version, |
|
21
|
|
|
'X-API-Version': global.apiVersion, |
|
22
|
|
|
'X-API-Stability': global.apiStability |
|
23
|
|
|
}, |
|
24
|
|
|
transformRequest: [function (data, headers) { |
|
25
|
|
|
var token = API.getToken() |
|
26
|
|
|
if (token) { |
|
27
|
|
|
headers['Authorization'] = 'Bearer ' + token |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (typeof data === 'object' && !(data instanceof FormData)) { |
|
31
|
|
|
data = queryString.stringify(data) |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return data |
|
35
|
|
|
}] |
|
36
|
|
|
}) |
|
37
|
|
|
|
|
38
|
|
|
API.getToken = function () { |
|
39
|
|
|
if ('INCLUDABLE_TOKEN' in process.env && process.env.INCLUDABLE_TOKEN) { |
|
40
|
|
|
return process.env.INCLUDABLE_TOKEN |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
let nrc = netrc(null) |
|
44
|
|
|
if (nrc && 'dashboard.includable.com' in nrc && 'password' in nrc['dashboard.includable.com']) { |
|
45
|
|
|
return nrc['dashboard.includable.com'].password |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return null |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
API.checkSignedIn = function () { |
|
52
|
|
|
const token = API.getToken() |
|
53
|
|
|
if (!token) { |
|
54
|
|
|
output.errSimple('You are not signed in. First run `inc login` before running this command.') |
|
55
|
|
|
process.exit() |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
API.setToken = function (token) { |
|
60
|
|
|
let nrc = netrc(null) |
|
61
|
|
|
if (!nrc) { |
|
62
|
|
|
throw new Error('Could not load netrc file.') |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
nrc['dashboard.includable.com'] = { |
|
66
|
|
|
login: 'x-oauth', |
|
67
|
|
|
password: token |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
netrc.save(nrc) |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
module.exports = API |
|
74
|
|
|
|