Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Add ability to drop namespace for a single Route
Browse files Browse the repository at this point in the history
  • Loading branch information
onechiporenko committed Aug 8, 2018
1 parent 6137789 commit 2b141da
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
21 changes: 13 additions & 8 deletions lib/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as express from 'express';
import {Lair} from 'lair-db';
import {CRUDOptions} from 'lair-db/dist/lair';
import { Lair } from 'lair-db';
import { CRUDOptions } from 'lair-db/dist/lair';
import methods = require('methods');
import {assert} from './utils';
import { assert } from './utils';

function defaultNext(req: express.Request, res: express.Response, data: any) {
return res.json(data);
Expand All @@ -26,7 +26,7 @@ export default class Route {
}

public static get(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext = defaultNext): Route {
const handler = (req, res, next, lair) => {
const handler = (req: express.Request, res: express.Response, next: express.NextFunction, lair: Lair) => {
const parameters = Object.keys(req.params);
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined;
const result = id ? lair.getOne(modelName, id, lairOptions) : lair.getAll(modelName, lairOptions);
Expand All @@ -36,14 +36,14 @@ export default class Route {
}

public static post(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext = defaultNext): Route {
return Route.createRoute('post', path, (req, res, next, lair) => {
return Route.createRoute('post', path, (req: express.Request, res: express.Response, next: express.NextFunction, lair: Lair) => {
const result = lair.createOne(modelName, req.body, lairOptions);
return customNext(req, res, result, lair);
});
}

public static put(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext = defaultNext): Route {
return Route.createRoute('put', path, (req, res, next, lair) => {
return Route.createRoute('put', path, (req: express.Request, res: express.Response, next: express.NextFunction, lair: Lair) => {
const parameters = Object.keys(req.params);
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined;
assert('identifier is not provided', !!id);
Expand All @@ -53,7 +53,7 @@ export default class Route {
}

public static patch(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext = defaultNext): Route {
return Route.createRoute('patch', path, (req, res, next, lair) => {
return Route.createRoute('patch', path, (req: express.Request, res: express.Response, next: express.NextFunction, lair: Lair) => {
const parameters = Object.keys(req.params);
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined;
assert('identifier is not provided', !!id);
Expand All @@ -63,7 +63,7 @@ export default class Route {
}

public static delete(path: string, modelName: string, customNext: CustomNext = defaultNext): Route {
const handler = (req, res, next, lair) => {
const handler = (req: express.Request, res: express.Response, next: express.NextFunction, lair: Lair) => {
const parameters = Object.keys(req.params);
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined;
assert('identifier is not provided', !!id);
Expand All @@ -75,5 +75,10 @@ export default class Route {

public handler: Handler;
public method;
/**
* Used to override `server.namespace` for current Route
* @type {string?}
*/
public namespace = null;
public path;
}
14 changes: 12 additions & 2 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as express from 'express';
import read = require('fs-readdir-recursive');
import * as http from 'http';
import {Factory, Lair} from 'lair-db';
import nPath = require('path');
import winston = require('winston');
import {printRoutesMap} from './express';
import Route from './route';
Expand Down Expand Up @@ -75,8 +76,17 @@ export default class Server {
}

public addRoute(route: Route) {
this.expressRouter[route.method](
route.path,
let source;
let path;
if (route.namespace === null) {
source = this.expressRouter;
path = route.path;
} else {
source = this.expressApp;
path = nPath.join(route.namespace, route.path);
}
source[route.method](
path,
(req, res, next) =>
route.handler.call(this.expressRouter, req, res, next, this.lair));
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swarm-host",
"version": "1.2.3",
"version": "1.3.0",
"description": "fake-server",
"main": "dist/index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions tests-data/test-routes/reset-namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Route from '../../lib/route';

const route = Route.createRoute('get', '/reset-namespace');
route.namespace = '';

export default route;
9 changes: 9 additions & 0 deletions tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ describe('#Server integration', () => {
done();
}));
});

it('should add a second route', done => {
this.server.startServer(() => chai.request(this.server.server)
.get('/reset-namespace')
.end((err, res) => {
expect(res).to.have.property('status', 200);
done();
}));
});
});

describe('#createRecords', () => {
Expand Down

0 comments on commit 2b141da

Please sign in to comment.