@lark-apaas/devtool-kits 1.2.16 → 1.2.17-alpha.0

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.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { Router, RequestHandler, Application } from 'express';
2
+ import { RequestHandler, Router, Application } from 'express';
3
+ import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
3
4
 
4
5
  /**
5
6
  * 标准化基础路径,确保以 '/' 开头且不包含 trailing slash
@@ -196,6 +197,41 @@ interface DevLogsMiddlewareOptions {
196
197
  */
197
198
  declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions): RouteMiddleware;
198
199
 
200
+ /**
201
+ * Express/Connect 兼容层
202
+ * 让 middleware 同时支持 Express 和 Vite/Connect
203
+ */
204
+
205
+ type AnyResponse = ServerResponse$1 & {
206
+ status?: (code: number) => AnyResponse;
207
+ json?: (data: unknown) => void;
208
+ send?: (data: unknown) => void;
209
+ };
210
+ type AnyRequest = IncomingMessage$1 & {
211
+ query?: Record<string, string>;
212
+ params?: Record<string, string>;
213
+ };
214
+ /**
215
+ * 发送 JSON 响应,兼容 Express 和 Connect
216
+ */
217
+ declare function sendJson(res: AnyResponse, data: unknown, statusCode?: number): void;
218
+ /**
219
+ * 发送错误响应
220
+ */
221
+ declare function sendError(res: AnyResponse, message: string, error?: unknown, statusCode?: number): void;
222
+ /**
223
+ * 发送成功响应
224
+ */
225
+ declare function sendSuccess(res: AnyResponse, data?: Record<string, unknown>): void;
226
+ /**
227
+ * 获取 query 参数,兼容 Express 和 Connect
228
+ */
229
+ declare function getQuery(req: AnyRequest): Record<string, string>;
230
+ /**
231
+ * 获取单个 query 参数
232
+ */
233
+ declare function getQueryParam(req: AnyRequest, key: string): string | undefined;
234
+
199
235
  /**
200
236
  * Register middlewares for Express-compatible servers or Vite
201
237
  * @param server - Express app or Vite middleware instance
@@ -235,4 +271,4 @@ declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions)
235
271
  */
236
272
  declare function registerMiddlewares(server: ExpressApp | ViteMiddleware, middlewares: Middleware[], options?: Partial<MiddlewareContext>): Promise<void>;
237
273
 
238
- export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares };
274
+ export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, getQuery, getQueryParam, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares, sendError, sendJson, sendSuccess };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { Router, RequestHandler, Application } from 'express';
2
+ import { RequestHandler, Router, Application } from 'express';
3
+ import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
3
4
 
4
5
  /**
5
6
  * 标准化基础路径,确保以 '/' 开头且不包含 trailing slash
@@ -196,6 +197,41 @@ interface DevLogsMiddlewareOptions {
196
197
  */
197
198
  declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions): RouteMiddleware;
198
199
 
200
+ /**
201
+ * Express/Connect 兼容层
202
+ * 让 middleware 同时支持 Express 和 Vite/Connect
203
+ */
204
+
205
+ type AnyResponse = ServerResponse$1 & {
206
+ status?: (code: number) => AnyResponse;
207
+ json?: (data: unknown) => void;
208
+ send?: (data: unknown) => void;
209
+ };
210
+ type AnyRequest = IncomingMessage$1 & {
211
+ query?: Record<string, string>;
212
+ params?: Record<string, string>;
213
+ };
214
+ /**
215
+ * 发送 JSON 响应,兼容 Express 和 Connect
216
+ */
217
+ declare function sendJson(res: AnyResponse, data: unknown, statusCode?: number): void;
218
+ /**
219
+ * 发送错误响应
220
+ */
221
+ declare function sendError(res: AnyResponse, message: string, error?: unknown, statusCode?: number): void;
222
+ /**
223
+ * 发送成功响应
224
+ */
225
+ declare function sendSuccess(res: AnyResponse, data?: Record<string, unknown>): void;
226
+ /**
227
+ * 获取 query 参数,兼容 Express 和 Connect
228
+ */
229
+ declare function getQuery(req: AnyRequest): Record<string, string>;
230
+ /**
231
+ * 获取单个 query 参数
232
+ */
233
+ declare function getQueryParam(req: AnyRequest, key: string): string | undefined;
234
+
199
235
  /**
200
236
  * Register middlewares for Express-compatible servers or Vite
201
237
  * @param server - Express app or Vite middleware instance
@@ -235,4 +271,4 @@ declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions)
235
271
  */
236
272
  declare function registerMiddlewares(server: ExpressApp | ViteMiddleware, middlewares: Middleware[], options?: Partial<MiddlewareContext>): Promise<void>;
237
273
 
238
- export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares };
274
+ export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, getQuery, getQueryParam, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares, sendError, sendJson, sendSuccess };
package/dist/index.js CHANGED
@@ -3449,7 +3449,100 @@ function createCollectLogsMiddleware(options = {}) {
3449
3449
  }
