Migrate to CommonJS

This commit is contained in:
Alexander Ungar 2023-03-27 10:06:28 +02:00
parent cd9c4d5464
commit 27648e7d2f
37 changed files with 104 additions and 106 deletions

View file

@ -4,7 +4,6 @@
"description": "A slick application for manging personal finances", "description": "A slick application for manging personal finances",
"license": "MIT", "license": "MIT",
"author": "Alexander Ungar", "author": "Alexander Ungar",
"type": "module",
"main": "dist/src/index.js", "main": "dist/src/index.js",
"scripts": { "scripts": {
"clean": "rimraf dist", "clean": "rimraf dist",
@ -16,11 +15,6 @@
"dev": "nodemon src/server.ts", "dev": "nodemon src/server.ts",
"test": "nodemon --config ./spec/nodemon.json" "test": "nodemon --config ./spec/nodemon.json"
}, },
"nodemonConfig": {
"execMap": {
"ts": "ts-node --esm"
}
},
"dependencies": { "dependencies": {
"body-parser": "^1.20.2", "body-parser": "^1.20.2",
"currency.js": "^2.0.4", "currency.js": "^2.0.4",

View file

@ -1,15 +1,16 @@
import express from "express"; import express from "express";
import helmet from "helmet"; import helmet from "helmet";
import environment from "./config/environment.js"; import environment from "./config/environment";
import expressLogger from "./logging/expressLogger.js"; import expressLogger from "./logging/expressLogger";
import ApiRouter from "./routes/apiRouter.js"; import ApiRouter from "./routes/apiRouter";
import BudgetService from "./usecase/budget/budgetService.js"; import BudgetService from "./usecase/budget/budgetService";
import { errorHandler } from "./middleware/errorHandler.js"; import { errorHandler } from "./middleware/errorHandler";
import BudgetMongoRepository from "./repository/budget/mongo/budgetMongoRepository.js"; import BudgetMongoRepository from "./repository/budget/mongo/budgetMongoRepository";
import BudgetSummaryMongoRepository from "./repository/budget/mongo/budgetSummaryMongoRepository.js"; import BudgetSummaryMongoRepository from "./repository/budget/mongo/budgetSummaryMongoRepository";
import TransactionService from "./usecase/transaction/transactionService.js"; import TransactionService from "./usecase/transaction/transactionService";
import TransactionMongoRepository from "./repository/transaction/mongo/TransactionMongoRepository.js"; import TransactionMongoRepository from "./repository/transaction/mongo/TransactionMongoRepository";
import OpenApiValidator from "express-openapi-validator"; import * as OpenApiValidator from "express-openapi-validator";
import * as path from "path";
const app = express(); const app = express();
@ -23,7 +24,7 @@ if (environment.isProd) {
app.use( app.use(
OpenApiValidator.middleware({ OpenApiValidator.middleware({
apiSpec: new URL(`../api/my-finance-pal.yml`, import.meta.url).pathname, apiSpec: path.join(__dirname, "..", "api", "my-finance-pal.yml"),
validateRequests: true, validateRequests: true,
validateResponses: true, validateResponses: true,
}) })

View file

@ -1,6 +1,6 @@
import mongoose from "mongoose"; import mongoose from "mongoose";
import environment from "./environment.js"; import environment from "./environment";
import logger from "../logging/logger.js"; import logger from "../logging/logger";
import * as util from "util"; import * as util from "util";
export const connect = async (): Promise<void> => { export const connect = async (): Promise<void> => {

View file

@ -1,6 +1,6 @@
import type UUID from "./uuid.js"; import type UUID from "./uuid";
import type Limit from "./limit.js"; import type Limit from "./limit";
import { type Transaction } from "./transaction.js"; import { type Transaction } from "./transaction";
export type BudgetId = UUID; export type BudgetId = UUID;

View file

@ -1,4 +1,4 @@
import { AppError } from "../middleware/errorHandler.js"; import { AppError } from "../middleware/errorHandler";
class Limit { class Limit {
constructor(public amount: number) { constructor(public amount: number) {

View file

@ -1,4 +1,4 @@
import type UUID from "./uuid.js"; import type UUID from "./uuid";
export type TransactionId = UUID; export type TransactionId = UUID;

View file

@ -1,5 +1,5 @@
import { v4 as uuidv4, validate as validateUUID } from "uuid"; import { v4 as uuidv4, validate as validateUUID } from "uuid";
import { AppError } from "../middleware/errorHandler.js"; import { AppError } from "../middleware/errorHandler";
class UUID { class UUID {
public value: string; public value: string;

View file

@ -1,6 +1,6 @@
import expressWinston from "express-winston"; import expressWinston from "express-winston";
import winston from "winston"; import winston from "winston";
import environment from "../config/environment.js"; import environment from "../config/environment";
const expressLogger = expressWinston.logger({ const expressLogger = expressWinston.logger({
level: environment.LOG_LEVEL, level: environment.LOG_LEVEL,

View file

@ -1,5 +1,5 @@
import winston from "winston"; import winston from "winston";
import environment from "../config/environment.js"; import environment from "../config/environment";
const logger = winston.createLogger({ const logger = winston.createLogger({
level: environment.LOG_LEVEL, level: environment.LOG_LEVEL,

View file

@ -1,5 +1,5 @@
import type * as Http from "http"; import type * as Http from "http";
import logger from "../logging/logger.js"; import logger from "../logging/logger";
import * as util from "util"; import * as util from "util";
import { type NextFunction, type Request, type Response } from "express"; import { type NextFunction, type Request, type Response } from "express";

View file

@ -7,6 +7,7 @@
// NOTE: DO NOT IMPORT ANY SOURCE CODE HERE // NOTE: DO NOT IMPORT ANY SOURCE CODE HERE
import dotenv from "dotenv"; import dotenv from "dotenv";
import { parse } from "ts-command-line-args"; import { parse } from "ts-command-line-args";
import path from "path";
// **** Types **** // // **** Types **** //
@ -27,7 +28,7 @@ const args = parse<Args>({
// Set the env file // Set the env file
const dotenvConfig = dotenv.config({ const dotenvConfig = dotenv.config({
path: new URL(`../env/${args.env}.env`, import.meta.url).pathname, path: path.join(__dirname, "..", "env", `${args.env}.env`),
}); });
if (dotenvConfig.error != null) { if (dotenvConfig.error != null) {
throw dotenvConfig.error; throw dotenvConfig.error;

View file

@ -1,4 +1,4 @@
import { type Budget } from "../../domain/budget.js"; import { type Budget } from "../../domain/budget";
interface BudgetRepository { interface BudgetRepository {
findAll: () => Promise<Budget[]>; findAll: () => Promise<Budget[]>;

View file

@ -1,4 +1,4 @@
import { type BudgetId, type BudgetSummary } from "../../domain/budget.js"; import { type BudgetId, type BudgetSummary } from "../../domain/budget";
interface BudgetSummaryRepository { interface BudgetSummaryRepository {
insert: (summary: BudgetSummary) => Promise<BudgetSummary>; insert: (summary: BudgetSummary) => Promise<BudgetSummary>;

View file

@ -1,4 +1,4 @@
import type TransactionEntity from "../../transaction/entity/transactionEntity.js"; import type TransactionEntity from "../../transaction/entity/transactionEntity";
interface BudgetSummaryEntity { interface BudgetSummaryEntity {
id: string; id: string;

View file

@ -1,9 +1,9 @@
import { type Budget, type BudgetSummary } from "../../../domain/budget.js"; import { type Budget, type BudgetSummary } from "../../../domain/budget";
import type BudgetSummaryEntity from "./budgetSummaryEntity.js"; import type BudgetSummaryEntity from "./budgetSummaryEntity";
import UUID from "../../../domain/uuid.js"; import UUID from "../../../domain/uuid";
import Limit from "../../../domain/limit.js"; import Limit from "../../../domain/limit";
import type BudgetEntity from "./budgetEntity.js"; import type BudgetEntity from "./budgetEntity";
import { TransactionEntityConverter } from "../../transaction/entity/converters.js"; import { TransactionEntityConverter } from "../../transaction/entity/converters";
export const BudgetEntityConverter = { export const BudgetEntityConverter = {
toDomain: (entity: BudgetEntity): Budget => ({ toDomain: (entity: BudgetEntity): Budget => ({

View file

@ -1,6 +1,6 @@
import type BudgetRepository from "../budgetRepository.js"; import type BudgetRepository from "../budgetRepository";
import { BudgetEntityConverter } from "../entity/converters.js"; import { BudgetEntityConverter } from "../entity/converters";
import { BudgetSummaryModel } from "./models.js"; import { BudgetSummaryModel } from "./models";
export const findBudgets: BudgetRepository["findAll"] = async () => { export const findBudgets: BudgetRepository["findAll"] = async () => {
const found = await BudgetSummaryModel.find().select("-transactions"); const found = await BudgetSummaryModel.find().select("-transactions");

View file

@ -1,7 +1,7 @@
import type BudgetSummaryRepository from "../budgetSummaryRepository.js"; import type BudgetSummaryRepository from "../budgetSummaryRepository";
import { BudgetSummaryEntityConverter } from "../entity/converters.js"; import { BudgetSummaryEntityConverter } from "../entity/converters";
import { type BudgetId } from "../../../domain/budget.js"; import { type BudgetId } from "../../../domain/budget";
import { BudgetSummaryModel } from "./models.js"; import { BudgetSummaryModel } from "./models";
export const insertBudgetSummary: BudgetSummaryRepository["insert"] = async ( export const insertBudgetSummary: BudgetSummaryRepository["insert"] = async (
summary summary

View file

@ -1,6 +1,6 @@
import mongoose from "mongoose"; import mongoose from "mongoose";
import budgetSummarySchema from "./schema/budgetSummarySchema.js"; import budgetSummarySchema from "./schema/budgetSummarySchema";
import type BudgetSummaryEntity from "../entity/budgetSummaryEntity.js"; import type BudgetSummaryEntity from "../entity/budgetSummaryEntity";
export const BudgetSummaryModel = mongoose.model<BudgetSummaryEntity>( export const BudgetSummaryModel = mongoose.model<BudgetSummaryEntity>(
"BudgetSummary", "BudgetSummary",

View file

@ -1,6 +1,6 @@
import mongoose, { Schema } from "mongoose"; import mongoose, { Schema } from "mongoose";
import type BudgetSummaryEntity from "../../entity/budgetSummaryEntity.js"; import type BudgetSummaryEntity from "../../entity/budgetSummaryEntity";
import transactionSchema from "../../../transaction/mongo/schema/transactionSchema.js"; import transactionSchema from "../../../transaction/mongo/schema/transactionSchema";
const Types = Schema.Types; const Types = Schema.Types;

View file

@ -1,6 +1,6 @@
import { type Transaction } from "../../../domain/transaction.js"; import { type Transaction } from "../../../domain/transaction";
import UUID from "../../../domain/uuid.js"; import UUID from "../../../domain/uuid";
import type TransactionEntity from "./transactionEntity.js"; import type TransactionEntity from "./transactionEntity";
export const TransactionEntityConverter = { export const TransactionEntityConverter = {
toEntity: (domain: Transaction): TransactionEntity => ({ toEntity: (domain: Transaction): TransactionEntity => ({

View file

@ -1,7 +1,7 @@
import type TransactionRepository from "../transactionRepository.js"; import type TransactionRepository from "../transactionRepository";
import { TransactionEntityConverter } from "../entity/converters.js"; import { TransactionEntityConverter } from "../entity/converters";
import { BudgetSummaryModel } from "../../budget/mongo/models.js"; import { BudgetSummaryModel } from "../../budget/mongo/models";
import type BudgetSummaryEntity from "../../budget/entity/budgetSummaryEntity.js"; import type BudgetSummaryEntity from "../../budget/entity/budgetSummaryEntity";
export const findAllTransactionsForBudget: TransactionRepository["findAllForBudget"] = export const findAllTransactionsForBudget: TransactionRepository["findAllForBudget"] =
async (budgetId) => { async (budgetId) => {

View file

@ -1,5 +1,5 @@
import mongoose, { Schema } from "mongoose"; import mongoose, { Schema } from "mongoose";
import type TransactionEntity from "../../entity/transactionEntity.js"; import type TransactionEntity from "../../entity/transactionEntity";
const Types = Schema.Types; const Types = Schema.Types;

View file

@ -1,5 +1,5 @@
import { type Transaction } from "../../domain/transaction.js"; import { type Transaction } from "../../domain/transaction";
import { type BudgetId } from "../../domain/budget.js"; import { type BudgetId } from "../../domain/budget";
interface TransactionRepository { interface TransactionRepository {
findAllForBudget: (budgetId: BudgetId) => Promise<Transaction[] | undefined>; findAllForBudget: (budgetId: BudgetId) => Promise<Transaction[] | undefined>;

View file

@ -1,4 +1,4 @@
import { type operations, type paths } from "../../generated/api.js"; import { type operations, type paths } from "../../generated/api";
const apiPaths: Record<keyof operations, keyof paths> = { const apiPaths: Record<keyof operations, keyof paths> = {
createBudget: "/budgets", createBudget: "/budgets",

View file

@ -1,8 +1,8 @@
import { Router } from "express"; import { Router } from "express";
import BudgetRouter from "./budget/budgetRouter.js"; import BudgetRouter from "./budget/budgetRouter";
import type BudgetUseCases from "../usecase/budget/budgetUseCases.js"; import type BudgetUseCases from "../usecase/budget/budgetUseCases";
import TransactionRouter from "./transaction/transactionRouter.js"; import TransactionRouter from "./transaction/transactionRouter";
import type TransactionUseCases from "../usecase/transaction/transactionUseCases.js"; import type TransactionUseCases from "../usecase/transaction/transactionUseCases";
const ApiRouter = ( const ApiRouter = (
budgetUseCases: BudgetUseCases, budgetUseCases: BudgetUseCases,

View file

@ -1,16 +1,16 @@
import { type Request, type Response, Router } from "express"; import { type Request, type Response, Router } from "express";
import type BudgetUseCases from "../../usecase/budget/budgetUseCases.js"; import type BudgetUseCases from "../../usecase/budget/budgetUseCases";
import asyncHandler from "express-async-handler"; import asyncHandler from "express-async-handler";
import { type NewBudgetDto } from "./dto/budget.js"; import { type NewBudgetDto } from "./dto/budget";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { import {
BudgetDtoConverter, BudgetDtoConverter,
BudgetSummaryDtoConverter, BudgetSummaryDtoConverter,
NewBudgetDtoConverter, NewBudgetDtoConverter,
} from "./dto/converters.js"; } from "./dto/converters";
import toExpressPath from "../toExpressPath.js"; import toExpressPath from "../toExpressPath";
import UUID from "../../domain/uuid.js"; import UUID from "../../domain/uuid";
import apiPaths from "../apiPaths.js"; import apiPaths from "../apiPaths";
export const createBudget = export const createBudget =
(createBudget: BudgetUseCases["createBudget"]) => (createBudget: BudgetUseCases["createBudget"]) =>

View file

@ -1,4 +1,4 @@
import { type components } from "../../../../generated/api.js"; import { type components } from "../../../../generated/api";
export type BudgetDto = components["schemas"]["Budget"]; export type BudgetDto = components["schemas"]["Budget"];
export type BudgetSummaryDto = components["schemas"]["BudgetSummary"]; export type BudgetSummaryDto = components["schemas"]["BudgetSummary"];

View file

@ -2,15 +2,15 @@ import {
type Budget, type Budget,
type BudgetSummary, type BudgetSummary,
type NewBudget, type NewBudget,
} from "../../../domain/budget.js"; } from "../../../domain/budget";
import Limit from "../../../domain/limit.js"; import Limit from "../../../domain/limit";
import { toDate, toIsoDate } from "../../../util/date.js"; import { toDate, toIsoDate } from "../../../util/date";
import { import {
type BudgetDto, type BudgetDto,
type BudgetSummaryDto, type BudgetSummaryDto,
type NewBudgetDto, type NewBudgetDto,
} from "./budget.js"; } from "./budget";
import { TransactionDtoConverter } from "../../transaction/dto/converters.js"; import { TransactionDtoConverter } from "../../transaction/dto/converters";
export const NewBudgetDtoConverter = { export const NewBudgetDtoConverter = {
toDomain: (dto: NewBudgetDto): NewBudget => ({ toDomain: (dto: NewBudgetDto): NewBudget => ({

View file

@ -1,8 +1,8 @@
import { type NewTransactionDto, type TransactionDto } from "./transaction.js"; import { type NewTransactionDto, type TransactionDto } from "./transaction";
import { import {
type NewTransaction, type NewTransaction,
type Transaction, type Transaction,
} from "../../../domain/transaction.js"; } from "../../../domain/transaction";
export const NewTransactionDtoConverter = { export const NewTransactionDtoConverter = {
toDomain: (dto: NewTransactionDto): NewTransaction => ({ toDomain: (dto: NewTransactionDto): NewTransaction => ({

View file

@ -1,4 +1,4 @@
import { type components } from "../../../../generated/api.js"; import { type components } from "../../../../generated/api";
export type NewTransactionDto = components["schemas"]["NewTransaction"]; export type NewTransactionDto = components["schemas"]["NewTransaction"];
export type TransactionDto = components["schemas"]["Transaction"]; export type TransactionDto = components["schemas"]["Transaction"];

View file

@ -1,13 +1,13 @@
import type TransactionUseCases from "../../usecase/transaction/transactionUseCases.js"; import type TransactionUseCases from "../../usecase/transaction/transactionUseCases";
import { type Request, type Response, Router } from "express"; import { type Request, type Response, Router } from "express";
import { import {
NewTransactionDtoConverter, NewTransactionDtoConverter,
TransactionDtoConverter, TransactionDtoConverter,
} from "./dto/converters.js"; } from "./dto/converters";
import UUID from "../../domain/uuid.js"; import UUID from "../../domain/uuid";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import toExpressPath from "../toExpressPath.js"; import toExpressPath from "../toExpressPath";
import apiPaths from "../apiPaths.js"; import apiPaths from "../apiPaths";
import asyncHandler from "express-async-handler"; import asyncHandler from "express-async-handler";
export const createTransaction = export const createTransaction =

View file

@ -1,12 +1,14 @@
import "./preStart.js"; import "./preStart";
import * as http from "http"; import * as http from "http";
import app from "./app.js"; import app from "./app";
import { listenToErrorEvents } from "./middleware/errorHandler.js"; import { listenToErrorEvents } from "./middleware/errorHandler";
import logger from "./logging/logger.js"; import logger from "./logging/logger";
import environment from "./config/environment.js"; import environment from "./config/environment";
import mongoDb from "./config/mongoDb.js"; import mongoDb from "./config/mongoDb";
await mongoDb.connect(); void (async () => {
await mongoDb.connect();
})();
const onListening = (server: http.Server) => (): void => { const onListening = (server: http.Server) => (): void => {
const addr = server.address(); const addr = server.address();

View file

@ -1,8 +1,8 @@
import { type BudgetSummary } from "../../domain/budget.js"; import { type BudgetSummary } from "../../domain/budget";
import type BudgetSummaryRepository from "../../repository/budget/budgetSummaryRepository.js"; import type BudgetSummaryRepository from "../../repository/budget/budgetSummaryRepository";
import type BudgetUseCases from "./budgetUseCases.js"; import type BudgetUseCases from "./budgetUseCases";
import UUID from "../../domain/uuid.js"; import UUID from "../../domain/uuid";
import type BudgetRepository from "../../repository/budget/budgetRepository.js"; import type BudgetRepository from "../../repository/budget/budgetRepository";
export const createBudget: ( export const createBudget: (
insertBudget: BudgetSummaryRepository["insert"] insertBudget: BudgetSummaryRepository["insert"]

View file

@ -3,7 +3,7 @@ import {
type BudgetId, type BudgetId,
type BudgetSummary, type BudgetSummary,
type NewBudget, type NewBudget,
} from "../../domain/budget.js"; } from "../../domain/budget";
interface BudgetUseCases { interface BudgetUseCases {
createBudget: (newBudget: NewBudget) => Promise<Budget>; createBudget: (newBudget: NewBudget) => Promise<Budget>;

View file

@ -1,7 +1,7 @@
import type TransactionRepository from "../../repository/transaction/transactionRepository.js"; import type TransactionRepository from "../../repository/transaction/transactionRepository";
import type TransactionUseCases from "./transactionUseCases.js"; import type TransactionUseCases from "./transactionUseCases";
import { type Transaction } from "../../domain/transaction.js"; import { type Transaction } from "../../domain/transaction";
import UUID from "../../domain/uuid.js"; import UUID from "../../domain/uuid";
import currency from "currency.js"; import currency from "currency.js";
const addNewTransactionToBudget: ( const addNewTransactionToBudget: (

View file

@ -1,8 +1,8 @@
import { type BudgetId } from "../../domain/budget.js"; import { type BudgetId } from "../../domain/budget";
import { import {
type NewTransaction, type NewTransaction,
type Transaction, type Transaction,
} from "../../domain/transaction.js"; } from "../../domain/transaction";
interface TransactionUseCases { interface TransactionUseCases {
addNewTransactionToBudget: ( addNewTransactionToBudget: (

View file

@ -4,9 +4,9 @@
/* Basic Options */ /* Basic Options */
// "incremental": true, /* Enable incremental compilation */ // "incremental": true, /* Enable incremental compilation */
"target": "ESNext", "target": "ES2022",
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "NodeNext", "module": "CommonJS",
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */ // "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */ // "allowJs": true, /* Allow javascript files to be compiled. */
@ -45,7 +45,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */ /* Module Resolution Options */
"moduleResolution": "NodeNext", "moduleResolution": "Node",
/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", "baseUrl": "./",
/* Base directory to resolve non-absolute module names. */ /* Base directory to resolve non-absolute module names. */