-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (46 loc) · 1.47 KB
/
index.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
require('reflect-metadata')
const express = require('express')
const bodyParser = require('body-parser')
const nubank = require('nubank-client')
const app = express()
app.use(bodyParser.json())
// ! Note: in a real scenario, nubank instance and its resources must be session scoped
const nubankInstance = nubank.createInstance()
app.get('/auth/request', async (req, res) => {
const qrCode = await nubankInstance.generateQRCode()
res.contentType('text/html').send(`<img src="${qrCode}" />`)
})
app.post('/auth', async (req, res) => {
const { login, password } = req.body
nubankInstance.setCredentials({ login, password })
try {
await nubankInstance.authenticate(5)
res.send()
} catch (err) {
res.status(401).json({ message: 'failed to authenticate, try again' })
}
})
// ! Note: in a real scenario, nubank resources must be session scoped
let bills
const getBillsResource = async () => {
if (!bills) {
bills = await nubankInstance.getBills()
}
return bills
}
app.get('/bills', async(req, res) => {
const bills = await getBillsResource()
const bill = await bills.getByCloseDate(req.query.closeDate);
res.json(bill)
})
app.get('/bills/open', async (req, res) => {
const bills = await getBillsResource()
const openBills = await bills.getOpen()
res.json(openBills)
})
app.get('/bills/future', async (req, res) => {
const bills = await getBillsResource()
const futureBills = await bills.getFuture()
res.json(futureBills)
})
app.listen(8000)