Skip to content

Commit

Permalink
feat: Deep dynamic slug parts for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
amiller-gh committed Mar 27, 2021
1 parent a33b630 commit 34a08d7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
33 changes: 23 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
8 changes: 4 additions & 4 deletions test/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});

});
Expand Down

0 comments on commit 34a08d7

Please sign in to comment.