@xfe-repo/mini-service 0.1.0 → 0.1.1

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/dist/index.d.mts CHANGED
@@ -20,7 +20,7 @@ type BffRequestParams = {
20
20
  jwt?: JWT;
21
21
  };
22
22
  type BFFRequestConfig = Partial<TaroRequestConfig> & BffRequestParams;
23
- declare const bffInterceptor: (chain: Chain) => Promise<any>;
23
+ declare const bffInterceptor: (chain: Chain) => any;
24
24
 
25
25
  interface CustomRequestTask<T> extends Promise<T> {
26
26
  abort(): void;
package/dist/index.d.ts CHANGED
@@ -20,7 +20,7 @@ type BffRequestParams = {
20
20
  jwt?: JWT;
21
21
  };
22
22
  type BFFRequestConfig = Partial<TaroRequestConfig> & BffRequestParams;
23
- declare const bffInterceptor: (chain: Chain) => Promise<any>;
23
+ declare const bffInterceptor: (chain: Chain) => any;
24
24
 
25
25
  interface CustomRequestTask<T> extends Promise<T> {
26
26
  abort(): void;
package/dist/index.js CHANGED
@@ -55,26 +55,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
55
55
  mod
56
56
  ));
57
57
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
- var __async = (__this, __arguments, generator) => {
59
- return new Promise((resolve, reject) => {
60
- var fulfilled = (value) => {
61
- try {
62
- step(generator.next(value));
63
- } catch (e) {
64
- reject(e);
65
- }
66
- };
67
- var rejected = (value) => {
68
- try {
69
- step(generator.throw(value));
70
- } catch (e) {
71
- reject(e);
72
- }
73
- };
74
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
75
- step((generator = generator.apply(__this, __arguments)).next());
76
- });
77
- };
78
58
 
79
59
  // src/index.ts
80
60
  var src_exports = {};
@@ -89,24 +69,30 @@ var import_taro2 = require("@tarojs/taro");
89
69
  var import_taro = __toESM(require("@tarojs/taro"));
90
70
 
91
71
  // src/bff/jwt-sign.ts
92
- var import_jose = require("jose");
72
+ var import_jsrsasign = require("jsrsasign");
93
73
  var ALG = "RS256";
