Skip to content

Commit

Permalink
feat(cjs): provide fetch and $fetch.raw exports
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 22, 2021
1 parent 1374ed1 commit 529af1c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Import:
// ESM / Typescript
import { $fetch } from 'ohmyfetch'

// CommonJS (only $fetch export is available)
// CommonJS
const { $fetch } = require('ohmyfetch')
```

Expand Down
9 changes: 6 additions & 3 deletions cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 6 additions & 3 deletions cjs/node.cjs
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 529af1c

Please sign in to comment.