imean-service-engine 2.0.1 → 2.0.3

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
@@ -393,6 +393,12 @@ interface RouteModuleOptions {
393
393
  * RoutePlugin 配置选项
394
394
  */
395
395
  interface RoutePluginOptions {
396
+ /**
397
+ * 全局路径前缀(应用于所有路由)
398
+ * 路径拼接顺序:全局前缀 + 模块前缀 + 路由路径
399
+ * 例如:prefix="/api", routePrefix="/v1", path="/users" => "/api/v1/users"
400
+ */
401
+ prefix?: string;
396
402
  /**
397
403
  * 全局中间件(应用于所有路由)
398
404
  * 执行顺序:全局中间件 -> 模块级中间件 -> 路由级中间件
@@ -441,13 +447,14 @@ declare function Page(options: RouteOptions): (target: Function, context: ClassM
441
447
  *
442
448
  * @example
443
449
  * ```typescript
444
- * // 使用全局中间件(如鉴权)
450
+ * // 使用全局前缀和中间件
445
451
  * const authMiddleware = async (ctx, next) => {
446
452
  * // 验证 token
447
453
  * await next();
448
454
  * };
449
455
  *
450
456
  * const routePlugin = new RoutePlugin({
457
+ * prefix: "/api", // 全局路径前缀,所有路由都会加上这个前缀
451
458
  * globalMiddlewares: [authMiddleware],
452
459
  * });
453
460
  * ```
@@ -456,6 +463,7 @@ declare class RoutePlugin implements Plugin<RouteModuleOptions> {
456
463
  readonly name = "route-plugin";
457
464
  readonly priority = PluginPriority.ROUTE;
458
465
  private engine;
466
+ private globalPrefix;
459
467
  private globalMiddlewares;
460
468
  /**
461
469
  * 构造函数
package/dist/index.d.ts CHANGED
@@ -393,6 +393,12 @@ interface RouteModuleOptions {
393
393
  * RoutePlugin 配置选项
394
394
  */
395
395
  interface RoutePluginOptions {
396
+ /**
397
+ * 全局路径前缀(应用于所有路由)
398
+ * 路径拼接顺序:全局前缀 + 模块前缀 + 路由路径
399
+ * 例如:prefix="/api", routePrefix="/v1", path="/users" => "/api/v1/users"
400
+ */
401
+ prefix?: string;
396
402
  /**
397
403
  * 全局中间件(应用于所有路由)
398
404
  * 执行顺序:全局中间件 -> 模块级中间件 -> 路由级中间件
@@ -441,13 +447,14 @@ declare function Page(options: RouteOptions): (target: Function, context: ClassM
441
447
  *
442
448
  * @example
443
449
  * ```typescript
444
- * // 使用全局中间件(如鉴权)
450
+ * // 使用全局前缀和中间件
445
451
  * const authMiddleware = async (ctx, next) => {
446
452
  * // 验证 token
447
453
  * await next();
448
454
  * };
449
455
  *
450
456
  * const routePlugin = new RoutePlugin({
457
+ * prefix: "/api", // 全局路径前缀,所有路由都会加上这个前缀
451
458
  * globalMiddlewares: [authMiddleware],
452
459
  * });
453
460
  * ```
@@ -456,6 +463,7 @@ declare class RoutePlugin implements Plugin<RouteModuleOptions> {
456
463
  readonly name = "route-plugin";
457
464
  readonly priority = PluginPriority.ROUTE;
458
465
  private engine;
466
+ private globalPrefix;
459
467
  private globalMiddlewares;
460
468
  /**
461
469
  * 构造函数
package/dist/index.js CHANGED
@@ -2124,12 +2124,14 @@ var RoutePlugin = class {
2124
2124
  name = "route-plugin";
2125
2125
  priority = 1e3 /* ROUTE */;
2126
2126
  engine;
2127
+ globalPrefix;
2127
2128
  globalMiddlewares;
2128
2129
  /**
2129
2130
  * 构造函数
2130
2131
  * @param options 插件配置选项
2131
2132
  */
2132
2133
  constructor(options) {
2134
+ this.globalPrefix = options?.prefix || "";
2133
2135
  this.globalMiddlewares = options?.globalMiddlewares || [];
2134
2136
  }
2135
2137
  /**
@@ -2182,7 +2184,7 @@ var RoutePlugin = class {
2182
2184
  const moduleOptions = moduleMetadata?.options || {};
2183
2185
  const routePrefix = moduleOptions.routePrefix || "";
2184
2186
  const paths = Array.isArray(routeOptions.path) ? routeOptions.path : [routeOptions.path];
2185
- const fullPaths = paths.map((p) => routePrefix + p);
2187
+ const fullPaths = paths.map((p) => this.globalPrefix + routePrefix + p);
2186
2188
  const routeHandler = async (ctx) => {
2187
2189
  const method = moduleInstance[methodName];
2188
2190
  if (typeof method !== "function") {
@@ -2190,10 +2192,19 @@ var RoutePlugin = class {
2190
2192
  }
2191
2193
  try {
2192
2194
  const result = await method.call(moduleInstance, ctx);
2195
+ if (result === void 0) {
2196
+ return new Response(null, { status: 204 });
2197
+ }
2193
2198
  if (result instanceof Response) {
2194
2199
  return result;
2195
2200
  }
2196
- if (typeof result === "string" || typeof result === "object" && result !== null && "type" in result) {
2201
+ if (typeof result === "string") {
2202
+ return result;
2203
+ }
2204
+ if (result === null) {
2205
+ return result;
2206
+ }
2207
+ if (typeof result === "object" && result !== null && "type" in result && (typeof result.type === "function" || typeof result.type === "string")) {
2197
2208
  return result;
2198
2209
  }
2199
2210
  return ctx.json(result);
package/dist/index.mjs CHANGED
@@ -2099,12 +2099,14 @@ var RoutePlugin = class {
2099
2099
  name = "route-plugin";
2100
2100
  priority = 1e3 /* ROUTE */;
2101
2101
  engine;
2102
+ globalPrefix;
2102
2103
  globalMiddlewares;
2103
2104
  /**
2104
2105
  * 构造函数
2105
2106
  * @param options 插件配置选项
2106
2107
  */
2107
2108
  constructor(options) {
2109
+ this.globalPrefix = options?.prefix || "";
2108
2110
  this.globalMiddlewares = options?.globalMiddlewares || [];
2109
2111
  }
2110
2112
  /**
@@ -2157,7 +2159,7 @@ var RoutePlugin = class {
2157
2159
  const moduleOptions = moduleMetadata?.options || {};
2158
2160
  const routePrefix = moduleOptions.routePrefix || "";
2159
2161
  const paths = Array.isArray(routeOptions.path) ? routeOptions.path : [routeOptions.path];
2160
- const fullPaths = paths.map((p) => routePrefix + p);
2162
+ const fullPaths = paths.map((p) => this.globalPrefix + routePrefix + p);
2161
2163
  const routeHandler = async (ctx) => {
2162
2164
  const method = moduleInstance[methodName];
2163
2165
  if (typeof method !== "function") {
@@ -2165,10 +2167,19 @@ var RoutePlugin = class {
2165
2167
  }
2166
2168
  try {
2167
2169
  const result = await method.call(moduleInstance, ctx);
2170
+ if (result === void 0) {
2171
+ return new Response(null, { status: 204 });
2172
+ }
2168
2173
  if (result instanceof Response) {
2169
2174
  return result;
2170
2175
  }
2171
- if (typeof result === "string" || typeof result === "object" && result !== null && "type" in result) {
2176
+ if (typeof result === "string") {
2177
+ return result;
2178
+ }
2179
+ if (result === null) {
2180
+ return result;
2181
+ }
2182
+ if (typeof result === "object" && result !== null && "type" in result && (typeof result.type === "function" || typeof result.type === "string")) {
2172
2183
  return result;
2173
2184
  }
2174
2185
  return ctx.json(result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imean-service-engine",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "基于Hono的轻量级微服务引擎框架",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",