@lark-apaas/devtool-kits 1.2.17-alpha.20 → 1.2.17-alpha.21
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/README.md +43 -0
- package/dist/index.cjs +112 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -1
- package/dist/index.d.ts +37 -1
- package/dist/index.js +106 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
2
|
import { Router, RequestHandler, Application } from 'express';
|
|
3
|
+
import { ServerResponse as ServerResponse$1, IncomingMessage as IncomingMessage$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
|
* Creates api-routes middleware for listing application API routes
|
|
201
237
|
* Used by the API Debug Panel in the platform
|
|
@@ -243,4 +279,4 @@ declare function createApiRoutesMiddleware(): RouteMiddleware;
|
|
|
243
279
|
*/
|
|
244
280
|
declare function registerMiddlewares(server: ExpressApp | ViteMiddleware, middlewares: Middleware[], options?: Partial<MiddlewareContext>): Promise<void>;
|
|
245
281
|
|
|
246
|
-
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createApiRoutesMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares };
|
|
282
|
+
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createApiRoutesMiddleware, 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
2
|
import { Router, RequestHandler, Application } from 'express';
|
|
3
|
+
import { ServerResponse as ServerResponse$1, IncomingMessage as IncomingMessage$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
|
* Creates api-routes middleware for listing application API routes
|
|
201
237
|
* Used by the API Debug Panel in the platform
|
|
@@ -243,4 +279,4 @@ declare function createApiRoutesMiddleware(): RouteMiddleware;
|
|
|
243
279
|
*/
|
|
244
280
|
declare function registerMiddlewares(server: ExpressApp | ViteMiddleware, middlewares: Middleware[], options?: Partial<MiddlewareContext>): Promise<void>;
|
|
245
281
|
|
|
246
|
-
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createApiRoutesMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares };
|
|
282
|
+
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type Options, type RouteInfo, type RouteMiddleware, createApiRoutesMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, getQuery, getQueryParam, handleDevProxyError, normalizeBasePath, parseAndGenerateNestResourceTemplate, postprocessDrizzleSchema, registerMiddlewares, sendError, sendJson, sendSuccess };
|
package/dist/index.js
CHANGED
|
@@ -3561,6 +3561,64 @@ function createCollectLogsMiddleware(options = {}) {
|
|
|
3561
3561
|
}
|
|
3562
3562
|
__name(createCollectLogsMiddleware, "createCollectLogsMiddleware");
|
|
3563
3563
|
|
|
3564
|
+
// src/middlewares/compat.ts
|
|
3565
|
+
function sendJson(res, data, statusCode = 200) {
|
|
3566
|
+
const statusFn = res.status;
|
|
3567
|
+
const jsonFn = res.json;
|
|
3568
|
+
if (typeof statusFn === "function" && typeof jsonFn === "function") {
|
|
3569
|
+
statusFn.call(res, statusCode);
|
|
3570
|
+
jsonFn.call(res, data);
|
|
3571
|
+
} else {
|
|
3572
|
+
res.statusCode = statusCode;
|
|
3573
|
+
res.setHeader("Content-Type", "application/json");
|
|
3574
|
+
res.end(JSON.stringify(data));
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
__name(sendJson, "sendJson");
|
|
3578
|
+
function sendError(res, message, error, statusCode = 500) {
|
|
3579
|
+
const errorData = {
|
|
3580
|
+
message,
|
|
3581
|
+
...error ? {
|
|
3582
|
+
error: serializeError3(error)
|
|
3583
|
+
} : {}
|
|
3584
|
+
};
|
|
3585
|
+
sendJson(res, errorData, statusCode);
|
|
3586
|
+
}
|
|
3587
|
+
__name(sendError, "sendError");
|
|
3588
|
+
function sendSuccess(res, data) {
|
|
3589
|
+
sendJson(res, {
|
|
3590
|
+
success: true,
|
|
3591
|
+
...data
|
|
3592
|
+
}, 200);
|
|
3593
|
+
}
|
|
3594
|
+
__name(sendSuccess, "sendSuccess");
|
|
3595
|
+
function getQuery(req) {
|
|
3596
|
+
if (req.query) {
|
|
3597
|
+
return req.query;
|
|
3598
|
+
}
|
|
3599
|
+
const url = new URL(req.url || "", `http://${req.headers.host}`);
|
|
3600
|
+
return Object.fromEntries(url.searchParams.entries());
|
|
3601
|
+
}
|
|
3602
|
+
__name(getQuery, "getQuery");
|
|
3603
|
+
function getQueryParam(req, key) {
|
|
3604
|
+
const query = getQuery(req);
|
|
3605
|
+
return query[key];
|
|
3606
|
+
}
|
|
3607
|
+
__name(getQueryParam, "getQueryParam");
|
|
3608
|
+
function serializeError3(error) {
|
|
3609
|
+
if (error instanceof Error) {
|
|
3610
|
+
return {
|
|
3611
|
+
name: error.name,
|
|
3612
|
+
message: error.message,
|
|
3613
|
+
stack: error.stack
|
|
3614
|
+
};
|
|
3615
|
+
}
|
|
3616
|
+
return {
|
|
3617
|
+
message: String(error)
|
|
3618
|
+
};
|
|
3619
|
+
}
|
|
3620
|
+
__name(serializeError3, "serializeError");
|
|
3621
|
+
|
|
3564
3622
|
// src/middlewares/api-routes/router.ts
|
|
3565
3623
|
import express4 from "express";
|
|
3566
3624
|
function extractModuleFromPath2(path7) {
|
|
@@ -3703,6 +3761,41 @@ function createApiRoutesMiddleware() {
|
|
|
3703
3761
|
__name(createApiRoutesMiddleware, "createApiRoutesMiddleware");
|
|
3704
3762
|
|
|
3705
3763
|
// src/middlewares/index.ts
|
|
3764
|
+
function enhanceForCompat(req, res) {
|
|
3765
|
+
if (!res.status) {
|
|
3766
|
+
res.status = function(code) {
|
|
3767
|
+
res.statusCode = code;
|
|
3768
|
+
return res;
|
|
3769
|
+
};
|
|
3770
|
+
}
|
|
3771
|
+
if (!res.json) {
|
|
3772
|
+
res.json = function(data) {
|
|
3773
|
+
res.setHeader("Content-Type", "application/json");
|
|
3774
|
+
res.end(JSON.stringify(data));
|
|
3775
|
+
return res;
|
|
3776
|
+
};
|
|
3777
|
+
}
|
|
3778
|
+
if (!res.send) {
|
|
3779
|
+
res.send = function(data) {
|
|
3780
|
+
if (typeof data === "object" && data !== null) {
|
|
3781
|
+
res.setHeader("Content-Type", "application/json");
|
|
3782
|
+
res.end(JSON.stringify(data));
|
|
3783
|
+
} else {
|
|
3784
|
+
res.end(String(data));
|
|
3785
|
+
}
|
|
3786
|
+
return res;
|
|
3787
|
+
};
|
|
3788
|
+
}
|
|
3789
|
+
if (!req.query) {
|
|
3790
|
+
const url = new URL(req.url || "", `http://${req.headers.host || "localhost"}`);
|
|
3791
|
+
req.query = Object.fromEntries(url.searchParams.entries());
|
|
3792
|
+
}
|
|
3793
|
+
}
|
|
3794
|
+
__name(enhanceForCompat, "enhanceForCompat");
|
|
3795
|
+
function isConnectServer(server) {
|
|
3796
|
+
return typeof server === "function" && !("set" in server) && !("engine" in server);
|
|
3797
|
+
}
|
|
3798
|
+
__name(isConnectServer, "isConnectServer");
|
|
3706
3799
|
function isRouteMiddleware(middleware) {
|
|
3707
3800
|
return "createRouter" in middleware && middleware.createRouter !== void 0;
|
|
3708
3801
|
}
|
|
@@ -3755,6 +3848,13 @@ async function registerMiddlewares(server, middlewares, options) {
|
|
|
3755
3848
|
rootDir: process.cwd(),
|
|
3756
3849
|
...options
|
|
3757
3850
|
};
|
|
3851
|
+
if (isConnectServer(server)) {
|
|
3852
|
+
server.use((req, res, next) => {
|
|
3853
|
+
enhanceForCompat(req, res);
|
|
3854
|
+
next();
|
|
3855
|
+
});
|
|
3856
|
+
console.log("[Middleware] Registered Express compatibility layer for Connect/Vite");
|
|
3857
|
+
}
|
|
3758
3858
|
const allMiddlewares = [
|
|
3759
3859
|
...middlewares
|
|
3760
3860
|
];
|
|
@@ -3786,10 +3886,15 @@ export {
|
|
|
3786
3886
|
createCollectLogsMiddleware,
|
|
3787
3887
|
createDevLogsMiddleware,
|
|
3788
3888
|
createOpenapiMiddleware,
|
|
3889
|
+
getQuery,
|
|
3890
|
+
getQueryParam,
|
|
3789
3891
|
handleDevProxyError,
|
|
3790
3892
|
normalizeBasePath,
|
|
3791
3893
|
parseAndGenerateNestResourceTemplate,
|
|
3792
3894
|
postprocessDrizzleSchema,
|
|
3793
|
-
registerMiddlewares
|
|
3895
|
+
registerMiddlewares,
|
|
3896
|
+
sendError,
|
|
3897
|
+
sendJson,
|
|
3898
|
+
sendSuccess
|
|
3794
3899
|
};
|
|
3795
3900
|
//# sourceMappingURL=index.js.map
|