Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for extra attributes in route handlers #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dist/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ const methodFn = method => (givenPath, handler) => {
if (!givenPath) throw new Error('You need to set a valid path');
if (!handler) throw new Error('You need to set a valid handler');

return (req, res, namespace) => {
return (req, res, namespace, ...args) => {
const path = givenPath === '/' ? '(/)' : givenPath;
const route = isPattern(path) ? path : new UrlPattern(`${namespace}${path}`, patternOpts);

const { params, query } = getParamsAndQuery(route, req.url);

if (params && req.method === method) {
return handler(Object.assign(req, { params, query }), res);
return handler(Object.assign(req, { params, query }), res, ...args);
}
};
};

const findRoute = (funcs, namespace = '') => (() => {
var _ref = _asyncToGenerator(function* (req, res) {
var _ref = _asyncToGenerator(function* (req, res, ...args) {
for (const fn of funcs) {
const result = yield fn(req, res, namespace);
const result = yield fn(req, res, namespace, ...args);
if (result || res.headersSent) return result;
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const methodFn = method => (givenPath, handler) => {
if (!givenPath) throw new Error('You need to set a valid path')
if (!handler) throw new Error('You need to set a valid handler')

return (req, res, namespace) => {
return (req, res, namespace, ...args) => {
const path = givenPath === '/' ? '(/)' : givenPath
const route = isPattern(path)
? path
Expand All @@ -16,14 +16,14 @@ const methodFn = method => (givenPath, handler) => {
const { params, query } = getParamsAndQuery(route, req.url)

if (params && req.method === method) {
return handler(Object.assign(req, { params, query }), res)
return handler(Object.assign(req, { params, query }), res, ...args)
}
}
}

const findRoute = (funcs, namespace = '') => async (req, res) => {
const findRoute = (funcs, namespace = '') => async (req, res, ...args) => {
for (const fn of funcs) {
const result = await fn(req, res, namespace)
const result = await fn(req, res, namespace, ...args)
if (result || res.headersSent) return result
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,15 @@ test('route with namespace with trailing slash', async t => {

t.is(fooResponse, 'foo')
})

test('route with extra arguments', async t => {
const serverWithSetup = fn =>
listen(micro((req, res) => fn(req, res, 'extra')))

const routes = router(get('/', (req, res, message) => ({ message })))

const url = await serverWithSetup(routes)
const response = await request(`${url}`)

t.is(JSON.parse(response).message, 'extra')
})