Skip to content

Commit

Permalink
refactor: introduce error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
acifani committed May 8, 2024
1 parent 9134a39 commit 305a452
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 50 deletions.
9 changes: 0 additions & 9 deletions actual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ export async function getAllTransactions(offbudget?: boolean) {
return transactions;
}

export async function getLatestBudget() {
const budgetMonths = await api.getBudgetMonths();
if (!budgetMonths?.length) {
return null;
}

return api.getBudgetMonth(budgetMonths.at(-1));
}

export async function getBudgetAtMonth(month: string) {
return api.getBudgetMonth(month);
}
58 changes: 17 additions & 41 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,32 @@ const app = express();

app.use(authMiddleware);

app.get('/budget/:month', async (req, res) => {
const budget = await actual.getBudgetAtMonth(req.params.month);
res.status(200).json(budget);
});

app.get('/accounts', async (_, res) => {
try {
const accounts = await actual.getAccounts();
return res.status(200).json(accounts);
} catch (e) {
console.error(e);
return res.status(500).send();
}
const accounts = await actual.getAccounts();
res.status(200).json(accounts);
});

app.get('/accounts/:accountid/transactions', async (req, res) => {
try {
const transactions = await actual.getTransactions(req.params.accountid);
return res.status(200).json(transactions);
} catch (e) {
console.error(e);
return res.status(500).send();
}
const transactions = await actual.getTransactions(req.params.accountid);
res.status(200).json(transactions);
});

app.get('/transactions', async (req, res) => {
try {
const offbudget = req.query['offbudget'] === 'true';
const transactions = await actual.getAllTransactions(offbudget);
return res.status(200).json(transactions);
} catch (e) {
console.error(e);
return res.status(500).send();
}
const offbudget = req.query['offbudget'] === 'true';
const transactions = await actual.getAllTransactions(offbudget);
res.status(200).json(transactions);
});

app.get('/budget', async (_, res) => {
try {
const budget = actual.getLatestBudget();
return res.status(200).json(budget);
} catch (e) {
console.error(e);
return res.status(500).send();
}
});

app.get('/budget/:month', async (req, res) => {
try {
const budget = await actual.getBudgetAtMonth(req.params.month);
return res.status(200).json(budget);
} catch (e) {
console.error(e);
return res.status(500).send();
}
});
const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
console.error(err);
res.sendStatus(500);
};
app.use(errorHandler);

// Server start
const port = process.env.PORT || 3000;
Expand Down

0 comments on commit 305a452

Please sign in to comment.