From dfa0b554c72f1fe03bf3dc3cb0f47b7d306edb63 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 4 Nov 2021 22:44:39 +0100 Subject: [PATCH] feat: experimental undici support --- README.md | 10 ++++++++++ build.config.ts | 3 ++- cjs/undici.cjs | 6 ++++++ package.json | 7 ++++++- playground/undici.ts | 13 +++++++++++++ src/undici.ts | 9 +++++++++ tsconfig.json | 3 ++- yarn.lock | 5 +++++ 8 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 cjs/undici.cjs create mode 100644 playground/undici.ts create mode 100644 src/undici.ts diff --git a/README.md b/README.md index d1f6d60a..e92f5b7b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build.config.ts b/build.config.ts index 54096eae..5c12ef77 100644 --- a/build.config.ts +++ b/build.config.ts @@ -5,6 +5,7 @@ export default defineBuildConfig({ emitCJS: false, entries: [ 'src/index', - 'src/node' + 'src/node', + 'src/undici' ] }) diff --git a/cjs/undici.cjs b/cjs/undici.cjs new file mode 100644 index 00000000..6181f134 --- /dev/null +++ b/cjs/undici.cjs @@ -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)) diff --git a/package.json b/package.json index a54fe40f..51fc016e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/playground/undici.ts b/playground/undici.ts new file mode 100644 index 00000000..b574a74b --- /dev/null +++ b/playground/undici.ts @@ -0,0 +1,13 @@ +import { $fetch } from '../src/undici' + +async function main () { + const r = await $fetch('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) +}) diff --git a/src/undici.ts b/src/undici.ts new file mode 100644 index 00000000..61651efb --- /dev/null +++ b/src/undici.ts @@ -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 }) diff --git a/tsconfig.json b/tsconfig.json index 3e167e4e..44f1c42e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "target": "ES6", + "target": "ESNext", + "module": "ESNext", "moduleResolution": "Node", "esModuleInterop": true, "outDir": "dist", diff --git a/yarn.lock b/yarn.lock index be972670..8f11bc16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"