Skip to content

Commit 45ff12a

Browse files
pyphiliakim
andauthored
refactor: apply tsconfig strict true (#2086)
* refactor: apply tsconfig strict true * refactor: fix tests * refactor: fix tests --------- Co-authored-by: kim <kim.phanhoang@epfl.ch>
1 parent ef2adaa commit 45ff12a

33 files changed

Lines changed: 187 additions & 183 deletions

src/config/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const once = (fn) => {
1414
return function (...args) {
1515
if (called) return;
1616
called = true;
17+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18+
// @ts-ignore
1719
return fn.apply(this, args);
1820
};
1921
};

src/services/auth/plugins/captcha/captcha.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default function captchaPreHandler(
2525
action: RecaptchaActionType,
2626
options?: { shouldFail: boolean },
2727
): RouteHandlerMethod {
28+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
29+
// @ts-ignore
2830
return async (request: FastifyRequest<{ Body: { captcha: string } }>, _reply: FastifyReply) => {
2931
const { captcha } = request.body;
3032
return await validateCaptcha(request, captcha, action, options);

src/services/auth/plugins/magicLink/magicLink.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
8282
schema: auth,
8383
preHandler: fastifyPassport.authenticate(
8484
PassportStrategy.WebMagicLink,
85+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
86+
// @ts-ignore
8587
async (request, reply, err, user?: PassportUser, info?: PassportInfo) => {
8688
// This function is called after the strategy has been executed.
8789
// It is necessary, so we match the behavior of the original implementation.

src/services/auth/plugins/passport/preHandlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export const guestAuthenticateAppsJWT = fastifyPassport.authenticate(
7979
export function matchOne<R extends RouteGenericInterface>(
8080
...strategies: RessourceAuthorizationStrategy<R>[]
8181
): RouteHandlerMethod {
82+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
83+
// @ts-expect-error
8284
return async (req: FastifyRequest<R>) => {
8385
if (!strategies.some((strategy) => strategy.test(req))) {
8486
// If none of the strategies pass, throw an error.

src/services/auth/plugins/passport/strategies/emailChange.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Authenticator } from '@fastify/passport';
44

55
import { EMAIL_CHANGE_JWT_SECRET } from '../../../../../config/secrets';
66
import { db } from '../../../../../drizzle/db';
7-
import { MemberNotFound, UnauthorizedMember } from '../../../../../utils/errors';
7+
import { MemberNotFound, UnauthorizedMember, buildError } from '../../../../../utils/errors';
88
import { MemberRepository } from '../../../../member/member.repository';
99
import { PassportStrategy } from '../strategies';
1010
import type { CustomStrategyOptions, StrictVerifiedCallback } from '../types';
@@ -47,9 +47,12 @@ export default (
4747
false,
4848
);
4949
}
50-
} catch (err) {
50+
} catch (error: unknown) {
5151
// Exception occurred while fetching member
52-
return done(options?.propagateError ? err : new UnauthorizedMember(), false);
52+
return done(
53+
options?.propagateError ? buildError(error) : new UnauthorizedMember(),
54+
false,
55+
);
5356
}
5457
},
5558
),

src/services/auth/plugins/passport/strategies/jwtApps.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Authenticator } from '@fastify/passport';
44

55
import { APPS_JWT_SECRET } from '../../../../../config/secrets';
66
import { db } from '../../../../../drizzle/db';
7-
import { UnauthorizedMember } from '../../../../../utils/errors';
7+
import { UnauthorizedMember, buildError } from '../../../../../utils/errors';
88
import { AccountRepository } from '../../../../account/account.repository';
99
import { ItemRepository } from '../../../../item/item.repository';
1010
import { PassportStrategy } from '../strategies';
@@ -53,11 +53,14 @@ export default (
5353
key,
5454
},
5555
});
56-
} catch (err) {
56+
} catch (error: unknown) {
5757
// Exception occurred while fetching item
5858
// itemRepository.getOneOrThrow() can fail for many reasons like the item was not found, database error, etc.
5959
// To avoid leaking information, we prefer to return UnauthorizedMember error.
60-
return done(options?.propagateError ? err : new UnauthorizedMember(), false);
60+
return done(
61+
options?.propagateError ? buildError(error) : new UnauthorizedMember(),
62+
false,
63+
);
6164
}
6265
},
6366
),

