@faasjs/request 0.0.3-beta.7 → 0.0.3-beta.71
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 +3 -3
- package/dist/index.d.ts +9 -0
- package/dist/index.js +203 -3
- package/dist/index.mjs +230 -4
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -52,10 +52,10 @@ ___
|
|
|
52
52
|
| Name | Type | Description |
|
|
53
53
|
| :------ | :------ | :------ |
|
|
54
54
|
| `agent?` | `boolean` | - |
|
|
55
|
-
| `auth?` | `string` | The authentication credentials to use for the request.
|
|
55
|
+
| `auth?` | `string` | The authentication credentials to use for the request. Format: `username:password` |
|
|
56
56
|
| `body?` | { `[key: string]`: `any`; } \| `string` | - |
|
|
57
|
-
| `downloadStream?` | `NodeJS.WritableStream` | Create a write stream to download a file. |
|
|
58
|
-
| `file?` | `string` | Path of uploading a file to the server. |
|
|
57
|
+
| `downloadStream?` | `NodeJS.WritableStream` | Create a write stream to download a file. ```ts const stream = createWriteStream('filepath') await request('https://example.com', { downloadStream: stream }) ``` |
|
|
58
|
+
| `file?` | `string` | Path of uploading a file to the server. ```ts await request('https://example.com', { file: 'filepath' }) ``` |
|
|
59
59
|
| `headers?` | `http.OutgoingHttpHeaders` | - |
|
|
60
60
|
| `logger?` | `Logger` | - |
|
|
61
61
|
| `method?` | `string` | The HTTP method to use when making the request. Defaults to GET. |
|
package/dist/index.d.ts
CHANGED
|
@@ -39,10 +39,19 @@ type RequestOptions = {
|
|
|
39
39
|
auth?: string;
|
|
40
40
|
/**
|
|
41
41
|
* Path of uploading a file to the server.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* await request('https://example.com', { file: 'filepath' })
|
|
45
|
+
* ```
|
|
42
46
|
*/
|
|
43
47
|
file?: string;
|
|
44
48
|
/**
|
|
45
49
|
* Create a write stream to download a file.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* const stream = createWriteStream('filepath')
|
|
53
|
+
* await request('https://example.com', { downloadStream: stream })
|
|
54
|
+
* ```
|
|
46
55
|
*/
|
|
47
56
|
downloadStream?: NodeJS.WritableStream;
|
|
48
57
|
pfx?: Buffer;
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
8
11
|
var __export = (target, all) => {
|
|
9
12
|
for (var name in all)
|
|
10
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -18,11 +21,207 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
21
|
return to;
|
|
19
22
|
};
|
|
20
23
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
28
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
29
|
mod
|
|
23
30
|
));
|
|
24
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
32
|
|
|
33
|
+
// ../logger/dist/index.js
|
|
34
|
+
var require_dist = __commonJS({
|
|
35
|
+
"../logger/dist/index.js"(exports, module2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
var __defProp2 = Object.defineProperty;
|
|
38
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
39
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
40
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
41
|
+
var __export2 = (target, all) => {
|
|
42
|
+
for (var name in all)
|
|
43
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
44
|
+
};
|
|
45
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
46
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
47
|
+
for (let key of __getOwnPropNames2(from))
|
|
48
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
49
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
50
|
+
}
|
|
51
|
+
return to;
|
|
52
|
+
};
|
|
53
|
+
var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
54
|
+
var src_exports2 = {};
|
|
55
|
+
__export2(src_exports2, {
|
|
56
|
+
Color: () => Color,
|
|
57
|
+
Logger: () => Logger2
|
|
58
|
+
});
|
|
59
|
+
module2.exports = __toCommonJS2(src_exports2);
|
|
60
|
+
var import_util = require("util");
|
|
61
|
+
var Color = /* @__PURE__ */ ((Color2) => {
|
|
62
|
+
Color2[Color2["DEFAULT"] = 39] = "DEFAULT";
|
|
63
|
+
Color2[Color2["BLACK"] = 30] = "BLACK";
|
|
64
|
+
Color2[Color2["RED"] = 31] = "RED";
|
|
65
|
+
Color2[Color2["GREEN"] = 32] = "GREEN";
|
|
66
|
+
Color2[Color2["ORANGE"] = 33] = "ORANGE";
|
|
67
|
+
Color2[Color2["BLUE"] = 34] = "BLUE";
|
|
68
|
+
Color2[Color2["MAGENTA"] = 35] = "MAGENTA";
|
|
69
|
+
Color2[Color2["CYAN"] = 36] = "CYAN";
|
|
70
|
+
Color2[Color2["GRAY"] = 90] = "GRAY";
|
|
71
|
+
return Color2;
|
|
72
|
+
})(Color || {});
|
|
73
|
+
var LevelColor = ((LevelColor2) => {
|
|
74
|
+
LevelColor2[
|
|
75
|
+
LevelColor2["debug"] = 90
|
|
76
|
+
/* GRAY */
|
|
77
|
+
] = "debug";
|
|
78
|
+
LevelColor2[
|
|
79
|
+
LevelColor2["info"] = 32
|
|
80
|
+
/* GREEN */
|
|
81
|
+
] = "info";
|
|
82
|
+
LevelColor2[
|
|
83
|
+
LevelColor2["warn"] = 33
|
|
84
|
+
/* ORANGE */
|
|
85
|
+
] = "warn";
|
|
86
|
+
LevelColor2[
|
|
87
|
+
LevelColor2["error"] = 31
|
|
88
|
+
/* RED */
|
|
89
|
+
] = "error";
|
|
90
|
+
return LevelColor2;
|
|
91
|
+
})(LevelColor || {});
|
|
92
|
+
var LevelPriority = {
|
|
93
|
+
debug: 0,
|
|
94
|
+
info: 1,
|
|
95
|
+
warn: 2,
|
|
96
|
+
error: 3
|
|
97
|
+
};
|
|
98
|
+
var Logger2 = class {
|
|
99
|
+
/**
|
|
100
|
+
* @param label {string} Prefix label
|
|
101
|
+
*/
|
|
102
|
+
constructor(label) {
|
|
103
|
+
this.colorfyOutput = true;
|
|
104
|
+
if (label)
|
|
105
|
+
this.label = label;
|
|
106
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
107
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
108
|
+
this.colorfyOutput = false;
|
|
109
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
110
|
+
this.cachedTimers = {};
|
|
111
|
+
this.size = 1e3;
|
|
112
|
+
this.stdout = console.log;
|
|
113
|
+
this.stderr = console.error;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @param message {string} message
|
|
117
|
+
* @param args {...any=} arguments
|
|
118
|
+
*/
|
|
119
|
+
debug(message, ...args) {
|
|
120
|
+
this.log("debug", message, ...args);
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @param message {string} message
|
|
125
|
+
* @param args {...any=} arguments
|
|
126
|
+
*/
|
|
127
|
+
info(message, ...args) {
|
|
128
|
+
this.log("info", message, ...args);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @param message {string} message
|
|
133
|
+
* @param args {...any=} arguments
|
|
134
|
+
*/
|
|
135
|
+
warn(message, ...args) {
|
|
136
|
+
this.log("warn", message, ...args);
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @param message {any} message or Error object
|
|
141
|
+
* @param args {...any=} arguments
|
|
142
|
+
*/
|
|
143
|
+
error(message, ...args) {
|
|
144
|
+
let stack = false;
|
|
145
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
146
|
+
if (e.stack) {
|
|
147
|
+
stack = true;
|
|
148
|
+
this.log("error", e.stack);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
if (!stack)
|
|
152
|
+
this.log("error", message, ...args);
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* @param key {string} timer's label
|
|
157
|
+
* @param level [string=debug] 日志级别,支持 debug、info、warn、error
|
|
158
|
+
*/
|
|
159
|
+
time(key, level = "debug") {
|
|
160
|
+
this.cachedTimers[key] = {
|
|
161
|
+
level,
|
|
162
|
+
time: (/* @__PURE__ */ new Date()).getTime()
|
|
163
|
+
};
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @param key {string} timer's label
|
|
168
|
+
* @param message {string} message
|
|
169
|
+
* @param args {...any=} arguments
|
|
170
|
+
*/
|
|
171
|
+
timeEnd(key, message, ...args) {
|
|
172
|
+
if (this.cachedTimers[key]) {
|
|
173
|
+
const timer = this.cachedTimers[key];
|
|
174
|
+
message = message + " +%ims";
|
|
175
|
+
args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
|
|
176
|
+
this[timer.level](message, ...args);
|
|
177
|
+
delete this.cachedTimers[key];
|
|
178
|
+
} else {
|
|
179
|
+
this.warn("timeEnd not found key %s", key);
|
|
180
|
+
this.debug(message);
|
|
181
|
+
}
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @param message {string} message
|
|
186
|
+
* @param args {...any=} arguments
|
|
187
|
+
*/
|
|
188
|
+
raw(message, ...args) {
|
|
189
|
+
if (this.silent)
|
|
190
|
+
return this;
|
|
191
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* @param color {number} color code
|
|
196
|
+
* @param message {string} message
|
|
197
|
+
*/
|
|
198
|
+
colorfy(color, message) {
|
|
199
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
200
|
+
}
|
|
201
|
+
log(level, message, ...args) {
|
|
202
|
+
if (this.silent)
|
|
203
|
+
return this;
|
|
204
|
+
if (LevelPriority[level] < this.level)
|
|
205
|
+
return this;
|
|
206
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
207
|
+
if (this.colorfyOutput && level !== "error")
|
|
208
|
+
output = this.colorfy(LevelColor[level], output);
|
|
209
|
+
else if (!this.colorfyOutput)
|
|
210
|
+
output = output.replace(/\n/g, "");
|
|
211
|
+
if (!output)
|
|
212
|
+
return this;
|
|
213
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
214
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
215
|
+
if (level === "error")
|
|
216
|
+
this.stderr(output);
|
|
217
|
+
else
|
|
218
|
+
this.stdout(output);
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
26
225
|
// src/index.ts
|
|
27
226
|
var src_exports = {};
|
|
28
227
|
__export(src_exports, {
|
|
@@ -36,7 +235,7 @@ var https = __toESM(require("https"));
|
|
|
36
235
|
var import_url = require("url");
|
|
37
236
|
var import_fs = require("fs");
|
|
38
237
|
var import_path = require("path");
|
|
39
|
-
var import_logger =
|
|
238
|
+
var import_logger = __toESM(require_dist());
|
|
40
239
|
var mock = null;
|
|
41
240
|
function setMock(handler) {
|
|
42
241
|
mock = handler;
|
|
@@ -136,7 +335,7 @@ async function request(url, {
|
|
|
136
335
|
res.on("end", () => {
|
|
137
336
|
const data = Buffer.concat(raw).toString();
|
|
138
337
|
logger.timeEnd(url, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
|
|
139
|
-
const response = /* @__PURE__ */ Object.create(null);
|
|
338
|
+
const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
|
|
140
339
|
response.request = options;
|
|
141
340
|
response.request.body = body;
|
|
142
341
|
response.statusCode = res.statusCode;
|
|
@@ -145,7 +344,7 @@ async function request(url, {
|
|
|
145
344
|
response.body = data;
|
|
146
345
|
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
147
346
|
try {
|
|
148
|
-
response.body = parse
|
|
347
|
+
response.body = (parse || JSON.parse)(response.body);
|
|
149
348
|
logger.debug("response.parse JSON");
|
|
150
349
|
} catch (error) {
|
|
151
350
|
console.warn("response plain body", response.body);
|
|
@@ -155,6 +354,7 @@ async function request(url, {
|
|
|
155
354
|
resolve(response);
|
|
156
355
|
else {
|
|
157
356
|
logger.debug("response.error %j", response);
|
|
357
|
+
response.message = `${res.statusCode} ${res.statusMessage}`;
|
|
158
358
|
reject(response);
|
|
159
359
|
}
|
|
160
360
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,235 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
+
}) : x)(function(x) {
|
|
10
|
+
if (typeof require !== "undefined")
|
|
11
|
+
return require.apply(this, arguments);
|
|
12
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
13
|
+
});
|
|
14
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
15
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
31
|
+
mod
|
|
32
|
+
));
|
|
33
|
+
|
|
34
|
+
// ../logger/dist/index.js
|
|
35
|
+
var require_dist = __commonJS({
|
|
36
|
+
"../logger/dist/index.js"(exports, module) {
|
|
37
|
+
"use strict";
|
|
38
|
+
var __defProp2 = Object.defineProperty;
|
|
39
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
40
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
41
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
42
|
+
var __export = (target, all) => {
|
|
43
|
+
for (var name in all)
|
|
44
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
45
|
+
};
|
|
46
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
47
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
48
|
+
for (let key of __getOwnPropNames2(from))
|
|
49
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
50
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
51
|
+
}
|
|
52
|
+
return to;
|
|
53
|
+
};
|
|
54
|
+
var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
55
|
+
var src_exports = {};
|
|
56
|
+
__export(src_exports, {
|
|
57
|
+
Color: () => Color,
|
|
58
|
+
Logger: () => Logger2
|
|
59
|
+
});
|
|
60
|
+
module.exports = __toCommonJS(src_exports);
|
|
61
|
+
var import_util = __require("util");
|
|
62
|
+
var Color = /* @__PURE__ */ ((Color2) => {
|
|
63
|
+
Color2[Color2["DEFAULT"] = 39] = "DEFAULT";
|
|
64
|
+
Color2[Color2["BLACK"] = 30] = "BLACK";
|
|
65
|
+
Color2[Color2["RED"] = 31] = "RED";
|
|
66
|
+
Color2[Color2["GREEN"] = 32] = "GREEN";
|
|
67
|
+
Color2[Color2["ORANGE"] = 33] = "ORANGE";
|
|
68
|
+
Color2[Color2["BLUE"] = 34] = "BLUE";
|
|
69
|
+
Color2[Color2["MAGENTA"] = 35] = "MAGENTA";
|
|
70
|
+
Color2[Color2["CYAN"] = 36] = "CYAN";
|
|
71
|
+
Color2[Color2["GRAY"] = 90] = "GRAY";
|
|
72
|
+
return Color2;
|
|
73
|
+
})(Color || {});
|
|
74
|
+
var LevelColor = ((LevelColor2) => {
|
|
75
|
+
LevelColor2[
|
|
76
|
+
LevelColor2["debug"] = 90
|
|
77
|
+
/* GRAY */
|
|
78
|
+
] = "debug";
|
|
79
|
+
LevelColor2[
|
|
80
|
+
LevelColor2["info"] = 32
|
|
81
|
+
/* GREEN */
|
|
82
|
+
] = "info";
|
|
83
|
+
LevelColor2[
|
|
84
|
+
LevelColor2["warn"] = 33
|
|
85
|
+
/* ORANGE */
|
|
86
|
+
] = "warn";
|
|
87
|
+
LevelColor2[
|
|
88
|
+
LevelColor2["error"] = 31
|
|
89
|
+
/* RED */
|
|
90
|
+
] = "error";
|
|
91
|
+
return LevelColor2;
|
|
92
|
+
})(LevelColor || {});
|
|
93
|
+
var LevelPriority = {
|
|
94
|
+
debug: 0,
|
|
95
|
+
info: 1,
|
|
96
|
+
warn: 2,
|
|
97
|
+
error: 3
|
|
98
|
+
};
|
|
99
|
+
var Logger2 = class {
|
|
100
|
+
/**
|
|
101
|
+
* @param label {string} Prefix label
|
|
102
|
+
*/
|
|
103
|
+
constructor(label) {
|
|
104
|
+
this.colorfyOutput = true;
|
|
105
|
+
if (label)
|
|
106
|
+
this.label = label;
|
|
107
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
108
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
109
|
+
this.colorfyOutput = false;
|
|
110
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
111
|
+
this.cachedTimers = {};
|
|
112
|
+
this.size = 1e3;
|
|
113
|
+
this.stdout = console.log;
|
|
114
|
+
this.stderr = console.error;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* @param message {string} message
|
|
118
|
+
* @param args {...any=} arguments
|
|
119
|
+
*/
|
|
120
|
+
debug(message, ...args) {
|
|
121
|
+
this.log("debug", message, ...args);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @param message {string} message
|
|
126
|
+
* @param args {...any=} arguments
|
|
127
|
+
*/
|
|
128
|
+
info(message, ...args) {
|
|
129
|
+
this.log("info", message, ...args);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @param message {string} message
|
|
134
|
+
* @param args {...any=} arguments
|
|
135
|
+
*/
|
|
136
|
+
warn(message, ...args) {
|
|
137
|
+
this.log("warn", message, ...args);
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @param message {any} message or Error object
|
|
142
|
+
* @param args {...any=} arguments
|
|
143
|
+
*/
|
|
144
|
+
error(message, ...args) {
|
|
145
|
+
let stack = false;
|
|
146
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
147
|
+
if (e.stack) {
|
|
148
|
+
stack = true;
|
|
149
|
+
this.log("error", e.stack);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
if (!stack)
|
|
153
|
+
this.log("error", message, ...args);
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @param key {string} timer's label
|
|
158
|
+
* @param level [string=debug] 日志级别,支持 debug、info、warn、error
|
|
159
|
+
*/
|
|
160
|
+
time(key, level = "debug") {
|
|
161
|
+
this.cachedTimers[key] = {
|
|
162
|
+
level,
|
|
163
|
+
time: (/* @__PURE__ */ new Date()).getTime()
|
|
164
|
+
};
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @param key {string} timer's label
|
|
169
|
+
* @param message {string} message
|
|
170
|
+
* @param args {...any=} arguments
|
|
171
|
+
*/
|
|
172
|
+
timeEnd(key, message, ...args) {
|
|
173
|
+
if (this.cachedTimers[key]) {
|
|
174
|
+
const timer = this.cachedTimers[key];
|
|
175
|
+
message = message + " +%ims";
|
|
176
|
+
args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
|
|
177
|
+
this[timer.level](message, ...args);
|
|
178
|
+
delete this.cachedTimers[key];
|
|
179
|
+
} else {
|
|
180
|
+
this.warn("timeEnd not found key %s", key);
|
|
181
|
+
this.debug(message);
|
|
182
|
+
}
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @param message {string} message
|
|
187
|
+
* @param args {...any=} arguments
|
|
188
|
+
*/
|
|
189
|
+
raw(message, ...args) {
|
|
190
|
+
if (this.silent)
|
|
191
|
+
return this;
|
|
192
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* @param color {number} color code
|
|
197
|
+
* @param message {string} message
|
|
198
|
+
*/
|
|
199
|
+
colorfy(color, message) {
|
|
200
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
201
|
+
}
|
|
202
|
+
log(level, message, ...args) {
|
|
203
|
+
if (this.silent)
|
|
204
|
+
return this;
|
|
205
|
+
if (LevelPriority[level] < this.level)
|
|
206
|
+
return this;
|
|
207
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
208
|
+
if (this.colorfyOutput && level !== "error")
|
|
209
|
+
output = this.colorfy(LevelColor[level], output);
|
|
210
|
+
else if (!this.colorfyOutput)
|
|
211
|
+
output = output.replace(/\n/g, "");
|
|
212
|
+
if (!output)
|
|
213
|
+
return this;
|
|
214
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
215
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
216
|
+
if (level === "error")
|
|
217
|
+
this.stderr(output);
|
|
218
|
+
else
|
|
219
|
+
this.stdout(output);
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
1
226
|
// src/index.ts
|
|
227
|
+
var import_logger = __toESM(require_dist());
|
|
2
228
|
import * as http from "http";
|
|
3
229
|
import * as https from "https";
|
|
4
230
|
import { URL } from "url";
|
|
5
231
|
import { readFileSync } from "fs";
|
|
6
232
|
import { basename } from "path";
|
|
7
|
-
import { Logger } from "@faasjs/logger";
|
|
8
233
|
var mock = null;
|
|
9
234
|
function setMock(handler) {
|
|
10
235
|
mock = handler;
|
|
@@ -44,7 +269,7 @@ async function request(url, {
|
|
|
44
269
|
logger
|
|
45
270
|
} = { headers: {} }) {
|
|
46
271
|
if (!logger)
|
|
47
|
-
logger = new Logger("request");
|
|
272
|
+
logger = new import_logger.Logger("request");
|
|
48
273
|
if (mock)
|
|
49
274
|
return mock(url, {
|
|
50
275
|
headers,
|
|
@@ -104,7 +329,7 @@ async function request(url, {
|
|
|
104
329
|
res.on("end", () => {
|
|
105
330
|
const data = Buffer.concat(raw).toString();
|
|
106
331
|
logger.timeEnd(url, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
|
|
107
|
-
const response = /* @__PURE__ */ Object.create(null);
|
|
332
|
+
const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
|
|
108
333
|
response.request = options;
|
|
109
334
|
response.request.body = body;
|
|
110
335
|
response.statusCode = res.statusCode;
|
|
@@ -113,7 +338,7 @@ async function request(url, {
|
|
|
113
338
|
response.body = data;
|
|
114
339
|
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
115
340
|
try {
|
|
116
|
-
response.body = parse
|
|
341
|
+
response.body = (parse || JSON.parse)(response.body);
|
|
117
342
|
logger.debug("response.parse JSON");
|
|
118
343
|
} catch (error) {
|
|
119
344
|
console.warn("response plain body", response.body);
|
|
@@ -123,6 +348,7 @@ async function request(url, {
|
|
|
123
348
|
resolve(response);
|
|
124
349
|
else {
|
|
125
350
|
logger.debug("response.error %j", response);
|
|
351
|
+
response.message = `${res.statusCode} ${res.statusMessage}`;
|
|
126
352
|
reject(response);
|
|
127
353
|
}
|
|
128
354
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/request",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.71",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,14 +15,13 @@
|
|
|
15
15
|
},
|
|
16
16
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tsup-node src/index.ts --format esm,cjs"
|
|
19
|
-
"build:types": "tsup-node src/index.ts --dts-only"
|
|
18
|
+
"build": "tsup-node src/index.ts --format esm,cjs --dts --clean"
|
|
20
19
|
},
|
|
21
20
|
"files": [
|
|
22
21
|
"dist"
|
|
23
22
|
],
|
|
24
23
|
"dependencies": {
|
|
25
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
24
|
+
"@faasjs/logger": "^0.0.3-beta.71"
|
|
26
25
|
},
|
|
27
26
|
"engines": {
|
|
28
27
|
"npm": ">=8.0.0",
|