From c76adb89e903397244d47d9a5337f6f45e6a4909 Mon Sep 17 00:00:00 2001 From: Leo Kewitz Date: Thu, 3 Oct 2024 09:44:05 +0200 Subject: [PATCH] refact: rewrite some files in ts --- package-lock.json | 96 +++++++++++++++++++ package.json | 2 + ...ransactions.js => account-transactions.ts} | 31 +++--- src/server/{index.js => index.ts} | 0 src/server/{routes.js => routes.ts} | 3 +- 5 files changed, 119 insertions(+), 13 deletions(-) rename src/server/controllers/{account-transactions.js => account-transactions.ts} (96%) rename src/server/{index.js => index.ts} (100%) rename src/server/{routes.js => routes.ts} (96%) diff --git a/package-lock.json b/package-lock.json index 307f28a8..85aea130 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,6 +37,8 @@ "devDependencies": { "@babel/cli": "^7.23.4", "@graphql-eslint/eslint-plugin": "^3.20.1", + "@types/express": "^5.0.0", + "@types/lodash": "^4.17.9", "@typescript-eslint/eslint-plugin": "^7.10.0", "@typescript-eslint/parser": "^7.10.0", "commitizen": "^4.3.0", @@ -3989,6 +3991,49 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/body-parser": { + "version": "1.19.5", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", + "integrity": "sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/express": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@types/express/-/express-5.0.0.tgz", + "integrity": "sha512-DvZriSMehGHL1ZNLzi6MidnsDhUZM/x2pRdDIKdwbUNqqwHxMlRdkxtn6/EPKyqKpHqTl/4nRZsRNLpZxZRpPQ==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^5.0.0", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-5.0.0.tgz", + "integrity": "sha512-AbXMTZGt40T+KON9/Fdxx0B2WK5hsgxcfXJLr5bFpZ7b4JCex2WyQPTEKdXqfHiY5nKKBScZ7yCoO6Pvgxfvnw==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, "node_modules/@types/graceful-fs": { "version": "4.1.9", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.9.tgz", @@ -3998,6 +4043,12 @@ "@types/node": "*" } }, + "node_modules/@types/http-errors": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz", + "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", + "dev": true + }, "node_modules/@types/istanbul-lib-coverage": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz", @@ -4028,6 +4079,18 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/lodash": { + "version": "4.17.9", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.9.tgz", + "integrity": "sha512-w9iWudx1XWOHW5lQRS9iKpK/XuRhnN+0T7HvdCCd802FYkT1AMTnxndJHGrNJwRoRHkslGr4S29tjm1cT7x/7w==", + "dev": true + }, + "node_modules/@types/mime": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", + "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", + "dev": true + }, "node_modules/@types/node": { "version": "18.18.12", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.18.12.tgz", @@ -4043,6 +4106,39 @@ "integrity": "sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==", "dev": true }, + "node_modules/@types/qs": { + "version": "6.9.16", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.16.tgz", + "integrity": "sha512-7i+zxXdPD0T4cKDuxCUXJ4wHcsJLwENa6Z3dCu8cfCK743OGy5Nu1RmAGqDPsoTDINVEcdXKRvR/zre+P2Ku1A==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true + }, + "node_modules/@types/send": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.4.tgz", + "integrity": "sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "1.15.7", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.7.tgz", + "integrity": "sha512-W8Ym+h8nhuRwaKPaDw34QUkwsGi6Rc4yYqvKFo5rm2FUEhCFbzVWrxXUxuKK8TASjWsysJY0nsmNCGhCOIsrOw==", + "dev": true, + "dependencies": { + "@types/http-errors": "*", + "@types/node": "*", + "@types/send": "*" + } + }, "node_modules/@types/stack-utils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz", diff --git a/package.json b/package.json index 23660afb..867f6830 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,8 @@ "devDependencies": { "@babel/cli": "^7.23.4", "@graphql-eslint/eslint-plugin": "^3.20.1", + "@types/express": "^5.0.0", + "@types/lodash": "^4.17.9", "@typescript-eslint/eslint-plugin": "^7.10.0", "@typescript-eslint/parser": "^7.10.0", "commitizen": "^4.3.0", diff --git a/src/server/controllers/account-transactions.js b/src/server/controllers/account-transactions.ts similarity index 96% rename from src/server/controllers/account-transactions.js rename to src/server/controllers/account-transactions.ts index 9636e752..6de5528e 100644 --- a/src/server/controllers/account-transactions.js +++ b/src/server/controllers/account-transactions.ts @@ -1,4 +1,5 @@ import { Parser } from '@json2csv/plainjs'; +import type { RequestHandler } from 'express'; import gqlV2 from 'graphql-tag'; import { difference, get, head, intersection, isNil, pick, toUpper, trim } from 'lodash'; import moment from 'moment'; @@ -454,16 +455,21 @@ const applyMapping = (mapping, row) => { return res; }; -/** - * @param {import('express').Request} req - * @param {import('express').Response} res - */ -const accountTransactions = async (req, res) => { +type Params = { + slug: string; + reportType: 'hostTransactions' | 'transactions'; + type?: 'credit' | 'debit'; + kind?: string; + format: 'json' | 'csv' | 'txt'; +}; + +const accountTransactions: RequestHandler = async (req, res) => { if (!['HEAD', 'GET'].includes(req.method)) { - return res.status(405).send({ error: { message: 'Method not allowed' } }); + res.status(405).send({ error: { message: 'Method not allowed' } }); + return; } - const variables = pick({ ...req.params, ...req.query }, [ + const variables: any = pick({ ...req.params, ...req.query }, [ 'account', 'accountingCategory', 'dateFrom', @@ -627,18 +633,18 @@ const accountTransactions = async (req, res) => { variables.fullDescription = req.params.reportType === 'hostTransactions' ? true : false; } - let fields = get(req.query, 'fields', '') + let fields = (get(req.query, 'fields', '') as string) .split(',') .map(trim) .filter((v) => !!v); if (fields.length === 0) { - const remove = get(req.query, 'remove', '') + const remove = (get(req.query, 'remove', '') as string) .split(',') .map(trim) .filter((v) => !!v); - const add = get(req.query, 'add', '') + const add = (get(req.query, 'add', '') as string) .split(',') .map(trim) .filter((v) => !!v); @@ -714,7 +720,8 @@ const accountTransactions = async (req, res) => { res.append('Access-Control-Expose-Headers', 'X-Exported-Rows'); res.append('X-Exported-Rows', result.transactions.totalCount); if (req.method === 'HEAD') { - return res.status(200).end(); + res.status(200).end(); + return; } if (result.transactions.totalCount === 0) { @@ -725,7 +732,7 @@ const accountTransactions = async (req, res) => { const mapping = pick(csvMapping, fields); const mappedTransactions = result.transactions.nodes.map((t) => applyMapping(mapping, t)); - res.write(json2csv(mappedTransactions)); + res.write(json2csv(mappedTransactions, {})); res.write(`\n`); if (result.transactions.totalCount > result.transactions.limit) { diff --git a/src/server/index.js b/src/server/index.ts similarity index 100% rename from src/server/index.js rename to src/server/index.ts diff --git a/src/server/routes.js b/src/server/routes.ts similarity index 96% rename from src/server/routes.js rename to src/server/routes.ts index 2b69ca06..fccf0702 100644 --- a/src/server/routes.js +++ b/src/server/routes.ts @@ -1,4 +1,5 @@ import cors from 'cors'; +import type { Express } from 'express'; import { idOrUuid } from './lib/utils'; import controllers from './controllers'; @@ -8,7 +9,7 @@ const requireApiKey = (req, res, next) => { next(); }; -export const loadRoutes = (app) => { +export const loadRoutes = (app: Express) => { app.use(cors()); app.get('/', (req, res) => {