@faasjs/request 0.0.2-beta.229 → 0.0.2-beta.256

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/lib/index.d.ts CHANGED
@@ -48,6 +48,7 @@ declare type Mock = (url: string, options: RequestOptions) => Promise<Response>;
48
48
  /**
49
49
  * 设置模拟请求
50
50
  * @param handler {function | null} 模拟函数,若设置为 null 则表示清除模拟函数
51
+ * @example setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))
51
52
  */
52
53
  export declare function setMock(handler: Mock | null): void;
53
54
  export declare function querystringify(obj: any): string;
@@ -55,7 +56,7 @@ export declare function querystringify(obj: any): string;
55
56
  * 发起网络请求
56
57
  * @param {string} url 请求路径或完整网址
57
58
  * @param {object=} [options={}] 参数和配置
58
- * @param {string} [options.methd=GET] 请求方法
59
+ * @param {string} [options.method=GET] 请求方法
59
60
  * @param {object} [options.query={}] 请求参数,放置于 path 后,若需放置在 body 中,请使用 body 参数
60
61
  * @param {object} [options.headers={}] 请求头
61
62
  * @param {object=} options.body 请求体
package/lib/index.es.js CHANGED
@@ -3,12 +3,13 @@ import * as https from 'https';
3
3
  import { URL } from 'url';
4
4
  import { readFileSync } from 'fs';
5
5
  import { basename } from 'path';
6
- import Logger from '@faasjs/logger';
6
+ import { Logger } from '@faasjs/logger';
7
7
 
8
8
  let mock = null;
9
9
  /**
10
10
  * 设置模拟请求
11
11
  * @param handler {function | null} 模拟函数,若设置为 null 则表示清除模拟函数
12
+ * @example setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))
12
13
  */
