@jayfong/x-server 2.12.6 → 2.12.7

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.
@@ -3,28 +3,51 @@
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  exports.__esModule = true;
6
- exports.RequestService = exports.RequestFormUrlencoded = exports.RequestFormStream = exports.RequestFormData = void 0;
6
+ exports.RequestService = exports.RequestFormUrlencoded = exports.RequestFormStream = exports.RequestFormFile = exports.RequestFormData = void 0;
7
+
8
+ var _assert = _interopRequireDefault(require("assert"));
7
9
 
8
10
  var _formData = _interopRequireDefault(require("form-data"));
9
11
 
10
12
  var _formstream = _interopRequireDefault(require("formstream"));
11
13
 
14
+ var _fs = _interopRequireDefault(require("fs"));
15
+
12
16
  var _got = _interopRequireDefault(require("got"));
13
17
 
14
18
  var _vtils = require("vtils");
15
19
 
16
20
  var _proxyAgent = require("proxy-agent");
17
21
 
22
+ var _stream = require("stream");
23
+
18
24
  class RequestFormUrlencoded extends URLSearchParams {}
19
25
 
20
26
  exports.RequestFormUrlencoded = RequestFormUrlencoded;
21
27
 
22
28
  class RequestFormData extends _formData.default {
23
- constructor(cb) {
29
+ constructor(cbOrObject) {
24
30
  super();
25
31
 
26
- if (cb) {
27
- cb(this);
32
+ if (cbOrObject) {
33
+ if (typeof cbOrObject === 'function') {
34
+ cbOrObject(this);
35
+ } else {
36
+ Object.keys(cbOrObject).forEach(key => {
37
+ const value = cbOrObject[key];
38
+
39
+ if (value instanceof RequestFormFile) {
40
+ var _value$options, _value$options2;
41
+
42
+ this.append(key, typeof value.file === 'string' ? _fs.default.createReadStream(value.file) : value.file, {
43
+ filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
44
+ contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
45
+ });
46
+ } else {
47
+ this.append(key, value);
48
+ }
49
+ });
50
+ }
28
51
  }
29
52
  }
30
53
 
@@ -33,11 +56,31 @@ class RequestFormData extends _formData.default {
33
56
  exports.RequestFormData = RequestFormData;
34
57
 
35
58
  class RequestFormStream extends _formstream.default {
36
- constructor(cb) {
59
+ constructor(cbOrObject) {
37
60
  super();
38
61
 
39
- if (cb) {
40
- cb(this);
62
+ if (cbOrObject) {
63
+ if (typeof cbOrObject === 'function') {
64
+ cbOrObject(this);
65
+ } else {
66
+ Object.keys(cbOrObject).forEach(key => {
67
+ const value = cbOrObject[key];
68
+
69
+ if (value instanceof RequestFormFile) {
70
+ if (typeof value.file === 'string') {
71
+ var _value$options3;
72
+
73
+ this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
74
+ } else if (Buffer.isBuffer(value.file)) {
75
+ this.buffer(key, value.file, value.options.name, value.options.type);
76
+ } else if (value.file instanceof _stream.Readable) {
77
+ this.stream(key, value.file, value.options.name, value.options.type);
78
+ }
79
+ } else {
80
+ this.field(key, value);
81
+ }
82
+ });
83
+ }
41
84
  }
42
85
  }
43
86
 
@@ -45,6 +88,20 @@ class RequestFormStream extends _formstream.default {
45
88
 
46
89
  exports.RequestFormStream = RequestFormStream;
47
90
 
91
+ class RequestFormFile {
92
+ constructor(file, options) {
93
+ this.file = file;
94
+ this.options = options;
95
+
96
+ if (Buffer.isBuffer(file) || file instanceof _stream.Readable) {
97
+ (0, _assert.default)(options == null ? void 0 : options.name, 'options.name is required');
98
+ }
99
+ }
100
+
101
+ }
102
+
103
+ exports.RequestFormFile = RequestFormFile;
104
+
48
105
  class RequestService {
49
106
  static getGotOptions(options, responseType) {
50
107
  var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
@@ -52,7 +109,9 @@ class RequestService {
52
109
  const gotOptions = {
53
110
  url: options.url,
54
111
  responseType: responseType === 'stream' ? undefined : responseType,
55
- rejectUnauthorized: false,
112
+ https: {
113
+ rejectUnauthorized: false
114
+ },
56
115
  isStream: responseType === 'stream'
57
116
  };
58
117
 
@@ -1,10 +1,12 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import FormData from 'form-data';
3
4
  import FormStream from 'formstream';
4
5
  import Request from 'got/dist/source/core';
5
6
  import { BaseService } from './base';
6
7
  import { CookieJar } from 'tough-cookie';
7
8
  import { OmitStrict } from 'vtils/types';
9
+ import { Readable } from 'stream';
8
10
  export interface RequestServiceOptions {
9
11
  /**
10
12
  * 请求地址
@@ -65,10 +67,29 @@ export interface RequestServicePostStreamOptions extends RequestServiceOptions {
65
67
  export declare class RequestFormUrlencoded extends URLSearchParams {
66
68
  }
67
69
  export declare class RequestFormData extends FormData {
68
- constructor(cb?: (formData: FormData) => FormData);
70
+ constructor(cbOrObject?: ((formData: FormData) => FormData) | Record<string, any>);
69
71
  }
70
72
  export declare class RequestFormStream extends FormStream {
71
- constructor(cb?: (formStream: FormStream) => FormStream);
73
+ constructor(cbOrObject?: ((formStream: FormStream) => FormStream) | Record<string, any>);
74
+ }
75
+ export declare class RequestFormFile {
76
+ file: any;
77
+ options?: {
78
+ name?: string;
79
+ type?: string;
80
+ };
81
+ constructor(file: Buffer, options: {
82
+ name: string;
83
+ type?: string;
84
+ });
85
+ constructor(file: Readable, options: {
86
+ name: string;
87
+ type?: string;
88
+ });
89
+ constructor(file: string, options?: {
90
+ name?: string;
91
+ type?: string;
92
+ });
72
93
  }
73
94
  export declare class RequestService implements BaseService {
74
95
  private options?;
@@ -1,25 +1,76 @@
1
+ import assert from 'assert';
1
2
  import FormData from 'form-data';
2
3
  import FormStream from 'formstream';
4
+ import fs from 'fs';
3
5
  import got from 'got';
4
6
  import { mapKeys } from 'vtils';
5
7
  import { ProxyAgent } from 'proxy-agent';
8
+ import { Readable } from 'stream';
6
9
  export class RequestFormUrlencoded extends URLSearchParams {}
7
10
  export class RequestFormData extends FormData {
8
- constructor(cb) {
11
+ constructor(cbOrObject) {
9
12
  super();
10
13
 
11
- if (cb) {
12
- cb(this);
14
+ if (cbOrObject) {
15
+ if (typeof cbOrObject === 'function') {
16
+ cbOrObject(this);
17
+ } else {
18
+ Object.keys(cbOrObject).forEach(key => {
19
+ const value = cbOrObject[key];
20
+
21
+ if (value instanceof RequestFormFile) {
22
+ var _value$options, _value$options2;
23
+
24
+ this.append(key, typeof value.file === 'string' ? fs.createReadStream(value.file) : value.file, {
25
+ filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
26
+ contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
27
+ });
28
+ } else {
29
+ this.append(key, value);
30
+ }
31
+ });
32
+ }
13
33
  }
14
34
  }
15
35
 
16
36
  }
17
37
  export class RequestFormStream extends FormStream {
18
- constructor(cb) {
38
+ constructor(cbOrObject) {
19
39
  super();
20
40
 
21
- if (cb) {
22
- cb(this);
41
+ if (cbOrObject) {
42
+ if (typeof cbOrObject === 'function') {
43
+ cbOrObject(this);
44
+ } else {
45
+ Object.keys(cbOrObject).forEach(key => {
46
+ const value = cbOrObject[key];
47
+
48
+ if (value instanceof RequestFormFile) {
49
+ if (typeof value.file === 'string') {
50
+ var _value$options3;
51
+
52
+ this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
53
+ } else if (Buffer.isBuffer(value.file)) {
54
+ this.buffer(key, value.file, value.options.name, value.options.type);
55
+ } else if (value.file instanceof Readable) {
56
+ this.stream(key, value.file, value.options.name, value.options.type);
57
+ }
58
+ } else {
59
+ this.field(key, value);
60
+ }
61
+ });
62
+ }
63
+ }
64
+ }
65
+
66
+ }
67
+ export class RequestFormFile {
68
+ constructor(file, options) {
69
+ this.file = file;
70
+ this.options = options;
71
+
72
+ if (Buffer.isBuffer(file) || file instanceof Readable) {
73
+ assert(options == null ? void 0 : options.name, 'options.name is required');
23
74
  }
24
75
  }
25
76
 
@@ -31,7 +82,9 @@ export class RequestService {
31
82
  const gotOptions = {
32
83
  url: options.url,
33
84
  responseType: responseType === 'stream' ? undefined : responseType,
34
- rejectUnauthorized: false,
85
+ https: {
86
+ rejectUnauthorized: false
87
+ },
35
88
  isStream: responseType === 'stream'
36
89
  };
37
90
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.12.6",
3
+ "version": "2.12.7",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",