#API Routing broken

8 messages · Page 1 of 1 (latest)

cosmic fable
#

I've seen reports before and I couldn't believe it... and now I'm here.

Nitro-Routes:

    '/api/projects/:projectId/os/:resourceType': {
      'get': Simplify<Serialize<Awaited<ReturnType<typeof import('../../server/api/projects/[projectId]/os/[resourceType]/index.get').default>>>>
      'post': Simplify<Serialize<Awaited<ReturnType<typeof import('../../server/api/projects/[projectId]/os/[resourceType]/index.post').default>>>>
    }
    '/api/projects/:projectId/os/networks': {
      'post': Simplify<Serialize<Awaited<ReturnType<typeof import('../../server/api/projects/[projectId]/os/networks/index.post').default>>>>
    }```

2 concurrent routes: one for `:id/os/:resourceType` (generic handler) and one for `:id/os/networks` (edge case handling). Latter only has a post-handler!

Code executed (server side): ```ts
  const flowApi = $fetch.create({ headers: event.headers, query: queryParams });

  // Get networks
  const networks = await flowApi<Network[]>(
    `/api/projects/${routeParams.id}/os/networks`
  );
  console.log("Got networks: ", networks);```

This is a get-Request.

The edge case handler does not get called, that's correct.
The generic get handler gets called - and suddenly the `resourceType` no longer exists in the event.

It should look like (after removing the edge case handler): ```ts
    matchedRoute: {
      path: '/api/projects/:projectId/os/:resourceType',
      handlers: [Object],
      params: [Object]
    },
    params: {
      projectId: '05caa06f-ac5a-4ab0-99f4-98fe9380ac99',
      resourceType: 'networks'
    }```

It really looks like (with edge handler): ```ts
    matchedRoute: {
      path: '/api/projects/:projectId/os/networks',
      handlers: [Object],
      params: [Object]
    },
    params: { projectId: '05caa06f-ac5a-4ab0-99f4-98fe9380ac99' }```

The matchedRoute points to the correct path. But if it would be really using it, it must fail because there's no get-handler for that path.
cosmic fable
#
    matchedRoute: {
      path: '/api/projects/:projectId/os/networks',
      handlers: {
        post: [Function (anonymous)] {
          __is_handler__: true,
          __resolve__: [Function: resolveHandler]
        },
        get: [Function (anonymous)] {
          __is_handler__: true,
          __resolve__: [Function: resolveHandler]
        }
      },
      params: { projectId: '05caa06f-ac5a-4ab0-99f4-98fe9380ac99' }
    }``` The get-entry in that handler object is wrong.
cosmic fable
#

@shadow sluice Good morning. Before I spend time creating a repro for a bugreport... is this known?

shadow sluice
#

I think the post/get are not included in the routing - so basically the route is matched first and then the method is evaluated.

cosmic fable
#

so... by design? Not a bug?

shadow sluice
#

I think it's definitely worth a discussion.

#

I think it's a limitation of how routing is implemented in nitro using radix3 (a consequence of speed/perf decisions) - but I know pooya is rethinking things for a new version of radix3, and this might be on his list

cosmic fable
#

Alright, I'll open up an issue in github, with repro. The current implementation ... hurts. Badly.