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

Nested register and wildcards sometimes mess up #179

Open
slootjes opened this issue Mar 27, 2021 · 0 comments
Open

Nested register and wildcards sometimes mess up #179

slootjes opened this issue Mar 27, 2021 · 0 comments

Comments

@slootjes
Copy link

I'm using register() on multiple depths, to illustrate it's something like this:

api.js:

const api = require('lambda-api')({
    base: '/api'
});

api.register(require('./api/user'), { prefix: '/me' });

api/user.js:

module.exports = (api, opts) => {
    api.register(require('./user/app'), { prefix: '/apps' });

api/user/app.js:

module.exports = (api, opts) => {

    api.get(':app/data', async (req, res) => {
        // code
    });

    api.get(':app/data/:scope', async (req, res) => {
        // code
    });

    api.get(':app/data/:scope/:key', async (req, res) => {
        // code
    });

    api.delete(':app/data/user/:key', async (req, res) => {
        // code
    });

    api.put(':app/data', async (req, res) => {
        // code
    });

    api.put(':app/data/user/:key/:value', async (req, res) => {
        // code
    });
}

When I do a GET '/api/me/apps/myapp/data/user' which should match the route ':app/data/:scope/:key' however it generated this error:

MethodError: Method not allowed
at REQUEST.parseRequest (/var/task/node_modules/lambda-api/lib/request.js:247:13)
at API.run (/var/task/node_modules/lambda-api/index.js:220:21)
at Runtime.module.exports.handle [as handler] (/var/task/functions/controller/api.js:17:22)
at Runtime.handleOnce (/var/runtime/Runtime.js:66:25) {
method: 'GET',
path: '/api/me/apps/myapp/data/user'
}

When rendering a full list of routes, this is returned:

╟──────────┼─────────────────────────────────────────────────────────────────────────╢
║  PUT     │  /api/me/apps/:app/data                                                 ║
╟──────────┼─────────────────────────────────────────────────────────────────────────╢
║  GET     │  /api/me/apps/:app/data                                                 ║
╟──────────┼─────────────────────────────────────────────────────────────────────────╢
║  DELETE  │  /api/me/apps/:app/data/user/:key                                       ║
╟──────────┼─────────────────────────────────────────────────────────────────────────╢
║  PUT     │  /api/me/apps/:app/data/user/:key/:value                                ║
╟──────────┼─────────────────────────────────────────────────────────────────────────╢
║  GET     │  /api/me/apps/:app/data/:scope                                          ║
╟──────────┼─────────────────────────────────────────────────────────────────────────╢
║  GET     │  /api/me/apps/:app/data/:scope/:key                                     ║
╟──────────┼─────────────────────────────────────────────────────────────────────────╢

So I'd say it should work just fine however it doesn't. However when I remove the "DELETE" and "PUT" methods from code, the GET route starts working. So it seems like the matching doesn't fully work when using nested routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant