-
Notifications
You must be signed in to change notification settings - Fork 0
V2 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
V2 #25
Changes from all commits
a317a57
0787c9d
da0674e
d8a0c75
71568bb
9272f8f
f7056e4
d92c109
d10d74b
e8a4861
8d0b50e
3926d56
73c702c
799551f
fef787b
3f0d3d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
# @exodus/fetch | ||
|
||
A small wrapper around global fetch and `node-fetch`: | ||
|
||
1. In React Native, global `fetch` is used without importing `node-fetch` | ||
|
||
2. In Browser, global `fetch` is used without importing `node-fetch` | ||
|
||
3. Otherwise `node-fetch` is imported, but when `window.fetch` is present (e.g. in Electron | ||
`renderer` process), it is used. | ||
|
||
4. Otherwise (e.g. in Electron `browser` process and Node.js), `node-fetch` is used. | ||
Implementation of an url builder and a more secure fetchival replacement |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { url } from './url.js' | ||
|
||
async function _fetch(method, link, opts, data) { | ||
// Unlike fetchival, don't silently ignore and override | ||
if (opts.body) throw new Error('unexpected pre-set body option') | ||
|
||
// Unlike fetchival, don't pollute the opts object we were given | ||
const res = await fetch(link, { | ||
...opts, | ||
method, | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
...opts.headers, | ||
}, | ||
...(data ? { body: JSON.stringify(data) } : {}), | ||
}) | ||
|
||
if (res.status >= 200 && res.status < 300) { | ||
if (opts.responseAs === 'response') return res | ||
if (res.status === 204) return null | ||
if (opts.responseAs === 'text') return res.text() | ||
return res.json() | ||
} | ||
|
||
const err = new Error(res.statusText) | ||
err.response = res | ||
throw err | ||
} | ||
|
||
export function fetcher(link, opts = {}) { | ||
if (!(link instanceof URL)) throw new TypeError('Url should be an instance of URL') | ||
|
||
const str = `${link}` | ||
if (str.includes('?') || str.includes('&')) throw new Error('Invalid url with params!') | ||
if (str.includes('#')) throw new Error('Invalid url with hash!') | ||
|
||
const _ = (sub, o = {}) => { | ||
// Unlike fetchival, this performs additional validation | ||
if (sub.includes('/')) throw new Error('Only simple subpaths are allowed!') | ||
const joined = str.endsWith('/') ? url`${link}${sub}` : url`${link}/${sub}` | ||
return fetcher(joined, { ...opts, ...o }) | ||
} | ||
|
||
_.head = (params) => _fetch('HEAD', params ? url`${link}?${params}` : link, opts) | ||
_.get = (params) => _fetch('GET', params ? url`${link}?${params}` : link, opts) | ||
_.post = (data) => _fetch('POST', link, opts, data) | ||
_.put = (data) => _fetch('PUT', link, opts, data) | ||
_.patch = (data) => _fetch('PATCH', link, opts, data) | ||
_.delete = () => _fetch('DELETE', link, opts) | ||
_.method = (method, ...args) => { | ||
switch (method) { | ||
case 'head': | ||
return _.head(...args) | ||
case 'get': | ||
return _.get(...args) | ||
case 'post': | ||
return _.post(...args) | ||
case 'put': | ||
return _.put(...args) | ||
case 'patch': | ||
return _.patch(...args) | ||
case 'delete': | ||
return _.delete(...args) | ||
default: | ||
throw new Error('Unexpected method') | ||
} | ||
} | ||
|
||
return _ | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the statusText is undefined case