@zenweb/template 4.0.0 → 4.1.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.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import { SetupFunction } from '@zenweb/core';
2
2
  import { TemplateSetupOption, TemplateOption } from './types';
3
- import { TEMPLATE_OPTION } from './render';
4
3
  export * from './types';
5
4
  export default function setup(option: TemplateSetupOption): SetupFunction;
6
5
  declare module '@zenweb/core' {
7
6
  interface Context {
8
- [TEMPLATE_OPTION]?: TemplateOption;
7
+ templateOption?: TemplateOption;
9
8
  /**
10
9
  * 启用并设置模版渲染
11
10
  * - 不传参数: 启用
package/dist/index.js CHANGED
@@ -19,17 +19,17 @@ const render_1 = require("./render");
19
19
  __exportStar(require("./types"), exports);
20
20
  function contextTemplate(template_or_option) {
21
21
  if (template_or_option === false) {
22
- delete this[render_1.TEMPLATE_OPTION];
22
+ this.templateOption = undefined;
23
23
  return;
24
24
  }
25
- if (!this[render_1.TEMPLATE_OPTION]) {
26
- this[render_1.TEMPLATE_OPTION] = {};
25
+ if (!this.templateOption) {
26
+ this.templateOption = {};
27
27
  }
28
28
  if (typeof template_or_option === 'string') {
29
- Object.assign(this[render_1.TEMPLATE_OPTION], { template: template_or_option });
29
+ Object.assign(this.templateOption, { template: template_or_option });
30
30
  }
31
31
  else if (typeof template_or_option === 'object') {
32
- Object.assign(this[render_1.TEMPLATE_OPTION], template_or_option);
32
+ Object.assign(this.templateOption, template_or_option);
33
33
  }
34
34
  }
35
35
  function setup(option) {
package/dist/render.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Context } from '@zenweb/core';
2
2
  import { ResultRender } from '@zenweb/result';
3
3
  import { TemplateSetupOption } from './types';
4
- export declare const TEMPLATE_OPTION: unique symbol;
5
4
  export declare class TemplateRender implements ResultRender {
6
5
  private _option;
6
+ enwrap: boolean;
7
7
  type: string;
8
8
  constructor(_option: TemplateSetupOption);
9
9
  match(ctx: Context): boolean;
package/dist/render.js CHANGED
@@ -1,20 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TemplateRender = exports.TEMPLATE_OPTION = void 0;
3
+ exports.TemplateRender = void 0;
4
4
  const result_1 = require("@zenweb/result");
5
5
  const utils_1 = require("./utils");
6
- exports.TEMPLATE_OPTION = Symbol('template@option');
7
6
  class TemplateRender {
8
7
  constructor(_option) {
9
8
  this._option = _option;
9
+ this.enwrap = false;
10
10
  this.type = this._option.type || 'html';
11
11
  }
12
12
  match(ctx) {
13
- const opt = ctx[exports.TEMPLATE_OPTION];
14
- // 匹配引擎, 如果不设置引擎则直接匹配
15
- if (opt && (!opt.engine || opt.engine === this._option.engineName)) {
16
- (0, utils_1.debug)('matchEngine: %o', this._option.engineName);
17
- return true;
13
+ const opt = ctx.templateOption;
14
+ if (opt) {
15
+ // 匹配引擎
16
+ if (opt.engine === this._option.engineName) {
17
+ (0, utils_1.debug)('matchEngine: %o', this._option.engineName);
18
+ return true;
19
+ }
20
+ // 指定模版
21
+ if (opt.template && this._option.templateAffix && opt.template.endsWith(this._option.templateAffix)) {
22
+ (0, utils_1.debug)('matchTemplateAffix: %o', this._option.templateAffix);
23
+ return true;
24
+ }
25
+ }
26
+ // 忽略路径规则
27
+ if (this._option.ignorePath) {
28
+ for (const reg of this._option.ignorePath) {
29
+ if (reg.test(ctx.path)) {
30
+ (0, utils_1.debug)('ignorePath: %o', reg);
31
+ return false;
32
+ }
33
+ }
18
34
  }
19
35
  // 匹配路径规则
20
36
  if (this._option.matchPath) {
@@ -27,7 +43,7 @@ class TemplateRender {
27
43
  }
28
44
  // 匹配 Accept 头类型
29
45
  if (this._option.matchAccept) {
30
- const _type = ctx.accepts(this._option.matchAccept);
46
+ const _type = ctx.accepts().some(accept => { var _a; return (_a = this._option.matchAccept) === null || _a === void 0 ? void 0 : _a.includes(accept); });
31
47
  if (_type) {
32
48
  (0, utils_1.debug)('matchAccept: %o', _type);
33
49
  return true;
@@ -36,7 +52,7 @@ class TemplateRender {
36
52
  return false;
37
53
  }
38
54
  render(ctx, data) {
39
- const opt = ctx[exports.TEMPLATE_OPTION] || {};
55
+ const opt = ctx.templateOption || {};
40
56
  let template = opt.template;
41
57
  if (data instanceof result_1.ResultFail) {
42
58
  data = { fail: data };
package/dist/types.d.ts CHANGED
@@ -10,9 +10,13 @@ export interface TemplateSetupOption {
10
10
  */
11
11
  type?: string;
12
12
  /**
13
- * 匹配路径结尾例如 `/.html$/i`
13
+ * 匹配路径,例如 `/.html$/i`
14
14
  */
15
15
  matchPath?: RegExp[];
16
+ /**
17
+ * 忽略路径,例如 `/^\/api\//i`
18
+ */
19
+ ignorePath?: RegExp[];
16
20
  /**
17
21
  * 匹配 Accept Type 头信息
18
22
  */
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@zenweb/template",
3
3
  "packageManager": "yarn@4.0.2",
4
- "version": "4.0.0",
4
+ "version": "4.1.0",
5
5
  "description": "zenweb template render module",
6
6
  "exports": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "scripts": {
9
9
  "vscode": "yarn dlx @yarnpkg/sdks vscode",
10
10
  "build": "rimraf dist && tsc",
11
- "prepublishOnly": "yarn run build",
11
+ "prepack": "yarn run build",
12
12
  "dev": "cd example && cross-env DEBUG=\\* ts-node app"
13
13
  },
14
14
  "files": [
@@ -27,17 +27,17 @@
27
27
  "homepage": "https://zenweb.node.ltd",
28
28
  "devDependencies": {
29
29
  "@types/node": "^20.10.6",
30
- "@zenweb/inject": "^4.0.6",
30
+ "@zenweb/inject": "^4.1.1",
31
31
  "@zenweb/template-handlebars": "^3.0.0",
32
- "@zenweb/template-nunjucks": "^3.0.0",
32
+ "@zenweb/template-nunjucks": "^3.3.0",
33
33
  "cross-env": "^7.0.3",
34
34
  "rimraf": "^4.4.1",
35
35
  "ts-node": "^10.9.1",
36
- "typescript": "^5.3.3",
37
- "zenweb": "^4.2.7"
36
+ "typescript": "~5.3.3",
37
+ "zenweb": "^4.5.0"
38
38
  },
39
39
  "dependencies": {
40
- "@zenweb/core": "^4.2.1",
41
- "@zenweb/result": "^3.12.6"
40
+ "@zenweb/core": "^4.3.2",
41
+ "@zenweb/result": "^4.0.0"
42
42
  }
43
43
  }