-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·221 lines (179 loc) · 6.51 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import os from 'os'
import url from 'url'
import fetch from 'node-fetch'
import chalk from 'chalk'
import crypto from 'crypto'
import { program, Option } from 'commander'
const log = console.log
const baseUrl = 'https://api.transferwise.com'
const config = {}
const dirname = path.dirname(url.fileURLToPath(import.meta.url))
const app = JSON.parse(fs.readFileSync(path.resolve(dirname, 'package.json'), 'utf-8'))
const appPath = path.resolve(os.homedir(), '.config', app.name)
const configPath = path.resolve(appPath, '.config.json')
let privateKeyPath = ''
function do2FA(token) {
const key = fs.readFileSync(privateKeyPath)
const signature = crypto.sign("sha256", Buffer.from(token), {
key,
});
return signature.toString('base64')
}
async function get(path, returnStream, headers) {
try {
const response = await fetch(`${baseUrl}${path}`, {
headers: {
'Authorization': `Bearer ${config['api-token']}`,
...headers,
}
})
if (!headers && response.status === 403 && response.headers.has('x-2fa-approval')) {
log(chalk.gray('Signing OTT...'))
const token = response.headers.get('x-2fa-approval')
return await get(path, returnStream, {
'x-2fa-approval': token,
'X-Signature': await do2FA(token),
})
}
if (!response.ok) {
const body = await response.text()
throw new Error(`Wise API returned ${response.status}: ${response.statusText}\n${body}`)
}
if (returnStream) {
return response.body
}
return await response.json()
} catch (error) {
if (!headers) {
log(chalk.red(`Error while trying to fetch ${baseUrl}${path}\n${error}`))
}
throw error
}
}
function main() {
program
.name(app.name)
.version(app.version)
const exportCommand = program.command('export')
exportCommand
.description('Exports to a file a complete month of statements')
.requiredOption('-k --key <file>', 'Private key file path')
.option('-m, --month <number>', 'Month to be exported', String(new Date().getMonth()))
.requiredOption('-o, --output <path>', 'Path to save exported file')
.addOption(new Option('-t, --type <type>', 'Type of output').default('csv').choices(['csv', 'pdf']))
.option('-c, --config <path>', 'Custom path for configuration file', configPath)
.action(exportFile)
const configCommand = program.command('config <name> [value]')
configCommand
.description('Read or set a configuration')
.action(configAction)
program.parse()
}
const exportFile = async (options) => {
try {
log(chalk.underline(`${app.name} v${app.version}`))
log('')
validateConfig(options.config)
const numberFormatter = new Intl.NumberFormat(config.locale)
const dateFormatter = new Intl.DateTimeFormat(config.locale)
const { month, type, key, output } = options
const selectedMonth = parseInt(month)
if (selectedMonth === NaN) {
throw new Error('Invalid month')
}
privateKeyPath = path.resolve(key)
const start = new Date()
start.setMonth(selectedMonth - 1)
start.setUTCDate(1)
start.setUTCHours(0, 0, 0, 0)
const end = new Date(start)
end.setUTCMonth(end.getUTCMonth() + 1)
end.setUTCDate(0)
end.setUTCHours(23, 59, 59, 999)
log(chalk.gray(`From ${start.toUTCString()} to ${end.toUTCString()}`))
log('')
log(chalk.green('Loading profiles...'))
const profiles = await get('/v2/profiles')
const profile = profiles.find(p => p.fullName === config.profile)
log(chalk.green('Loading balances...'))
const balances = await get(`/v3/profiles/${profile.id}/balances?types=STANDARD`)
const balance = balances.find(b => b.currency === config.currency)
const parsedPath = path.parse(output)
if (type === 'csv') {
log(chalk.green('Loading statments...'))
const statments = await get(`/v1/profiles/${profile.id}/balance-statements/${balance.id}/statement.json?intervalStart=${start.toISOString()}&intervalEnd=${end.toISOString()}&type=FLAT`)
const transactions = statments.transactions.map(t => [t.date, t.details.description, t.amount.value])
log(chalk.green(`Writing "${parsedPath.base}" file into "${parsedPath.dir}"...`))
const stream = fs.createWriteStream(path.resolve(output))
stream.once('open', () => {
stream.write('DATA;;\n')
for (const transaction of transactions) {
let signal = ''
const line = transaction.map((t, i) => {
if (typeof t === 'number') {
signal = t < 0 ? '"D"' : '"C"'
return numberFormatter.format(t)
}
if (i === 0) {
return dateFormatter.format(new Date(t))
}
return `"${t}"`;
})
line.push(signal)
stream.write(`${line.join(';')}\n`)
}
stream.end()
})
}
if (type === 'pdf') {
log(chalk.green(`Downloading "${parsedPath.base}" file into "${parsedPath.dir}"...`))
const pdf = await get(`/v1/profiles/${profile.id}/balance-statements/${balance.id}/statement.pdf?intervalStart=${start.toISOString()}&intervalEnd=${end.toISOString()}&type=FLAT&statementLocale=${config['pdf-locale']}`, true)
const stream = fs.createWriteStream(path.resolve(output))
await new Promise((resolve, reject) => {
pdf.pipe(stream);
pdf.on('error', reject);
stream.on('finish', resolve);
})
}
log(chalk.bold.green('Done, enjoy your saved time!'))
} catch (error) {
log('')
if (error instanceof Error) {
log(`${error.name}: ${error.message}`)
log(error.stack)
} else {
log(error)
}
log(chalk.red('Program exited due to error. 😢'))
process.exitCode = 1
}
}
function validateConfig(configPath) {
const file = fs.readFileSync(path.resolve(configPath), 'utf-8')
const parsed = JSON.parse(file)
const keys = ['api-token', 'profile', 'locale', 'pdf-locale', 'currency']
for (const key of keys) {
if (!parsed[key]) {
throw new Error(`${key} is missing on configuration`)
}
}
Object.assign(config, parsed)
}
function configAction(name, value) {
fs.mkdirSync(appPath, { recursive: true })
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, '{}', 'utf-8')
}
const file = fs.readFileSync(configPath, 'utf-8')
const parsed = JSON.parse(file)
if (value) {
parsed[name] = value
fs.writeFileSync(configPath, JSON.stringify(parsed, null, 2), 'utf-8')
} else {
log(parsed[name])
}
}
main()