Skip to content

Commit

Permalink
Add additional HTTP methods to router DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Lindsey Smith committed Nov 21, 2016
1 parent fdb72e0 commit 036c4bb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
24 changes: 22 additions & 2 deletions packages/starspot-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,32 @@ namespace Router {
router.addRoute("OPTIONS", path, controller, "cors");
// OPTIONS /photos/1234 -> PhotosController.cors()
router.addRoute("OPTIONS", memberPath, controller, "cors");
}
};

get = (path: string, { controller, method }: RouteOptions): void => {
let router = this._router;
router.addRoute("GET", path, controller, method);
}
};

post = (path: string, { controller, method }: RouteOptions): void => {
let router = this._router;
router.addRoute("POST", path, controller, method);
};

patch = (path: string, { controller, method }: RouteOptions): void => {
let router = this._router;
router.addRoute("PATCH", path, controller, method);
};

delete = (path: string, { controller, method }: RouteOptions): void => {
let router = this._router;
router.addRoute("DELETE", path, controller, method);
};

options = (path: string, { controller, method }: RouteOptions): void => {
let router = this._router;
router.addRoute("OPTIONS", path, controller, method);
};
}
}

Expand Down
24 changes: 23 additions & 1 deletion packages/starspot-core/test/router-test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { expect } from "chai";
import { Router } from "../src/";
import { HTTPVerb } from "../src/router";

describe("Router", () => {

describe("mapping", () => {

it("supports simple routes", () => {
["GET", "POST", "PATCH", "DELETE", "OPTIONS"].forEach((method: HTTPVerb) => {
it(`supports ${method} routes`, () => {
class MyRouter extends Router {
map(dsl: Router.DSL) {
dsl[method.toLowerCase()]("my-route", {controller: "controller", method: "method"});
}
}

let router = new MyRouter();
router.seal();

expect(router.handlersFor(method, "my-route").length).to.equal(1);
});
});

it("supports resources", () => {
class MyRouter extends Router {
map({ resources }: Router.DSL) {
resources("my-route");
Expand All @@ -16,6 +32,12 @@ describe("Router", () => {
router.seal();

expect(router.handlersFor("GET", "my-route").length).to.equal(1);
expect(router.handlersFor("POST", "my-route").length).to.equal(1);
expect(router.handlersFor("OPTIONS", "my-route").length).to.equal(1);
expect(router.handlersFor("GET", "my-route/:id").length).to.equal(1);
expect(router.handlersFor("PATCH", "my-route/:id").length).to.equal(1);
expect(router.handlersFor("DELETE", "my-route/:id").length).to.equal(1);
expect(router.handlersFor("OPTIONS", "my-route/:id").length).to.equal(1);
});

});
Expand Down

0 comments on commit 036c4bb

Please sign in to comment.