mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-04 18:05:32 -05:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import type { FastifyPluginCallback } from 'fastify';
|
|
|
|
import fp from 'fastify-plugin';
|
|
|
|
import { getRedirectParams } from '../utils/redirection';
|
|
|
|
/**
|
|
* Plugin for handling missing endpoints.
|
|
*
|
|
* @param fastify The Fastify instance.
|
|
* @param _options Options passed to the plugin via `fastify.register(plugin, options)`.
|
|
* @param done Callback to signal that the logic has completed.
|
|
*/
|
|
const fourOhFour: FastifyPluginCallback = (fastify, _options, done) => {
|
|
// If the request accepts JSON and does not specifically prefer text/html,
|
|
// this will return a 404 JSON response. Everything else will be redirected.
|
|
fastify.setNotFoundHandler((req, reply) => {
|
|
const logger = fastify.log.child({ req });
|
|
logger.info('User requested path that does not exist');
|
|
|
|
const accepted = req.accepts().type(['json', 'html']);
|
|
if (accepted == 'json') {
|
|
void reply.code(404).send({ error: 'path not found' });
|
|
} else {
|
|
const { origin } = getRedirectParams(req);
|
|
void reply.status(302);
|
|
void reply.redirectWithMessage(`${origin}/404`, {
|
|
type: 'danger',
|
|
content: `We couldn't find path ${req.url}`
|
|
});
|
|
}
|
|
});
|
|
done();
|
|
};
|
|
|
|
export default fp(fourOhFour, {
|
|
dependencies: ['redirect-with-message', '@fastify/accepts']
|
|
});
|