@faasjs/request 0.0.3-beta.86 → 0.0.3-beta.88
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/dist/index.d.mts +96 -0
- package/dist/index.js +54 -64
- package/dist/index.mjs +9 -11
- package/package.json +3 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import * as http from 'http';
|
|
2
|
+
import { Logger } from '@faasjs/logger';
|
|
3
|
+
|
|
4
|
+
type Request = {
|
|
5
|
+
headers?: http.OutgoingHttpHeaders;
|
|
6
|
+
method?: string;
|
|
7
|
+
host?: string;
|
|
8
|
+
path?: string;
|
|
9
|
+
query?: http.OutgoingHttpHeaders;
|
|
10
|
+
body?: {
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
type Response<T = any> = {
|
|
15
|
+
request?: Request;
|
|
16
|
+
statusCode?: number;
|
|
17
|
+
statusMessage?: string;
|
|
18
|
+
headers: http.OutgoingHttpHeaders;
|
|
19
|
+
body: T;
|
|
20
|
+
};
|
|
21
|
+
type RequestOptions = {
|
|
22
|
+
headers?: http.OutgoingHttpHeaders;
|
|
23
|
+
/**
|
|
24
|
+
* The HTTP method to use when making the request. Defaults to GET.
|
|
25
|
+
*/
|
|
26
|
+
method?: string;
|
|
27
|
+
query?: {
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
};
|
|
30
|
+
body?: {
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
} | string;
|
|
33
|
+
timeout?: number;
|
|
34
|
+
/**
|
|
35
|
+
* The authentication credentials to use for the request.
|
|
36
|
+
*
|
|
37
|
+
* Format: `username:password`
|
|
38
|
+
*/
|
|
39
|
+
auth?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Path of uploading a file to the server.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* await request('https://example.com', { file: 'filepath' })
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
file?: string;
|
|
48
|
+
/**
|
|
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
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
downloadStream?: NodeJS.WritableStream;
|
|
57
|
+
pfx?: Buffer;
|
|
58
|
+
passphrase?: string;
|
|
59
|
+
agent?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Body parser. Defaults to `JSON.parse`.
|
|
62
|
+
*/
|
|
63
|
+
parse?: (body: string) => any;
|
|
64
|
+
logger?: Logger;
|
|
65
|
+
};
|
|
66
|
+
type Mock = (url: string, options: RequestOptions) => Promise<Response>;
|
|
67
|
+
/**
|
|
68
|
+
* Mock requests
|
|
69
|
+
* @param handler {function | null} null to disable mock
|
|
70
|
+
* @example setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))
|
|
71
|
+
*/
|
|
72
|
+
declare function setMock(handler: Mock | null): void;
|
|
73
|
+
declare function querystringify(obj: any): string;
|
|
74
|
+
/**
|
|
75
|
+
* Request
|
|
76
|
+
* @param {string} url Url
|
|
77
|
+
* @param {object=} [options={}] Options
|
|
78
|
+
* @param {string} [options.method=GET] Method
|
|
79
|
+
* @param {object} [options.query={}] Query
|
|
80
|
+
* @param {object} [options.headers={}] Headers
|
|
81
|
+
* @param {object=} options.body Body
|
|
82
|
+
* @param {number=} options.timeout Timeout
|
|
83
|
+
* @param {string=} options.auth Auth, format: user:password
|
|
84
|
+
* @param {string=} options.file Upload file path
|
|
85
|
+
* @param {WritableStream=} options.downloadStream Download stream
|
|
86
|
+
* @param {Buffer=} options.pfx pfx
|
|
87
|
+
* @param {string=} options.passphrase passphrase
|
|
88
|
+
* @param {boolean=} options.agent agent
|
|
89
|
+
* @param {parse=} options.parse body parser, default is JSON.parse
|
|
90
|
+
*
|
|
91
|
+
* @returns {promise}
|
|
92
|
+
* @url https://faasjs.com/doc/request.html
|
|
93
|
+
*/
|
|
94
|
+
declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
|
|
95
|
+
|
|
96
|
+
export { Request, RequestOptions, Response, querystringify, request, setMock };
|
package/dist/index.js
CHANGED
|
@@ -1,42 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var http = require('http');
|
|
4
|
+
var https = require('https');
|
|
5
|
+
var url = require('url');
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var path = require('path');
|
|
8
|
+
var logger = require('@faasjs/logger');
|
|
9
|
+
|
|
10
|
+
function _interopNamespace(e) {
|
|
11
|
+
if (e && e.__esModule) return e;
|
|
12
|
+
var n = Object.create(null);
|
|
13
|
+
if (e) {
|
|
14
|
+
Object.keys(e).forEach(function (k) {
|
|
15
|
+
if (k !== 'default') {
|
|
16
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
17
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () { return e[k]; }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
17
23
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
)
|
|
24
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
n.default = e;
|
|
25
|
+
return Object.freeze(n);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var http__namespace = /*#__PURE__*/_interopNamespace(http);
|
|
29
|
+
var https__namespace = /*#__PURE__*/_interopNamespace(https);
|
|
25
30
|
|
|
26
31
|
// src/index.ts
|
|
27
|
-
var src_exports = {};
|
|
28
|
-
__export(src_exports, {
|
|
29
|
-
querystringify: () => querystringify,
|
|
30
|
-
request: () => request,
|
|
31
|
-
setMock: () => setMock
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(src_exports);
|
|
34
|
-
var http = __toESM(require("http"));
|
|
35
|
-
var https = __toESM(require("https"));
|
|
36
|
-
var import_url = require("url");
|
|
37
|
-
var import_fs = require("fs");
|
|
38
|
-
var import_path = require("path");
|
|
39
|
-
var import_logger = require("@faasjs/logger");
|
|
40
32
|
var mock = null;
|
|
41
33
|
function setMock(handler) {
|
|
42
34
|
mock = handler;
|
|
@@ -60,7 +52,7 @@ function querystringify(obj) {
|
|
|
60
52
|
}
|
|
61
53
|
return pairs.length ? pairs.join("&") : "";
|
|
62
54
|
}
|
|
63
|
-
async function request(url, {
|
|
55
|
+
async function request(url$1, {
|
|
64
56
|
headers,
|
|
65
57
|
method,
|
|
66
58
|
query,
|
|
@@ -73,18 +65,18 @@ async function request(url, {
|
|
|
73
65
|
passphrase,
|
|
74
66
|
agent,
|
|
75
67
|
parse,
|
|
76
|
-
logger
|
|
68
|
+
logger: logger$1
|
|
77
69
|
} = { headers: {} }) {
|
|
78
|
-
if (!logger)
|
|
79
|
-
logger = new
|
|
70
|
+
if (!logger$1)
|
|
71
|
+
logger$1 = new logger.Logger("request");
|
|
80
72
|
if (mock) {
|
|
81
|
-
logger.debug("mock %s %j", url, {
|
|
73
|
+
logger$1.debug("mock %s %j", url$1, {
|
|
82
74
|
headers,
|
|
83
75
|
method,
|
|
84
76
|
query,
|
|
85
77
|
body
|
|
86
78
|
});
|
|
87
|
-
return mock(url, {
|
|
79
|
+
return mock(url$1, {
|
|
88
80
|
headers,
|
|
89
81
|
method,
|
|
90
82
|
query,
|
|
@@ -92,14 +84,14 @@ async function request(url, {
|
|
|
92
84
|
});
|
|
93
85
|
}
|
|
94
86
|
if (query) {
|
|
95
|
-
if (!url.includes("?"))
|
|
96
|
-
url += "?";
|
|
97
|
-
else if (!url.endsWith("?"))
|
|
98
|
-
url += "&";
|
|
99
|
-
url += querystringify(query);
|
|
87
|
+
if (!url$1.includes("?"))
|
|
88
|
+
url$1 += "?";
|
|
89
|
+
else if (!url$1.endsWith("?"))
|
|
90
|
+
url$1 += "&";
|
|
91
|
+
url$1 += querystringify(query);
|
|
100
92
|
}
|
|
101
|
-
const uri = new
|
|
102
|
-
const protocol = uri.protocol === "https:" ?
|
|
93
|
+
const uri = new url.URL(url$1);
|
|
94
|
+
const protocol = uri.protocol === "https:" ? https__namespace : http__namespace;
|
|
103
95
|
if (!uri.protocol)
|
|
104
96
|
throw Error("Unknown protocol");
|
|
105
97
|
const options = {
|
|
@@ -125,7 +117,7 @@ async function request(url, {
|
|
|
125
117
|
if (body && !options.headers["Content-Length"])
|
|
126
118
|
options.headers["Content-Length"] = Buffer.byteLength(body);
|
|
127
119
|
return await new Promise(function(resolve, reject) {
|
|
128
|
-
logger.debug("request %j", {
|
|
120
|
+
logger$1.debug("request %j", {
|
|
129
121
|
...options,
|
|
130
122
|
body
|
|
131
123
|
});
|
|
@@ -142,7 +134,7 @@ async function request(url, {
|
|
|
142
134
|
});
|
|
143
135
|
res.on("end", () => {
|
|
144
136
|
const data = Buffer.concat(raw).toString();
|
|
145
|
-
logger.timeEnd(url, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
|
|
137
|
+
logger$1.timeEnd(url$1, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
|
|
146
138
|
const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
|
|
147
139
|
response.request = options;
|
|
148
140
|
response.request.body = body;
|
|
@@ -153,7 +145,7 @@ async function request(url, {
|
|
|
153
145
|
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
154
146
|
try {
|
|
155
147
|
response.body = (parse || JSON.parse)(response.body);
|
|
156
|
-
logger.debug("response.parse JSON");
|
|
148
|
+
logger$1.debug("response.parse JSON");
|
|
157
149
|
} catch (error) {
|
|
158
150
|
console.warn("response plain body", response.body);
|
|
159
151
|
console.error(error);
|
|
@@ -161,8 +153,8 @@ async function request(url, {
|
|
|
161
153
|
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
162
154
|
resolve(response);
|
|
163
155
|
else {
|
|
164
|
-
logger.debug("response.error %j", response);
|
|
165
|
-
response.message = `${res.statusCode} ${res.statusMessage} ${url}`;
|
|
156
|
+
logger$1.debug("response.error %j", response);
|
|
157
|
+
response.message = `${res.statusCode} ${res.statusMessage} ${url$1}`;
|
|
166
158
|
reject(response);
|
|
167
159
|
}
|
|
168
160
|
});
|
|
@@ -174,10 +166,10 @@ async function request(url, {
|
|
|
174
166
|
const crlf = "\r\n";
|
|
175
167
|
const boundary = `--${Math.random().toString(16)}`;
|
|
176
168
|
const delimiter = `${crlf}--${boundary}`;
|
|
177
|
-
const headers2 = [`Content-Disposition: form-data; name="file"; filename="${
|
|
169
|
+
const headers2 = [`Content-Disposition: form-data; name="file"; filename="${path.basename(file)}"${crlf}`];
|
|
178
170
|
const multipartBody = Buffer.concat([
|
|
179
171
|
Buffer.from(delimiter + crlf + headers2.join("") + crlf),
|
|
180
|
-
|
|
172
|
+
fs.readFileSync(file),
|
|
181
173
|
Buffer.from(`${delimiter}--`)
|
|
182
174
|
]);
|
|
183
175
|
req.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
|
|
@@ -185,16 +177,14 @@ async function request(url, {
|
|
|
185
177
|
req.write(multipartBody);
|
|
186
178
|
}
|
|
187
179
|
req.on("error", function(e) {
|
|
188
|
-
logger.timeEnd(url, "response.error %j", e);
|
|
180
|
+
logger$1.timeEnd(url$1, "response.error %j", e);
|
|
189
181
|
reject(e);
|
|
190
182
|
});
|
|
191
|
-
logger.time(url);
|
|
183
|
+
logger$1.time(url$1);
|
|
192
184
|
req.end();
|
|
193
185
|
});
|
|
194
186
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
setMock
|
|
200
|
-
});
|
|
187
|
+
|
|
188
|
+
exports.querystringify = querystringify;
|
|
189
|
+
exports.request = request;
|
|
190
|
+
exports.setMock = setMock;
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import * as http from 'http';
|
|
2
|
+
import * as https from 'https';
|
|
3
|
+
import { URL } from 'url';
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
5
|
+
import { basename } from 'path';
|
|
6
|
+
import { Logger } from '@faasjs/logger';
|
|
7
|
+
|
|
1
8
|
// src/index.ts
|
|
2
|
-
import * as http from "http";
|
|
3
|
-
import * as https from "https";
|
|
4
|
-
import { URL } from "url";
|
|
5
|
-
import { readFileSync } from "fs";
|
|
6
|
-
import { basename } from "path";
|
|
7
|
-
import { Logger } from "@faasjs/logger";
|
|
8
9
|
var mock = null;
|
|
9
10
|
function setMock(handler) {
|
|
10
11
|
mock = handler;
|
|
@@ -160,8 +161,5 @@ async function request(url, {
|
|
|
160
161
|
req.end();
|
|
161
162
|
});
|
|
162
163
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
request,
|
|
166
|
-
setMock
|
|
167
|
-
};
|
|
164
|
+
|
|
165
|
+
export { querystringify, request, setMock };
|
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.88",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,13 +15,13 @@
|
|
|
15
15
|
},
|
|
16
16
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tsup-node src/index.ts --
|
|
18
|
+
"build": "tsup-node src/index.ts --config ../../tsup.config.json"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
24
|
+
"@faasjs/logger": "^0.0.3-beta.88"
|
|
25
25
|
},
|
|
26
26
|
"engines": {
|
|
27
27
|
"npm": ">=8.0.0",
|