@ningboyz/wxmp 1.0.8 → 1.0.10

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.
@@ -12,4 +12,4 @@ declare class WxClient {
12
12
  login(params: IWxLoginParams): Promise<import('@ningboyz/types/src/response').IResponse<TCore.IUserResponse[]>>;
13
13
  }
14
14
  declare const createWxClient: (config: IWxConfig) => WxClient;
15
- export { type WxClient, createWxClient };
15
+ export { WxClient, createWxClient };
@@ -16,5 +16,6 @@ class t {
16
16
  }
17
17
  const c = (r) => t.create(r);
18
18
  export {
19
+ t as WxClient,
19
20
  c as createWxClient
20
21
  };
package/es/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { WxClient, createWxClient } from './client/wxClient';
2
2
  import { WxRequest, createWxRequest } from './request/wxRequest';
3
- import { IWxConfig, IWxInternalRequestConfig, IWxLoginParams, IWxQueryParams, IWxResponse, TWxConfig, TWxInterceptorsRequest, TWxInterceptorsResponse, TWxLoginParams } from './types';
4
- export { type WxClient, createWxClient, type WxRequest, createWxRequest, type IWxConfig, type IWxInternalRequestConfig, type IWxLoginParams, type IWxQueryParams, type IWxResponse, type TWxInterceptorsRequest, type TWxInterceptorsResponse, TWxConfig, TWxLoginParams };
3
+ import { IWxConfig, IWxInternalRequestConfig, IWxLoginParams, IWxQueryParams, IWxRequestOptions, IWxResponse, TWxConfig, TWxHttpMethod, TWxInterceptorsRequest, TWxInterceptorsResponse, TWxLoginParams } from './types';
4
+ export { type WxClient, createWxClient, type WxRequest, createWxRequest, type IWxConfig, type IWxInternalRequestConfig, type IWxLoginParams, type IWxQueryParams, type IWxRequestOptions, type IWxResponse, type TWxHttpMethod, type TWxInterceptorsRequest, type TWxInterceptorsResponse, TWxConfig, TWxLoginParams };
@@ -1,5 +1,5 @@
1
1
  import { TResp } from '@ningboyz/types';
