Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIMS-531: Absolute Imports for Express API starting the absolute paths #1979

Merged
merged 12 commits into from
Dec 29, 2023
Merged
7 changes: 4 additions & 3 deletions express-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ ENV CONTAINERIZED=true
# Copy files, excluding those in .dockerignore
COPY . .

# Install TypeScript. Needed for build process.
RUN npm i -D typescript
# Install packages. Needed for build process.
RUN npm i

# Generate swagger-output
RUN npm run swagger
Expand All @@ -34,6 +34,7 @@ ENV NODE_ENV=production
ENV CONTAINERIZED=true

# Install packages. Needed even for compiled build.
# Only installs non-dev dependencies
COPY package.json .
RUN npm i

Expand All @@ -44,5 +45,5 @@ RUN apt-get install -y curl
# Copy compiled build from base
COPY --from=base /express-api/dist .

CMD [ "node", "server.js" ]
CMD [ "node", "src/server.js" ]

8 changes: 0 additions & 8 deletions express-api/constants/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions express-api/controllers/index.ts

This file was deleted.

17 changes: 11 additions & 6 deletions express-api/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { JestConfigWithTsJest } from 'ts-jest';
import { pathsToModuleNameMapper } from 'ts-jest';
import { compilerOptions } from './tsconfig.json';

const jestConfig: JestConfigWithTsJest = {
testEnvironment: 'node',
Expand All @@ -13,11 +15,11 @@ const jestConfig: JestConfigWithTsJest = {
},
collectCoverage: true,
collectCoverageFrom: [
'controllers/**/*.ts',
'middleware/**/*.ts',
'utilities/**/*.ts',
'routes/**/*.ts',
'express.ts',
'src/controllers/**/*.ts',
'src/middleware/**/*.ts',
'src/utilities/**/*.ts',
'src/routes/**/*.ts',
'src/express.ts',
],
coveragePathIgnorePatterns: ['index.ts'],
coverageReporters: [['lcov', { projectRoot: '..' }]],
Expand All @@ -28,11 +30,14 @@ const jestConfig: JestConfigWithTsJest = {
lines: 80,
statements: 80,
},
'express.ts': {
'src/express.ts': {
branches: 0, // Because rate limiter is omitted when testing
},
},
randomize: true, // Randomizes order of tests
roots: ['.'],
modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
};

export default jestConfig;
7 changes: 0 additions & 7 deletions express-api/middleware/index.ts

This file was deleted.

8 changes: 5 additions & 3 deletions express-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "",
"main": "server.ts",
"scripts": {
"dev": "nodemon --exec ts-node -r dotenv/config server.ts dotenv_config_path=../.env",
"build": "tsc",
"dev": "nodemon --exec ts-node -r tsconfig-paths/register -r dotenv/config ./src/server.ts dotenv_config_path=../.env",
"build": "tsc && tsc-alias",
"lint": "eslint './' --ext .ts,.js",
"lint:fix": "npm run lint -- --fix",
"format": "prettier --write \"./**/*.{js,ts,json}\"",
Expand All @@ -14,7 +14,7 @@
"coverage": "jest --coverage",
"test:unit": "jest --testPathPattern=/tests/unit",
"test:integration": "jest --testPathPattern=/tests/integration",
"swagger": "node ./swagger/swagger.mjs"
"swagger": "node ./src/swagger/swagger.mjs"
},
"author": "",
"license": "ISC",
Expand Down Expand Up @@ -58,6 +58,8 @@
"swagger-autogen": "2.23.7",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"tsc-alias": "1.8.8",
"tsconfig-paths": "4.2.0",
"typescript": "5.2.2"
}
}
11 changes: 0 additions & 11 deletions express-api/routes/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataSource } from 'typeorm';
import { CustomWinstonLogger } from './typeorm/utilities/CustomWinstonLogger';
import { CustomWinstonLogger } from '@/typeorm/utilities/CustomWinstonLogger';

