Skip to content
Draft

V2 #25

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
cache: 'npm'
- name: Install dependencies
run: npm ci --ignore-scripts
Expand Down
11 changes: 1 addition & 10 deletions README.md
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
8 changes: 0 additions & 8 deletions core.js

This file was deleted.

62 changes: 0 additions & 62 deletions create-fetchival.js

This file was deleted.

82 changes: 0 additions & 82 deletions experimental/create-fetchival.js

This file was deleted.

3 changes: 0 additions & 3 deletions experimental/fetchival.js

This file was deleted.

1 change: 0 additions & 1 deletion fetch.browser.js

This file was deleted.

26 changes: 0 additions & 26 deletions fetch.js

This file was deleted.

9 changes: 0 additions & 9 deletions fetch.native.js

This file was deleted.

71 changes: 71 additions & 0 deletions fetcher.js
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const err = new Error(res.statusText)
const err = new Error(`${res.status} ${res.statusText || ''}`.trim())

Fix the statusText is undefined case

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 _
}
5 changes: 0 additions & 5 deletions fetchival.browser.js

This file was deleted.

6 changes: 0 additions & 6 deletions fetchival.js

This file was deleted.

9 changes: 0 additions & 9 deletions index.browser.js

This file was deleted.

9 changes: 0 additions & 9 deletions index.js

This file was deleted.

Loading
Loading