@zenweb/template 4.1.0 → 5.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/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.js";
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,11 @@
1
+ import { $getContext } from "@zenweb/core";
2
+ /**
3
+ * 启用并设置模版渲染
4
+ * - 不传参数: 启用
5
+ * - false: 关闭模版渲染
6
+ * - string: 设置模版文件名
7
+ * - TemplateOption: 详细设置
8
+ */
9
+ export function $template(template_or_option) {
10
+ return $getContext().template(template_or_option);
11
+ }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { SetupFunction } from '@zenweb/core';
2
- import { TemplateSetupOption, TemplateOption } from './types';
3
- export * from './types';
2
+ import { TemplateSetupOption, TemplateOption } from './types.js';
3
+ export * from './types.js';
4
+ export * from './global.js';
4
5
  export default function setup(option: TemplateSetupOption): SetupFunction;
5
6
  declare module '@zenweb/core' {
6
7
  interface Context {
package/dist/index.js CHANGED
@@ -1,22 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- const result_1 = require("@zenweb/result");
18
- const render_1 = require("./render");
19
- __exportStar(require("./types"), exports);
1
+ import { RenderManager } from '@zenweb/result';
2
+ import { TemplateRender } from './render.js';
3
+ export * from './types.js';
4
+ export * from './global.js';
20
5
  function contextTemplate(template_or_option) {
21
6
  if (template_or_option === false) {
22
7
  this.templateOption = undefined;
@@ -32,18 +17,17 @@ function contextTemplate(template_or_option) {
32
17
  Object.assign(this.templateOption, template_or_option);
33
18
  }
34
19
  }
35
- function setup(option) {
20
+ export default function setup(option) {
36
21
  return async function template(setup) {
37
22
  if (!option.engineName) {
38
23
  option.engineName = option.engine.name;
39
24
  }
40
25
  setup.debug('option: %o', option);
41
26
  setup.assertModuleExists('result', '@zenweb/result');
42
- const renderManager = await setup.core.injector.getInstance(result_1.RenderManager);
43
- renderManager.add(new render_1.TemplateRender(option));
27
+ const renderManager = await setup.core.injector.getInstance(RenderManager);
28
+ renderManager.add(new TemplateRender(option));
44
29
  if (!('template' in setup.app.context)) {
45
30
  setup.defineContextProperty('template', { value: contextTemplate });
46
31
  }
47
32
  };
48
33
  }
49
- exports.default = setup;
package/dist/render.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Context } from '@zenweb/core';
2
2
  import { ResultRender } from '@zenweb/result';
3
- import { TemplateSetupOption } from './types';
3
+ import { TemplateSetupOption } from './types.js';
4
4
  export declare class TemplateRender implements ResultRender {
5
5
  private _option;
6
6
  enwrap: boolean;
package/dist/render.js CHANGED
@@ -1,25 +1,24 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TemplateRender = void 0;
4
- const result_1 = require("@zenweb/result");
5
- const utils_1 = require("./utils");
6
- class TemplateRender {
1
+ import { ResultFail } from '@zenweb/result';
2
+ import { debug } from './utils.js';
3
+ export class TemplateRender {
4
+ _option;
5
+ enwrap = false;
6
+ type;
7
7
  constructor(_option) {
8
8
  this._option = _option;
9
- this.enwrap = false;
10
- this.type = this._option.type || 'html';
9
+ this.type = _option.type || 'html';
11
10
  }
12
11
  match(ctx) {
13
12
  const opt = ctx.templateOption;
14
13
  if (opt) {
15
14
  // 匹配引擎
16
15
  if (opt.engine === this._option.engineName) {
17
- (0, utils_1.debug)('matchEngine: %o', this._option.engineName);
16
+ debug('matchEngine: %o', this._option.engineName);
18
17
  return true;
19
18
  }
20
19
  // 指定模版
21
20
  if (opt.template && this._option.templateAffix && opt.template.endsWith(this._option.templateAffix)) {
22
- (0, utils_1.debug)('matchTemplateAffix: %o', this._option.templateAffix);
21
+ debug('matchTemplateAffix: %o', this._option.templateAffix);
23
22
  return true;
24
23
  }
25
24
  }
@@ -27,7 +26,7 @@ class TemplateRender {
27
26
  if (this._option.ignorePath) {
28
27
  for (const reg of this._option.ignorePath) {
29
28
  if (reg.test(ctx.path)) {
30
- (0, utils_1.debug)('ignorePath: %o', reg);
29
+ debug('ignorePath: %o', reg);
31
30
  return false;
32
31
  }
33
32
  }
@@ -36,16 +35,16 @@ class TemplateRender {
36
35
  if (this._option.matchPath) {
37
36
  for (const reg of this._option.matchPath) {
38
37
  if (reg.test(ctx.path)) {
39
- (0, utils_1.debug)('matchPath: %o', reg);
38
+ debug('matchPath: %o', reg);
40
39
  return true;
41
40
  }
42
41
  }
43
42
  }
44
43
  // 匹配 Accept 头类型
45
44
  if (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); });
45
+ const _type = ctx.accepts().some(accept => this._option.matchAccept?.includes(accept));
47
46
  if (_type) {
48
- (0, utils_1.debug)('matchAccept: %o', _type);
47
+ debug('matchAccept: %o', _type);
49
48
  return true;
50
49
  }
51
50
  }
@@ -54,7 +53,7 @@ class TemplateRender {
54
53
  render(ctx, data) {
55
54
  const opt = ctx.templateOption || {};
56
55
  let template = opt.template;
57
- if (data instanceof result_1.ResultFail) {
56
+ if (data instanceof ResultFail) {
58
57
  data = { fail: data };
59
58
  if (opt.failTemplate !== false) {
60
59
  template = opt.failTemplate || this._option.failTemplate || template;
@@ -69,4 +68,3 @@ class TemplateRender {
69
68
  return this._option.engine(template, data);
70
69
  }
71
70
  }
72
- exports.TemplateRender = TemplateRender;
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
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/dist/utils.js CHANGED
@@ -1,13 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cwdPath = exports.debug = void 0;
4
- const path = require("path");
5
- const core_1 = require("@zenweb/core");
6
- exports.debug = core_1.debug.extend('template');
7
- function cwdPath(p) {
1
+ import * as path from 'path';
2
+ import { debug as _debug } from '@zenweb/core';
3
+ export const debug = _debug.extend('template');
4
+ export function cwdPath(p) {
8
5
  if (p.startsWith('./')) {
9
6
  return path.join(process.cwd(), p.slice(2));
10
7
  }
11
8
  return p;
12
9
  }
13
- exports.cwdPath = cwdPath;
package/package.json CHANGED
@@ -1,15 +1,14 @@
1
1
  {
2
2
  "name": "@zenweb/template",
3
- "packageManager": "yarn@4.0.2",
4
- "version": "4.1.0",
3
+ "type": "module",
4
+ "version": "5.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
- "vscode": "yarn dlx @yarnpkg/sdks vscode",
10
9
  "build": "rimraf dist && tsc",
11
- "prepack": "yarn run build",
12
- "dev": "cd example && cross-env DEBUG=\\* ts-node app"
10
+ "prepack": "npm run build",
11
+ "dev": "cd example && node --env-file=.env --import tsx/esm app.ts"
13
12
  },
14
13
  "files": [
15
14
  "dist"
@@ -27,17 +26,16 @@
27
26
  "homepage": "https://zenweb.node.ltd",
28
27
  "devDependencies": {
29
28
  "@types/node": "^20.10.6",
30
- "@zenweb/inject": "^4.1.1",
29
+ "@zenweb/inject": "^5.1.0",
31
30
  "@zenweb/template-handlebars": "^3.0.0",
32
31
  "@zenweb/template-nunjucks": "^3.3.0",
33
- "cross-env": "^7.0.3",
34
32
  "rimraf": "^4.4.1",
35
- "ts-node": "^10.9.1",
36
- "typescript": "~5.3.3",
37
- "zenweb": "^4.5.0"
33
+ "tsx": "^4.19.1",
34
+ "typescript": "^5.3.3",
35
+ "zenweb": "^5.1.0"
38
36
  },
39
37
  "dependencies": {
40
- "@zenweb/core": "^4.3.2",
41
- "@zenweb/result": "^4.0.0"
38
+ "@zenweb/core": "^5.1.0",
39
+ "@zenweb/result": "^5.0.0"
42
40
  }
43
41
  }