Skip to content

Commit aca1412

Browse files
committed
initial commit
0 parents  commit aca1412

17 files changed

+4310
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
9+
[*.js]
10+
indent_style = space
11+
indent_size = 2
12+
13+
[{package.json,*.yml,*.cjson}]
14+
indent_style = space
15+
indent_size = 2

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"@nuxtjs/eslint-config-typescript"
4+
]
5+
}

.github/workflows/ci.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
ci:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- run: corepack enable
17+
- uses: actions/setup-node@v3
18+
with:
19+
node-version: 16
20+
cache: "pnpm"
21+
- run: pnpm install
22+
- run: pnpm lint
23+
- run: pnpm build
24+
- run: pnpm vitest

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.vscode
2+
node_modules
3+
*.log*
4+
.DS_Store
5+
coverage
6+
dist
7+
types
8+
.conf*

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 - UnJS
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# node-fetch-native
2+
3+
[![npm version][npm-version-src]][npm-version-href]
4+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
5+
[![Github Actions][github-actions-src]][github-actions-href]
6+
<!-- [![Codecov][codecov-src]][codecov-href] -->
7+
8+
A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) for better backward and forward compatibility.
9+
10+
Why this package?
11+
12+
- We can no longer `require('node-fetch')` with latest version. This stopped popular libraries from upgrading and dependency conflicts between `node-fetch@2` and `node-fetch@3`.
13+
- With upcoming versions of Node.js, native `fetch` is being supported. We are prepared for native fetch support using this package yet keep supporting older Node versions.
14+
15+
✅ Prefer to **native globals** when available (`fetch`, `Blob`, `File`, `FormData`, `Headers`, `Request`, and `Response`) when available (Node.js [experimental fetch](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch))
16+
17+
✅ Compact build and less install size with **zero dependencies**
18+
19+
✅ Support both **CommonJS** (`require`) and **ESM** (`import`) usage
20+
21+
✅ Use native version if imported without `node` condition using [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) with **zero bundle overhead**
22+
23+
## Usage
24+
25+
Install `node-fetch-native` dependency:
26+
27+
```sh
28+
# npm
29+
npm i node-fetch-native
30+
31+
# yarn
32+
yarn add node-fetch-native
33+
34+
# pnpm
35+
pnpm i node-fetch-native
36+
```
37+
38+
You can now either import or require the dependency:
39+
40+
```js
41+
// ESM
42+
import fetch from `node-fetch-native`
43+
44+
// CommonJS
45+
const fetch = require('node-fetch-native')
46+
```
47+
48+
Other exports:
49+
50+
```js
51+
// ESM
52+
import { fetch, Blob, FormData, Headers, Request, Response } from `node-fetch-native`
53+
54+
// CommonJS
55+
const { fetch, Blob, FormData, Headers, Request, Response } = require('node-fetch-native')
56+
```
57+
58+
## Alias to `node-fetch`
59+
60+
Using this method, you can ensure all project dependencies and usages of `node-fetch` can benefit from improved `node-fetch-native` and won't conflict between `node-fetch@2` and `node-fetch@3`.
61+
62+
### npm
63+
64+
Using npm [overrides](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides):
65+
66+
```jsonc
67+
// package.json
68+
{
69+
"overrides": {
70+
"node-fetch": "npm:node-fetch-native@latest"
71+
}
72+
}
73+
```
74+
75+
### yarn
76+
77+
Using yarn [selective dependency resolutions](https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/):
78+
79+
```jsonc
80+
// package.json
81+
{
82+
"resolutions": {
83+
"node-fetch": "npm:node-fetch-native@latest"
84+
}
85+
}
86+
```
87+
88+
### pnpm
89+
90+
Using [pnpm.overrides](https://pnpm.io/package_json#pnpmoverrides):
91+
92+
```jsonc
93+
// package.json
94+
{
95+
"pnpm": {
96+
"overrides": {
97+
"node-fetch": "npm:node-fetch-native@latest"
98+
}
99+
}
100+
}
101+
```
102+
103+
## License
104+
105+
Made with 💛
106+
107+
[node-fetch is published under the MIT license](https://github.com/node-fetch/node-fetch/blob/main/LICENSE.md)
108+
109+
<!-- Badges -->
110+
[npm-version-src]: https://img.shields.io/npm/v/node-fetch-native?style=flat-square
111+
[npm-version-href]: https://npmjs.com/package/node-fetch-native
112+
113+
[npm-downloads-src]: https://img.shields.io/npm/dm/node-fetch-native?style=flat-square
114+
[npm-downloads-href]: https://npmjs.com/package/node-fetch-native
115+
116+
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/node-fetch-native/ci/main?style=flat-square
117+
[github-actions-href]: https://github.com/unjs/node-fetch-native/actions?query=workflow%3Aci
118+
119+
<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/node-fetch-native/main?style=flat-square
120+
[codecov-href]: https://codecov.io/gh/unjs/node-fetch-native -->

build.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
declaration: false,
5+
rollup: {
6+
emitCJS: true,
7+
inlineDependencies: true
8+
},
9+
entries: [
10+
'src/index',
11+
'src/native'
12+
]
13+
})

lib/index.cjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const nodeFetch = require('../dist/index.cjs')
2+
3+
function fetch (input, options) {
4+
return nodeFetch.fetch(input, options)
5+
}
6+
7+
for (const key in nodeFetch) {
8+
fetch[key] = nodeFetch[key]
9+
}
10+
11+
module.exports = fetch

lib/index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare const fetch: typeof globalThis.fetch
2+
declare const Blob: typeof globalThis.Blob
3+
declare const File: typeof globalThis.File
4+
declare const FormData: typeof globalThis.FormData
5+
declare const Headers: typeof globalThis.Headers
6+
declare const Request: typeof globalThis.Request
7+
declare const Response: typeof globalThis.Response
8+
9+
export { fetch, Blob, File, FormData, Headers, Request, Response }
10+
export default fetch

package.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "node-fetch-native",
3+
"version": "0.1.0",
4+
"description": "",
5+
"repository": "unjs/node-fetch-native",
6+
"license": "MIT",
7+
"sideEffects": false,
8+
"type": "module",
9+
"exports": {
10+
".": {
11+
"node": {
12+
"require": "./lib/index.cjs",
13+
"import": "./dist/index.mjs"
14+
},
15+
"import": "./dist/native.mjs"
16+
}
17+
},
18+
"main": "./lib/index.cjs",
19+
"module": "./dist/index.mjs",
20+
"types": "./lib/index.d.ts",
21+
"files": [
22+
"dist",
23+
"lib"
24+
],
25+
"scripts": {
26+
"build": "unbuild",
27+
"lint": "eslint --ext .ts,.js,.mjs,.cjs .",
28+
"prepack": "unbuild",
29+
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
30+
"test": "pnpm lint && pnpm build && vitest run"
31+
},
32+
"devDependencies": {
33+
"@nuxtjs/eslint-config-typescript": "latest",
34+
"c8": "latest",
35+
"eslint": "latest",
36+
"node-fetch": "^3.2.4",
37+
"standard-version": "latest",
38+
"typescript": "latest",
39+
"unbuild": "latest",
40+
"vitest": "latest"
41+
},
42+
"packageManager": "pnpm@7.0.0"
43+
}

0 commit comments

Comments
 (0)