Skip to content

Commit

Permalink
feat: Windows support.
Browse files Browse the repository at this point in the history
  • Loading branch information
amiller-gh committed Mar 27, 2021
1 parent b05d0f6 commit a33b630
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 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.1.0",
"version": "0.2.0",
"description": "REST apps for the lazy developer.",
"main": "dist/src/index.js",
"scripts": {
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,17 @@ function discoverAPI(router: Express.Router, apiDir: string){
if(fileStats.name[0] === '.' || fileStats.name[0] === '_' || !fileStats.name.endsWith('.js')) return next();

// Construct both the absolute file path, and public facing API path
var filePath = path.join(root, fileStats.name),
apiPath = filePath.replace(apiDir, '').replace(/\/index.js$/, '').replace(/.js$/, '');
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$/, '');

// Push them to our queue. This later sorted in order of route precedence.
queue.push({ apiPath, filePath });
Expand Down
17 changes: 16 additions & 1 deletion test/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ const api = loll(express, {
app.use('/api', api);

// Start Server
http.createServer(app).listen(PORT, function(){
const server = http.createServer(app).listen(PORT, function(){
console.log(('Express server listening on port ' + app.get('port')));
});

describe('API Discovery', function() {
describe('it should', function() {

this.afterAll(() => {
server.close();
})

it('work with default exports', async function() {
await GET('/api', { status: 'success', data: false });
});
Expand Down Expand Up @@ -78,5 +82,16 @@ describe('API Discovery', function() {
await GET('/api/optional-params', {});
});


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);
});

it('work with named optional params – windows', async function() {
await GET('/api/optional-params/win/what', { dynamic: 'what' });
await GET('/api/optional-params', {});
});

});
});
5 changes: 5 additions & 0 deletions test/fixtures/discovery/optional-params/win/[dynamic].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Express from 'express';

export function GET(req: Express.Request) {
return req.params;
}
5 changes: 5 additions & 0 deletions test/fixtures/discovery/required-params/win/(dynamic).ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Express from 'express';

export function GET(req: Express.Request) {
return req.params;
}
6 changes: 5 additions & 1 deletion test/sideways.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ const api = loll(express, {
app.use('/api', api);

// Start Server
http.createServer(app).listen(PORT, function(){
const server = http.createServer(app).listen(PORT, function(){
console.log(('Express server listening on port ' + app.get('port')));
});

describe('API Discovery', function() {
describe('it should', function() {

this.afterAll(() => {
server.close();
})

it('works at root', async function() {
await GET('/api', { status: 'success', data: 'ok' });
});
Expand Down

0 comments on commit a33b630

Please sign in to comment.