wisdomtreetest 1.0.2 → 1.0.3
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/index.d.ts +13 -4
- package/index.js +29 -24
- package/package.json +5 -9
package/index.d.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Fetch https://baidu.com using the platform's `fetch` API, print the response
|
|
3
|
+
* body to stdout, and return the same body string.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
+
* Works in modern browsers and Node.js >= 18 (which exposes a global `fetch`).
|
|
6
|
+
* For older Node.js versions, polyfill `globalThis.fetch` before calling.
|
|
7
|
+
*
|
|
8
|
+
* @param options Optional settings.
|
|
9
|
+
* @returns A promise that resolves with the response body returned by baidu.com.
|
|
5
10
|
*/
|
|
6
|
-
declare function hello(): string
|
|
11
|
+
declare function hello(options?: hello.HelloOptions): Promise<string>;
|
|
7
12
|
|
|
8
13
|
declare namespace hello {
|
|
14
|
+
interface HelloOptions {
|
|
15
|
+
/** Request timeout in milliseconds. Defaults to 5000. */
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
}
|
|
9
18
|
// Named export to support `import { hello } from 'wisdomtreetest'`.
|
|
10
|
-
const hello: () => string
|
|
19
|
+
const hello: (options?: HelloOptions) => Promise<string>;
|
|
11
20
|
// Default export interop.
|
|
12
21
|
const _default: typeof hello;
|
|
13
22
|
export { _default as default };
|
package/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const https = require('https');
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
|
-
* Fetch https://baidu.com
|
|
4
|
+
* Fetch https://baidu.com using the platform's `fetch` API and print the
|
|
5
|
+
* response body to stdout. Works in both modern browsers and Node.js >= 18
|
|
6
|
+
* (which exposes a global `fetch`). For older Node.js versions, the caller
|
|
7
|
+
* is expected to polyfill `globalThis.fetch` (e.g. via `node-fetch`).
|
|
7
8
|
*
|
|
8
9
|
* @param {object} [options] Optional settings.
|
|
9
10
|
* @param {number} [options.timeoutMs=5000] Request timeout in milliseconds.
|
|
@@ -16,34 +17,38 @@ async function hello({ timeoutMs = 5000 } = {}) {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
|
-
* Perform
|
|
20
|
-
* the
|
|
20
|
+
* Perform a GET request against https://baidu.com using the global `fetch`
|
|
21
|
+
* and resolve with the response body as a UTF-8 string.
|
|
21
22
|
*
|
|
22
23
|
* @param {number} timeoutMs Request timeout in milliseconds.
|
|
23
24
|
* @returns {Promise<string>} The response body.
|
|
24
25
|
*/
|
|
25
|
-
function fetchBaiduHomepage(timeoutMs) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
reject(new Error(`Unexpected status code from baidu.com: ${statusCode}`));
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
26
|
+
async function fetchBaiduHomepage(timeoutMs) {
|
|
27
|
+
if (typeof globalThis.fetch !== 'function') {
|
|
28
|
+
throw new Error(
|
|
29
|
+
'Global `fetch` is not available. Please use Node.js >= 18 or polyfill `globalThis.fetch`.'
|
|
30
|
+
);
|
|
31
|
+
}
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
response.on('error', reject);
|
|
40
|
-
});
|
|
33
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
34
|
+
const timeoutId = controller
|
|
35
|
+
? setTimeout(() => controller.abort(new Error(`Request to baidu.com timed out after ${timeoutMs}ms`)), timeoutMs)
|
|
36
|
+
: null;
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
try {
|
|
39
|
+
const response = await globalThis.fetch('https://baidu.com', {
|
|
40
|
+
method: 'GET',
|
|
41
|
+
signal: controller ? controller.signal : undefined,
|
|
44
42
|
});
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
throw new Error(`Unexpected status code from baidu.com: ${response.status}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return await response.text();
|
|
49
|
+
} finally {
|
|
50
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
51
|
+
}
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
module.exports = hello;
|
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wisdomtreetest",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "An isomorphic helper that fetches https://baidu.com and prints the response.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hello",
|
|
7
7
|
"demo",
|
|
8
8
|
"wisdomtreetest",
|
|
9
9
|
"example"
|
|
10
10
|
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"install": "echo 2222222"
|
|
13
|
-
},
|
|
11
|
+
"scripts": {},
|
|
14
12
|
"homepage": "https://github.com/wisdomtree/wisdomtreetest#readme",
|
|
15
13
|
"bugs": {
|
|
16
14
|
"url": "https://github.com/wisdomtree/wisdomtreetest/issues",
|
|
@@ -63,14 +61,12 @@
|
|
|
63
61
|
"config": {
|
|
64
62
|
"name": "wisdomtreetest"
|
|
65
63
|
},
|
|
66
|
-
"dependencies": {
|
|
67
|
-
"wisdomtreetest": "^1.0.0"
|
|
68
|
-
},
|
|
64
|
+
"dependencies": {},
|
|
69
65
|
"peerDependenciesMeta": {},
|
|
70
66
|
"bundledDependencies": [],
|
|
71
67
|
"overrides": {},
|
|
72
68
|
"engines": {
|
|
73
|
-
"node": ">=
|
|
69
|
+
"node": ">=18.0.0",
|
|
74
70
|
"npm": ">=6.0.0"
|
|
75
71
|
},
|
|
76
72
|
"os": [
|