@cloudpss/fetch 0.4.19 → 0.4.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # @cloudpss/fetch
2
2
 
3
- Provides `fetch` for node.js, and a simple wrapper of [proxy-agent](https://github.com/TooTallNate/node-proxy-agent), use conditional exports to export instance of `ProxyAgent` in node environment, `undefined` in browsers.
3
+ Provides `fetch` for node.js, a simple wrapper of [node-fetch](https://github.com/node-fetch/node-fetch).
@@ -1,17 +1,17 @@
1
- export declare const fetch: typeof globalThis.fetch;
2
- export declare const Headers: {
1
+ declare const _fetch: typeof fetch;
2
+ declare const _Headers: {
3
3
  new (init?: HeadersInit | undefined): Headers;
4
4
  prototype: Headers;
5
5
  };
6
- export declare const Response: {
6
+ declare const _Response: {
7
7
  new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response;
8
8
  prototype: Response;
9
9
  error(): Response;
10
10
  redirect(url: string | URL, status?: number | undefined): Response;
11
11
  };
12
- export declare const Request: {
12
+ declare const _Request: {
13
13
  new (input: RequestInfo | URL, init?: RequestInit | undefined): Request;
14
14
  prototype: Request;
15
15
  };
16
- export declare const agent: unknown;
16
+ export { _fetch as fetch, _Headers as Headers, _Response as Response, _Request as Request };
17
17
  export default fetch;
@@ -1,10 +1,10 @@
1
- if (typeof globalThis == 'undefined' || typeof globalThis.fetch != 'function') {
2
- throw new TypeError(`fetch is not defined on the global object, you should load polyfill of globalThis and fetch.`);
1
+ if (typeof fetch != 'function') {
2
+ throw new TypeError(`fetch is not defined on the global object, you should load polyfill of fetch.`);
3
3
  }
4
- export const fetch = globalThis.fetch;
5
- export const Headers = globalThis.Headers;
6
- export const Response = globalThis.Response;
7
- export const Request = globalThis.Request;
8
- export const agent = undefined;
4
+ const _fetch = fetch;
5
+ const _Headers = Headers;
6
+ const _Response = Response;
7
+ const _Request = Request;
8
+ export { _fetch as fetch, _Headers as Headers, _Response as Response, _Request as Request };
9
9
  export default fetch;
10
10
  //# sourceMappingURL=index-browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index-browser.js","sourceRoot":"","sources":["../src/index-browser.ts"],"names":[],"mappings":"AAAA,IAAI,OAAO,UAAU,IAAI,WAAW,IAAI,OAAO,UAAU,CAAC,KAAK,IAAI,UAAU,EAAE;IAC3E,MAAM,IAAI,SAAS,CAAC,8FAA8F,CAAC,CAAC;CACvH;AAED,MAAM,CAAC,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;AACtC,MAAM,CAAC,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC;AAC1C,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;AAC5C,MAAM,CAAC,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC;AAC1C,MAAM,CAAC,MAAM,KAAK,GAAY,SAAS,CAAC;AACxC,eAAe,KAAK,CAAC"}
1
+ {"version":3,"file":"index-browser.js","sourceRoot":"","sources":["../src/index-browser.ts"],"names":[],"mappings":"AAAA,IAAI,OAAO,KAAK,IAAI,UAAU,EAAE;IAC5B,MAAM,IAAI,SAAS,CAAC,+EAA+E,CAAC,CAAC;CACxG;AAED,MAAM,MAAM,GAAG,KAAK,CAAC;AACrB,MAAM,QAAQ,GAAG,OAAO,CAAC;AACzB,MAAM,SAAS,GAAG,QAAQ,CAAC;AAC3B,MAAM,QAAQ,GAAG,OAAO,CAAC;AACzB,OAAO,EAAE,MAAM,IAAI,KAAK,EAAE,QAAQ,IAAI,OAAO,EAAE,SAAS,IAAI,QAAQ,EAAE,QAAQ,IAAI,OAAO,EAAE,CAAC;AAE5F,eAAe,KAAK,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const fetch: typeof globalThis.fetch, agent: unknown;
1
+ export declare const fetch: typeof globalThis.fetch;
2
2
  export declare const Headers: {
3
3
  new (init?: HeadersInit | undefined): Headers;
4
4
  prototype: Headers;
package/dist/index.js CHANGED
@@ -1,27 +1,15 @@
1
- import { ProxyAgent } from 'proxy-agent';
2
1
  import nodeFetch, { Headers as NodeHeaders, Response as NodeResponse, Request as NodeRequest } from 'node-fetch';
2
+ import { agent } from '@cloudpss/proxy-agent';
3
3
  /** 生成 fetch */
4
4
  function makeFetch() {
5
- const hasProxy = !!process.env['HTTP_PROXY'] ||
6
- !!process.env['http_proxy'] ||
7
- !!process.env['HTTPS_PROXY'] ||
8
- !!process.env['https_proxy'] ||
9
- !!process.env['ALL_PROXY'] ||
10
- !!process.env['all_proxy'];
11
- if (hasProxy) {
12
- const agent = new ProxyAgent();
13
- return [
14
- function fetch(url, init) {
15
- return nodeFetch(url, { ...init, agent });
16
- },
17
- agent,
18
- ];
19
- }
20
- else {
21
- return [nodeFetch, undefined];
5
+ if (agent) {
6
+ return function fetch(url, init) {
7
+ return nodeFetch(url, { ...init, agent });
8
+ };
22
9
  }
10
+ return nodeFetch;
23
11
  }
24
- export const [fetch, agent] = makeFetch();
12
+ export const fetch = makeFetch();
25
13
  export const Headers = NodeHeaders;
26
14
  export const Response = NodeResponse;
27
15
  export const Request = NodeRequest;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,SAAS,EAAE,EAAE,OAAO,IAAI,WAAW,EAAE,QAAQ,IAAI,YAAY,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,YAAY,CAAC;AAEjH,eAAe;AACf,SAAS,SAAS;IACd,MAAM,QAAQ,GACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;QAC3B,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;QAC3B,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;QAC5B,CAAC,CAAC,OAAO,CAAC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M,KAAK,GAAG,SAAS,EAAE,CAAC;AACjC,MAAM,CAAC,MAAM,OAAO,GAAG,WAAmD,CAAC;AAC3E,MAAM,CAAC,MAAM,QAAQ,GAAG,YAAqD,CAAC;AAC9E,MAAM,CAAC,MAAM,OAAO,GAAG,WAAmD,CAAC;AAE3E,eAAe,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@cloudpss/fetch",
3
- "version": "0.4.19",
3
+ "version": "0.4.21",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
+ "sideEffects": false,
7
8
  "main": "dist/index.js",
8
9
  "module": "dist/index.js",
9
10
  "types": "dist/index.d.ts",
@@ -20,7 +21,7 @@
20
21
  "clean": "rimraf dist"
21
22
  },
22
23
  "dependencies": {
23
- "node-fetch": "^3.3.1",
24
- "proxy-agent": "^6.2.0"
24
+ "@cloudpss/proxy-agent": "~0.4.21",
25
+ "node-fetch": "^3.3.1"
25
26
  }
26
27
  }
@@ -1,10 +1,11 @@
1
- if (typeof globalThis == 'undefined' || typeof globalThis.fetch != 'function') {
2
- throw new TypeError(`fetch is not defined on the global object, you should load polyfill of globalThis and fetch.`);
1
+ if (typeof fetch != 'function') {
2
+ throw new TypeError(`fetch is not defined on the global object, you should load polyfill of fetch.`);
3
3
  }
4
4
 
5
- export const fetch = globalThis.fetch;
6
- export const Headers = globalThis.Headers;
7
- export const Response = globalThis.Response;
8
- export const Request = globalThis.Request;
9
- export const agent: unknown = undefined;
5
+ const _fetch = fetch;
6
+ const _Headers = Headers;
7
+ const _Response = Response;
8
+ const _Request = Request;
9
+ export { _fetch as fetch, _Headers as Headers, _Response as Response, _Request as Request };
10
+
10
11
  export default fetch;
package/src/index.ts CHANGED
@@ -1,30 +1,18 @@
1
- import { ProxyAgent } from 'proxy-agent';
2
1
  import type { RequestInfo as NodeRequestInfo, RequestInit as NodeRequestInit } from 'node-fetch';
3
2
  import nodeFetch, { Headers as NodeHeaders, Response as NodeResponse, Request as NodeRequest } from 'node-fetch';
3
+ import { agent } from '@cloudpss/proxy-agent';
4
4
 
5
5
  /** 生成 fetch */
6
- function makeFetch(): [typeof globalThis.fetch, unknown] {
7
- const hasProxy =
8
- !!process.env['HTTP_PROXY'] ||
9
- !!process.env['http_proxy'] ||
10
- !!process.env['HTTPS_PROXY'] ||
11
- !!process.env['https_proxy'] ||
12
- !!process.env['ALL_PROXY'] ||
13
- !!process.env['all_proxy'];
14
- if (hasProxy) {
15
- const agent = new ProxyAgent();
16
- return [
17
- function fetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
18
- return nodeFetch(url as NodeRequestInfo, { ...init, agent } as NodeRequestInit) as Promise<Response>;
19
- },
20
- agent,
21
- ];
22
- } else {
23
- return [nodeFetch as typeof globalThis.fetch, undefined];
6
+ function makeFetch(): typeof globalThis.fetch {
7
+ if (agent) {
8
+ return function fetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
9
+ return nodeFetch(url as NodeRequestInfo, { ...init, agent } as NodeRequestInit) as Promise<Response>;
10
+ };
24
11
  }
12
+ return nodeFetch as typeof fetch;
25
13
  }
26
14
 
27
- export const [fetch, agent] = makeFetch();
15
+ export const fetch = makeFetch();
28
16
  export const Headers = NodeHeaders as unknown as typeof globalThis.Headers;
29
17
  export const Response = NodeResponse as unknown as typeof globalThis.Response;
30
18
  export const Request = NodeRequest as unknown as typeof globalThis.Request;