diff --git a/lib/fetch/request.js b/lib/fetch/request.js index e8988af738b..f5522486c1d 100644 --- a/lib/fetch/request.js +++ b/lib/fetch/request.js @@ -38,6 +38,8 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => { signal.removeEventListener('abort', abort) }) +let patchMethodWarning = false + // https://fetch.spec.whatwg.org/#request-class class Request { // https://fetch.spec.whatwg.org/#dom-request @@ -335,6 +337,14 @@ class Request { // 4. Set request’s method to method. request.method = method } + + if (!patchMethodWarning && request.method === 'patch') { + process.emitWarning('Using `patch` is highly likely to result in a `405 Method Not Allowed`. `PATCH` is much more likely to succeed.', { + code: 'UNDICI-FETCH-patch' + }) + + patchMethodWarning = true + } } // 26. If init["signal"] exists, then set signal to it. diff --git a/test/fetch/issue-2294-patch-method.js b/test/fetch/issue-2294-patch-method.js new file mode 100644 index 00000000000..bac7ef51aa9 --- /dev/null +++ b/test/fetch/issue-2294-patch-method.js @@ -0,0 +1,21 @@ +'use strict' + +const { test } = require('tap') +const { Request } = require('../..') + +test('Using `patch` method emits a warning.', (t) => { + t.plan(1) + + const { emitWarning } = process + + t.teardown(() => { + process.emitWarning = emitWarning + }) + + process.emitWarning = (warning, options) => { + t.equal(options.code, 'UNDICI-FETCH-patch') + } + + // eslint-disable-next-line no-new + new Request('https://a', { method: 'patch' }) +})