Skip to content

Commit 0e54bdc

Browse files
authored
Merge pull request #5 from Habu-Kagumba/add-request-context
Refactor initialisation logic for Zabo SDK
2 parents 3cd2b0a + b489f93 commit 0e54bdc

File tree

2 files changed

+61
-56
lines changed

2 files changed

+61
-56
lines changed

bin/www

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ server.listen(port);
3030
server.on('error', onError);
3131
server.on('listening', onListening);
3232
(async () => {
33-
await open('http://localhost:3000');
33+
await open(`http://localhost:${port}`);
3434
})();
3535

3636
/**

routes/index.js

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,77 @@
11
const express = require('express')
22
const router = express.Router()
3-
43
// Import Zabo SDK
54
const Zabo = require('zabo-sdk-js')
65

7-
// Initializing the JS SDK
8-
// Docs: https://zabo.com/docs/#initializing-the-js-sdks
9-
Zabo.init({
10-
apiKey: process.env.PUBLIC_API_KEY,
11-
secretKey: process.env.SECRET_API_KEY,
12-
env: 'sandbox'
13-
})
14-
.then(zabo => {
15-
let myUser = null
16-
17-
router.post('/accounts', async (req, res) => {
18-
const account = req.body
6+
router.post('/accounts', async (req, res) => {
7+
let myUser
8+
const zabo = req.app.locals.zabo
9+
const account = req.body
1910

11+
try {
12+
if (!myUser) {
13+
// Create a User
14+
// Docs: https://zabo.com/docs/#create-a-user
15+
myUser = await zabo.users.create(account)
16+
} else {
17+
// Add Account
18+
// Docs: https://zabo.com/docs/#add-account-to-existing-user
2019
try {
21-
if (!myUser) {
22-
// Create a User
23-
// Docs: https://zabo.com/docs/#create-a-user
24-
myUser = await zabo.users.create(account)
25-
} else {
26-
// Add Account
27-
// Docs: https://zabo.com/docs/#add-account-to-existing-user
28-
try {
29-
await zabo.users.addAccount(myUser, account)
30-
myUser.accounts.push(account)
31-
} catch (e) {
32-
if (!e.message.includes("already belongs")) {
33-
console.error(e)
34-
}
35-
// Else do nothing because the account already belongs to the user.
36-
}
20+
await zabo.users.addAccount(myUser, account)
21+
myUser.accounts.push(account)
22+
} catch (e) {
23+
if (!e.message.includes("already belongs")) {
24+
console.error(e)
3725
}
26+
// Else do nothing because the account already belongs to the user.
27+
}
28+
}
3829

39-
// Get Transactions
40-
// Docs: https://zabo.com/docs/#get-transaction-history
41-
const transactions = await zabo.transactions.getList({
42-
userId: myUser.id,
43-
accountId: account.id
44-
})
30+
// Get Transactions
31+
// Docs: https://zabo.com/docs/#get-transaction-history
32+
const transactions = await zabo.transactions.getList({
33+
userId: myUser.id,
34+
accountId: account.id
35+
})
4536

46-
// Get Balances
47-
// Docs: https://zabo.com/docs/#get-a-specific-balance
48-
const balances = await zabo.users.getBalances({
49-
userId: myUser.id,
50-
accountId: account.id,
51-
currencies: ['ALL']
52-
})
37+
// Get Balances
38+
// Docs: https://zabo.com/docs/#get-a-specific-balance
39+
const balances = await zabo.users.getBalances({
40+
userId: myUser.id,
41+
accountId: account.id,
42+
currencies: ['ALL']
43+
})
5344

54-
res.send({
55-
accounts: myUser.accounts,
56-
balances: balances.data,
57-
transactions: transactions
58-
})
59-
} catch (err) {
60-
res.status(500).send({ message: err.message })
61-
}
45+
res.send({
46+
accounts: myUser.accounts,
47+
balances: balances.data,
48+
transactions: transactions
6249
})
50+
} catch (err) {
51+
res.status(500).send({ message: err.message })
52+
}
53+
})
6354

64-
router.get('/', function (req, res, next) {
65-
res.render('index', {
66-
title: 'Zabo Node.js Quickstart App',
67-
clientId: process.env.CLIENT_ID
55+
router.get('/', async (req, res, next) => {
56+
try {
57+
if (!req.app.locals.zabo) {
58+
// Initializing the JS SDK
59+
// Docs: https://zabo.com/docs/#initializing-the-js-sdks
60+
const zabo = await Zabo.init({
61+
apiKey: process.env.PUBLIC_API_KEY,
62+
secretKey: process.env.SECRET_API_KEY,
63+
env: 'sandbox'
6864
})
65+
req.app.locals.zabo = zabo
66+
}
67+
68+
res.render('index', {
69+
title: 'Zabo Node.js Quickstart App',
70+
clientId: process.env.CLIENT_ID
6971
})
70-
})
72+
} catch (error) {
73+
res.status(500).send({ message: error.message })
74+
}
75+
})
7176

7277
module.exports = router

0 commit comments

Comments
 (0)