@cloudpss/fetch 0.4.0

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 ADDED
@@ -0,0 +1,3 @@
1
+ # @cloudpss/fetch
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.
@@ -0,0 +1,17 @@
1
+ export declare const fetch: typeof globalThis.fetch;
2
+ export declare const Headers: {
3
+ new (init?: HeadersInit | undefined): Headers;
4
+ prototype: Headers;
5
+ };
6
+ export declare const Response: {
7
+ new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response;
8
+ prototype: Response;
9
+ error(): Response;
10
+ redirect(url: string | URL, status?: number | undefined): Response;
11
+ };
12
+ export declare const Request: {
13
+ new (input: RequestInfo | URL, init?: RequestInit | undefined): Request;
14
+ prototype: Request;
15
+ };
16
+ export declare const agent: unknown;
17
+ export default fetch;
@@ -0,0 +1,10 @@
1
+ if (typeof globalThis == 'undefined' || typeof globalThis.fetch != 'function') {
2
+ throw new Error(`fetch is not defined on the global object, you should load polyfill of globalThis and fetch.`);
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;
9
+ export default fetch;
10
+ //# sourceMappingURL=index-browser.js.map
@@ -0,0 +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,KAAK,CAAC,8FAA8F,CAAC,CAAC;CACnH;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;AAExC,eAAe,KAAK,CAAC"}
@@ -0,0 +1,16 @@
1
+ export declare const fetch: typeof globalThis.fetch, agent: unknown;
2
+ export declare const Headers: {
3
+ new (init?: HeadersInit | undefined): Headers;
4
+ prototype: Headers;
5
+ };
6
+ export declare const Response: {
7
+ new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response;
8
+ prototype: Response;
9
+ error(): Response;
10
+ redirect(url: string | URL, status?: number | undefined): Response;
11
+ };
12
+ export declare const Request: {
13
+ new (input: RequestInfo | URL, init?: RequestInit | undefined): Request;
14
+ prototype: Request;
15
+ };
16
+ export default fetch;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ import ProxyAgent from 'proxy-agent';
2
+ import nodeFetch, { Headers as NodeHeaders, Response as NodeResponse, Request as NodeRequest } from 'node-fetch';
3
+ /** 生成 fetch */
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];
22
+ }
23
+ }
24
+ export const [fetch, agent] = makeFetch();
25
+ export const Headers = NodeHeaders;
26
+ export const Response = NodeResponse;
27
+ export const Request = NodeRequest;
28
+ export default fetch;
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":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etch",
3
+ "version": "0.4.0",
4
+ "author": "CloudPSS",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "browser": "dist/index-browser.js",
11
+ "exports": {
12
+ "types": "./dist/index.d.ts",
13
+ "node": "./dist/index.js",
14
+ "default": "./dist/index-browser.js"
15
+ },
16
+ "scripts": {
17
+ "start": "yarn clean && tsc --watch",
18
+ "build": "yarn clean && tsc",
19
+ "prepublishOnly": "yarn build",
20
+ "clean": "rimraf dist"
21
+ },
22
+ "dependencies": {
23
+ "node-fetch": "^3.2.10",
24
+ "proxy-agent": "^5.0.0"
25
+ }
26
+ }
@@ -0,0 +1,11 @@
1
+ if (typeof globalThis == 'undefined' || typeof globalThis.fetch != 'function') {
2
+ throw new Error(`fetch is not defined on the global object, you should load polyfill of globalThis and fetch.`);
3
+ }
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;
10
+
11
+ export default fetch;
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import ProxyAgent from 'proxy-agent';
2
+ import type { RequestInfo as NodeRequestInfo, RequestInit as NodeRequestInit } from 'node-fetch';
3
+ import nodeFetch, { Headers as NodeHeaders, Response as NodeResponse, Request as NodeRequest } from 'node-fetch';
4
+
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];
24
+ }
25
+ }
26
+
27
+ export const [fetch, agent] = makeFetch();
28
+ export const Headers = NodeHeaders as unknown as typeof globalThis.Headers;
29
+ export const Response = NodeResponse as unknown as typeof globalThis.Response;
30
+ export const Request = NodeRequest as unknown as typeof globalThis.Request;
31
+
32
+ export default fetch;
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig",
3
+ "include": ["./src/"],
4
+ "compilerOptions": {
5
+ "outDir": "./dist"
6
+ }
7
+ }