src/services/auth/plugins/passport/strategies/jwtChallengeVerifier.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { Authenticator } from '@fastify/passport';
55

66
import { JWT_SECRET } from '../../../../../config/secrets';
77
import { db } from '../../../../../drizzle/db';
8-
import { ChallengeFailed, MemberNotFound, UnauthorizedMember } from '../../../../../utils/errors';
8+
import {
9+
ChallengeFailed,
10+
MemberNotFound,
11+
UnauthorizedMember,
12+
buildError,
13+
} from '../../../../../utils/errors';
914
import { AccountRepository } from '../../../../account/account.repository';
1015
import { SHORT_TOKEN_PARAM } from '../constants';
1116
import { PassportStrategy } from '../strategies';
@@ -57,9 +62,9 @@ export default (
5762
false,
5863
);
5964
}
60-
} catch (err) {
65+
} catch (error: unknown) {
6166
// Exception occurred while fetching member
62-
return done(spreadException ? err : new UnauthorizedMember(), false);
67+
return done(spreadException ? buildError(error) : new UnauthorizedMember(), false);
6368
}
6469
},
6570
),

src/services/file/file.service.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ describe('FileService', () => {
115115
it('upload failure will delete file', async () => {
116116
const uploadFileMock = jest.spyOn(s3Repository, 'uploadFiles').mockRejectedValue('error');
117117
const deleteFileMock = jest.spyOn(s3Repository, 'deleteFiles').mockImplementation(doNothing);
118-
await expect(s3FileService.upload(member, uploadPayload)).rejects.toMatchObject(
119-
new UploadFileUnexpectedError(expect.anything()),
118+
await expect(s3FileService.upload(member, uploadPayload)).rejects.toThrow(
119+
new UploadFileUnexpectedError({ memberId: member.id }),
120120
);
121121
expect(uploadFileMock).toHaveBeenCalled();
122122
expect(deleteFileMock).toHaveBeenCalled();
@@ -276,19 +276,19 @@ describe('FileService', () => {
276276
const copyMock = jest
277277
.spyOn(s3Repository, 'copyFile')
278278
.mockImplementation(async () => 'string');
279-
await s3FileService.copy(member, copyPayload);
279+
await s3FileService.copy(member.id, copyPayload);
280280
expect(copyMock).toHaveBeenCalled();
281281
});
282282

283283
it('empty originalPath throws', async () => {
284284
await expect(
285-
s3FileService.copy(member, { ...copyPayload, originalPath: '' }),
285+
s3FileService.copy(member.id, { ...copyPayload, originalPath: '' }),
286286
).rejects.toMatchObject(new CopyFileInvalidPathError(expect.anything()));
287287
});
288288

289289
it('empty newFilePath throws', async () => {
290290
await expect(
291-
s3FileService.copy(member, { ...copyPayload, newFilePath: '' }),
291+
s3FileService.copy(member.id, { ...copyPayload, newFilePath: '' }),
292292
).rejects.toMatchObject(new CopyFileInvalidPathError(expect.anything()));
293293
});
294294
});

src/services/file/file.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class FileService {
161161
}
162162

163163
async copy(
164-
member: MinimalMember,
164+
memberId: MinimalMember['id'],
165165
data: {
166166
newId?: string;
167167
newFilePath: string;
@@ -180,7 +180,7 @@ class FileService {
180180

181181
return this.repository.copyFile({
182182
newId,
183-
memberId: member.id,
183+
memberId,
184184
originalPath,
185185
newFilePath,
186186
mimetype,

src/services/file/repositories/local.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class LocalFileRepository implements FileRepository {
6969
try {
7070
await access(this.buildFullPath(filepath));
7171
} catch (e) {
72-
if (e.code === 'ENOENT') {
72+
if (e !== null && typeof e === 'object' && 'code' in e && e.code === 'ENOENT') {
7373
throw new LocalFileNotFound({ filepath });
7474
}
7575
throw e;

0 commit comments

Comments
 (0)