Skip to content

Commit

Permalink
fetch: warn when using patch method (nodejs#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 26135ef commit 128c850
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions test/fetch/issue-2294-patch-method.js
Original file line number Diff line number Diff line change
@@ -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' })
})

0 comments on commit 128c850

Please sign in to comment.