@zenweb/cache 4.0.0 → 4.0.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/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.0.1] - 2023-8-17
4
+ cached 中间件增加 type 参数
5
+
3
6
  ## [4.0.0] - 2023-8-8
4
7
  发布
@@ -1,8 +1,18 @@
1
1
  import { Context, Middleware } from "@zenweb/core";
2
2
  import { LockGetOption } from "./types";
3
+ interface CachedMiddlewareOption extends Omit<LockGetOption, 'parse' | 'decompress'> {
4
+ /**
5
+ * 输出内容类型
6
+ * - 默认 json
7
+ * - 需要指定输出类型,因为缓存的是 body 主体,并不包含类型
8
+ */
9
+ type?: string;
10
+ }
3
11
  /**
4
12
  * 缓存中间件
13
+ * - 缓存中间件缓存的是 ctx.body 最终结果,所以可以缓存任何返回类型
5
14
  * - 会根据客户端请求头判断是否需要解压
6
15
  * @param key 缓存 key 如果不指定则默认使用 ctx.path
7
16
  */
8
- export declare function cached(key?: string | ((ctx: Context) => string | Promise<string>), opt?: Omit<LockGetOption, 'parse' | 'decompress'>): Middleware;
17
+ export declare function cached(key?: string | ((ctx: Context) => string | Promise<string>), opt?: CachedMiddlewareOption): Middleware;
18
+ export {};
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cached = void 0;
4
4
  /**
5
5
  * 缓存中间件
6
+ * - 缓存中间件缓存的是 ctx.body 最终结果,所以可以缓存任何返回类型
6
7
  * - 会根据客户端请求头判断是否需要解压
7
8
  * @param key 缓存 key 如果不指定则默认使用 ctx.path
8
9
  */
@@ -21,6 +22,9 @@ function cached(key, opt) {
21
22
  if (!decompress && result.compressed) {
22
23
  ctx.set('Content-Encoding', 'gzip');
23
24
  }
25
+ if (!ctx.type) {
26
+ ctx.type = (opt === null || opt === void 0 ? void 0 : opt.type) || 'json';
27
+ }
24
28
  ctx.body = result.data;
25
29
  };
26
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/cache",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Zenweb Cache module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",