From 34a08d717560768ac928c195de4b525903a3f8e1 Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Sat, 27 Mar 2021 02:56:21 -0700 Subject: [PATCH] feat: Deep dynamic slug parts for Windows. --- package.json | 2 +- src/index.ts | 33 +++++++++++++------ test/discovery.test.ts | 8 ++--- .../[path]}/(dynamic).ts | 0 .../[path]/[required].ts} | 0 5 files changed, 28 insertions(+), 15 deletions(-) rename test/fixtures/discovery/{required-params/win => windows-optional/[path]}/(dynamic).ts (100%) rename test/fixtures/discovery/{optional-params/win/[dynamic].ts => windows-required/[path]/[required].ts} (100%) diff --git a/package.json b/package.json index d8ea0f9..d0e28a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loll", - "version": "0.2.0", + "version": "0.2.1", "description": "REST apps for the lazy developer.", "main": "dist/src/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index 8381d38..7d237e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -132,16 +132,29 @@ function discoverAPI(router: Express.Router, apiDir: string){ // Construct both the absolute file path, and public facing API path const filePath = path.join(root, fileStats.name); - let apiPath = fileStats.name; - if (apiPath.startsWith('(') && apiPath.endsWith(').js')) { - apiPath = `:${apiPath.slice(1, -4)}.js`; - } - - if (apiPath.startsWith('[') && apiPath.endsWith('].js')) { - apiPath = `:${apiPath.slice(1, -4)}?.js`; - } - - apiPath = path.join(root, apiPath).replace(apiDir, '').replace(/\/index.js$/, '').replace(/.js$/, ''); + console.log(filePath); + let apiPath = path.join(root, fileStats.name).split(path.sep).map((part) => { + console.log(part) + if (part.startsWith('[') && part.endsWith(']')) { + part = `:${part.slice(1, -1)}`; + } + + if (part.startsWith('(') && part.endsWith(')')) { + part = `:${part.slice(1, -1)}?`; + } + + if (part.startsWith('[') && part.endsWith('].js')) { + part = `:${part.slice(1, -4)}.js`; + } + + if (part.startsWith('(') && part.endsWith(').js')) { + part = `:${part.slice(1, -4)}?.js`; + } + + return part; + }).join(path.sep); + console.log(apiPath); + apiPath = apiPath.replace(apiDir, '').replace(/\/index.js$/, '').replace(/.js$/, ''); // Push them to our queue. This later sorted in order of route precedence. queue.push({ apiPath, filePath }); diff --git a/test/discovery.test.ts b/test/discovery.test.ts index 7ae3ad4..b994d0f 100644 --- a/test/discovery.test.ts +++ b/test/discovery.test.ts @@ -84,13 +84,13 @@ describe('API Discovery', function() { it('work with named required params – windows', async function() { - await GET('/api/required-params/win/what', { dynamic: 'what' }); - await GET('/api/required-params', ERROR_RESPONSE); + await GET('/api/windows-required/win/what', { required: 'what', path: 'win' }); + await GET('/api/windows-required', ERROR_RESPONSE); }); it('work with named optional params – windows', async function() { - await GET('/api/optional-params/win/what', { dynamic: 'what' }); - await GET('/api/optional-params', {}); + await GET('/api/windows-optional/win/what', { dynamic: 'what', path: 'win' }); + await GET('/api/windows-optional/win', { path: 'win' }); }); }); diff --git a/test/fixtures/discovery/required-params/win/(dynamic).ts b/test/fixtures/discovery/windows-optional/[path]/(dynamic).ts similarity index 100% rename from test/fixtures/discovery/required-params/win/(dynamic).ts rename to test/fixtures/discovery/windows-optional/[path]/(dynamic).ts diff --git a/test/fixtures/discovery/optional-params/win/[dynamic].ts b/test/fixtures/discovery/windows-required/[path]/[required].ts similarity index 100% rename from test/fixtures/discovery/optional-params/win/[dynamic].ts rename to test/fixtures/discovery/windows-required/[path]/[required].ts