Skip to content

Commit 0281c1a

Browse files
chore: expose chainable async/sync fn, fix Rsa::from, optim d.ts
1 parent 96fb6d5 commit 0281c1a

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

index.d.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="node" />
22
import { AgentOptions } from "https";
3-
import { CipherKey, BinaryLike, KeyLike } from 'crypto'
3+
import { CipherKey, BinaryLike, KeyLike, KeyObject } from 'crypto'
44
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
55
import { default as Base } from '@thenorthmemory/multipart';
66

@@ -132,6 +132,14 @@ export namespace WechatpayAxiosPlugin {
132132
* Crypto hash functions utils.
133133
*/
134134
class Hash {
135+
static ALGO_MD5: 'MD5';
136+
137+
static ALGO_HMAC_SHA256: 'HMAC-SHA256';
138+
139+
static isKeyObject(thing: any): boolean;
140+
141+
static keyObjectFrom(thing: Buffer): KeyObject;
142+
135143
/**
136144
* Calculate the input string with an optional secret `key` in MD5,
137145
* when the `key` is Falsey, this method works as normal `MD5`.
@@ -429,11 +437,26 @@ export namespace WechatpayAxiosPlugin {
429437
/**
430438
* Alias of the `RSA_PKCS1_OAEP_PADDING` mode
431439
*/
432-
readonly RSA_PKCS1_OAEP_PADDING: 4;
440+
static RSA_PKCS1_OAEP_PADDING: 4;
433441
/**
434442
* Alias of the `RSA_PKCS1_PADDING` mode
435443
*/
436-
readonly RSA_PKCS1_PADDING: 1;
444+
static RSA_PKCS1_PADDING: 1;
445+
446+
static KEY_TYPE_PUBLIC: 'public';
447+
448+
static KEY_TYPE_PRIVATE: 'private';
449+
450+
static isKeyObject(thing: any): boolean;
451+
452+
static fromPkcs8(str: string): KeyObject;
453+
454+
static fromPkcs1(str: string, type: 'public' | 'private'): KeyObject;
455+
456+
static fromSpki(str: string): KeyObject;
457+
458+
static from(thing: KeyLike, type: 'public' | 'private'): KeyObject;
459+
437460
/**
438461
* Encrypts text with sha256WithRSAEncryption/RSA_PKCS1_OAEP_PADDING.
439462
* Node Limits >= 12.9.0 (`oaepHash` was added)

lib/decorator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const EV3_RES_HEADER_PLATFORM_SERIAL = 'Cannot found the serial(`%s`)\'s configu
3232
const EV3_RES_HEADER_SIGNATURE_DIGEST = 'Verify the response\'s data with: timestamp=%s, nonce=%s, signature=%s, cert={%s: ...} failed.';
3333

3434
function isValidAsymmetricKey(thing) {
35-
return ((utils.isString(thing) || utils.isBuffer(thing)) && thing.length) || Rsa.isKeyObject(thing);
35+
return ((utils.isString(thing) || utils.isBuffer(thing)) && thing.length > 0) || Rsa.isKeyObject(thing);
3636
}
3737

3838
/**

lib/rsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Rsa {
102102
let input = thing;
103103
if (format && kind && offset) {
104104
const key = Buffer.from(thing.slice(offset), base64);
105-
input = { key, format, type };
105+
input = { key, format, type: kind };
106106
}
107107

108108
return (protocol.startsWith(this.KEY_TYPE_PRIVATE) || type === this.KEY_TYPE_PRIVATE ? createPrivateKey : createPublicKey)(input);

lib/wechatpay.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Decorator = require('./decorator');
22

3+
const chain = 'chain';
34
const CLIENT = Symbol('CLIENT');
5+
const enumerable = true;
46

57
/**
68
* Normalize the `str` by the rules: `PascalCase` -> `camelCase` & `camelCase` -> `camel-case` & `$dynamic$` -> `{dynamic}`
@@ -98,19 +100,19 @@ class Wechatpay {
98100
chain(pathname) {
99101
const client = this[CLIENT];
100102

101-
/* eslint-disable object-curly-newline, arrow-body-style */
103+
/* eslint-disable object-curly-newline, arrow-body-style, object-property-newline */
102104
return ['post', 'put', 'patch'].reduce((resource, method) => {
103-
return Object.defineProperty(resource, method, { value: { [method](data, config) {
105+
return Object.defineProperty(resource, method, { enumerable, value: { async [method](data, config) {
104106
return client.request(pathname, method, data, config);
105107
} }[method] });
106108
}, ['delete', 'get'].reduce((resource, method) => {
107-
return Object.defineProperty(resource, method, { value: { [method](config) {
109+
return Object.defineProperty(resource, method, { enumerable, value: { async [method](config) {
108110
return client.request(pathname, method, undefined, config);
109111
} }[method] });
110-
}, Object.defineProperty({ [pathname](data, config) {
112+
}, Object.defineProperty({ async [pathname](data, config) {
111113
return client.request(pathname, 'post', data, config);
112-
} }[pathname], 'chain', { value: (thing) => this.compose(pathname, thing) })));
113-
/* eslint-enable object-curly-newline, arrow-body-style */
114+
} }[pathname], chain, { enumerable, value: { [chain]: (thing) => this.compose(pathname, thing) }[chain] })));
115+
/* eslint-enable object-curly-newline, arrow-body-style, object-property-newline */
114116
}
115117

116118
/**

0 commit comments

Comments
 (0)