94
- var cachedPrivateKey = null;
95
- var createJwtSign = (payload, privateKeyPem, keyId) => __async(void 0, null, function* () {
74
+ var createJwtSign = (payload, privateKeyPem, keyId) => {
96
75
  if (!privateKeyPem) return "";
97
- if (!cachedPrivateKey) {
98
- cachedPrivateKey = yield (0, import_jose.importPKCS8)(privateKeyPem, ALG).catch((_err) => {
99
- return null;
100
- });
76
+ try {
77
+ const sHeader = JSON.stringify({ alg: ALG, typ: "JWT", kid: keyId });
78
+ const defaultPayload = {
79
+ sys_mch_id: "eshetang-official",
80
+ iss: "https://auth.eshetang.com",
81
+ jti: Math.random().toString(36),
82
+ // Expires in 60 seconds
83
+ exp: Math.floor(Date.now() / 1e3) + 60
84
+ };
85
+ const sPayload = JSON.stringify(__spreadValues(__spreadValues({}, defaultPayload), payload));
86
+ const authorization = import_jsrsasign.KJUR.jws.JWS.sign(ALG, sHeader, sPayload, privateKeyPem);
87
+ return `Bearer ${authorization}`;
88
+ } catch (error) {
89
+ console.error("createJwtSign error:", error);
90
+ return "";
101
91
  }
102
- if (!cachedPrivateKey) return "";
103
- const defaultPayload = { sys_mch_id: "eshetang-official" };
104
- const authorization = yield new import_jose.SignJWT(__spreadValues(__spreadValues({}, defaultPayload), payload)).setProtectedHeader({ alg: ALG, typ: "JWT", kid: keyId }).setIssuer("https://auth.eshetang.com").setIssuedAt().setJti(Math.random().toString(36)).setExpirationTime("60s").sign(cachedPrivateKey).catch(console.error);
105
- return `Bearer ${authorization}`;
106
- });
92
+ };
107
93
 
108
94
  // src/bff/interceptor.ts
109
- var bffInterceptor = (chain) => __async(void 0, null, function* () {
95
+ var bffInterceptor = (chain) => {
110
96
  const requestParams = chain.requestParams;
111
97
  let { url, baseURL, data, params, header, jwt } = requestParams;
112
98
  if (url && !/^http/.test(url)) {
@@ -114,7 +100,7 @@ var bffInterceptor = (chain) => __async(void 0, null, function* () {
114
100
  }
115
101
  const timestamp = (/* @__PURE__ */ new Date()).getTime();
116
102
  const _a = jwt || {}, { privateKey, keyId } = _a, payload = __objRest(_a, ["privateKey", "keyId"]);
117
- const authorization = yield createJwtSign(__spreadProps(__spreadValues({}, payload), { sub: header["userToken"] }), privateKey, keyId);
103
+ const authorization = createJwtSign(__spreadProps(__spreadValues({}, payload), { sub: header["userToken"] }), privateKey, keyId);
118
104
  header = __spreadValues({ timestamp: `${timestamp}`, authorization }, header);
119
105
  data = __spreadValues(__spreadValues({}, data), params);
120
106
  for (const param in data) {
@@ -132,7 +118,7 @@ var bffInterceptor = (chain) => __async(void 0, null, function* () {
132
118
  (_b = (_a2 = import_taro.default).stopPullDownRefresh) == null ? void 0 : _b.call(_a2);
133
119
  return bffData;
134
120
  });
135
- });
121
+ };
136
122
 
137
123
  // src/index.ts
138
124
  (0, import_taro2.addInterceptor)(bffInterceptor);
package/dist/index.mjs CHANGED
@@ -29,26 +29,6 @@ var __objRest = (source, exclude) => {
29
29
  }
30
30
  return target;
31
31
  };
32
- var __async = (__this, __arguments, generator) => {
33
- return new Promise((resolve, reject) => {
34
- var fulfilled = (value) => {
35
- try {
36
- step(generator.next(value));
37
- } catch (e) {
38
- reject(e);
39
- }
40
- };
41
- var rejected = (value) => {
42
- try {
43
- step(generator.throw(value));
44
- } catch (e) {
45
- reject(e);
46
- }
47
- };
48
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
- step((generator = generator.apply(__this, __arguments)).next());
50
- });
51
- };
52
32
 
53
33
  // src/index.ts
54
34
  import { addInterceptor, interceptors, request } from "@tarojs/taro";
@@ -57,24 +37,30 @@ import { addInterceptor, interceptors, request } from "@tarojs/taro";
57
37
  import Taro from "@tarojs/taro";
58
38
 
59
39
  // src/bff/jwt-sign.ts
60
- import { importPKCS8, SignJWT } from "jose";
40
+ import { KJUR } from "jsrsasign";
61
41
  var ALG = "RS256";
62
- var cachedPrivateKey = null;
63
- var createJwtSign = (payload, privateKeyPem, keyId) => __async(void 0, null, function* () {
42
+ var createJwtSign = (payload, privateKeyPem, keyId) => {
64
43
  if (!privateKeyPem) return "";
65
- if (!cachedPrivateKey) {
66
- cachedPrivateKey = yield importPKCS8(privateKeyPem, ALG).catch((_err) => {
67
- return null;
68
- });
44
+ try {
45
+ const sHeader = JSON.stringify({ alg: ALG, typ: "JWT", kid: keyId });
46
+ const defaultPayload = {
47
+ sys_mch_id: "eshetang-official",
48
+ iss: "https://auth.eshetang.com",
49
+ jti: Math.random().toString(36),
50
+ // Expires in 60 seconds
51
+ exp: Math.floor(Date.now() / 1e3) + 60
52
+ };
53
+ const sPayload = JSON.stringify(__spreadValues(__spreadValues({}, defaultPayload), payload));
54
+ const authorization = KJUR.jws.JWS.sign(ALG, sHeader, sPayload, privateKeyPem);
55
+ return `Bearer ${authorization}`;
56
+ } catch (error) {
57
+ console.error("createJwtSign error:", error);
58
+ return "";
69
59
  }
70
- if (!cachedPrivateKey) return "";
71
- const defaultPayload = { sys_mch_id: "eshetang-official" };
72
- const authorization = yield new SignJWT(__spreadValues(__spreadValues({}, defaultPayload), payload)).setProtectedHeader({ alg: ALG, typ: "JWT", kid: keyId }).setIssuer("https://auth.eshetang.com").setIssuedAt().setJti(Math.random().toString(36)).setExpirationTime("60s").sign(cachedPrivateKey).catch(console.error);
73
- return `Bearer ${authorization}`;
74
- });
60
+ };
75
61
 
76
62
  // src/bff/interceptor.ts
77
- var bffInterceptor = (chain) => __async(void 0, null, function* () {
63
+ var bffInterceptor = (chain) => {
78
64
  const requestParams = chain.requestParams;
79
65
  let { url, baseURL, data, params, header, jwt } = requestParams;
80
66
  if (url && !/^http/.test(url)) {
@@ -82,7 +68,7 @@ var bffInterceptor = (chain) => __async(void 0, null, function* () {
82
68
  }
83
69
  const timestamp = (/* @__PURE__ */ new Date()).getTime();
84
70
  const _a = jwt || {}, { privateKey, keyId } = _a, payload = __objRest(_a, ["privateKey", "keyId"]);
85
- const authorization = yield createJwtSign(__spreadProps(__spreadValues({}, payload), { sub: header["userToken"] }), privateKey, keyId);
71
+ const authorization = createJwtSign(__spreadProps(__spreadValues({}, payload), { sub: header["userToken"] }), privateKey, keyId);
86
72
  header = __spreadValues({ timestamp: `${timestamp}`, authorization }, header);
87
73
  data = __spreadValues(__spreadValues({}, data), params);
88
74
  for (const param in data) {
@@ -100,7 +86,7 @@ var bffInterceptor = (chain) => __async(void 0, null, function* () {
100
86
  (_b = (_a2 = Taro).stopPullDownRefresh) == null ? void 0 : _b.call(_a2);
101
87
  return bffData;
102
88
  });
103
- });
89
+ };
104
90
 
105
91
  // src/index.ts
106
92
  addInterceptor(bffInterceptor);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xfe-repo/mini-service",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "sideEffects": false,
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,9 +21,10 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "@tarojs/taro": "4.0.8",
24
- "jose": "^6.1.1"
24
+ "jsrsasign": "^11.1.0"
25
25
  },
26
26
  "devDependencies": {
27
+ "@types/jsrsasign": "^10.5.15",
27
28
  "@types/node": "^20.16.5",
28
29
  "@xfe-repo/eslint-config": "0.0.5",
29
30
  "@xfe-repo/typescript-config": "0.0.6"