@tuya-sat/micro-dev-proxy 3.0.6 → 3.0.7-beta.2

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.
@@ -34,7 +34,7 @@ export default class AppProxy {
34
34
  init(): Promise<void>;
35
35
  getCsrf(): Promise<void>;
36
36
  getLoginCookie(): Promise<void>;
37
- processReq(proxyReq: Parameters<Options['onProxyReq']>[0]): void;
37
+ processReq(proxyReq: Parameters<Options['onProxyReq']>[0], req: any, res: any): void;
38
38
  combinedHeader(prevHeadar: any): any;
39
- processRes(responseBuffer: any): Promise<any>;
39
+ processRes(responseBuffer: any, proxyRes: any, req: any, res: any): Promise<any>;
40
40
  }
package/dist/appProxy.js CHANGED
@@ -42,6 +42,10 @@ const initLogin_1 = __importStar(require("./initLogin"));
42
42
  const token_1 = __importDefault(require("./token"));
43
43
  const util_1 = require("./util");
44
44
  const url_1 = require("url");
45
+ const fs = __importStar(require("fs"));
46
+ const path = __importStar(require("path"));
47
+ const mfPath = path.join(process.cwd(), 'manifest.json');
48
+ const mf = fs.existsSync(mfPath) && JSON.parse(fs.readFileSync(mfPath, 'utf8'));
45
49
  class AppProxy {
46
50
  constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf = true, logSign, }) {
47
51
  this.loginCookie = new token_1.default();
@@ -98,7 +102,7 @@ class AppProxy {
98
102
  });
99
103
  });
100
104
  }
101
- processReq(proxyReq) {
105
+ processReq(proxyReq, req, res) {
102
106
  //整合需要添加的header头
103
107
  const combineHeaders = this.combinedHeader(this.additionHeaders);
104
108
  Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
@@ -131,7 +135,8 @@ class AppProxy {
131
135
  this.csrf && (proccessedHeader[csrf_1.HEADERS_CSRF] = this.csrfValue.value);
132
136
  return proccessedHeader;
133
137
  }
134
- processRes(responseBuffer) {
138
+ processRes(responseBuffer, proxyRes, req, res) {
139
+ var _a;
135
140
  return __awaiter(this, void 0, void 0, function* () {
136
141
  let response;
137
142
  try {
@@ -141,6 +146,25 @@ class AppProxy {
141
146
  this.log('----json parse responseBuffer string error-----', err);
142
147
  return responseBuffer;
143
148
  }
149
+ const apiPathMatchReg = /\/(:[^/]+|\$?{[^/]+})/g;
150
+ const apis = mf.apis || [];
151
+ if (!(req === null || req === void 0 ? void 0 : req.headers['micro-app-id'].includes('main-app'))) {
152
+ const method = (req === null || req === void 0 ? void 0 : req.method) || 'GET';
153
+ const urlPath = ((_a = req === null || req === void 0 ? void 0 : req.path) === null || _a === void 0 ? void 0 : _a.replace(/^\s*\/(custom-api|open-api)/, '')) || '';
154
+ let isExists = false;
155
+ apis.forEach((api) => {
156
+ const isLegal = new RegExp(`^${api.path.replace(apiPathMatchReg, '/[^/]+')}/?$`).test(urlPath);
157
+ if (isLegal && method.toLowerCase() === api.method.toLowerCase()) {
158
+ isExists = true;
159
+ }
160
+ });
161
+ if (!isExists) {
162
+ return JSON.stringify({
163
+ msg: `api:${urlPath} 未在manifest.json中注册`,
164
+ code: '888888',
165
+ });
166
+ }
167
+ }
144
168
  if (response.code === 1010 && !this.isMainApp) {
145
169
  let modifyText = '';
146
170
  yield this.getLoginCookie()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-proxy",
3
- "version": "3.0.6",
3
+ "version": "3.0.7-beta.2",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {