From 08d060519d701891eb468cdb72fa2a6a3c472426 Mon Sep 17 00:00:00 2001 From: Gijsdeman Date: Wed, 26 Feb 2025 22:23:08 +0100 Subject: [PATCH] refactor: last lint issues --- package.json | 2 +- src/auth/LDAPStrategy.ts | 26 ++++++++++++++++++++------ src/auth/LocalStrategy.ts | 6 ++++-- src/controllers/CompanyController.ts | 4 ++-- src/controllers/ContractController.ts | 6 +++--- src/controllers/InvoiceController.ts | 8 ++++---- src/controllers/ListParams.ts | 3 ++- src/controllers/ProductController.ts | 4 ++-- src/helpers/fileHelper.ts | 3 ++- src/timedevents/events/ldapGroups.ts | 5 +++-- tsconfig.json | 3 +-- yarn.lock | 24 ++++++++++++------------ 12 files changed, 56 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 565dd70..de84f10 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "gen-client": "nswag openapi2tsclient /input:src/public/swagger.json /output:../parelpracht-client/src/clients/server.generated.ts /ServiceHost:.", "db:diagram": "typeorm-uml ormconfig.json", "db:validate": "ts-node src/dbvalidator/validate.ts", - "lint": "eslint src", + "lint": "yarn tsoa && eslint src", "lint:fix": "eslint . --fix", "format": "prettier --ignore-path .gitignore --check .", "format:fix": "prettier --ignore-path .gitignore --write ." diff --git a/src/auth/LDAPStrategy.ts b/src/auth/LDAPStrategy.ts index 491ff71..599eee5 100644 --- a/src/auth/LDAPStrategy.ts +++ b/src/auth/LDAPStrategy.ts @@ -12,6 +12,19 @@ import { ExpressRequest } from '../types'; const isDefined = (i: string | undefined) => i !== undefined && i !== ''; +export interface LDAPUser { + sAMAccountName: string; + memberOfFlattened: string[]; + memberOf: string[]; + mail: string; + givenName: string; + sn: string; +} + +interface AuthInfo { + message: string; +} + export const ldapEnabled = () => isDefined(process.env.LDAP_URL) && isDefined(process.env.LDAP_BINDDN) && @@ -29,7 +42,7 @@ export const LDAPStrategy = new Strategy({ }, }); -const checkAllowedRoles = async (ldapUser: any): Promise => { +const checkAllowedRoles = async (ldapUser: LDAPUser): Promise => { const roles = await AppDataSource.getRepository(Role).find(); const userRoles: Roles[] = []; roles.forEach((role) => { @@ -40,7 +53,7 @@ const checkAllowedRoles = async (ldapUser: any): Promise => { return userRoles; }; -export const updateUserInformation = async (user: User, ldapUser: any): Promise => { +export const updateUserInformation = async (user: User, ldapUser: LDAPUser): Promise => { const userRoles = await checkAllowedRoles(ldapUser); await new UserService().assignRoles(user, userRoles); @@ -55,7 +68,8 @@ export const updateUserInformation = async (user: User, ldapUser: any): Promise< }; export const ldapLogin = (req: ExpressRequest, res: express.Response, next: express.NextFunction) => { - passport.authenticate('ldapauth', async (err: any, ldapUser: any, info: any) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call -- this seems to be correct + passport.authenticate('ldapauth', async (err: Error, ldapUser: LDAPUser, info: AuthInfo) => { if (err) { return next(err); } @@ -85,13 +99,13 @@ export const ldapLogin = (req: ExpressRequest, res: express.Response, next: expr lastName: ldapUser.sn, email: ldapUser.mail, function: '', - } as any as User; + } as User; user = await userRepo.save(user); identity = { id: user.id, username: ldapUser.sAMAccountName, - } as any as IdentityLDAP; + } as IdentityLDAP; identity = await identityRepo.save(identity); identity = await identityRepo.findOne({ where: { id: identity.id }, @@ -105,7 +119,7 @@ export const ldapLogin = (req: ExpressRequest, res: express.Response, next: expr await updateUserInformation(identity.user, ldapUser); - return req.logIn(identity.user, (e: any) => { + return req.logIn(identity.user, (e: Error) => { // When the user enabled "remember me", we give the session cookie an // expiration date of 30 days if (req.body.rememberMe === true) { diff --git a/src/auth/LocalStrategy.ts b/src/auth/LocalStrategy.ts index 55c0556..5617c96 100644 --- a/src/auth/LocalStrategy.ts +++ b/src/auth/LocalStrategy.ts @@ -33,6 +33,7 @@ export default new LocalStrategy( usernameField: 'email', passwordField: 'password', }, + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- async is allowed here async (email, password, done) => { const userRepo = AppDataSource.getRepository(User); const identityRepo = AppDataSource.getRepository(IdentityLocal); @@ -69,14 +70,15 @@ export default new LocalStrategy( ); export const localLogin = (req: ExpressRequest, res: express.Response, next: express.NextFunction) => { - passport.authenticate('local', (err: any, user: any) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call -- this seems to be correct + passport.authenticate('local', (err: Error, user: User) => { if (err) { return next(err); } if (!user) { return next(new ApiError(HTTPStatus.BadRequest, INVALID_LOGIN)); } - return req.logIn(user, (e: any) => { + return req.logIn(user, (e: Error) => { // When the user enabled "remember me", we give the session cookie an // expiration date of 30 days if (req.body.rememberMe === true) { diff --git a/src/controllers/CompanyController.ts b/src/controllers/CompanyController.ts index af31868..28cfe46 100644 --- a/src/controllers/CompanyController.ts +++ b/src/controllers/CompanyController.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { Readable } from 'stream'; import { body } from 'express-validator'; import { Company } from '../entity/Company'; import { Invoice } from '../entity/Invoice'; @@ -219,7 +219,7 @@ export class CompanyController extends Controller { @Get('{id}/file/{fileId}') @Security('local', ['GENERAL', 'ADMIN']) @Response(401) - public async getCompanyFile(id: number, fileId: number): Promise { + public async getCompanyFile(id: number, fileId: number): Promise { const file = await new FileService(CompanyFile).getFile(id, fileId); return FileHelper.putFileInResponse(this, file); diff --git a/src/controllers/ContractController.ts b/src/controllers/ContractController.ts index 9fb8450..1e89a4a 100644 --- a/src/controllers/ContractController.ts +++ b/src/controllers/ContractController.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { Readable } from 'stream'; import { body } from 'express-validator'; import { Contract } from '../entity/Contract'; import ContractService, { ContractListResponse, ContractParams } from '../services/ContractService'; @@ -323,7 +323,7 @@ export class ContractController extends Controller { id: number, @Body() params: GenerateContractParams, @Request() req: ExpressRequest, - ): Promise { + ): Promise { await validate( [ body('language').isIn(Object.values(Language)), @@ -374,7 +374,7 @@ export class ContractController extends Controller { @Get('{id}/file/{fileId}') @Security('local', ['SIGNEE', 'FINANCIAL', 'GENERAL', 'ADMIN', 'AUDIT']) @Response(401) - public async getContractFile(id: number, fileId: number): Promise { + public async getContractFile(id: number, fileId: number): Promise { const file = await new FileService(ContractFile).getFile(id, fileId); return FileHelper.putFileInResponse(this, file); diff --git a/src/controllers/InvoiceController.ts b/src/controllers/InvoiceController.ts index f0aa111..581b065 100644 --- a/src/controllers/InvoiceController.ts +++ b/src/controllers/InvoiceController.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { Readable } from 'stream'; import { body, ValidationChain } from 'express-validator'; import { Invoice } from '../entity/Invoice'; import { ApiError, HTTPStatus, WrappedApiError } from '../helpers/error'; @@ -180,7 +180,7 @@ export class InvoiceController extends Controller { id: number, @Body() params: GenerateInvoiceParams, @Request() req: ExpressRequest, - ): Promise { + ): Promise { await validate( [ body('language').isIn(Object.values(Language)), @@ -228,7 +228,7 @@ export class InvoiceController extends Controller { @Get('{id}/file/{fileId}') @Security('local', ['SIGNEE', 'FINANCIAL', 'GENERAL', 'ADMIN', 'AUDIT']) @Response(401) - public async getInvoiceFile(id: number, fileId: number): Promise { + public async getInvoiceFile(id: number, fileId: number): Promise { const file = await new FileService(InvoiceFile).getFile(id, fileId); return FileHelper.putFileInResponse(this, file); @@ -278,7 +278,7 @@ export class InvoiceController extends Controller { public async generateCustomInvoice( @Body() params: CustomInvoiceGenSettings, @Request() req: ExpressRequest, - ): Promise { + ): Promise { await validate( [ body('language').isIn(Object.values(Language)), diff --git a/src/controllers/ListParams.ts b/src/controllers/ListParams.ts index 19dafa6..e7b8b24 100644 --- a/src/controllers/ListParams.ts +++ b/src/controllers/ListParams.ts @@ -18,5 +18,6 @@ export type SortDirection = 'ASC' | 'DESC'; export interface ListOrFilter { column: string; - values: unknown[]; + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO check if change required after rewriting queries + values: any[]; } diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 64689dd..d3060b5 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { Readable } from 'stream'; import { body } from 'express-validator'; import { Product } from '../entity/Product'; import ProductService, { @@ -230,7 +230,7 @@ export class ProductController extends Controller { @Get('{id}/file/{fileId}') @Security('local', ['GENERAL', 'ADMIN']) @Response(401) - public async getProductFile(id: number, fileId: number): Promise { + public async getProductFile(id: number, fileId: number): Promise { const file = await new FileService(ProductFile).getFile(id, fileId); return FileHelper.putFileInResponse(this, file); diff --git a/src/helpers/fileHelper.ts b/src/helpers/fileHelper.ts index c2650b7..7400c11 100644 --- a/src/helpers/fileHelper.ts +++ b/src/helpers/fileHelper.ts @@ -1,5 +1,6 @@ import * as fs from 'fs'; import path from 'path'; +import { Readable } from 'stream'; import mime from 'mime'; import BaseFile from '../entity/file/BaseFile'; import { Controller } from 'tsoa'; @@ -18,7 +19,7 @@ export default class FileHelper { * @param controller Controller that handles the request * @param file File to add to the response */ - public static putFileInResponse(controller: Controller, file: BaseFile): fs.ReadStream { + public static putFileInResponse(controller: Controller, file: BaseFile): Readable { const stat = fs.statSync(file.location); controller.setStatus(200); diff --git a/src/timedevents/events/ldapGroups.ts b/src/timedevents/events/ldapGroups.ts index 57e64d9..dc73fb3 100644 --- a/src/timedevents/events/ldapGroups.ts +++ b/src/timedevents/events/ldapGroups.ts @@ -1,6 +1,6 @@ import { createClient, SearchCallbackResponse, SearchEntry } from 'ldapjs'; import { IdentityLDAP } from '../../entity/IdentityLDAP'; -import { updateUserInformation } from '../../auth'; +import { LDAPUser, updateUserInformation } from '../../auth'; import AppDataSource from '../../database'; export default async function ldapGroups() { @@ -28,7 +28,8 @@ export default async function ldapGroups() { } res.on('searchEntry', (entry: SearchEntry) => { - updateUserInformation(identity.user, entry.object).catch((err) => console.error(err)); + // TODO check if there is a better way to cast this type + updateUserInformation(identity.user, entry.object as unknown as LDAPUser).catch((err) => console.error(err)); }); }, ); diff --git a/tsconfig.json b/tsconfig.json index d7d9e12..18c92c9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -39,11 +39,10 @@ /* Advanced Options */ "forceConsistentCasingInFileNames": true, - "resolveJsonModule": true }, "ts-node": { "files": true }, - "include": ["src", "src/**/*.json", "build", "build/**/*.json"], + "include": ["src", "src/**/*.json", "build/**/*.ts", "build/**/*.json"], "exclude": ["node_modules", "dist"] } diff --git a/yarn.lock b/yarn.lock index c94c276..99811b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1608,13 +1608,13 @@ __metadata: linkType: hard "axios@npm:*, axios@npm:^1.7.9": - version: 1.7.9 - resolution: "axios@npm:1.7.9" + version: 1.8.1 + resolution: "axios@npm:1.8.1" dependencies: follow-redirects: "npm:^1.15.6" form-data: "npm:^4.0.0" proxy-from-env: "npm:^1.1.0" - checksum: 10c0/b7a41e24b59fee5f0f26c1fc844b45b17442832eb3a0fb42dd4f1430eb4abc571fe168e67913e8a1d91c993232bd1d1ab03e20e4d1fee8c6147649b576fc1b0b + checksum: 10c0/b2e1d5a61264502deee4b50f0a6df0aa3b174c546ccf68c0dff714a2b8863232e0bd8cb5b84f853303e97f242a98260f9bb9beabeafe451ad5af538e9eb7ac22 languageName: node linkType: hard @@ -2465,13 +2465,13 @@ __metadata: linkType: hard "eslint-config-prettier@npm:^10.0.1": - version: 10.0.1 - resolution: "eslint-config-prettier@npm:10.0.1" + version: 10.0.2 + resolution: "eslint-config-prettier@npm:10.0.2" peerDependencies: eslint: ">=7.0.0" bin: eslint-config-prettier: build/bin/cli.js - checksum: 10c0/e2434931669d211663c0493f2c1640a670a02ba4503a68f056a7eda133f383acbbb983a4a7bd0ad6cb3b2bc4d5731c3be8b32fe28e35087a76fea45f7061ae70 + checksum: 10c0/e0ef3c442661a26fc6e82acec5bb9a418c4a8f65ec8adf0983d3aaba7716d2ed448358b063cce6e3c272c847d14cb856ddf30031770c6571e2b2c3e2a439afd4 languageName: node linkType: hard @@ -2839,11 +2839,11 @@ __metadata: linkType: hard "fastq@npm:^1.6.0": - version: 1.19.0 - resolution: "fastq@npm:1.19.0" + version: 1.19.1 + resolution: "fastq@npm:1.19.1" dependencies: reusify: "npm:^1.0.4" - checksum: 10c0/d6a001638f1574a696660fcbba5300d017760432372c801632c325ca7c16819604841c92fd3ccadcdacec0966ca336363a5ff57bc5f0be335d8ea7ac6087b98f + checksum: 10c0/ebc6e50ac7048daaeb8e64522a1ea7a26e92b3cee5cd1c7f2316cdca81ba543aa40a136b53891446ea5c3a67ec215fbaca87ad405f102dd97012f62916905630 languageName: node linkType: hard @@ -5449,9 +5449,9 @@ __metadata: linkType: hard "reusify@npm:^1.0.4": - version: 1.0.4 - resolution: "reusify@npm:1.0.4" - checksum: 10c0/c19ef26e4e188f408922c46f7ff480d38e8dfc55d448310dfb518736b23ed2c4f547fb64a6ed5bdba92cd7e7ddc889d36ff78f794816d5e71498d645ef476107 + version: 1.1.0 + resolution: "reusify@npm:1.1.0" + checksum: 10c0/4eff0d4a5f9383566c7d7ec437b671cc51b25963bd61bf127c3f3d3f68e44a026d99b8d2f1ad344afff8d278a8fe70a8ea092650a716d22287e8bef7126bb2fa languageName: node linkType: hard