jeremydaly / jeremydaly/lambda-api

Nested register and wildcards sometimes mess up

Open
#179 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.5k
Forks
127
Avg merge
31m
Merged PRs (30d)
2

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/request.js at REQUEST.parseRequest and index.js at API.run, then reproduce the nested register routes shown in the issue. Confirm that the GET route is accepted alongside the DELETE and PUT routes, and add or update the relevant routing test; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.