From 39f0c656e776b7dbe2de6f0f1de049ada58e0f56 Mon Sep 17 00:00:00 2001 From: DerZade Date: Fri, 6 Oct 2023 22:47:24 +0200 Subject: [PATCH] refactor(api): :beers: update to node 20 --- api/.eslintrc.js | 8 ------- api/.eslintrc.json | 14 +++++++++++++ api/package.json | 4 ++++ api/src/database.ts | 6 +++--- api/src/index.ts | 30 +++++++++++++++------------ api/src/models/container.model.ts | 2 +- api/src/models/index.ts | 4 ++-- api/src/models/page.model.ts | 2 +- api/src/utils/EventsService.ts | 2 +- api/src/utils/express.ts | 2 +- api/src/utils/sitemap.ts | 2 +- api/src/utils/sso.ts | 11 +++++----- api/src/v1/index.ts | 10 ++++----- api/src/v1/routes/container.router.ts | 8 +++---- api/src/v1/routes/events.router.ts | 4 ++-- api/src/v1/routes/page.router.ts | 8 +++---- api/src/v1/routes/upload.router.ts | 10 ++++----- api/tsconfig.json | 5 +++-- 18 files changed, 74 insertions(+), 58 deletions(-) delete mode 100644 api/.eslintrc.js create mode 100644 api/.eslintrc.json diff --git a/api/.eslintrc.js b/api/.eslintrc.js deleted file mode 100644 index 53392fa..0000000 --- a/api/.eslintrc.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - extends: ['standard-with-typescript'], - rules: { - '@typescript-eslint/indent': ['error', 4], - '@typescript-eslint/semi': ['error', 'always'], - '@typescript-eslint/strict-boolean-expressions': 'off' - } -}; diff --git a/api/.eslintrc.json b/api/.eslintrc.json new file mode 100644 index 0000000..9e6385f --- /dev/null +++ b/api/.eslintrc.json @@ -0,0 +1,14 @@ +{ + "extends": ["standard-with-typescript"], + "env": { + "node": true + }, + "rules": { + "@typescript-eslint/indent": ["error", 4], + "@typescript-eslint/semi": ["error", "always"], + "@typescript-eslint/strict-boolean-expressions": "off" + }, + "parserOptions": { + "project": "./tsconfig.json" + } +} diff --git a/api/package.json b/api/package.json index 1ba6a1d..b513469 100644 --- a/api/package.json +++ b/api/package.json @@ -3,6 +3,10 @@ "version": "1.0.0", "description": "", "main": "index.js", + "type": "module", + "engines": { + "node": ">=16" + }, "scripts": { "lint": "eslint --ext .js,.ts ./src", "serve": "ts-node-dev --inspect --respawn --transpile-only ./src/index.ts", diff --git a/api/src/database.ts b/api/src/database.ts index c209295..50d09d9 100644 --- a/api/src/database.ts +++ b/api/src/database.ts @@ -1,11 +1,11 @@ import 'reflect-metadata'; -import { join } from 'path'; +import { fileURLToPath } from 'node:url'; import { Sequelize } from 'sequelize-typescript'; -import { Container, Page } from './models'; +import { Container, Page } from './models/index.js'; const sequelize = new Sequelize({ dialect: 'sqlite', - storage: join(__dirname, '../data/database.sqlite'), + storage: fileURLToPath(new URL('../data/database.sqlite', import.meta.url)), logging: false }); diff --git a/api/src/index.ts b/api/src/index.ts index 372d36a..9ba8e24 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -1,23 +1,27 @@ import { join } from 'path'; -import { existsSync } from 'fs'; +import { fileURLToPath } from 'node:url'; +import { existsSync, readFileSync } from 'fs'; // eslint-disable-next-line import/no-duplicates -import * as express from 'express'; +import express from 'express'; // eslint-disable-next-line import/no-duplicates import { type Request, type Response, type NextFunction } from 'express'; -import * as bodyParser from 'body-parser'; -import * as morgan from 'morgan'; -import * as cors from 'cors'; -import * as cookieParser from 'cookie-parser'; -import * as compression from 'compression'; +import bodyParser from 'body-parser'; +import morgan from 'morgan'; +import cors from 'cors'; +import cookieParser from 'cookie-parser'; +import compression from 'compression'; -import v1Router from './v1'; +import v1Router from './v1/index.js'; -import './database'; -import { getSitemap } from './utils/sitemap'; -import { wrapAsync } from './utils/express'; -import { Page } from './models'; +import './database.js'; +import { getSitemap } from './utils/sitemap.js'; +import { wrapAsync } from './utils/express.js'; +import { Page } from './models/index.js'; + +// eslint-disable-next-line @typescript-eslint/naming-convention +const __dirname = fileURLToPath(new URL('.', import.meta.url)); const app = express(); @@ -65,7 +69,7 @@ app.get('/sitemap.xml', wrapAsync(async (req, res) => { // frontend if (existsSync(join(__dirname, '../frontend'))) { // eslint-disable-next-line @typescript-eslint/no-var-requires - const assetsManifest: Record = require(join(__dirname, '../frontend', 'assets-manifest.json')); + const assetsManifest: Record = JSON.parse(readFileSync(join(__dirname, '../frontend', 'assets-manifest.json'), { encoding: 'utf-8' })); const cacheHeaders: Record = {}; diff --git a/api/src/models/container.model.ts b/api/src/models/container.model.ts index 732ec23..d3e886b 100644 --- a/api/src/models/container.model.ts +++ b/api/src/models/container.model.ts @@ -14,7 +14,7 @@ import { AfterDestroy } from 'sequelize-typescript'; -import { Page } from '.'; +import { Page } from './index.js'; @Table({ paranoid: true diff --git a/api/src/models/index.ts b/api/src/models/index.ts index d9b2d86..ac9eb0a 100644 --- a/api/src/models/index.ts +++ b/api/src/models/index.ts @@ -1,4 +1,4 @@ -import Page from './page.model'; -import Container from './container.model'; +import Page from './page.model.js'; +import Container from './container.model.js'; export { Page, Container }; diff --git a/api/src/models/page.model.ts b/api/src/models/page.model.ts index 6b2db64..5f3f608 100644 --- a/api/src/models/page.model.ts +++ b/api/src/models/page.model.ts @@ -9,7 +9,7 @@ import { HasMany } from 'sequelize-typescript'; -import { Container } from '.'; +import { Container } from './index.js'; @DefaultScope({ include: [ diff --git a/api/src/utils/EventsService.ts b/api/src/utils/EventsService.ts index 9651373..71a214b 100644 --- a/api/src/utils/EventsService.ts +++ b/api/src/utils/EventsService.ts @@ -1,4 +1,4 @@ -import * as equals from 'fast-deep-equal'; +import equals from 'fast-deep-equal'; import fetch from 'node-fetch'; export interface ArmaEvent { diff --git a/api/src/utils/express.ts b/api/src/utils/express.ts index fe51b2f..9bca038 100644 --- a/api/src/utils/express.ts +++ b/api/src/utils/express.ts @@ -1,6 +1,6 @@ import { type Request, type Response, type NextFunction, type RequestHandler } from 'express'; import { validationResult } from 'express-validator'; -import ResponseError from './ResponseError'; +import ResponseError from './ResponseError.js'; type AsyncRequestHandler = (...params: Parameters) => Promise>; diff --git a/api/src/utils/sitemap.ts b/api/src/utils/sitemap.ts index d7eb8c1..dbb3a77 100644 --- a/api/src/utils/sitemap.ts +++ b/api/src/utils/sitemap.ts @@ -1,7 +1,7 @@ import * as fs from 'fs'; import { SitemapStream, streamToPromise } from 'sitemap'; import { createGzip } from 'zlib'; -import { Page } from '../models'; +import { Page } from '../models/index.js'; let cachedSitemap: Buffer; diff --git a/api/src/utils/sso.ts b/api/src/utils/sso.ts index fc272b7..60e37a7 100644 --- a/api/src/utils/sso.ts +++ b/api/src/utils/sso.ts @@ -1,11 +1,12 @@ -import { type Request, type Response, type NextFunction } from 'express/index'; +import { type Request, type Response, type NextFunction } from 'express'; +import { fileURLToPath } from 'node:url'; +import { readFileSync } from 'node:fs'; import fetch from 'node-fetch'; -import { globalErrorHandler } from './express'; -import ResponseError from './ResponseError'; +import { globalErrorHandler } from './express.js'; +import ResponseError from './ResponseError.js'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const config = require('../../config/config.json'); +const config = JSON.parse(readFileSync(fileURLToPath(new URL('../../config/config.json', import.meta.url)), { encoding: 'utf-8' })); interface SSOUser { admin: boolean diff --git a/api/src/v1/index.ts b/api/src/v1/index.ts index 2c85869..cb0107b 100644 --- a/api/src/v1/index.ts +++ b/api/src/v1/index.ts @@ -1,9 +1,9 @@ import { Router } from 'express'; -import { globalErrorHandler } from '../utils/express'; -import pageRouter from './routes/page.router'; -import containerRouter from './routes/container.router'; -import uploadRouter from './routes/upload.router'; -import eventsRouter from './routes/events.router'; +import { globalErrorHandler } from '../utils/express.js'; +import pageRouter from './routes/page.router.js'; +import containerRouter from './routes/container.router.js'; +import uploadRouter from './routes/upload.router.js'; +import eventsRouter from './routes/events.router.js'; const v1Router = Router(); diff --git a/api/src/v1/routes/container.router.ts b/api/src/v1/routes/container.router.ts index 44a2a07..c134c66 100644 --- a/api/src/v1/routes/container.router.ts +++ b/api/src/v1/routes/container.router.ts @@ -1,9 +1,9 @@ import { Router } from 'express'; -import { wrapAsync, globalErrorHandler, return422 } from '../../utils/express'; +import { wrapAsync, globalErrorHandler, return422 } from '../../utils/express.js'; import { param, body, matchedData } from 'express-validator'; -import { Container } from '../../models'; -import { ssoCheckAuthorized } from '../../utils/sso'; -import ResponseError from '../../utils/ResponseError'; +import { Container } from '../../models/index.js'; +import { ssoCheckAuthorized } from '../../utils/sso.js'; +import ResponseError from '../../utils/ResponseError.js'; const defaultContainerRules = [ body('heading').optional().isString(), diff --git a/api/src/v1/routes/events.router.ts b/api/src/v1/routes/events.router.ts index 9d86ad0..789ac4a 100644 --- a/api/src/v1/routes/events.router.ts +++ b/api/src/v1/routes/events.router.ts @@ -1,6 +1,6 @@ import { Router } from 'express'; -import { globalErrorHandler, wrapAsync } from '../../utils/express'; -import { ArmaEventsService } from '../../utils/EventsService'; +import { globalErrorHandler, wrapAsync } from '../../utils/express.js'; +import { ArmaEventsService } from '../../utils/EventsService.js'; const armaEventService = ArmaEventsService.getInstance(); diff --git a/api/src/v1/routes/page.router.ts b/api/src/v1/routes/page.router.ts index fb1ec13..76dd8d9 100644 --- a/api/src/v1/routes/page.router.ts +++ b/api/src/v1/routes/page.router.ts @@ -1,9 +1,9 @@ import { Router } from 'express'; -import { wrapAsync, globalErrorHandler, return422 } from '../../utils/express'; -import { Page } from '../../models'; +import { wrapAsync, globalErrorHandler, return422 } from '../../utils/express.js'; +import { Page } from '../../models/index.js'; import { body, matchedData } from 'express-validator'; -import { ssoCheckAuthorized } from '../../utils/sso'; -import ResponseError from '../../utils/ResponseError'; +import { ssoCheckAuthorized } from '../../utils/sso.js'; +import ResponseError from '../../utils/ResponseError.js'; const pageRouter = Router(); diff --git a/api/src/v1/routes/upload.router.ts b/api/src/v1/routes/upload.router.ts index 952f6cd..f058600 100644 --- a/api/src/v1/routes/upload.router.ts +++ b/api/src/v1/routes/upload.router.ts @@ -1,9 +1,9 @@ import { Router } from 'express'; -import { globalErrorHandler, wrapAsync } from '../../utils/express'; -import { UploadService } from '../../utils/UploadService'; -import { ssoCheckAuthorized } from '../../utils/sso'; -import ResponseError from '../../utils/ResponseError'; -import * as bodyParser from 'body-parser'; +import { globalErrorHandler, wrapAsync } from '../../utils/express.js'; +import { UploadService } from '../../utils/UploadService.js'; +import { ssoCheckAuthorized } from '../../utils/sso.js'; +import ResponseError from '../../utils/ResponseError.js'; +import bodyParser from 'body-parser'; const uploadService = UploadService.getInstance(); diff --git a/api/tsconfig.json b/api/tsconfig.json index aa9cb57..f5a0b69 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -3,8 +3,9 @@ "experimentalDecorators": true, "emitDecoratorMetadata": true, "allowSyntheticDefaultImports": true, - "module": "commonjs", - "target": "es6", + "module": "Node16", + "moduleResolution": "Node16", + "target": "es2016", "sourceMap": true, "outDir": "./build", "strictNullChecks": true