Skip to content

Commit 53479d3

Browse files
committed
fetch: warn when using patch method
1 parent db635f7 commit 53479d3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/fetch/request.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
3838
signal.removeEventListener('abort', abort)
3939
})
4040

41+
let patchMethodWarning = false
42+
4143
// https://fetch.spec.whatwg.org/#request-class
4244
class Request {
4345
// https://fetch.spec.whatwg.org/#dom-request
@@ -328,6 +330,14 @@ class Request {
328330

329331
// 4. Set request’s method to method.
330332
request.method = method
333+
334+
if (!patchMethodWarning && method === 'patch') {
335+
process.emitWarning('Using `patch` is highly likely to result in a `405 Method Not Allowed`. `PATCH` is much more likely to succeed.', {
336+
code: 'UNDICI-FETCH-patch'
337+
})
338+
339+
patchMethodWarning = true
340+
}
331341
}
332342

333343
// 26. If init["signal"] exists, then set signal to it.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { Request } = require('../..')
5+
6+
test('Using `patch` method emits a warning.', (t) => {
7+
t.plan(1)
8+
9+
const { emitWarning } = process
10+
11+
t.teardown(() => {
12+
process.emitWarning = emitWarning
13+
})
14+
15+
process.emitWarning = (warning, options) => {
16+
t.equal(options.code, 'UNDICI-FETCH-patch')
17+
}
18+
19+
// eslint-disable-next-line no-new
20+
new Request('https://a', { method: 'patch' })
21+
})

0 commit comments

Comments
 (0)