Web API for code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

45 lines
1.1 KiB

  1. import fp from 'fastify-plugin';
  2. import {FastifyInstance} from 'fastify';
  3. declare module 'fastify' {
  4. interface FastifyInstance {
  5. allStaticRoutes: {
  6. method: string,
  7. url: string,
  8. }[]
  9. }
  10. }
  11. const fastifyHomeRoute = async (app: FastifyInstance) => {
  12. app.decorateRequest('allStaticRoutes', null);
  13. app.addHook('onRoute', (routeData) => {
  14. if (routeData.url.includes('/:')) {
  15. return;
  16. }
  17. const mutableServer = app as unknown as Record<string, unknown>;
  18. mutableServer['allStaticRoutes'] = !Array.isArray(mutableServer['allStaticRoutes'])
  19. ? [
  20. {
  21. method: routeData.method,
  22. url: routeData.url,
  23. }
  24. ]
  25. : [
  26. ...mutableServer['allStaticRoutes'],
  27. {
  28. method: routeData.method,
  29. url: routeData.url,
  30. },
  31. ];
  32. });
  33. app.addHook('onReady', () => {
  34. const mutableServer = app as unknown as Record<string, unknown>;
  35. if (!mutableServer['allStaticRoutes']) {
  36. mutableServer['allStaticRoutes'] = [];
  37. }
  38. })
  39. }
  40. export default fp(fastifyHomeRoute);