Skip to content

Commit

Permalink
feat!: use export condition to automatically use node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 25, 2021
1 parent c222d86 commit b81082b
Show file tree
Hide file tree
Showing 6 changed files with 1,496 additions and 2,590 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ yarn add ohmyfetch
Import:

```js
// Universal (requires global.fetch)
// ESM / Typescript
import { $fetch } from 'ohmyfetch'

// NodeJS / Isomorphic
import { $fetch } from 'ohmyfetch/node'

// NodeJS / Isomorphic (CommonJS)
const { $fetch } = require('ohmyfetch/node')
// CommonJS
const { $fetch } = require('ohmyfetch')
```

<details>
<summary>Spoiler</summary>
<img src="https://media.giphy.com/media/Dn1QRA9hqMcoMz9zVZ/giphy.gif">
</details>

## ✔️ Works in Node.js

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) polyfill! No changes required.

## ✔️ Parsing Response

`$fetch` Smartly parses JSON and native values using [destr](https://github.com/unjs/destr) and fallback to text if cannot parse
`$fetch` Smartly parses JSON and native values using [destr](https://github.com/unjs/destr) and fallback to text if cannot parse.

```js
const { users } = await $fetch('/api/users')
Expand Down
8 changes: 8 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: [
'src/index',
'src/node'
]
})
1 change: 0 additions & 1 deletion node.js

This file was deleted.

1 change: 0 additions & 1 deletion node.mjs

This file was deleted.

23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
"node": {
"import": "./dist/node.mjs",
"require": "./dist/node.js"
},
"default": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"./node": {
"import": "./dist/node.mjs",
"require": "./dist/node.js"
}
},
"main": "./dist/index.js",
"main": "./dist/node.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
Expand All @@ -24,16 +30,17 @@
"index.*"
],
"scripts": {
"build": "siroc build",
"build": "unbuild",
"prepack": "yarn build",
"lint": "eslint --ext .ts .",
"play": "jiti playground/index.ts",
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
},
"dependencies": {
"destr": "^1.1.0",
"node-fetch": "^2.6.1",
"ufo": "^0.6.10"
"ufo": "^0.7.9"
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
Expand All @@ -46,9 +53,9 @@
"jest": "latest",
"jiti": "latest",
"listhen": "latest",
"siroc": "latest",
"standard-version": "latest",
"ts-jest": "latest",
"typescript": "latest"
"typescript": "latest",
"unbuild": "latest"
}
}
Loading

0 comments on commit b81082b

Please sign in to comment.