From c1e4265235097196d6cd57974fd94c412c971da9 Mon Sep 17 00:00:00 2001 From: piyook Date: Tue, 21 May 2024 13:40:31 +0100 Subject: [PATCH] feat(): enable api prefix in path if required --- .env | 3 ++- docker-compose.yml | 1 + package.json | 1 + src/api/posts/api.ts | 8 ++++++-- src/api/users/api.ts | 6 +++++- src/server.ts | 8 +++++--- src/utilities/file-scan.ts | 10 ++++++++-- src/utilities/server-page.ts | 6 +++++- 8 files changed, 33 insertions(+), 10 deletions(-) diff --git a/.env b/.env index 7b4b474..84fedbd 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -SERVER_PORT=8000 \ No newline at end of file +SERVER_PORT=8000 +USE_API_URL_PREFIX=api \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 301904a..cd72368 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,5 +8,6 @@ services: NODE_ENV: development HUSKY: 0 SERVER_PORT: 8000 + USE_API_URL_PREFIX: api ports: - 8000:8000 diff --git a/package.json b/package.json index 4aa0c6b..94d8ff6 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "dev": "tsx watch ./src/server.ts", "start": "docker compose up -d --force-recreate", "stop": "docker compose down --volumes", + "rebuild": "docker compose down --volumes && docker compose up -d --force-recreate --build", "restart": "npm stop && npm start", "nuke": "bash nuke.sh", "prepare": "husky || true", diff --git a/src/api/posts/api.ts b/src/api/posts/api.ts index 99dfc15..952190e 100644 --- a/src/api/posts/api.ts +++ b/src/api/posts/api.ts @@ -2,11 +2,15 @@ import { db } from '../../models/db.js'; // Example of msw data auto REST handler generation function handler(pathName: string) { - return [...db.post.toHandlers('rest')]; + // Need to add a prefix here for automatic REST handler generation to a specific path + const prefix = process.env?.USE_API_URL_PREFIX + ? '/' + process.env.USE_API_URL_PREFIX + : ''; + return [...db.post.toHandlers('rest', prefix)]; } export default handler; -// To test localhost:9090/posts - to see all posts +// To test localhost:9090/api/posts - to see all posts // to add post send POST request to localhost:9090/posts with body {"userId": 101, "title": "new post title", "body": "new post body"} // to check its been added visit localhost:9090/posts/101 diff --git a/src/api/users/api.ts b/src/api/users/api.ts index cdd2053..9bf774e 100644 --- a/src/api/users/api.ts +++ b/src/api/users/api.ts @@ -2,7 +2,11 @@ import { db } from '../../models/db.js'; // Example of msw data auto REST handler generation function handler(pathName: string) { - return [...db.user.toHandlers('rest')]; + // Need to add a prefix here for automatic REST handler generation to a specific path + const prefix = process.env?.USE_API_URL_PREFIX + ? '/' + process.env.USE_API_URL_PREFIX + : ''; + return [...db.user.toHandlers('rest', prefix)]; } export default handler; diff --git a/src/server.ts b/src/server.ts index affa507..60b97ac 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,6 +1,6 @@ import 'dotenv/config'; import { createServer } from '@mswjs/http-middleware'; -import { userSeeder, postSeeder } from './seeders/index.js'; +import * as seeders from './seeders/index.js'; import getApiPaths from './utilities/file-scan.js'; import serverPage from './utilities/server-page.js'; @@ -10,7 +10,9 @@ const httpServer = createServer(...apiHandlers, ...serverPage(apiPaths)); httpServer.listen(process.env.SERVER_PORT); -userSeeder(); -postSeeder(); +// Execute dB seeder functions +for (const seeder of Object.values(seeders)) { + seeder(); +} console.log(`SERVER UP AND RUNNING ON LOCALHOST:${process.env.SERVER_PORT}`); diff --git a/src/utilities/file-scan.ts b/src/utilities/file-scan.ts index c561af8..b1f9941 100644 --- a/src/utilities/file-scan.ts +++ b/src/utilities/file-scan.ts @@ -19,13 +19,17 @@ export default async function getApiPaths() { const files = fs.readdirSync(apiFolder); + const prefix = process.env?.USE_API_URL_PREFIX + ? process.env.USE_API_URL_PREFIX + '/' + : ''; + for (const file of files) { const filePath = path.join(apiFolder, file); const stats = fs.statSync(filePath); if (stats.isDirectory()) { // Get the directory name - console.log(`Api Path: /${file}`); + console.log(`Api Path: ${prefix}${file}`); apiPaths.push(file); // Import index.ts file (assuming it's in each directory) - this returns a promise @@ -46,7 +50,9 @@ export default async function getApiPaths() { .then((handlers) => { for (const [index, handler] of handlers.entries()) { // Add new handlers with the desired apiPath to return array - remember to spread these out as may be more than one - apiHandlers.push(...handler.default(apiPaths[index])); + apiHandlers.push( + ...handler.default(`${prefix}${apiPaths[index]}`), + ); } }) .then(() => { diff --git a/src/utilities/server-page.ts b/src/utilities/server-page.ts index 29d7198..9fcf6a7 100644 --- a/src/utilities/server-page.ts +++ b/src/utilities/server-page.ts @@ -1,6 +1,10 @@ import 'dotenv/config'; import { http, HttpResponse } from 'msw'; +const prefix = process.env?.USE_API_URL_PREFIX + ? process.env.USE_API_URL_PREFIX + '/' + : ''; + const homePage = (apiPaths: string[]) => { const htmlString = ` @@ -8,7 +12,7 @@ const homePage = (apiPaths: string[]) => {

SERVER RUNNING ON localhost:${process.env.SERVER_PORT}

Active Paths:

- ${apiPaths.map((path) => '

/' + path).join('

')} + ${apiPaths.map((path) => '

/' + prefix + path).join('

')}