Skip to content

Commit

Permalink
refactor(api): 🍻 update to node 20
Browse files Browse the repository at this point in the history
  • Loading branch information
DerZade committed Oct 6, 2023
1 parent 1a11312 commit a0d5d23
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 58 deletions.
8 changes: 0 additions & 8 deletions api/.eslintrc.js

This file was deleted.

14 changes: 14 additions & 0 deletions api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 4 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions api/src/database.ts
Original file line number Diff line number Diff line change
@@ -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
});

Expand Down
30 changes: 17 additions & 13 deletions api/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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<string, string> = require(join(__dirname, '../frontend', 'assets-manifest.json'));
const assetsManifest: Record<string, string> = JSON.parse(readFileSync(join(__dirname, '../frontend', 'assets-manifest.json'), { encoding: 'utf-8' }));

const cacheHeaders: Record<string, string | undefined> = {};

Expand Down
2 changes: 1 addition & 1 deletion api/src/models/container.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
AfterDestroy
} from 'sequelize-typescript';

import { Page } from '.';
import { Page } from './index.js';

@Table({
paranoid: true
Expand Down
4 changes: 2 additions & 2 deletions api/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
2 changes: 1 addition & 1 deletion api/src/models/page.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
HasMany
} from 'sequelize-typescript';

import { Container } from '.';
import { Container } from './index.js';

@DefaultScope({
include: [
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/EventsService.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/express.ts
Original file line number Diff line number Diff line change
@@ -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<RequestHandler>) => Promise<ReturnType<RequestHandler>>;

Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/sitemap.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
11 changes: 6 additions & 5 deletions api/src/utils/sso.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions api/src/v1/index.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
8 changes: 4 additions & 4 deletions api/src/v1/routes/container.router.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions api/src/v1/routes/events.router.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
8 changes: 4 additions & 4 deletions api/src/v1/routes/page.router.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
10 changes: 5 additions & 5 deletions api/src/v1/routes/upload.router.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
5 changes: 3 additions & 2 deletions api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0d5d23

Please sign in to comment.