@zenweb/template 4.0.0 → 5.0.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/README.md CHANGED
@@ -1,33 +1,23 @@
1
- # zenweb-template
1
+ # template - 服务端模版渲染
2
2
 
3
- zenweb template render module
3
+ ## 功能说明
4
4
 
5
- ## Install
5
+ 在服务器端使用模版引擎渲染模版并根据请求条件按需输出
6
6
 
7
- ```bash
8
- yarn add @zenweb/template
9
- ```
10
-
11
- ### nunjucks
12
-
13
- ```bash
14
- yarn add @zenweb/template-nunjucks
15
- ```
7
+ ## 演示
16
8
 
17
- ### handlebars
9
+ ### 安装
18
10
 
19
11
  ```bash
20
- yarn add @zenweb/template-handlebars
12
+ yarn add @zenweb/template @zenweb/template-nunjucks
21
13
  ```
22
14
 
23
- ## App Config
15
+ ### 配置
24
16
 
25
- src/index.ts
26
17
  ```ts
27
18
  import { create } from 'zenweb';
28
19
  import modTemplate from '@zenweb/template';
29
20
  import nunjucks from '@zenweb/template-nunjucks';
30
- import handlebars from '@zenweb/template-handlebars';
31
21
 
32
22
  create()
33
23
 
@@ -39,55 +29,59 @@ create()
39
29
  }),
40
30
  }))
41
31
 
42
- // Multiple Engines
43
- .setup(modTemplate({
44
- type: 'xml',
45
- matchPath: [/\.xml$/i],
46
- templateAffix: '.xml',
47
- engineName: 'xml',
48
- engine: handlebars(),
49
- }))
50
-
51
32
  .start();
52
33
  ```
53
34
 
54
- ## Use
35
+ 控制器使用
55
36
 
56
- src/controller/demo.ts
57
37
  ```ts
58
- import { Context, mapping } from "zenweb";
38
+ import { Context, mapping, Body, $body } from 'zenweb';
59
39
 
60
- export class DemoController {
61
- @mapping({ path: ['/', '/index.html'] })
62
- index(ctx: Context) {
63
- ctx.template('index.html');
64
- return { name: 'Hello' };
40
+ export class Controller {
41
+ @mapping({ path: '/', method: 'POST' })
42
+ post(body: Body) {
43
+ console.log(body.type); // POST body 内容类型
44
+ console.log(body.data); // POST Body 内容解析完成后的数据
65
45
  }
66
46
 
67
- @mapping({ path: '/test.html' })
68
- test() {
69
- return { name: 'Test' };
47
+ @mapping({ path: '/', method: 'POST' })
48
+ async post() {
49
+ console.log(await getAge()); // 类型转换&校验
70
50
  }
51
+ }
71
52
 
72
- @mapping()
73
- xml(ctx: Context) {
74
- ctx.template({ engine: 'xml' })
75
- return { name: 'Test' };
76
- }
53
+ async function getAge() {
54
+ return (await $body.get({ age: '!int' })).age;
77
55
  }
78
56
  ```
79
57
 
58
+ 模版代码
59
+
80
60
  template/index.html
81
61
  ```html
82
62
  <h1>Hello {{name}}</h1>
83
63
  ```
84
64
 
85
- template/test.html
86
- ```html
87
- <h1>Hello {{name}}</h1>
88
- ```
65
+ ## 依赖模块
89
66
 
90
- template/xml.xml
91
- ```xml
92
- <root>{{name}}</root>
93
- ```
67
+ - @zenweb/result
68
+
69
+ ## Core 挂载项
70
+
71
+
72
+
73
+ ## Context 挂载项
74
+
75
+ - `templateOption`
76
+ - `template(option)` 设置当前请求的模版渲染选项
77
+
78
+ ## 全局模式
79
+
80
+ | 方法 | 功能 |
81
+ | ----- | ---- |
82
+ | $template() | 设置当前请求的模版渲染选项 |
83
+
84
+ ## 目前已支持的模版引擎
85
+
86
+ [nunjucks](https://www.npmjs.com/package/@zenweb/template-nunjucks)
87
+ [handlebars](https://www.npmjs.com/package/@zenweb/template-handlebars)
@@ -0,0 +1,9 @@
1
+ import { TemplateOption } from "./types";
2
+ /**
3
+ * 启用并设置模版渲染
4
+ * - 不传参数: 启用
5
+ * - false: 关闭模版渲染
6
+ * - string: 设置模版文件名
7
+ * - TemplateOption: 详细设置
8
+ */
9
+ export declare function $template(template_or_option?: false | string | TemplateOption): void;
package/dist/global.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.$template = void 0;
4
+ const core_1 = require("@zenweb/core");
5
+ /**
6
+ * 启用并设置模版渲染
7
+ * - 不传参数: 启用
8
+ * - false: 关闭模版渲染
9
+ * - string: 设置模版文件名
10
+ * - TemplateOption: 详细设置
11
+ */
12
+ function $template(template_or_option) {
13
+ return (0, core_1.$getContext)().template(template_or_option);
14
+ }
15
+ exports.$template = $template;
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
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';
4
+ export * from './global';
5
5
  export default function setup(option: TemplateSetupOption): SetupFunction;
6
6
  declare module '@zenweb/core' {
7
7
  interface Context {
8
- [TEMPLATE_OPTION]?: TemplateOption;
8
+ templateOption?: TemplateOption;
9
9
  /**
10
10
  * 启用并设置模版渲染
11
11
  * - 不传参数: 启用
package/dist/index.js CHANGED
@@ -17,19 +17,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  const result_1 = require("@zenweb/result");
18
18
  const render_1 = require("./render");
19
19
  __exportStar(require("./types"), exports);
20
+ __exportStar(require("./global"), exports);
20
21
  function contextTemplate(template_or_option) {
21
22
  if (template_or_option === false) {
22
- delete this[render_1.TEMPLATE_OPTION];
23
+ this.templateOption = undefined;
23
24
  return;
24
25
  }
25
- if (!this[render_1.TEMPLATE_OPTION]) {
26
- this[render_1.TEMPLATE_OPTION] = {};
26
+ if (!this.templateOption) {
27
+ this.templateOption = {};
27
28
  }
28
29
  if (typeof template_or_option === 'string') {
29
- Object.assign(this[render_1.TEMPLATE_OPTION], { template: template_or_option });
30
+ Object.assign(this.templateOption, { template: template_or_option });
30
31
  }
31
32
  else if (typeof template_or_option === 'object') {
32
- Object.assign(this[render_1.TEMPLATE_OPTION], template_or_option);
33
+ Object.assign(this.templateOption, template_or_option);
33
34
  }
34
35
  }
35
36
  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/dist/utils.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- import { Debugger } from '@zenweb/core';
2
- export declare const debug: Debugger;
1
+ export declare const debug: import("@zenweb/core").Debugger;
3
2
  export declare function cwdPath(p: string): string;
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": "5.0.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": "^5.0.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": "^5.0.0"
38
38
  },
39
39
  "dependencies": {
40
- "@zenweb/core": "^4.2.1",
41
- "@zenweb/result": "^3.12.6"
40
+ "@zenweb/core": "^5.0.0",
41
+ "@zenweb/result": "^4.1.3"
42
42
  }
43
43
  }