node-easywechat 3.5.16 → 3.5.18

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/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
3
 
4
+ ## v3.5.18 (2023-12-26)
5
+
6
+ - Fix: 修复微信支付上传图片时报错的问题 (#64)
7
+
8
+ ## v3.5.17 (2023-12-20)
9
+
10
+ - Fix: 修复TS问题 (#62)
11
+
4
12
  ## v3.5.16 (2023-12-20)
5
13
 
6
14
  - Fix: 优化消息类型,允许未定义字段的存在 (#62)
@@ -26,6 +26,6 @@ declare class Message {
26
26
  */
27
27
  toString(): string;
28
28
  }
29
- interface Message extends Recordable, HasAttributesMixin {
29
+ interface Message extends Record<string, any>, HasAttributesMixin {
30
30
  }
31
31
  export = Message;
@@ -1,7 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
3
5
  import Crypto from 'crypto';
4
6
  import Stream from 'stream';
7
+ import Fs from 'fs';
5
8
  export declare const createHash: (str: Crypto.BinaryLike, type?: string, target?: Crypto.BinaryToTextEncoding) => any;
6
9
  export declare const createHmac: (str: Crypto.BinaryLike, key: Crypto.BinaryLike, type?: string, target?: Crypto.BinaryToTextEncoding) => any;
7
10
  /**
@@ -102,3 +105,13 @@ export declare const buildXml: (data: Record<string, any>, rootName?: string) =>
102
105
  * @returns
103
106
  */
104
107
  export declare const createUserAgent: (appends?: string[]) => string;
108
+ /**
109
+ * 流转Buffer
110
+ * @param stream 可读流
111
+ */
112
+ export declare const streamToBuffer: (stream: Fs.ReadStream) => Promise<Buffer>;
113
+ /**
114
+ * Buffer转流
115
+ * @param buffer Buffer对象
116
+ */
117
+ export declare const bufferToStream: (buffer: Buffer) => Stream.Duplex;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.createUserAgent = exports.buildXml = exports.parseXml = exports.singleItem = exports.strSnake = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.rtrim = exports.ltrim = exports.trim = exports.applyMixins = exports.inArray = exports.isIp = exports.isIpv6 = exports.isIpv4 = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.makeSignature = exports.randomString = exports.parseQueryString = exports.buildQueryString = exports.getTimestamp = exports.createFileHash = exports.createHmac = exports.createHash = void 0;
15
+ exports.bufferToStream = exports.streamToBuffer = exports.createUserAgent = exports.buildXml = exports.parseXml = exports.singleItem = exports.strSnake = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.rtrim = exports.ltrim = exports.trim = exports.applyMixins = exports.inArray = exports.isIp = exports.isIpv6 = exports.isIpv4 = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.makeSignature = exports.randomString = exports.parseQueryString = exports.buildQueryString = exports.getTimestamp = exports.createFileHash = exports.createHmac = exports.createHash = void 0;
16
16
  const crypto_1 = __importDefault(require("crypto"));
17
17
  const qs_1 = __importDefault(require("qs"));
18
18
  const xml2js_1 = __importDefault(require("xml2js"));
@@ -337,3 +337,27 @@ const createUserAgent = function (appends = []) {
337
337
  return values.join(' ');
338
338
  };
339
339
  exports.createUserAgent = createUserAgent;
340
+ /**
341
+ * 流转Buffer
342
+ * @param stream 可读流
343
+ */
344
+ const streamToBuffer = function (stream) {
345
+ return new Promise((resolve, reject) => {
346
+ let buffers = [];
347
+ stream.on('error', reject);
348
+ stream.on('data', data => buffers.push(data));
349
+ stream.on('end', () => resolve(Buffer.concat(buffers)));
350
+ });
351
+ };
352
+ exports.streamToBuffer = streamToBuffer;
353
+ /**
354
+ * Buffer转流
355
+ * @param buffer Buffer对象
356
+ */
357
+ const bufferToStream = function (buffer) {
358
+ let stream = new stream_1.default.Duplex();
359
+ stream.push(buffer);
360
+ stream.push(null);
361
+ return stream;
362
+ };
363
+ exports.bufferToStream = bufferToStream;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import fs from 'fs';
3
4
  import { Method, AxiosRequestConfig, AxiosInstance } from "axios";
4
5
  import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
@@ -22,12 +23,12 @@ declare class Client implements HttpClientInterface {
22
23
  * 文件上传
23
24
  * @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml
24
25
  * @param uri 接口地址
25
- * @param file 文件路径或文件可读流
26
+ * @param file 文件路径、文件Buffer或文件可读流
26
27
  * @param meta 文件元信息,包含 filename 和 sha256 两个字段
27
28
  * @param filename 文件名,必须以 .jpg、.bmp、.png 为后缀
28
29
  * @returns
29
30
  */
30
- uploadMedia(uri: string, file: string | fs.ReadStream, meta?: Record<string, any>, filename?: string): Promise<HttpClientResponse>;
31
+ uploadMedia(uri: string, file: string | fs.ReadStream | Buffer, meta?: Record<string, any>, filename?: string): Promise<HttpClientResponse>;
31
32
  /**
32
33
  * 判断是否是V3请求
33
34
  * @param url 请求地址
@@ -41,7 +42,7 @@ declare class Client implements HttpClientInterface {
41
42
  * @param payload 请求载荷
42
43
  * @returns
43
44
  */
44
- protected createSignature(method: string, url: string, payload: AxiosRequestConfig<any>): string;
45
+ createSignature(method: string, url: string, payload: AxiosRequestConfig<any>): string;
45
46
  /**
46
47
  * 创建签名(V2)
47
48
  * @param body 请求参数
@@ -61,7 +61,7 @@ class Client {
61
61
  if (!payload.headers['user-agent'] && !payload.headers['User-Agent']) {
62
62
  payload.headers['user-agent'] = (0, Utils_1.createUserAgent)();
63
63
  }
64
- if (this.isV3Request(url)) {
64
+ if (this.isV3Request(url) && !payload.headers['authorization']) {
65
65
  payload.headers['authorization'] = this.createSignature(method, url, payload);
66
66
  }
67
67
  else {
@@ -95,7 +95,7 @@ class Client {
95
95
  * 文件上传
96
96
  * @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml
97
97
  * @param uri 接口地址
98
- * @param file 文件路径或文件可读流
98
+ * @param file 文件路径、文件Buffer或文件可读流
99
99
  * @param meta 文件元信息,包含 filename 和 sha256 两个字段
100
100
  * @param filename 文件名,必须以 .jpg、.bmp、.png 为后缀
101
101
  * @returns
@@ -103,17 +103,21 @@ class Client {
103
103
  uploadMedia(uri, file, meta = null, filename = null) {
104
104
  return __awaiter(this, void 0, void 0, function* () {
105
105
  if (typeof file === 'string') {
106
- file = fs_1.default.createReadStream(file);
106
+ file = fs_1.default.readFileSync(file);
107
107
  }
108
+ else if (typeof file !== 'string' && !Buffer.isBuffer(file)) {
109
+ file = yield (0, Utils_1.streamToBuffer)(file);
110
+ }
111
+ filename = filename !== null && filename !== void 0 ? filename : 'file.jpg';
108
112
  if (!meta) {
109
113
  meta = {
110
- filename: filename !== null && filename !== void 0 ? filename : 'file.jpg',
111
- sha256: yield (0, Utils_1.createFileHash)(file, 'sha256'),
114
+ filename: filename,
115
+ sha256: yield (0, Utils_1.createHash)(file, 'sha256'),
112
116
  };
113
117
  }
114
118
  let metaJson = JSON.stringify(meta);
115
119
  let formData = new form_data_1.default();
116
- formData.append('file', file);
120
+ formData.append('file', file, filename);
117
121
  formData.append('meta', metaJson, {
118
122
  contentType: 'application/json',
119
123
  });
@@ -4,28 +4,28 @@ import axios from 'axios';
4
4
  declare module 'axios' {
5
5
 
6
6
  interface AxiosInstance {
7
- <T = Recordable, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<R>;
8
- request<T = Recordable, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<R>;
9
- get<T = Recordable, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
10
- delete<T = Recordable, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
11
- head<T = Recordable, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
12
- post<T = Recordable, R = AxiosResponse<T>>(url: string, data?: Recordable, config?: AxiosRequestConfig): Promise<R>;
13
- put<T = Recordable, R = AxiosResponse<T>>(url: string, data?: Recordable, config?: AxiosRequestConfig): Promise<R>;
14
- patch<T = Recordable, R = AxiosResponse<T>>(url: string, data?: Recordable, config?: AxiosRequestConfig): Promise<R>;
7
+ <T = Record<string, any>, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<R>;
8
+ request<T = Record<string, any>, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<R>;
9
+ get<T = Record<string, any>, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
10
+ delete<T = Record<string, any>, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
11
+ head<T = Record<string, any>, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
12
+ post<T = Record<string, any>, R = AxiosResponse<T>>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<R>;
13
+ put<T = Record<string, any>, R = AxiosResponse<T>>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<R>;
14
+ patch<T = Record<string, any>, R = AxiosResponse<T>>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<R>;
15
15
  }
16
16
 
17
17
  interface AxiosRequestConfig {
18
18
  /**
19
19
  * 要发送的xml数据,会自动解析并赋值到data属性,同时设置content-type=text/xml
20
20
  */
21
- xml?: string | Recordable;
21
+ xml?: string | Record<string, any>;
22
22
  /**
23
23
  * 要发送的json数据,会自动解析并赋值到data属性,同时设置content-type=application/json
24
24
  */
25
- json?: string | Recordable;
25
+ json?: string | Record<string, any>;
26
26
  /**
27
27
  * 要发送的FormData数据,会自动解析并赋值到data属性,同时设置根据内容提取headers
28
28
  */
29
- formData?: Recordable;
29
+ formData?: Record<string, any>;
30
30
  }
31
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-easywechat",
3
- "version": "3.5.16",
3
+ "version": "3.5.18",
4
4
  "description": "EasyWechat SDK for Node.js (NOT OFFICIAL)",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,2 +0,0 @@
1
-
2
- type Recordable<T = any> = Record<string, T>;