Registry Mirror
Skip to content

File tree

‎apps/content/docs/plugins/method-override.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ sidebar:
77

88
## How It Works
99

10-
HTML forms only support the GET and POST methods. When a POST request carries a `method` query parameter with an allowed value, the plugin routes the request as if it used that method and removes the parameter before input decoding. Values outside the allowed list are ignored and the request is processed as a regular POST.
10+
HTML forms only support the GET and POST methods. When a POST request carries a `_method` query parameter with an allowed value, the plugin routes the request as if it used that method and removes the parameter before input decoding. Values outside the allowed list are ignored and the request is processed as a regular POST.
1111

1212
```html
13-
<form method="post" action="/api/todos/1?method=DELETE">
13+
<form method="post" action="/api/todos/1?_method=DELETE">
1414
<button>Delete todo</button>
1515
</form>
1616
```
@@ -27,9 +27,9 @@ const handler = new OpenAPIHandler(router, {
2727
/**
2828
* The query parameter carrying the override method.
2929
*
30-
* @default 'method'
30+
* @default '_method'
3131
*/
32-
param: 'method',
32+
param: '_method',
3333

3434
/**
3535
* The methods a POST request may be overridden to.

‎packages/server/src/plugins/method-override.test.ts‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('methodOverrideHandlerPlugin', () => {
4949
it('ignores non-POST requests', async () => {
5050
const { result, next, nextResult } = await invokeInterceptor({
5151
method: 'GET',
52-
url: '/todos/1?method=DELETE',
52+
url: '/todos/1?_method=DELETE',
5353
})
5454

5555
expect(next).toHaveBeenCalledExactlyOnceWith()
@@ -69,7 +69,7 @@ describe('methodOverrideHandlerPlugin', () => {
6969
it('overrides the method and strips the param', async () => {
7070
const { result, next, nextResult } = await invokeInterceptor({
7171
method: 'POST',
72-
url: '/todos/1?method=DELETE',
72+
url: '/todos/1?_method=DELETE',
7373
})
7474

7575
expect(next).toHaveBeenCalledExactlyOnceWith(
@@ -81,7 +81,7 @@ describe('methodOverrideHandlerPlugin', () => {
8181
})
8282

8383
it('matches the override value case-insensitively', async () => {
84-
const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=delete' })
84+
const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?_method=delete' })
8585

8686
expect(next).toHaveBeenCalledExactlyOnceWith(
8787
expect.objectContaining({
@@ -93,7 +93,7 @@ describe('methodOverrideHandlerPlugin', () => {
9393
it('uses the last occurrence when the param is repeated and strips all of them', async () => {
9494
const { next } = await invokeInterceptor({
9595
method: 'POST',
96-
url: '/todos/1?method=PUT&method=DELETE',
96+
url: '/todos/1?_method=PUT&_method=DELETE',
9797
})
9898

9999
expect(next).toHaveBeenCalledExactlyOnceWith(
@@ -104,7 +104,7 @@ describe('methodOverrideHandlerPlugin', () => {
104104
})
105105

106106
it('preserves other query params and the hash', async () => {
107-
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?x=1&method=DELETE&y=2#h' })
107+
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?x=1&_method=DELETE&y=2#h' })
108108

109109
expect(next).toHaveBeenCalledExactlyOnceWith(
110110
expect.objectContaining({
@@ -118,7 +118,7 @@ describe('methodOverrideHandlerPlugin', () => {
118118
async (method) => {
119119
const { result, next, nextResult } = await invokeInterceptor({
120120
method: 'POST',
121-
url: `/todos/1?method=${method}`,
121+
url: `/todos/1?_method=${method}`,
122122
})
123123

124124
expect(next).toHaveBeenCalledExactlyOnceWith()
@@ -128,20 +128,56 @@ describe('methodOverrideHandlerPlugin', () => {
128128

129129
it('honors a custom param name', async () => {
130130
const { next } = await invokeInterceptor(
131-
{ method: 'POST', url: '/todos/1?_method=DELETE&method=PUT' },
132-
{ param: '_method' },
131+
{ method: 'POST', url: '/todos/1?method=DELETE&_method=PUT' },
132+
{ param: 'method' },
133133
)
134134

135135
expect(next).toHaveBeenCalledExactlyOnceWith(
136136
expect.objectContaining({
137-
request: expect.objectContaining({ method: 'DELETE', url: '/todos/1?method=PUT' }),
137+
request: expect.objectContaining({ method: 'DELETE', url: '/todos/1?_method=PUT' }),
138+
}),
139+
)
140+
})
141+
142+
it('does not re-encode the remaining query string', async () => {
143+
const { next } = await invokeInterceptor({
144+
method: 'POST',
145+
url: '/a?q=a%20b+c&_method=DELETE&redirect=/x?y=1,2&e=%7E*&flag&&k=&?_method=keep#h',
146+
})
147+
148+
expect(next).toHaveBeenCalledExactlyOnceWith(
149+
expect.objectContaining({
150+
request: expect.objectContaining({
151+
method: 'DELETE',
152+
url: '/a?q=a%20b+c&redirect=/x?y=1,2&e=%7E*&flag&&k=&?_method=keep#h',
153+
}),
154+
}),
155+
)
156+
})
157+
158+
it('keeps a remaining empty query component', async () => {
159+
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?_method=DELETE&' })
160+
161+
expect(next).toHaveBeenCalledExactlyOnceWith(
162+
expect.objectContaining({
163+
request: expect.objectContaining({ method: 'DELETE', url: '/a?' }),
164+
}),
165+
)
166+
})
167+
168+
it('matches an encoded param name', async () => {
169+
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?%5Fmethod=DELETE&x=1' })
170+
171+
expect(next).toHaveBeenCalledExactlyOnceWith(
172+
expect.objectContaining({
173+
request: expect.objectContaining({ method: 'DELETE', url: '/a?x=1' }),
138174
}),
139175
)
140176
})
141177

142178
it('honors custom allowed methods', async () => {
143179
const { next } = await invokeInterceptor(
144-
{ method: 'POST', url: '/todos/1?method=GET' },
180+
{ method: 'POST', url: '/todos/1?_method=GET' },
145181
{ methods: ['get'] },
146182
)
147183

@@ -152,7 +188,7 @@ describe('methodOverrideHandlerPlugin', () => {
152188
)
153189

154190
const { next: next2 } = await invokeInterceptor(
155-
{ method: 'POST', url: '/todos/1?method=DELETE' },
191+
{ method: 'POST', url: '/todos/1?_method=DELETE' },
156192
{ methods: ['GET'] },
157193
)
158194

@@ -171,7 +207,7 @@ describe('methodOverrideHandlerPlugin', () => {
171207
)
172208

173209
const { matched, response } = await handler.handle(
174-
new Request('https://example.com/ping?method=DELETE', {
210+
new Request('https://example.com/ping?_method=DELETE', {
175211
method: 'POST',
176212
headers: {
177213
'content-type': 'application/json',

‎packages/server/src/plugins/method-override.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface MethodOverrideHandlerPluginOptions {
1313
/**
1414
* The query parameter carrying the override method.
1515
*
16-
* @default 'method'
16+
* @default '_method'
1717
*/
1818
param?: string | undefined
1919

@@ -47,7 +47,7 @@ export class MethodOverrideHandlerPlugin<T extends Context> implements StandardH
4747
private readonly methods: ReadonlySet<string>
4848

4949
constructor(options: MethodOverrideHandlerPluginOptions = {}) {
50-
this.param = options.param ?? 'method'
50+
this.param = options.param ?? '_method'
5151
this.methods = new Set(
5252
(options.methods ?? ['PUT', 'PATCH', 'DELETE']).map((method) => method.toUpperCase()),
5353
)
@@ -78,8 +78,13 @@ export class MethodOverrideHandlerPlugin<T extends Context> implements StandardH
7878
return next()
7979
}
8080

81-
params.delete(this.param)
82-
const url = `${pathname}${params.size ? `?${params}` : ''}${hash ?? ''}` as StandardUrl
81+
// Drop only the override param, leaving the rest of the query untouched.
82+
const rest = search!
83+
.slice(1)
84+
.split('&')
85+
.filter((pair) => new URLSearchParams(`?${pair}`).keys().next().value !== this.param)
86+
const url =
87+
`${pathname}${rest.length ? `?${rest.join('&')}` : ''}${hash ?? ''}` as StandardUrl
8388

8489
return next({
8590
...interceptorOptions,

‎tests/openapi/method-override.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ describe('methodOverrideHandlerPlugin with OpenAPIHandler', () => {
3838
vi.clearAllMocks()
3939
})
4040

41-
it('routes a POST with ?method=DELETE to the DELETE procedure with clean input', async () => {
41+
it('routes a POST with ?_method=DELETE to the DELETE procedure with clean input', async () => {
4242
const { matched, response } = await handler.handle(
43-
new Request('https://example.com/todos/1?method=DELETE', {
43+
new Request('https://example.com/todos/1?_method=DELETE', {
4444
method: 'POST',
4545
headers: {
4646
'content-type': 'application/x-www-form-urlencoded',
@@ -72,7 +72,7 @@ describe('methodOverrideHandlerPlugin with OpenAPIHandler', () => {
7272

7373
it('does not leak the override param into detailed query input', async () => {
7474
const { matched, response } = await handler.handle(
75-
new Request('https://example.com/articles/1?method=PUT&tag=news', {
75+
new Request('https://example.com/articles/1?_method=PUT&tag=news', {
7676
method: 'POST',
7777
headers: {
7878
'content-type': 'application/json',
@@ -95,7 +95,7 @@ describe('methodOverrideHandlerPlugin with OpenAPIHandler', () => {
9595

9696
it('silently ignores a disallowed override value', async () => {
9797
const { matched } = await handler.handle(
98-
new Request('https://example.com/todos/1?method=FOO', {
98+
new Request('https://example.com/todos/1?_method=FOO', {
9999
method: 'POST',
100100
}),
101101
)

0 commit comments

Comments
 (0)