3450
3450
  __name(createCollectLogsMiddleware, "createCollectLogsMiddleware");
3451
3451
 
3452
+ // src/middlewares/compat.ts
3453
+ function sendJson(res, data, statusCode = 200) {
3454
+ const statusFn = res.status;
3455
+ const jsonFn = res.json;
3456
+ if (typeof statusFn === "function" && typeof jsonFn === "function") {
3457
+ statusFn.call(res, statusCode);
3458
+ jsonFn.call(res, data);
3459
+ } else {
3460
+ res.statusCode = statusCode;
3461
+ res.setHeader("Content-Type", "application/json");
3462
+ res.end(JSON.stringify(data));
3463
+ }
3464
+ }
3465
+ __name(sendJson, "sendJson");
3466
+ function sendError(res, message, error, statusCode = 500) {
3467
+ const errorData = {
3468
+ message,
3469
+ ...error ? {
3470
+ error: serializeError3(error)
3471
+ } : {}
3472
+ };
3473
+ sendJson(res, errorData, statusCode);
3474
+ }
3475
+ __name(sendError, "sendError");
3476
+ function sendSuccess(res, data) {
3477
+ sendJson(res, {
3478
+ success: true,
3479
+ ...data
3480
+ }, 200);
3481
+ }
3482
+ __name(sendSuccess, "sendSuccess");
3483
+ function getQuery(req) {
3484
+ if (req.query) {
3485
+ return req.query;
3486
+ }
3487
+ const url = new URL(req.url || "", `http://${req.headers.host}`);
3488
+ return Object.fromEntries(url.searchParams.entries());
3489
+ }
3490
+ __name(getQuery, "getQuery");
3491
+ function getQueryParam(req, key) {
3492
+ const query = getQuery(req);
3493
+ return query[key];
3494
+ }
3495
+ __name(getQueryParam, "getQueryParam");
3496
+ function serializeError3(error) {
3497
+ if (error instanceof Error) {
3498
+ return {
3499
+ name: error.name,
3500
+ message: error.message,
3501
+ stack: error.stack
3502
+ };
3503
+ }
3504
+ return {
3505
+ message: String(error)
3506
+ };
3507
+ }
3508
+ __name(serializeError3, "serializeError");
3509
+
3452
3510
  // src/middlewares/index.ts
3511
+ function enhanceForCompat(req, res) {
3512
+ if (!res.status) {
3513
+ res.status = function(code) {
3514
+ res.statusCode = code;
3515
+ return res;
3516
+ };
3517
+ }
3518
+ if (!res.json) {
3519
+ res.json = function(data) {
3520
+ res.setHeader("Content-Type", "application/json");
3521
+ res.end(JSON.stringify(data));
3522
+ return res;
3523
+ };
3524
+ }
3525
+ if (!res.send) {
3526
+ res.send = function(data) {
3527
+ if (typeof data === "object" && data !== null) {
3528
+ res.setHeader("Content-Type", "application/json");
3529
+ res.end(JSON.stringify(data));
3530
+ } else {
3531
+ res.end(String(data));
3532
+ }
3533
+ return res;
3534
+ };
3535
+ }
3536
+ if (!req.query) {
3537
+ const url = new URL(req.url || "", `http://${req.headers.host || "localhost"}`);
3538
+ req.query = Object.fromEntries(url.searchParams.entries());
3539
+ }
3540
+ }
3541
+ __name(enhanceForCompat, "enhanceForCompat");
3542
+ function isConnectServer(server) {
3543
+ return typeof server === "function" && !("set" in server) && !("engine" in server);
3544
+ }
3545
+ __name(isConnectServer, "isConnectServer");
3453
3546
  function isRouteMiddleware(middleware) {
3454
3547
  return "createRouter" in middleware && middleware.createRouter !== void 0;
3455
3548
  }
@@ -3502,6 +3595,13 @@ async function registerMiddlewares(server, middlewares, options) {
3502
3595
  rootDir: process.cwd(),
3503
3596
  ...options
3504
3597
  };
3598
+ if (isConnectServer(server)) {
3599
+ server.use((req, res, next) => {
3600
+ enhanceForCompat(req, res);
3601
+ next();
3602
+ });
3603
+ console.log("[Middleware] Registered Express compatibility layer for Connect/Vite");
3604
+ }
3505
3605
  const allMiddlewares = [
3506
3606
  ...middlewares
3507
3607
  ];
@@ -3532,10 +3632,15 @@ export {
3532
3632
  createCollectLogsMiddleware,
3533
3633
  createDevLogsMiddleware,
3534
3634
  createOpenapiMiddleware,
3635
+ getQuery,
3636
+ getQueryParam,
3535
3637
  handleDevProxyError,
3536
3638
  normalizeBasePath,
3537
3639
  parseAndGenerateNestResourceTemplate,
3538
3640
  postprocessDrizzleSchema,
3539
- registerMiddlewares
3641
+ registerMiddlewares,
3642
+ sendError,
3643
+ sendJson,
3644
+ sendSuccess
3540
3645
  };
3541
3646
  //# sourceMappingURL=index.js.map