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",
"license": "MIT",
"author": "Alexander Ungar",
"type": "module",
"main": "dist/src/index.js",
"scripts": {
"clean": "rimraf dist",
@ -16,11 +15,6 @@
"dev": "nodemon src/server.ts",
"test": "nodemon --config ./spec/nodemon.json"
},
"nodemonConfig": {
"execMap": {
"ts": "ts-node --esm"
}
},
"dependencies": {
"body-parser": "^1.20.2",
"currency.js": "^2.0.4",

View file

@ -1,15 +1,16 @@
import express from "express";
import helmet from "helmet";
import environment from "./config/environment.js";
import expressLogger from "./logging/expressLogger.js";
import ApiRouter from "./routes/apiRouter.js";
import BudgetService from "./usecase/budget/budgetService.js";
import { errorHandler } from "./middleware/errorHandler.js";
import BudgetMongoRepository from "./repository/budget/mongo/budgetMongoRepository.js";
import BudgetSummaryMongoRepository from "./repository/budget/mongo/budgetSummaryMongoRepository.js";
import TransactionService from "./usecase/transaction/transactionService.js";
import TransactionMongoRepository from "./repository/transaction/mongo/TransactionMongoRepository.js";
import OpenApiValidator from "express-openapi-validator";
import environment from "./config/environment";
import expressLogger from "./logging/expressLogger";
import ApiRouter from "./routes/apiRouter";
import BudgetService from "./usecase/budget/budgetService";
import { errorHandler } from "./middleware/errorHandler";
import BudgetMongoRepository from "./repository/budget/mongo/budgetMongoRepository";
import BudgetSummaryMongoRepository from "./repository/budget/mongo/budgetSummaryMongoRepository";
import TransactionService from "./usecase/transaction/transactionService";
import TransactionMongoRepository from "./repository/transaction/mongo/TransactionMongoRepository";
import * as OpenApiValidator from "express-openapi-validator";
import * as path from "path";
const app = express();
@ -23,7 +24,7 @@ if (environment.isProd) {
app.use(
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,
validateResponses: true,
})

View file

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

View file

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

View file

@ -1,4 +1,4 @@
import { AppError } from "../middleware/errorHandler.js";
import { AppError } from "../middleware/errorHandler";
class Limit {
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;

View file

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

View file

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

View file

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

View file

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

View file

@ -7,6 +7,7 @@
// NOTE: DO NOT IMPORT ANY SOURCE CODE HERE
import dotenv from "dotenv";
import { parse } from "ts-command-line-args";
import path from "path";
// **** Types **** //
@ -27,7 +28,7 @@ const args = parse<Args>({
// Set the env file
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) {
throw dotenvConfig.error;

View file

@ -1,4 +1,4 @@
import { type Budget } from "../../domain/budget.js";
import { type Budget } from "../../domain/budget";
interface BudgetRepository {
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 {
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 {
id: string;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
import { type Transaction } from "../../domain/transaction.js";
import { type BudgetId } from "../../domain/budget.js";
import { type Transaction } from "../../domain/transaction";
import { type BudgetId } from "../../domain/budget";
interface TransactionRepository {
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> = {
createBudget: "/budgets",

View file

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

View file

@ -1,16 +1,16 @@
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 { type NewBudgetDto } from "./dto/budget.js";
import { type NewBudgetDto } from "./dto/budget";
import { StatusCodes } from "http-status-codes";
import {
BudgetDtoConverter,
BudgetSummaryDtoConverter,
NewBudgetDtoConverter,
} from "./dto/converters.js";
import toExpressPath from "../toExpressPath.js";
import UUID from "../../domain/uuid.js";
import apiPaths from "../apiPaths.js";
} from "./dto/converters";
import toExpressPath from "../toExpressPath";
import UUID from "../../domain/uuid";
import apiPaths from "../apiPaths";
export const 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 BudgetSummaryDto = components["schemas"]["BudgetSummary"];

View file

@ -2,15 +2,15 @@ import {
type Budget,
type BudgetSummary,
type NewBudget,
} from "../../../domain/budget.js";
import Limit from "../../../domain/limit.js";
import { toDate, toIsoDate } from "../../../util/date.js";
} from "../../../domain/budget";
import Limit from "../../../domain/limit";
import { toDate, toIsoDate } from "../../../util/date";
import {
type BudgetDto,
type BudgetSummaryDto,
type NewBudgetDto,
} from "./budget.js";
import { TransactionDtoConverter } from "../../transaction/dto/converters.js";
} from "./budget";
import { TransactionDtoConverter } from "../../transaction/dto/converters";
export const NewBudgetDtoConverter = {
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 {
type NewTransaction,
type Transaction,
} from "../../../domain/transaction.js";
} from "../../../domain/transaction";
export const NewTransactionDtoConverter = {
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 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 {
NewTransactionDtoConverter,
TransactionDtoConverter,
} from "./dto/converters.js";
import UUID from "../../domain/uuid.js";
} from "./dto/converters";
import UUID from "../../domain/uuid";
import { StatusCodes } from "http-status-codes";
import toExpressPath from "../toExpressPath.js";
import apiPaths from "../apiPaths.js";
import toExpressPath from "../toExpressPath";
import apiPaths from "../apiPaths";
import asyncHandler from "express-async-handler";
export const createTransaction =

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,9 +4,9 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ESNext",
"target": "ES2022",
/* 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'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
@ -45,7 +45,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "NodeNext",
"moduleResolution": "Node",
/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./",
/* Base directory to resolve non-absolute module names. */