Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

What is the best way to handle API versioning using this template?

You must be logged in to vote

Replies: 1 comment

Comment options

I implemented basic ability for this in my main route handler (unfortunately it means all swagger docs reference all routes at this time)

import { Router } from 'express';
import fs from 'fs/promises';
import path from 'path';
import { pathToFileURL } from 'url';
import { logger } from '@/common/utils/logger';

const router = Router();
const ignore = ['.DS_Store', 'index.ts'];

async function loadRoutes() {
  const files = await fs.readdir(path.join(__dirname));

  for (const file of files) {
    if (!ignore.includes(file)) {
      const filePath = path.join(__dirname, file);
      const fileURL = pathToFileURL(filePath).href;

      const routeName = file.split('.')[0];
      const routeVersion = file.split('.')[1];

      const mod = await import(fileURL);
      if (mod.default) {
        logger.info(`Added ${routeVersion}/${routeName}`);
        router.use(`/${routeVersion}/${routeName}`, mod.default);
      }
    }
  }
}

(async () => {
  await loadRoutes();
})();

export default router;

Then each route is routepath.version.ts
so, 'latest/docs' would be in docs.latest.ts

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.