unjs / h3

⚡️ Minimal H(TTP) framework built for high performance and portability
https://h3.unjs.io/
MIT License
3.66k stars 215 forks source link

route params dont seem to work for PUT or DELETE #864

Open acidjazz opened 2 months ago

acidjazz commented 2 months ago

Environment

in /server/api/[...slug].ts i have

const router = createRouter()
router.put('/user/:user/pen/:pen', update)

then my handler function has the following.

const update = defineEventHandler(async (event) => {
  console.log(event.context.params)
....

which ends up showing:

{ _: 'user/1/pen/1' }

Reproduction

ill whip up a stackblitz asap and report back

Describe the bug

i should see context.params.id and context.params.user populated

Additional context

No response

Logs

No response

acidjazz commented 2 months ago

confirmed for DELETE as well

pi0 commented 2 months ago

Thanks, please ping me when made reproduction but i would guess issue is from old radix3 behavior. If you can try against h3-nightly (v2) that would be amazing.

acidjazz commented 2 months ago

https://stackblitz.com/edit/github-zdmzwb?file=server%2Fapi%2F%5B...slug%5D.ts

I found that this was because I did not prepend the route with a /

import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();

router.get(
  '/**',
  defineEventHandler((event) => 'not found')
);

router.put(
  'user/:user/pen/:id',
  defineEventHandler((event) => {
    return {
      parms: event.context.params,
    };
  })
);

export default useBase('/api/', router.handler);

returns :

data : {
  "parms": {
    "_": "user/1/pen/2"
  }
} 

while adding the slash:

import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();

router.get(
  '/**',
  defineEventHandler((event) => 'not found')
);

router.put(
  '/user/:user/pen/:id',
  defineEventHandler((event) => {
    return {
      parms: event.context.params,
    };
  })
);

export default useBase('/api/', router.handler);

works fine:

data : {
  "parms": {
    "user": "1",
    "id": "2"
  }
} 

i'm not sure if this is expected behavior, if it is im happy to close this