koa-router-rx
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -74,37 +74,39 @@ 'use strict'; | ||
function RxRouter(options) { | ||
const registry = {}; | ||
class RxRouter extends _koaRouter2.default { | ||
const resolveRouteForMethod = method => router => (path, epic) => { | ||
if (path in registry) { | ||
return registry[path].subscribeEpicResponse(method, epic); | ||
constructor(...args) { | ||
super(...args); | ||
this.registry = {}; | ||
} | ||
registerEpic(method, path, epic) { | ||
if (path in this.registry) { | ||
return this.registry[path].subscribeEpicResponse(method, epic); | ||
} else { | ||
registry[path] = new Route(router, path); | ||
return registry[path].subscribeEpicResponse(method, epic); | ||
this.registry[path] = new Route(this, path); | ||
return this.registry[path].subscribeEpicResponse(method, epic); | ||
} | ||
}; | ||
} | ||
const methods = { | ||
get: resolveRouteForMethod('GET'), | ||
post: resolveRouteForMethod('POST'), | ||
put: resolveRouteForMethod('PUT'), | ||
del: resolveRouteForMethod('DELETE'), | ||
delete: resolveRouteForMethod('DELETE') | ||
}; | ||
get(path, epic) { | ||
return this.registerEpic('GET', path, epic); | ||
} | ||
return new Proxy(new _koaRouter2.default(), { | ||
get: function (target, property, receiver) { | ||
if (property in methods) { | ||
return methods[property](target); | ||
} else { | ||
return target[property]; | ||
} | ||
} | ||
}); | ||
post(path, epic) { | ||
return this.registerEpic('POST', path, epic); | ||
} | ||
put(path, epic) { | ||
return this.registerEpic('PUT', path, epic); | ||
} | ||
del(path, epic) { | ||
return this.registerEpic('DELETE', path, epic); | ||
} | ||
delete(path, epic) { | ||
return this.del(path, epic); | ||
} | ||
} | ||
exports.default = new Proxy(_koaRouter2.default, { | ||
construct: (target, argumentsList, newTarget) => RxRouter(...argumentsList), | ||
apply: (target, thisArg, argumentsList) => RxRouter(...argumentsList) | ||
}); | ||
exports.default = RxRouter; | ||
module.exports = exports['default']; |
{ | ||
"name": "koa-router-rx", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Reactive router middleware for koa", | ||
@@ -29,2 +29,3 @@ "main": "dist/index.js", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"flow-gen-files": "flow-copy-source -v lib dist", | ||
"flow-typed-install": "flow-typed install" | ||
@@ -45,2 +46,3 @@ }, | ||
"flow-bin": "0.36.0", | ||
"flow-copy-source": "1.1.0", | ||
"flow-typed": "2.0.0", | ||
@@ -47,0 +49,0 @@ "jest": "17.0.3", |
10194
5
14
88