const {
POSTGRES_USER,
Expand Down
8 changes: 8 additions & 0 deletions express-api/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import networking from '@/constants/networking';
import switches from '@/constants/switches';

const constants = {
...networking,
...switches,
};
export default constants;
9 changes: 9 additions & 0 deletions express-api/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { healthCheck } from '@/controllers/healthController';
import * as ltsa from '@/controllers/ltsa/ltsaController';
import * as parcels from '@/controllers/parcels/parcelsController';

export default {
healthCheck,
...ltsa,
...parcels,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { stubResponse } from '../../utilities/stubResponse';
import { stubResponse } from '@/utilities/stubResponse';

/**
* @description Used to retrieve property information from LTSA.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { stubResponse } from '../../utilities/stubResponse';
import { stubResponse } from '@/utilities/stubResponse';

/**
* @description Gets information about a particular parcel by the Id provided in the URL parameter.
Expand Down
12 changes: 6 additions & 6 deletions express-api/express.ts → express-api/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import compression from 'compression';
import cors from 'cors';
import rateLimit from 'express-rate-limit';
import { keycloak, protectedRoute } from '@bcgov/citz-imb-kc-express';
import router from './routes';
import middleware from './middleware';
import constants from './constants';
import { KEYCLOAK_OPTIONS } from './middleware/keycloak/keycloakOptions';
import router from '@/routes';
import middleware from '@/middleware';
import constants from '@/constants';
import { KEYCLOAK_OPTIONS } from '@/middleware/keycloak/keycloakOptions';
import swaggerUi from 'swagger-ui-express';
import swaggerJSON from './swagger/swagger-output.json';
import swaggerJSON from '@/swagger/swagger-output.json';

const app: Application = express();

Expand All @@ -19,7 +19,7 @@ const { TESTING, FRONTEND_URL } = constants;
// Express Rate Limiter Configuration
export const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 1000, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
max: 1000, // Limit each IP to 1000 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
Expand Down
7 changes: 7 additions & 0 deletions express-api/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import headerHandler from '@/middleware/headerHandler';
import morganMiddleware from '@/middleware/morganHttpLogging';

export default {
headerHandler,
morganMiddleware,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KCOptions, KeycloakUser } from '@bcgov/citz-imb-kc-express';
import logger from '../../utilities/winstonLogger';
import logger from '@/utilities/winstonLogger';

export const KEYCLOAK_OPTIONS: KCOptions = {
afterUserLogin: (user: KeycloakUser) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import morgan from 'morgan';
import logger from '../utilities/winstonLogger';
import logger from '@/utilities/winstonLogger';

/**
* Middleware function that configures Morgan to use a custom logger with the http severity.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import controllers from '../controllers';
import controllers from '@/controllers';
import express from 'express';

const router = express.Router();
Expand Down
11 changes: 11 additions & 0 deletions express-api/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ltsaRouter from '@/routes/ltsaRouter';
import healthRouter from '@/routes/healthRouter';
import parcelsRouter from '@/routes/parcelsRouter';

const router = {
healthRouter,
ltsaRouter,
parcelsRouter,
};

export default router;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import controllers from '../controllers';
import controllers from '@/controllers';
import express from 'express';

const router = express.Router();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import controllers from '../controllers';
import controllers from '@/controllers';
import express from 'express';

export const PARCELS_ROUTE = '/properties/parcels';
Expand Down
8 changes: 4 additions & 4 deletions express-api/server.ts → express-api/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logger from './utilities/winstonLogger';
import constants from './constants';
import app from './express';
import { AppDataSource } from './appDataSource';
import logger from '@/utilities/winstonLogger';
import constants from '@/constants';
import app from '@/express';
import { AppDataSource } from '@/appDataSource';

const { API_PORT } = constants;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractLogger, LogLevel, LogMessage } from 'typeorm';
import logger from '../../utilities/winstonLogger';
import logger from '@/utilities/winstonLogger';

export class CustomWinstonLogger extends AbstractLogger {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format, createLogger, transports } from 'winston';
import constants from '../constants';
import constants from '@/constants';

const { timestamp, combine, json } = format;
const { TESTING } = constants;
Expand Down
2 changes: 1 addition & 1 deletion express-api/tests/integration/express/express.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import app, { limiter } from '../../../express';
import app, { limiter } from '@/express';
import supertest from 'supertest';

const request = supertest(app);
Expand Down
2 changes: 1 addition & 1 deletion express-api/tests/integration/health/getHealth.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import app from '../../../express';
import app from '@/express';
import supertest from 'supertest';

const request = supertest(app);
Expand Down
4 changes: 2 additions & 2 deletions express-api/tests/integration/ltsa/getLtsa.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ILtsaOrder } from '../../../controllers/ltsa/ILtsaOrder';
import app from '../../../express';
import { ILtsaOrder } from '@/controllers/ltsa/ILtsaOrder';
import app from '@/express';
import supertest from 'supertest';

const request = supertest(app);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import app from '../../../express';
import app from '@/express';
import supertest from 'supertest';

const request = supertest(app);
Expand Down
4 changes: 2 additions & 2 deletions express-api/tests/integration/parcels/parcels.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import supertest from 'supertest';
import app from '../../../express';
import app from '@/express';
import { faker } from '@faker-js/faker';
import { IParcel } from '../../../controllers/parcels/IParcel';
import { IParcel } from '@/controllers/parcels/IParcel';

const request = supertest(app);
const API_PATH = '/api/v2/properties/parcels';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import controllers from '../../../controllers';
import controllers from '@/controllers';
import { Request, Response } from 'express';

describe('UNIT - Testing controller for /health routes', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import controllers from '../../../../controllers';
import controllers from '@/controllers';

describe('UNIT - Testing controllers for /ltsa routes', () => {
const mockRequest = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IParcel } from '../../../../controllers/parcels/IParcel';
import { IParcel } from '@/controllers/parcels/IParcel';
import { faker } from '@faker-js/faker';
import controllers from '../../../../controllers';
import controllers from '@/controllers';
import { Request, Response } from 'express';
import { MockReq, MockRes, getRequestHandlerMocks } from '../../../testUtils/factories';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IdirIdentityProvider } from '@bcgov/citz-imb-kc-express';
import { KEYCLOAK_OPTIONS } from '../../../../middleware/keycloak/keycloakOptions';
import logger from '../../../../utilities/winstonLogger';
import { KEYCLOAK_OPTIONS } from '@/middleware/keycloak/keycloakOptions';
import logger from '@/utilities/winstonLogger';

describe('UNIT - Keycloak Options', () => {
const user = {
Expand Down
2 changes: 1 addition & 1 deletion express-api/tests/unit/utilities/stubResponse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stubResponse } from '../../../utilities/stubResponse';
import { stubResponse } from '@/utilities/stubResponse';
import { Response } from 'express';

// TODO: Remove this test, along with the stub controller when all routes are fully implemented.
Expand Down
2 changes: 1 addition & 1 deletion express-api/tests/unit/utilities/winstonLogger.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logger from '../../../utilities/winstonLogger';
import logger from '@/utilities/winstonLogger';

describe('UNIT - winston logger', () => {
it('should be created with the expected settings', () => {
Expand Down
6 changes: 5 additions & 1 deletion express-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"module": "commonjs",
"esModuleInterop": true,
"target": "es2022",
Expand All @@ -9,7 +13,7 @@
"outDir": "dist",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true // Used for swagger-output.json
"resolveJsonModule": true
},
"include": ["./**/*"]
}