13
14
  function setMock(handler) {
14
15
  mock = handler;
@@ -36,7 +37,7 @@ function querystringify(obj) {
36
37
  * 发起网络请求
37
38
  * @param {string} url 请求路径或完整网址
38
39
  * @param {object=} [options={}] 参数和配置
39
- * @param {string} [options.methd=GET] 请求方法
40
+ * @param {string} [options.method=GET] 请求方法
40
41
  * @param {object} [options.query={}] 请求参数,放置于 path 后,若需放置在 body 中,请使用 body 参数
41
42
  * @param {object} [options.headers={}] 请求头
42
43
  * @param {object=} options.body 请求体
@@ -76,7 +77,7 @@ async function request(url, { headers, method, query, body, timeout, auth, file,
76
77
  const uri = new URL(url);
77
78
  const protocol = uri.protocol === 'https:' ? https : http;
78
79
  if (!uri.protocol)
79
- throw Error('Unkonw protocol');
80
+ throw Error('Unknown protocol');
80
81
  const options = {
81
82
  headers: {},
82
83
  host: uri.host ? uri.host.replace(/:[0-9]+$/, '') : uri.host,
@@ -152,12 +153,12 @@ async function request(url, { headers, method, query, body, timeout, auth, file,
152
153
  if (file) {
153
154
  const crlf = '\r\n';
154
155
  const boundary = `--${Math.random().toString(16)}`;
155
- const delimeter = `${crlf}--${boundary}`;
156
+ const delimiter = `${crlf}--${boundary}`;
156
157
  const headers = [`Content-Disposition: form-data; name="file"; filename="${basename(file)}"${crlf}`];
157
158
  const multipartBody = Buffer.concat([
158
- Buffer.from(delimeter + crlf + headers.join('') + crlf),
159
+ Buffer.from(delimiter + crlf + headers.join('') + crlf),
159
160
  readFileSync(file),
160
- Buffer.from(`${delimeter}--`)
161
+ Buffer.from(`${delimiter}--`)
161
162
  ]);
162
163
  req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
163
164
  req.setHeader('Content-Length', multipartBody.length);
package/lib/index.js CHANGED
@@ -7,9 +7,7 @@ var https = require('https');
7
7
  var url = require('url');
8
8
  var fs = require('fs');
9
9
  var path = require('path');
10
- var Logger = require('@faasjs/logger');
11
-
12
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
+ var logger = require('@faasjs/logger');
13
11
 
14
12
  function _interopNamespace(e) {
15
13
  if (e && e.__esModule) return e;
@@ -20,25 +18,23 @@ function _interopNamespace(e) {
20
18
  var d = Object.getOwnPropertyDescriptor(e, k);
21
19
  Object.defineProperty(n, k, d.get ? d : {
22
20
  enumerable: true,
23
- get: function () {
24
- return e[k];
25
- }
21
+ get: function () { return e[k]; }
26
22
  });
27
23
  }
28
24
  });
29
25
  }
30
- n['default'] = e;
26
+ n["default"] = e;
31
27
  return Object.freeze(n);
32
28
  }
33
29
 
34
30
  var http__namespace = /*#__PURE__*/_interopNamespace(http);
35
31
  var https__namespace = /*#__PURE__*/_interopNamespace(https);
36
- var Logger__default = /*#__PURE__*/_interopDefaultLegacy(Logger);
37
32
 
38
33
  let mock = null;
39
34
  /**
40
35
  * 设置模拟请求
41
36
  * @param handler {function | null} 模拟函数,若设置为 null 则表示清除模拟函数
37
+ * @example setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))
42
38
  */
43
39
  function setMock(handler) {
44
40
  mock = handler;
@@ -66,7 +62,7 @@ function querystringify(obj) {
66
62
  * 发起网络请求
67
63
  * @param {string} url 请求路径或完整网址
68
64
  * @param {object=} [options={}] 参数和配置
69
- * @param {string} [options.methd=GET] 请求方法
65
+ * @param {string} [options.method=GET] 请求方法
70
66
  * @param {object} [options.query={}] 请求参数,放置于 path 后,若需放置在 body 中,请使用 body 参数
71
67
  * @param {object} [options.headers={}] 请求头
72
68
  * @param {object=} options.body 请求体
@@ -86,7 +82,7 @@ async function request(url$1, { headers, method, query, body, timeout, auth, fil
86
82
  headers: {},
87
83
  query: {}
88
84
  }) {
89
- const log = new Logger__default['default']('request');
85
+ const log = new logger.Logger('request');
90
86
  if (mock)
91
87
  return mock(url$1, {
92
88
  headers,
@@ -106,7 +102,7 @@ async function request(url$1, { headers, method, query, body, timeout, auth, fil
106
102
  const uri = new url.URL(url$1);
107
103
  const protocol = uri.protocol === 'https:' ? https__namespace : http__namespace;
108
104
  if (!uri.protocol)
109
- throw Error('Unkonw protocol');
105
+ throw Error('Unknown protocol');
110
106
  const options = {
111
107
  headers: {},
112
108
  host: uri.host ? uri.host.replace(/:[0-9]+$/, '') : uri.host,
@@ -182,12 +178,12 @@ async function request(url$1, { headers, method, query, body, timeout, auth, fil
182
178
  if (file) {
183
179
  const crlf = '\r\n';
184
180
  const boundary = `--${Math.random().toString(16)}`;
185
- const delimeter = `${crlf}--${boundary}`;
181
+ const delimiter = `${crlf}--${boundary}`;
186
182
  const headers = [`Content-Disposition: form-data; name="file"; filename="${path.basename(file)}"${crlf}`];
187
183
  const multipartBody = Buffer.concat([
188
- Buffer.from(delimeter + crlf + headers.join('') + crlf),
184
+ Buffer.from(delimiter + crlf + headers.join('') + crlf),
189
185
  fs.readFileSync(file),
190
- Buffer.from(`${delimeter}--`)
186
+ Buffer.from(`${delimiter}--`)
191
187
  ]);
192
188
  req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
193
189
  req.setHeader('Content-Length', multipartBody.length);
@@ -202,7 +198,7 @@ async function request(url$1, { headers, method, query, body, timeout, auth, fil
202
198
  });
203
199
  }
204
200
 
205
- exports['default'] = request;
201
+ exports["default"] = request;
206
202
  exports.querystringify = querystringify;
207
203
  exports.request = request;
208
204
  exports.setMock = setMock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/request",
3
- "version": "0.0.2-beta.229",
3
+ "version": "0.0.2-beta.256",
4
4
  "license": "MIT",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.es.js",
@@ -17,8 +17,8 @@
17
17
  "files": [
18
18
  "lib"
19
19
  ],
20
- "dependencies": {
21
- "@faasjs/logger": "^0.0.2-beta.212"
20
+ "peerDependencies": {
21
+ "@faasjs/logger": "^0.0.2-beta.243"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/debug": "*",
@@ -28,5 +28,5 @@
28
28
  "rollup-plugin-typescript2": "*",
29
29
  "typescript": "*"
30
30
  },
31
- "gitHead": "0a6479e766c4755d66fd92f55dd49f49fe47f841"
31
+ "gitHead": "92a5a276312a4229a0da806e195c9e4a843dfbc6"
32
32
  }