Skip to content

Instantly share code, notes, and snippets.

@galvez
Created July 31, 2023 22:40
Show Gist options
  • Save galvez/f867dd6d6d07db186dc484df659dae57 to your computer and use it in GitHub Desktop.
Save galvez/f867dd6d6d07db186dc484df659dae57 to your computer and use it in GitHub Desktop.
global.JSON.get = makeMethod('GET')
global.JSON.post = makeMethod('POST')
global.JSON.put = makeMethod('PUT')
function makeMethod (method) {
return function (url, options = {}) {
const headers = {
...options.headers,
'Content-Type': 'application/json',
}
if (typeof options.body === 'object') {
options.body = JSON.stringify(options.body)
}
return fetch(url, { ...options, method, headers })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment