#creating auto suggestion and type hint for nest API routes for front-end

1 messages · Page 1 of 1 (latest)

lone trench
#

i recently used Nuxt and i like its auto suggestion for API routes and type hint of API return value so i was trying to make something similar

i was able to create auto suggestion for API route for frontend
now how to create type hint of routes ? any suggestion and trick any method or

all of this alreay created and mention it plz..

#
async function generateRoutesTypeFile(app: INestApplication) {
  const server = app.getHttpServer();
  const router = server._events.request._router;

  const routes: string[] = router.stack
    .map((layer: any) => {
      if (layer.route) {
        return layer.route?.path;
      }
    })
    .filter((item: any) => item !== undefined);

  const typeDefinition = `
    declare type ApiRoutes =
      ${routes.map((route) => `| '${route}'`).join('\n')}
      ;
  `;

  const dirPath = join(__dirname, '..', '..', 'front-end', 'types');
  const filePath = join(dirPath, 'routes.d.ts');

  // Ensure the directory exists
  if (!existsSync(dirPath)) {
    mkdirSync(dirPath, { recursive: true });
  }

  writeFileSync(filePath, typeDefinition);
}
lone trench
#

oh! and not using swagger

lone trench
#

does swagger provide if data type is array of string or number ? because i am only getting "array" for [string]