diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e12db5..d0e82a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## v1.6.0 + +[compare changes](https://github.com/unjs/node-fetch-native/compare/v1.5.1...v1.6.0) + +### 🚀 Enhancements + +- **proxy:** Export `fetch` and `createFetch` ([#108](https://github.com/unjs/node-fetch-native/pull/108)) +- **proxy:** Support no_proxy ([#109](https://github.com/unjs/node-fetch-native/pull/109)) + +### 🩹 Fixes + +- **proxy:** Update environment variable check order ([742d27e](https://github.com/unjs/node-fetch-native/commit/742d27e)) + +### ❤️ Contributors + +- Pooya Parsa ([@pi0](http://github.com/pi0)) + ## v1.5.1 [compare changes](https://github.com/unjs/node-fetch-native/compare/v1.5.0...v1.5.1) diff --git a/package.json b/package.json index 016dedc..c5236e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-fetch-native", - "version": "1.5.1", + "version": "1.6.0", "description": "better fetch for Node.js. Works on any JavaScript runtime!", "repository": "unjs/node-fetch-native", "license": "MIT", @@ -141,4 +141,4 @@ "vitest": "^1.1.0" }, "packageManager": "pnpm@8.12.1" -} +} \ No newline at end of file diff --git a/src/proxy.ts b/src/proxy.ts index 8dab3af..e39a19d 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -8,45 +8,6 @@ import { HttpsProxyAgent } from "https-proxy-agent"; import type { ProxyOptions } from "../proxy"; import { fetch as _fetch } from "node-fetch-native"; -export function createProxy(opts: ProxyOptions = {}) { - const uri = - opts.url || - process.env.https_proxy || - process.env.http_proxy || - process.env.HTTPS_PROXY || - process.env.HTTP_PROXY; - - if (!uri) { - return { - agent: undefined, - dispatcher: undefined, - }; - } - - const _noProxy = opts.noProxy || process.env.no_proxy || process.env.NO_PROXY; - const noProxy = typeof _noProxy === "string" ? _noProxy.split(",") : _noProxy; - - const nodeAgent = new NodeProxyAgent({ uri, noProxy }); - - // https://undici.nodejs.org/#/docs/api/ProxyAgent - const undiciAgent = new UndiciProxyAgent({ - uri, - noProxy, - }); - - return { - agent: nodeAgent, - dispatcher: undiciAgent, - }; -} - -export function createFetch(proxyOptions: ProxyOptions = {}) { - const proxy = createProxy(proxyOptions); - return (url, fetchOptions) => _fetch(url, { ...proxy, ...fetchOptions }); -} - -export const fetch = createFetch({}); - // ---------------------------------------------- // Utils // ---------------------------------------------- @@ -170,3 +131,46 @@ class NodeProxyAgent extends Agent { super.destroy(); } } + +// ---------------------------------------------- +// Main exports +// ---------------------------------------------- + +export function createProxy(opts: ProxyOptions = {}) { + const uri = + opts.url || + process.env.https_proxy || + process.env.http_proxy || + process.env.HTTPS_PROXY || + process.env.HTTP_PROXY; + + if (!uri) { + return { + agent: undefined, + dispatcher: undefined, + }; + } + + const _noProxy = opts.noProxy || process.env.no_proxy || process.env.NO_PROXY; + const noProxy = typeof _noProxy === "string" ? _noProxy.split(",") : _noProxy; + + const nodeAgent = new NodeProxyAgent({ uri, noProxy }); + + // https://undici.nodejs.org/#/docs/api/ProxyAgent + const undiciAgent = new UndiciProxyAgent({ + uri, + noProxy, + }); + + return { + agent: nodeAgent, + dispatcher: undiciAgent, + }; +} + +export function createFetch(proxyOptions: ProxyOptions = {}) { + const proxy = createProxy(proxyOptions); + return (url, fetchOptions) => _fetch(url, { ...proxy, ...fetchOptions }); +} + +export const fetch = createFetch({});