2
- import { IWxConfig, IWxQueryParams } from '../types';
2
+ import { IWxConfig, IWxQueryParams, IWxRequestOptions } from '../types';
3
3
  declare class WxRequest {
4
4
  private config;
5
5
  private constructor();
@@ -13,6 +13,7 @@ declare class WxRequest {
13
13
  */
14
14
  postJson<T>(url: string, querys?: IWxQueryParams, params?: object, headers?: Record<string, string>): Promise<TResp.IResponse<T>>;
15
15
  get<T>(url: string, querys?: IWxQueryParams, headers?: Record<string, string>): Promise<TResp.IResponse<T>>;
16
+ request<T>(url: string, options: IWxRequestOptions): Promise<TResp.IResponse<T>>;
16
17
  private send;
17
18
  private handleSuccess;
18
19
  private tryHandleSessionExpired;
@@ -1,7 +1,7 @@
1
1
  import { TResp as f } from "@ningboyz/types";
2
- import s from "lodash";
3
- import { REQUEST_TIMEOUT_MILLISECONDS as p } from "../types.js";
4
- const T = 200, N = 300, m = "application/json", E = "application/x-www-form-urlencoded";
2
+ import i from "lodash";
3
+ import { REQUEST_TIMEOUT_MILLISECONDS as T } from "../types.js";
4
+ const m = 200, N = 300, g = "application/json", E = "application/x-www-form-urlencoded";
5
5
  class u {
6
6
  config;
7
7
  constructor(e) {
@@ -13,97 +13,101 @@ class u {
13
13
  /**
14
14
  * 表单提交。body 字段名 params,值是 JSON 字符串,对应 apis 里的 post。
15
15
  */
16
- post(e, i, t, r) {
17
- const n = s.isNil(t) ? void 0 : { params: JSON.stringify(t) }, o = this.createHeader(s.isNil(t) ? void 0 : E, r);
18
- return this.send("POST", e, i, n, o);
16
+ post(e, s, t, o) {
17
+ const n = i.isNil(t) ? void 0 : { params: JSON.stringify(t) }, r = this.createHeader(i.isNil(t) ? void 0 : E, o);
18
+ return this.send("POST", e, s, n, r);
19
19
  }
20
20
  /**
21
21
  * JSON 提交,对应 apis 里的 postJson。国密加密接口请走此方法。
22
22
  */
23
- postJson(e, i, t, r) {
24
- const n = this.createHeader(m, r);
25
- return this.send("POST", e, i, t, n);
23
+ postJson(e, s, t, o) {
24
+ const n = this.createHeader(g, o);
25
+ return this.send("POST", e, s, t, n);
26
26
  }
27
- get(e, i, t) {
28
- const r = this.createHeader(void 0, t);
29
- return this.send("GET", e, i, void 0, r);
27
+ get(e, s, t) {
28
+ const o = this.createHeader(void 0, t);
29
+ return this.send("GET", e, s, void 0, o);
30
30
  }
31
- send(e, i, t, r, n) {
31
+ request(e, s) {
32
+ const t = s.contentType ?? (s.method === "GET" ? void 0 : g), o = this.createHeader(t, s.headers);
33
+ return this.send(s.method, e, s.querys, s.data, o);
34
+ }
35
+ send(e, s, t, o, n) {
32
36
  return (async () => {
33
- let o = {
34
- url: i,
37
+ let r = {
38
+ url: s,
35
39
  method: e,
36
40
  querys: t,
37
- data: r,
41
+ data: o,
38
42
  header: n
39
43
  };
40
- s.isNil(this.config.interceptorsRequest) || (o = await this.config.interceptorsRequest(o));
41
- const a = this.createRequestUrl(o.url, o.querys), h = this.config.timeout ?? p;
44
+ i.isNil(this.config.interceptorsRequest) || (r = await this.config.interceptorsRequest(r));
45
+ const a = this.createRequestUrl(r.url, r.querys), h = this.config.timeout ?? T;
42
46
  return await new Promise((d) => {
43
47
  try {
44
48
  this.getMiniProgramWx().request({
45
49
  url: a,
46
- method: o.method,
47
- data: o.data,
48
- header: o.header,
50
+ method: r.method,
51
+ data: r.data,
52
+ header: r.header,
49
53
  timeout: h,
50
54
  success: (c) => {
51
- this.handleSuccess(c, o).then(d);
55
+ this.handleSuccess(c, r).then(d);
52
56
  },
53
57
  fail: (c) => {
54
58
  d(f.TResponse.toError(c.errMsg));
55
59
  }
56
60
  });
57
61
  } catch (c) {
58
- const l = c instanceof Error ? c.message : "请求失败";
59
- d(f.TResponse.toError(l));
62
+ const p = c instanceof Error ? c.message : "请求失败";
63
+ d(f.TResponse.toError(p));
60
64
  }
61
65
  });
62
66
  })();
63
67
  }
64
- async handleSuccess(e, i) {
65
- if (e.statusCode < T || e.statusCode >= N)
68
+ async handleSuccess(e, s) {
69
+ if (e.statusCode < m || e.statusCode >= N)
66
70
  return f.TResponse.toError(`请求失败,状态码 ${e.statusCode}`);
67
71
  let t = {
68
72
  data: e.data,
69
73
  statusCode: e.statusCode,
70
- config: i
74
+ config: s
71
75
  };
72
- return s.isNil(this.config.interceptorsResponse) || (t = await this.config.interceptorsResponse(t)), this.tryHandleSessionExpired(t.data), t.data;
76
+ return i.isNil(this.config.interceptorsResponse) || (t = await this.config.interceptorsResponse(t)), this.tryHandleSessionExpired(t.data), t.data;
73
77
  }
74
78
  tryHandleSessionExpired(e) {
75
- if (s.isNil(this.config.loseTokenState) || s.isNil(this.config.sessionExpiredErrorKey) || s.isNil(e) || typeof e != "object")
79
+ if (i.isNil(this.config.loseTokenState) || i.isNil(this.config.sessionExpiredErrorKey) || i.isNil(e) || typeof e != "object")
76
80
  return;
77
81
  e.ERRORKEY === this.config.sessionExpiredErrorKey && this.config.loseTokenState();
78
82
  }
79
83
  getMiniProgramWx() {
80
84
  const e = globalThis.wx;
81
- if (s.isNil(e))
85
+ if (i.isNil(e))
82
86
  throw new Error("当前环境不存在微信小程序 wx");
83
87
  return e;
84
88
  }
85
- createRequestUrl(e, i) {
86
- const t = this.config.baseURL.endsWith("/") ? this.config.baseURL.slice(0, -1) : this.config.baseURL, r = e.startsWith("/") ? e : `/${e}`, n = {
89
+ createRequestUrl(e, s) {
90
+ const t = this.config.baseURL.endsWith("/") ? this.config.baseURL.slice(0, -1) : this.config.baseURL, o = e.startsWith("/") ? e : `/${e}`, n = {
87
91
  r: Date.now(),
88
92
  ...this.createAuthQuerys(),
89
- ...this.toLowerCase(i)
90
- }, o = Object.entries(n).filter(([, a]) => !s.isNil(a)).map(([a, h]) => `${encodeURIComponent(a)}=${encodeURIComponent(String(h))}`).join("&");
91
- return o.length === 0 ? `${t}${r}` : `${t}${r}?${o}`;
93
+ ...this.toLowerCase(s)
94
+ }, r = Object.entries(n).filter(([, a]) => !i.isNil(a)).map(([a, h]) => `${encodeURIComponent(a)}=${encodeURIComponent(String(h))}`).join("&");
95
+ return r.length === 0 ? `${t}${o}` : `${t}${o}?${r}`;
92
96
  }
93
97
  createAuthQuerys() {
94
98
  const e = {};
95
- return !s.isNil(this.config.whobuild) && this.config.whobuild !== 0 && (e.whobuild = this.config.whobuild), !s.isNil(this.config.userIndx) && this.config.userIndx !== 0 && (e.userindx = this.config.userIndx), e;
99
+ return !i.isNil(this.config.whobuild) && this.config.whobuild !== 0 && (e.whobuild = this.config.whobuild), !i.isNil(this.config.userIndx) && this.config.userIndx !== 0 && (e.userindx = this.config.userIndx), e;
96
100
  }
97
- createHeader(e, i) {
101
+ createHeader(e, s) {
98
102
  const t = {};
99
- return s.isNil(e) || (t["content-type"] = e), !s.isNil(this.config.usrtoken) && this.config.usrtoken !== "" && (t.usrtoken = this.config.usrtoken), !s.isNil(this.config.dbConfig) && this.config.dbConfig !== "" && (t.dbconfig = this.config.dbConfig), s.isNil(i) || Object.assign(t, i), t;
103
+ return i.isNil(e) || (t["content-type"] = e), !i.isNil(this.config.usrtoken) && this.config.usrtoken !== "" && (t.usrtoken = this.config.usrtoken), !i.isNil(this.config.dbConfig) && this.config.dbConfig !== "" && (t.dbconfig = this.config.dbConfig), i.isNil(s) || Object.assign(t, s), t;
100
104
  }
101
105
  toLowerCase(e) {
102
- return s.isNil(e) ? {} : s.mapKeys(e, (i, t) => t.toLowerCase());
106
+ return i.isNil(e) ? {} : i.mapKeys(e, (s, t) => t.toLowerCase());
103
107
  }
104
108
  }
105
- const C = (g) => u.create(g);
109
+ const y = (l) => u.create(l);
106
110
  export {
107
111
  u as WxRequest,
108
- C as createWxRequest
112
+ y as createWxRequest
109
113
  };
package/es/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  declare const REQUEST_TIMEOUT_MILLISECONDS = 60000;
2
+ export type TWxHttpMethod = "GET" | "POST" | "PUT" | "DELETE";
2
3
  export interface IWxQueryParams {
3
4
  [key: string]: string | number | boolean | null | undefined;
4
5
  }
@@ -9,7 +10,7 @@ export interface IWxQueryParams {
9
10
  export interface IWxInternalRequestConfig {
10
11
  /** 业务 path,不含 baseURL / query */
11
12
  url: string;
12
- method: "GET" | "POST";
13
+ method: TWxHttpMethod;
13
14
  querys?: IWxQueryParams;
14
15
  data?: object;
15
16
  header: Record<string, string>;
@@ -24,6 +25,13 @@ export interface IWxResponse<T = unknown> {
24
25
  statusCode: number;
25
26
  config: IWxInternalRequestConfig;
26
27
  }
28
+ export interface IWxRequestOptions {
29
+ method: TWxHttpMethod;
30
+ querys?: IWxQueryParams;
31
+ data?: object;
32
+ headers?: Record<string, string>;
33
+ contentType?: string;
34
+ }
27
35
  export type TWxInterceptorsRequest = (request: IWxInternalRequestConfig) => IWxInternalRequestConfig | Promise<IWxInternalRequestConfig>;
28
36
  export type TWxInterceptorsResponse = (response: IWxResponse) => IWxResponse | Promise<IWxResponse>;
29
37
  export interface IWxConfig {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ningboyz/wxmp",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "private": false,
5
5
  "description": "宁波甬政微信小程序库",
6
6
  "author": "nbyt-syq",