chore(api): migrate to fastify v5 (#57576)

This commit is contained in:
Oliver Eyton-Williams
2025-06-02 04:37:57 +02:00
committed by GitHub
parent 690d05c7b9
commit 89402caa9d
14 changed files with 301 additions and 298 deletions

View File

@@ -81,7 +81,7 @@ ajv.addFormat('objectid', {
});
export const buildOptions = {
logger: getLogger(),
loggerInstance: process.env.NODE_ENV === 'test' ? undefined : getLogger(),
genReqId: () => randomBytes(8).toString('hex'),
disableRequestLogging: true
};
@@ -166,7 +166,6 @@ export const build = async (
// TODO: bounce unauthed requests before checking CSRF token. This will
// mean moving csrfProtection into custom plugin and testing separately,
// because it's a pain to mess around with other cookies/hook order.
// @ts-expect-error - @fastify/csrf-protection needs to update their types
// eslint-disable-next-line @typescript-eslint/unbound-method
fastify.addHook('onRequest', fastify.csrfProtection);
fastify.addHook('onRequest', fastify.send401IfNoUser);

View File

@@ -15,7 +15,6 @@ async function setupServer() {
const fastify = Fastify({ logger: true, disableRequestLogging: true });
await fastify.register(cookies);
await fastify.register(csrf);
// @ts-expect-error - @fastify/csrf-protection needs to update their types
// eslint-disable-next-line @typescript-eslint/unbound-method
fastify.addHook('onRequest', fastify.csrfProtection);

View File

@@ -3,13 +3,6 @@ import Fastify from 'fastify';
import mailer from './mailer';
describe('mailer', () => {
it('should throw if not given a provider', async () => {
const fastify = Fastify();
await expect(fastify.register(mailer)).rejects.toThrow(
"The mailer plugin must be passed a provider via register's options."
);
});
it('should send an email via the provider', async () => {
const fastify = Fastify();
const send = jest.fn();

View File

@@ -28,13 +28,6 @@ const plugin: FastifyPluginCallback<{ provider: MailProvider }> = (
) => {
const { provider } = options;
if (!provider)
return done(
Error(
"The mailer plugin must be passed a provider via register's options."
)
);
fastify.decorate('sendEmail', async (args: SendEmailArgs) => {
const logger = fastify.log.child({ args });
logger.info('Sending Email');

View File

@@ -420,17 +420,17 @@ describe('userRoutes', () => {
test("POST deletes all the user's cookies", async () => {
const res = await superPost('/account/delete');
const setCookie = res.headers['set-cookie'];
const setCookie = res.headers['set-cookie'] as string[];
expect(setCookie).toEqual(
expect.arrayContaining([
expect.stringMatching(
/^jwt_access_token=; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
/^_csrf=; Max-Age=0; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
),
expect.stringMatching(
/^csrf_token=; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
/^csrf_token=; Max-Age=0; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
),
expect.stringMatching(
/^_csrf=; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
/^jwt_access_token=; Max-Age=0; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
)
])
);

View File

@@ -15,13 +15,13 @@ describe('GET /signout', () => {
expect(setCookie).toEqual(
expect.arrayContaining([
expect.stringMatching(
/^jwt_access_token=; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
/^jwt_access_token=; Max-Age=0; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
),
expect.stringMatching(
/^csrf_token=; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
/^csrf_token=; Max-Age=0; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
),
expect.stringMatching(
/^_csrf=; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
/^_csrf=; Max-Age=0; Path=\/; Expires=Thu, 01 Jan 1970 00:00:00 GMT/
)
])
);

View File

@@ -77,7 +77,6 @@ describe('server', () => {
expect(res.headers).toMatchObject({
'cache-control': 'no-store',
'content-security-policy': "frame-ancestors 'none'",
'content-type': 'text/html; charset=utf-8',
'x-content-type-options': 'nosniff',
'x-frame-options': 'DENY'
});