diff --git a/README.md b/README.md index 64435c9..127d120 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Import: // ESM / Typescript import { $fetch } from 'ohmyfetch' -// CommonJS (only $fetch export is available) +// CommonJS const { $fetch } = require('ohmyfetch') ``` diff --git a/cjs/index.cjs b/cjs/index.cjs index 83b4d3a..de8efe7 100644 --- a/cjs/index.cjs +++ b/cjs/index.cjs @@ -1,3 +1,6 @@ -exports.$fetch = (...args) => import('../dist/index.mjs') - .then(r => r.$fetch) - .then($fetch => $fetch(...args)) +const getExport = name => import('../dist/index.mjs').then(r => r[name]) +const createCaller = name => (...args) => getExport(name).then(fn => fn(...args)) + +exports.fetch = createCaller('fetch') +exports.$fetch = createCaller('$fetch') +exports.$fetch.raw = (...args) => getExport('$fetch').then($fetch => $fetch.raw(...args)) diff --git a/cjs/node.cjs b/cjs/node.cjs index 77e46ef..2cb2135 100644 --- a/cjs/node.cjs +++ b/cjs/node.cjs @@ -1,3 +1,6 @@ -exports.$fetch = (...args) => import('../dist/node.mjs') - .then(r => r.$fetch) - .then($fetch => $fetch(...args)) +const getExport = name => import('../dist/node.mjs').then(r => r[name]) +const createCaller = name => (...args) => getExport(name).then(fn => fn(...args)) + +exports.fetch = createCaller('fetch') +exports.$fetch = createCaller('$fetch') +exports.$fetch.raw = (...args) => getExport('$fetch').then($fetch => $fetch.raw(...args))