Skip to content

Commit

Permalink
feat: experimental undici support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 4, 2021
1 parent ec83366 commit dfa0b55
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const { $fetch } = require('ohmyfetch')
We use [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) to detect Node.js
and automatically use [node-fetch](https://github.com/node-fetch/node-fetch). If `globalThis.fetch` is available, will be used instead.

### undici support

In order to use experimental fetch implementation from [nodejs/undici](https://github.com/nodejs/undici), You can import from `ohmyfetch/undici`.

```js
import { $fetch } from 'ohmyfetch/undici'
```

On Node.js versions older than `16.5`, node-fetch will be used as the fallback.

## ✔️ Parsing Response

`$fetch` Smartly parses JSON and native values using [destr](https://github.com/unjs/destr) and fallback to text if it fails to parse.
Expand Down
3 changes: 2 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineBuildConfig({
emitCJS: false,
entries: [
'src/index',
'src/node'
'src/node',
'src/undici'
]
})
6 changes: 6 additions & 0 deletions cjs/undici.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const getExport = name => import('../dist/undici.mjs').then(r => r[name])
const createCaller = name => (input, init) => getExport(name).then(fn => fn(input, init))

exports.fetch = createCaller('fetch')
exports.$fetch = createCaller('$fetch')
exports.$fetch.raw = (input, init) => getExport('$fetch').then($fetch => $fetch.raw(input, init))
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"./node": {
"import": "./dist/node.mjs",
"require": "./cjs/node.cjs"
},
"./undici": {
"import": "./dist/undici.mjs",
"require": "./cjs/undici.cjs"
}
},
"main": "./cjs/node.cjs",
Expand All @@ -40,7 +44,8 @@
"dependencies": {
"destr": "^1.1.0",
"node-fetch": "^3.0.0",
"ufo": "^0.7.9"
"ufo": "^0.7.9",
"undici": "^4.9.5"
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
Expand Down
13 changes: 13 additions & 0 deletions playground/undici.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { $fetch } from '../src/undici'

async function main () {
const r = await $fetch<string>('http://google.com/404')
// eslint-disable-next-line no-console
console.log(r)
}

main().catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})
9 changes: 9 additions & 0 deletions src/undici.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fetch as undiciFetch } from 'undici'
import nodeFetch from 'node-fetch'
import { createFetch } from './base'

export * from './base'

export const fetch = globalThis.fetch || undiciFetch || nodeFetch

export const $fetch = createFetch({ fetch })
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ES6",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true,
"outDir": "dist",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,11 @@ unbuild@latest:
typescript "^4.4.4"
untyped "^0.2.9"

undici@^4.9.5:
version "4.9.5"
resolved "https://registry.yarnpkg.com/undici/-/undici-4.9.5.tgz#6531b6b2587c2c42d77c0dded83d058a328775f8"
integrity sha512-t59IFVYiMnFThboJL9izqwsDEfSbZDPZ/8iCYBCkEFLy63x9m4YaNt0E+r5+X993syC9M0W/ksusZC9YuAamMg==

universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
Expand Down

0 comments on commit dfa0b55

Please sign in to comment.