@tsed/platform-views 7.0.0-beta.2 → 7.0.0-beta.20

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/jest.config.js ADDED
@@ -0,0 +1,14 @@
1
+ // For a detailed explanation regarding each configuration property, visit:
2
+ // https://jestjs.io/docs/en/configuration.html
3
+
4
+ module.exports = {
5
+ ...require("@tsed/jest-config")(__dirname, "platform-views"),
6
+ coverageThreshold: {
7
+ global: {
8
+ branches: 100,
9
+ functions: 80,
10
+ lines: 92,
11
+ statements: 92.3
12
+ }
13
+ }
14
+ };
@@ -1,31 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.View = void 0;
4
- const core_1 = require("@tsed/core");
5
- /**
6
- * Use a view and sends the rendered HTML string to the client. Optional parameter:
7
- *
8
- * * viewOptions, an object whose properties define local variables for the view.
9
- *
10
- * The view argument is a string that is the file path of the view file to render.
11
- * This can be an absolute path, or a path relative to the views setting.
12
- * If the path does not contain a file extension, then the view engine setting determines the file extension.
13
- * If the path does contain a file extension, then Express will load the module for the specified template engine (via require())
14
- * and render it using the loaded module’s __express function.
15
- *
16
- * For more information, see [Using template engines with Express](http://expressjs.com/guide/using-template-engines.html).
17
- *
18
- * > NOTE: The view argument performs file system operations like reading a file from disk and evaluating Node.js modules,
19
- * and as so for security reasons should not contain input from the end-user.
20
- *
21
- * @param path Relative path to the view file.
22
- * @param options Additional options
23
- * @decorator
24
- * @operation
25
- * @response
26
- */
27
- function View(path, options) {
28
- return (0, core_1.StoreSet)("view", { path, options });
29
- }
30
- exports.View = View;
4
+ var schema_1 = require("@tsed/schema");
5
+ Object.defineProperty(exports, "View", { enumerable: true, get: function () { return schema_1.View; } });
31
6
  //# sourceMappingURL=view.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/decorators/view.ts"],"names":[],"mappings":";;;AAAA,qCAAoC;AAEpC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,IAAI,CAAC,IAAY,EAAE,OAAgB;IACjD,OAAO,IAAA,eAAQ,EAAC,MAAM,EAAE,EAAC,IAAI,EAAE,OAAO,EAAC,CAAQ,CAAC;AAClD,CAAC;AAFD,oBAEC","sourcesContent":["import {StoreSet} from \"@tsed/core\";\n\n/**\n * Use a view and sends the rendered HTML string to the client. Optional parameter:\n *\n * * viewOptions, an object whose properties define local variables for the view.\n *\n * The view argument is a string that is the file path of the view file to render.\n * This can be an absolute path, or a path relative to the views setting.\n * If the path does not contain a file extension, then the view engine setting determines the file extension.\n * If the path does contain a file extension, then Express will load the module for the specified template engine (via require())\n * and render it using the loaded module’s __express function.\n *\n * For more information, see [Using template engines with Express](http://expressjs.com/guide/using-template-engines.html).\n *\n * > NOTE: The view argument performs file system operations like reading a file from disk and evaluating Node.js modules,\n * and as so for security reasons should not contain input from the end-user.\n *\n * @param path Relative path to the view file.\n * @param options Additional options\n * @decorator\n * @operation\n * @response\n */\nexport function View(path: string, options?: Object): MethodDecorator {\n return StoreSet(\"view\", {path, options}) as any;\n}\n"]}
1
+ {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/decorators/view.ts"],"names":[],"mappings":";;;AAAA,uCAAkC;AAA1B,8FAAA,IAAI,OAAA","sourcesContent":["export {View} from \"@tsed/schema\";\n"]}
package/lib/cjs/index.js CHANGED
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
+ /**
3
+ * @file Automatically generated by barrelsby.
4
+ */
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const tslib_1 = require("tslib");
4
- (0, tslib_1.__exportStar)(require("./decorators/view"), exports);
5
- (0, tslib_1.__exportStar)(require("./domain/PlatformViewsSettings"), exports);
6
- (0, tslib_1.__exportStar)(require("./services/PlatformViews"), exports);
7
+ tslib_1.__exportStar(require("./decorators/view"), exports);
8
+ tslib_1.__exportStar(require("./domain/PlatformViewsSettings"), exports);
9
+ tslib_1.__exportStar(require("./services/PlatformViews"), exports);
7
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAAkC;AAClC,8EAA+C;AAC/C,wEAAyC","sourcesContent":["export * from \"./decorators/view\";\nexport * from \"./domain/PlatformViewsSettings\";\nexport * from \"./services/PlatformViews\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,4DAAkC;AAClC,yEAA+C;AAC/C,mEAAyC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/view\";\nexport * from \"./domain/PlatformViewsSettings\";\nexport * from \"./services/PlatformViews\";\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -1,88 +1,64 @@
1
1
  "use strict";
2
- var _PlatformViews_extensions, _PlatformViews_engines, _PlatformViews_cachePaths;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.PlatformViews = void 0;
5
4
  const tslib_1 = require("tslib");
6
5
  const core_1 = require("@tsed/core");
7
6
  const di_1 = require("@tsed/di");
8
- const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
7
+ const engines_1 = require("@tsed/engines");
8
+ const fs_1 = tslib_1.__importDefault(require("fs"));
9
9
  const path_1 = require("path");
10
10
  const PlatformViewsSettings_1 = require("../domain/PlatformViewsSettings");
11
- function patchEJS(ejs) {
11
+ async function patchEJS(ejs) {
12
+ if (!ejs) {
13
+ const mod = await Promise.resolve().then(() => tslib_1.__importStar(require("ejs")));
14
+ ejs = mod.default;
15
+ }
12
16
  return {
13
17
  compile(str, { client, ...options }) {
14
- return (ejs || require("ejs")).compile(str, options);
18
+ return ejs.compile(str, options);
15
19
  }
16
20
  };
17
21
  }
18
- async function tryImport(name) {
19
- try {
20
- return await Promise.resolve().then(() => (0, tslib_1.__importStar)(require(name)));
21
- }
22
- catch (er) {
23
- // istanbul ignore next
24
- }
25
- }
26
22
  /**
27
23
  * @platform
28
24
  */
29
25
  let PlatformViews = class PlatformViews {
30
- constructor() {
31
- _PlatformViews_extensions.set(this, void 0);
32
- _PlatformViews_engines.set(this, new Map());
33
- _PlatformViews_cachePaths.set(this, new Map());
34
- }
26
+ env;
27
+ root;
28
+ cache;
29
+ disabled;
30
+ viewEngine;
31
+ extensionsOptions;
32
+ engineOptions;
33
+ #extensions;
34
+ #engines = new Map();
35
+ #cachePaths = new Map();
35
36
  async $onInit() {
36
37
  if (!this.disabled) {
37
- (0, tslib_1.__classPrivateFieldSet)(this, _PlatformViews_extensions, new Map(Object.entries({
38
+ this.#extensions = new Map(Object.entries({
38
39
  ...PlatformViewsSettings_1.PLATFORM_VIEWS_EXTENSIONS,
39
40
  ...this.extensionsOptions
40
- })), "f");
41
- await this.loadFromConsolidate();
42
- await this.loadFromTsedEngines();
41
+ }));
42
+ await this.loadEngines();
43
43
  }
44
44
  }
45
- /**
46
- * @deprecated
47
- */
48
- async loadFromConsolidate() {
49
- const cons = await tryImport("consolidate");
50
- if (cons) {
51
- cons.requires.ejs = patchEJS(cons.requires.ejs);
52
- (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_extensions, "f").forEach((engineType) => {
53
- if (cons[engineType]) {
54
- const options = this.getEngineOptions(engineType);
55
- if (options.requires) {
56
- cons.requires[engineType] = options.requires;
57
- }
58
- this.registerEngine(engineType, {
59
- options,
60
- render: cons[engineType]
61
- });
45
+ async loadEngines() {
46
+ engines_1.requires.set("ejs", await patchEJS(engines_1.requires.get("ejs")));
47
+ this.#extensions.forEach((engineType) => {
48
+ if (engines_1.engines.has(engineType)) {
49
+ const options = this.getEngineOptions(engineType);
50
+ if (options.requires) {
51
+ engines_1.requires.set(engineType, options.requires);
62
52
  }
63
- });
64
- }
65
- }
66
- async loadFromTsedEngines() {
67
- const tsed = await tryImport("@tsed/engines");
68
- if (tsed) {
69
- tsed.requires.set("ejs", patchEJS(tsed.requires.get("ejs")));
70
- (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_extensions, "f").forEach((engineType) => {
71
- if (tsed.engines.has(engineType)) {
72
- const options = this.getEngineOptions(engineType);
73
- if (options.requires) {
74
- tsed.requires.set(engineType, options.requires);
75
- }
76
- this.registerEngine(engineType, {
77
- options,
78
- render: tsed.getEngine(engineType)
79
- });
80
- }
81
- });
82
- }
53
+ this.registerEngine(engineType, {
54
+ options,
55
+ render: (0, engines_1.getEngine)(engineType)
56
+ });
57
+ }
58
+ });
83
59
  }
84
60
  getEngines() {
85
- return [...(0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_extensions, "f").entries()].map(([extension, engineType]) => {
61
+ return [...this.#extensions.entries()].map(([extension, engineType]) => {
86
62
  const engine = this.getEngine(this.getExtension(engineType));
87
63
  return {
88
64
  extension,
@@ -91,17 +67,17 @@ let PlatformViews = class PlatformViews {
91
67
  });
92
68
  }
93
69
  registerEngine(engineType, engine) {
94
- (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_engines, "f").set(engineType, engine);
70
+ this.#engines.set(engineType, engine);
95
71
  return this;
96
72
  }
97
73
  getEngine(type) {
98
- return (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_engines, "f").get((0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_extensions, "f").get(type) || type);
74
+ return this.#engines.get(this.#extensions.get(type) || type);
99
75
  }
100
76
  getEngineOptions(engineType) {
101
77
  return (0, core_1.getValue)(this.engineOptions, engineType, {});
102
78
  }
103
79
  async render(viewPath, options = {}) {
104
- const { path, extension } = (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_cachePaths, "f").get(viewPath) || (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_cachePaths, "f").set(viewPath, this.resolve(viewPath)).get(viewPath);
80
+ const { path, extension } = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath);
105
81
  const engine = this.getEngine(extension);
106
82
  if (!engine) {
107
83
  throw new Error(`Engine not found to render the following "${viewPath}"`);
@@ -126,36 +102,35 @@ let PlatformViews = class PlatformViews {
126
102
  };
127
103
  }
128
104
  };
129
- _PlatformViews_extensions = new WeakMap(), _PlatformViews_engines = new WeakMap(), _PlatformViews_cachePaths = new WeakMap();
130
- (0, tslib_1.__decorate)([
105
+ tslib_1.__decorate([
131
106
  (0, di_1.Constant)("env"),
132
- (0, tslib_1.__metadata)("design:type", String)
107
+ tslib_1.__metadata("design:type", String)
133
108
  ], PlatformViews.prototype, "env", void 0);
134
- (0, tslib_1.__decorate)([
109
+ tslib_1.__decorate([
135
110
  (0, di_1.Constant)("views.root", `${process.cwd()}/views`),
136
- (0, tslib_1.__metadata)("design:type", String)
111
+ tslib_1.__metadata("design:type", String)
137
112
  ], PlatformViews.prototype, "root", void 0);
138
- (0, tslib_1.__decorate)([
113
+ tslib_1.__decorate([
139
114
  (0, di_1.Constant)("views.cache"),
140
- (0, tslib_1.__metadata)("design:type", Boolean)
115
+ tslib_1.__metadata("design:type", Boolean)
141
116
  ], PlatformViews.prototype, "cache", void 0);
142
- (0, tslib_1.__decorate)([
117
+ tslib_1.__decorate([
143
118
  (0, di_1.Constant)("views.disabled", false),
144
- (0, tslib_1.__metadata)("design:type", String)
119
+ tslib_1.__metadata("design:type", String)
145
120
  ], PlatformViews.prototype, "disabled", void 0);
146
- (0, tslib_1.__decorate)([
121
+ tslib_1.__decorate([
147
122
  (0, di_1.Constant)("views.viewEngine", "ejs"),
148
- (0, tslib_1.__metadata)("design:type", String)
123
+ tslib_1.__metadata("design:type", String)
149
124
  ], PlatformViews.prototype, "viewEngine", void 0);
150
- (0, tslib_1.__decorate)([
125
+ tslib_1.__decorate([
151
126
  (0, di_1.Constant)("views.extensions", {}),
152
- (0, tslib_1.__metadata)("design:type", Object)
127
+ tslib_1.__metadata("design:type", Object)
153
128
  ], PlatformViews.prototype, "extensionsOptions", void 0);
154
- (0, tslib_1.__decorate)([
129
+ tslib_1.__decorate([
155
130
  (0, di_1.Constant)("views.options", {}),
156
- (0, tslib_1.__metadata)("design:type", Object)
131
+ tslib_1.__metadata("design:type", Object)
157
132
  ], PlatformViews.prototype, "engineOptions", void 0);
158
- PlatformViews = (0, tslib_1.__decorate)([
133
+ PlatformViews = tslib_1.__decorate([
159
134
  (0, di_1.Module)({
160
135
  views: {
161
136
  exists: true
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformViews.js","sourceRoot":"","sources":["../../../src/services/PlatformViews.ts"],"names":[],"mappings":";;;;;AAAA,qCAAyC;AACzC,iCAA0C;AAC1C,yDAAoB;AACpB,+BAA4C;AAC5C,2EAKyC;AAEzC,SAAS,QAAQ,CAAC,GAAQ;IACxB,OAAO;QACL,OAAO,CAAC,GAAW,EAAE,EAAC,MAAM,EAAE,GAAG,OAAO,EAAM;YAC5C,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACvD,CAAC;KACF,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,SAAS,CAAC,IAAY;IACnC,IAAI;QACF,OAAO,qEAAa,IAAI,GAAC,CAAC;KAC3B;IAAC,OAAO,EAAE,EAAE;QACX,uBAAuB;KACxB;AACH,CAAC;AAED;;GAEG;AAMH,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAsBE,4CAAiC;QACjC,iCAAW,IAAI,GAAG,EAA8B,EAAC;QACjD,oCAAc,IAAI,GAAG,EAA6C,EAAC;IA0HrE,CAAC;IAxHC,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,oCAAA,IAAI,6BAAe,IAAI,GAAG,CACxB,MAAM,CAAC,OAAO,CAAC;gBACb,GAAG,iDAAyB;gBAC5B,GAAG,IAAI,CAAC,iBAAiB;aAC1B,CAAC,CACH,MAAA,CAAC;YAEF,MAAM,IAAI,CAAC,mB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AAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,QAAQ,CAAC;QAEpD,OAAO;YACL,IAAI;YACJ,SAAS;SACV,CAAC;IACJ,CAAC;CACF,CAAA;;AAhJC;IADC,IAAA,aAAQ,EAAC,KAAK,CAAC;;0CACP;AAGT;IADC,IAAA,aAAQ,EAAC,YAAY,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,QAAQ,CAAC;;2CAC3B;AAGtB;IADC,IAAA,aAAQ,EAAC,aAAa,CAAC;;4CACA;AAGxB;IADC,IAAA,aAAQ,EAAC,gBAAgB,EAAE,KAAK,CAAC;;+CACR;AAG1B;IADC,IAAA,aAAQ,EAAC,kBAAkB,EAAE,KAAK,CAAC;;iDACR;AAG5B;IADC,IAAA,aAAQ,EAAC,kBAAkB,EAAE,EAAE,CAAC;;wDACyB;AAG1D;IADC,IAAA,aAAQ,EAAC,eAAe,EAAE,EAAE,CAAC;;oDACsC;AApBzD,aAAa;IALzB,IAAA,WAAM,EAAC;QACN,KAAK,EAAE;YACL,MAAM,EAAE,IAAI;SACb;KACF,CAAC;GACW,aAAa,CAkJzB;AAlJY,sCAAa","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {Constant, Module} from \"@tsed/di\";\nimport Fs from \"fs\";\nimport {extname, join, resolve} from \"path\";\nimport {\n PLATFORM_VIEWS_EXTENSIONS,\n PlatformViewEngine,\n PlatformViewsEngineOptions,\n PlatformViewsExtensionsTypes\n} from \"../domain/PlatformViewsSettings\";\n\nfunction patchEJS(ejs: any) {\n return {\n compile(str: string, {client, ...options}: any) {\n return (ejs || require(\"ejs\")).compile(str, options);\n }\n };\n}\n\nasync function tryImport(name: string) {\n try {\n return await import(name);\n } catch (er) {\n // istanbul ignore next\n }\n}\n\n/**\n * @platform\n */\n@Module({\n views: {\n exists: true\n }\n})\nexport class PlatformViews {\n @Constant(\"env\")\n env: Env;\n\n @Constant(\"views.root\", `${process.cwd()}/views`)\n readonly root: string;\n\n @Constant(\"views.cache\")\n readonly cache: boolean;\n\n @Constant(\"views.disabled\", false)\n readonly disabled: string;\n\n @Constant(\"views.viewEngine\", \"ejs\")\n readonly viewEngine: string;\n\n @Constant(\"views.extensions\", {})\n protected extensionsOptions: PlatformViewsExtensionsTypes;\n\n @Constant(\"views.options\", {})\n protected engineOptions: Record<string, PlatformViewsEngineOptions>;\n\n #extensions: Map<string, string>;\n #engines = new Map<string, PlatformViewEngine>();\n #cachePaths = new Map<string, {path: string; extension: string}>();\n\n async $onInit() {\n if (!this.disabled) {\n this.#extensions = new Map(\n Object.entries({\n ...PLATFORM_VIEWS_EXTENSIONS,\n ...this.extensionsOptions\n })\n );\n\n await this.loadFromConsolidate();\n await this.loadFromTsedEngines();\n }\n }\n\n /**\n * @deprecated\n */\n async loadFromConsolidate() {\n const cons = await tryImport(\"consolidate\");\n if (cons) {\n cons.requires.ejs = patchEJS(cons.requires.ejs);\n\n this.#extensions.forEach((engineType) => {\n if ((cons as any)[engineType]) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n (cons.requires as any)[engineType] = options.requires;\n }\n\n this.registerEngine(engineType, {\n options,\n render: (cons as any)[engineType]\n });\n }\n });\n }\n }\n\n async loadFromTsedEngines() {\n const tsed = await tryImport(\"@tsed/engines\");\n\n if (tsed) {\n tsed.requires.set(\"ejs\", patchEJS(tsed.requires.get(\"ejs\")));\n\n this.#extensions.forEach((engineType) => {\n if (tsed.engines.has(engineType)) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n tsed.requires.set(engineType, options.requires);\n }\n\n this.registerEngine(engineType, {\n options,\n render: tsed.getEngine(engineType)\n });\n }\n });\n }\n }\n\n getEngines() {\n return [...this.#extensions.entries()].map(([extension, engineType]) => {\n const engine = this.getEngine(this.getExtension(engineType))!;\n\n return {\n extension,\n engine\n };\n });\n }\n\n registerEngine(engineType: string, engine: PlatformViewEngine) {\n this.#engines.set(engineType, engine);\n\n return this;\n }\n\n getEngine(type: string) {\n return this.#engines.get(this.#extensions.get(type) || type);\n }\n\n getEngineOptions(engineType: string): PlatformViewsEngineOptions {\n return getValue(this.engineOptions, engineType, {});\n }\n\n async render(viewPath: string, options: any = {}): Promise<string> {\n const {path, extension} = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath)!;\n const engine = this.getEngine(extension);\n\n if (!engine) {\n throw new Error(`Engine not found to render the following \"${viewPath}\"`);\n }\n\n return engine.render(path, Object.assign({cache: this.cache || this.env === Env.PROD}, engine.options, options));\n }\n\n protected getExtension(viewPath: string) {\n return (extname(viewPath) || this.viewEngine).replace(/\\./, \"\");\n }\n\n protected resolve(viewPath: string) {\n const extension = this.getExtension(viewPath);\n\n viewPath = viewPath.replace(extname(viewPath), \"\") + \".\" + extension;\n\n const path =\n [\n viewPath,\n resolve(join(this.root, viewPath)),\n resolve(join(process.cwd(), \"views\", viewPath)),\n resolve(join(process.cwd(), \"public\", viewPath))\n ].find((file) => Fs.existsSync(file)) || viewPath;\n\n return {\n path,\n extension\n };\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformViews.js","sourceRoot":"","sources":["../../../src/services/PlatformViews.ts"],"names":[],"mappings":";;;;AAAA,qCAAyC;AACzC,iCAA0C;AAC1C,2CAA2D;AAC3D,oDAAoB;AACpB,+BAA4C;AAC5C,2EAKyC;AAEzC,KAAK,UAAU,QAAQ,CAAC,GAAQ;IAC9B,IAAI,CAAC,GAAG,EAAE;QACR,MAAM,GAAG,GAAG,gEAAa,KAAK,GAAC,CAAC;QAChC,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC;KACnB;IAED,OAAO;QACL,OAAO,CAAC,GAAW,EAAE,EAAC,MAAM,EAAE,GAAG,OAAO,EAAM;YAC5C,OAAO,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACnC,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;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,CAAC;;4CACA;AAGxB;IADC,IAAA,aAAQ,EAAC,gBAAgB,EAAE,KAAK,CAAC;;+CACR;AAG1B;IADC,IAAA,aAAQ,EAAC,kBAAkB,EAAE,KAAK,CAAC;;iDACR;AAG5B;IADC,IAAA,aAAQ,EAAC,kBAAkB,EAAE,EAAE,CAAC;;wDACyB;AAG1D;IADC,IAAA,aAAQ,EAAC,eAAe,EAAE,EAAE,CAAC;;oDACsC;AApBzD,aAAa;IALzB,IAAA,WAAM,EAAC;QACN,KAAK,EAAE;YACL,MAAM,EAAE,IAAI;SACb;KACF,CAAC;GACW,aAAa,CAoHzB;AApHY,sCAAa","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {Constant, Module} from \"@tsed/di\";\nimport {engines, getEngine, requires} from \"@tsed/engines\";\nimport Fs from \"fs\";\nimport {extname, join, resolve} from \"path\";\nimport {\n PLATFORM_VIEWS_EXTENSIONS,\n PlatformViewEngine,\n PlatformViewsEngineOptions,\n PlatformViewsExtensionsTypes\n} from \"../domain/PlatformViewsSettings\";\n\nasync function patchEJS(ejs: any) {\n if (!ejs) {\n const mod = await import(\"ejs\");\n ejs = mod.default;\n }\n\n return {\n compile(str: string, {client, ...options}: any) {\n return ejs.compile(str, options);\n }\n };\n}\n\n/**\n * @platform\n */\n@Module({\n views: {\n exists: true\n }\n})\nexport class PlatformViews {\n @Constant(\"env\")\n env: Env;\n\n @Constant(\"views.root\", `${process.cwd()}/views`)\n readonly root: string;\n\n @Constant(\"views.cache\")\n readonly cache: boolean;\n\n @Constant(\"views.disabled\", false)\n readonly disabled: string;\n\n @Constant(\"views.viewEngine\", \"ejs\")\n readonly viewEngine: string;\n\n @Constant(\"views.extensions\", {})\n protected extensionsOptions: PlatformViewsExtensionsTypes;\n\n @Constant(\"views.options\", {})\n protected engineOptions: Record<string, PlatformViewsEngineOptions>;\n\n #extensions: Map<string, string>;\n #engines = new Map<string, PlatformViewEngine>();\n #cachePaths = new Map<string, {path: string; extension: string}>();\n\n async $onInit() {\n if (!this.disabled) {\n this.#extensions = new Map(\n Object.entries({\n ...PLATFORM_VIEWS_EXTENSIONS,\n ...this.extensionsOptions\n })\n );\n\n await this.loadEngines();\n }\n }\n\n async loadEngines() {\n requires.set(\"ejs\", await patchEJS(requires.get(\"ejs\")));\n\n this.#extensions.forEach((engineType) => {\n if (engines.has(engineType)) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n requires.set(engineType, options.requires);\n }\n\n this.registerEngine(engineType, {\n options,\n render: getEngine(engineType)\n });\n }\n });\n }\n\n getEngines() {\n return [...this.#extensions.entries()].map(([extension, engineType]) => {\n const engine = this.getEngine(this.getExtension(engineType))!;\n\n return {\n extension,\n engine\n };\n });\n }\n\n registerEngine(engineType: string, engine: PlatformViewEngine) {\n this.#engines.set(engineType, engine);\n\n return this;\n }\n\n getEngine(type: string) {\n return this.#engines.get(this.#extensions.get(type) || type);\n }\n\n getEngineOptions(engineType: string): PlatformViewsEngineOptions {\n return getValue(this.engineOptions, engineType, {});\n }\n\n async render(viewPath: string, options: any = {}): Promise<string> {\n const {path, extension} = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath)!;\n const engine = this.getEngine(extension);\n\n if (!engine) {\n throw new Error(`Engine not found to render the following \"${viewPath}\"`);\n }\n\n return engine.render(path, Object.assign({cache: this.cache || this.env === Env.PROD}, engine.options, options));\n }\n\n protected getExtension(viewPath: string) {\n return (extname(viewPath) || this.viewEngine).replace(/\\./, \"\");\n }\n\n protected resolve(viewPath: string) {\n const extension = this.getExtension(viewPath);\n\n viewPath = viewPath.replace(extname(viewPath), \"\") + \".\" + extension;\n\n const path =\n [\n viewPath,\n resolve(join(this.root, viewPath)),\n resolve(join(process.cwd(), \"views\", viewPath)),\n resolve(join(process.cwd(), \"public\", viewPath))\n ].find((file) => Fs.existsSync(file)) || viewPath;\n\n return {\n path,\n extension\n };\n }\n}\n"]}
@@ -1,27 +1,2 @@
1
- import { StoreSet } from "@tsed/core";
2
- /**
3
- * Use a view and sends the rendered HTML string to the client. Optional parameter:
4
- *
5
- * * viewOptions, an object whose properties define local variables for the view.
6
- *
7
- * The view argument is a string that is the file path of the view file to render.
8
- * This can be an absolute path, or a path relative to the views setting.
9
- * If the path does not contain a file extension, then the view engine setting determines the file extension.
10
- * If the path does contain a file extension, then Express will load the module for the specified template engine (via require())
11
- * and render it using the loaded module’s __express function.
12
- *
13
- * For more information, see [Using template engines with Express](http://expressjs.com/guide/using-template-engines.html).
14
- *
15
- * > NOTE: The view argument performs file system operations like reading a file from disk and evaluating Node.js modules,
16
- * and as so for security reasons should not contain input from the end-user.
17
- *
18
- * @param path Relative path to the view file.
19
- * @param options Additional options
20
- * @decorator
21
- * @operation
22
- * @response
23
- */
24
- export function View(path, options) {
25
- return StoreSet("view", { path, options });
26
- }
1
+ export { View } from "@tsed/schema";
27
2
  //# sourceMappingURL=view.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/decorators/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY,EAAE,OAAgB;IACjD,OAAO,QAAQ,CAAC,MAAM,EAAE,EAAC,IAAI,EAAE,OAAO,EAAC,CAAQ,CAAC;AAClD,CAAC","sourcesContent":["import {StoreSet} from \"@tsed/core\";\n\n/**\n * Use a view and sends the rendered HTML string to the client. Optional parameter:\n *\n * * viewOptions, an object whose properties define local variables for the view.\n *\n * The view argument is a string that is the file path of the view file to render.\n * This can be an absolute path, or a path relative to the views setting.\n * If the path does not contain a file extension, then the view engine setting determines the file extension.\n * If the path does contain a file extension, then Express will load the module for the specified template engine (via require())\n * and render it using the loaded module’s __express function.\n *\n * For more information, see [Using template engines with Express](http://expressjs.com/guide/using-template-engines.html).\n *\n * > NOTE: The view argument performs file system operations like reading a file from disk and evaluating Node.js modules,\n * and as so for security reasons should not contain input from the end-user.\n *\n * @param path Relative path to the view file.\n * @param options Additional options\n * @decorator\n * @operation\n * @response\n */\nexport function View(path: string, options?: Object): MethodDecorator {\n return StoreSet(\"view\", {path, options}) as any;\n}\n"]}
1
+ {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/decorators/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC","sourcesContent":["export {View} from \"@tsed/schema\";\n"]}
package/lib/esm/index.js CHANGED
@@ -1,4 +1,7 @@
1
- export * from "./decorators/view";
2
- export * from "./domain/PlatformViewsSettings";
3
- export * from "./services/PlatformViews";
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./decorators/view.js";
5
+ export * from "./domain/PlatformViewsSettings.js";
6
+ export * from "./services/PlatformViews.js";
4
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,0BAA0B,CAAC","sourcesContent":["export * from \"./decorators/view\";\nexport * from \"./domain/PlatformViewsSettings\";\nexport * from \"./services/PlatformViews\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,mBAAmB,CAAC;AAClC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,0BAA0B,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/view\";\nexport * from \"./domain/PlatformViewsSettings\";\nexport * from \"./services/PlatformViews\";\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -1,85 +1,61 @@
1
- var _PlatformViews_extensions, _PlatformViews_engines, _PlatformViews_cachePaths;
2
- import { __classPrivateFieldGet, __classPrivateFieldSet, __decorate, __metadata } from "tslib";
1
+ import { __decorate, __metadata } from "tslib";
3
2
  import { Env, getValue } from "@tsed/core";
4
3
  import { Constant, Module } from "@tsed/di";
4
+ import { engines, getEngine, requires } from "@tsed/engines";
5
5
  import Fs from "fs";
6
6
  import { extname, join, resolve } from "path";
7
- import { PLATFORM_VIEWS_EXTENSIONS } from "../domain/PlatformViewsSettings";
8
- function patchEJS(ejs) {
7
+ import { PLATFORM_VIEWS_EXTENSIONS } from "../domain/PlatformViewsSettings.js";
8
+ async function patchEJS(ejs) {
9
+ if (!ejs) {
10
+ const mod = await import("ejs");
11
+ ejs = mod.default;
12
+ }
9
13
  return {
10
14
  compile(str, { client, ...options }) {
11
- return (ejs || require("ejs")).compile(str, options);
15
+ return ejs.compile(str, options);
12
16
  }
13
17
  };
14
18
  }
15
- async function tryImport(name) {
16
- try {
17
- return await import(name);
18
- }
19
- catch (er) {
20
- // istanbul ignore next
21
- }
22
- }
23
19
  /**
24
20
  * @platform
25
21
  */
26
22
  let PlatformViews = class PlatformViews {
27
- constructor() {
28
- _PlatformViews_extensions.set(this, void 0);
29
- _PlatformViews_engines.set(this, new Map());
30
- _PlatformViews_cachePaths.set(this, new Map());
31
- }
23
+ env;
24
+ root;
25
+ cache;
26
+ disabled;
27
+ viewEngine;
28
+ extensionsOptions;
29
+ engineOptions;
30
+ #extensions;
31
+ #engines = new Map();
32
+ #cachePaths = new Map();
32
33
  async $onInit() {
33
34
  if (!this.disabled) {
34
- __classPrivateFieldSet(this, _PlatformViews_extensions, new Map(Object.entries({
35
+ this.#extensions = new Map(Object.entries({
35
36
  ...PLATFORM_VIEWS_EXTENSIONS,
36
37
  ...this.extensionsOptions
37
- })), "f");
38
- await this.loadFromConsolidate();
39
- await this.loadFromTsedEngines();
38
+ }));
39
+ await this.loadEngines();
40
40
  }
41
41
  }
42
- /**
43
- * @deprecated
44
- */
45
- async loadFromConsolidate() {
46
- const cons = await tryImport("consolidate");
47
- if (cons) {
48
- cons.requires.ejs = patchEJS(cons.requires.ejs);
49
- __classPrivateFieldGet(this, _PlatformViews_extensions, "f").forEach((engineType) => {
50
- if (cons[engineType]) {
51
- const options = this.getEngineOptions(engineType);
52
- if (options.requires) {
53
- cons.requires[engineType] = options.requires;
54
- }
55
- this.registerEngine(engineType, {
56
- options,
57
- render: cons[engineType]
58
- });
42
+ async loadEngines() {
43
+ requires.set("ejs", await patchEJS(requires.get("ejs")));
44
+ this.#extensions.forEach((engineType) => {
45
+ if (engines.has(engineType)) {
46
+ const options = this.getEngineOptions(engineType);
47
+ if (options.requires) {
48
+ requires.set(engineType, options.requires);
59
49
  }
60
- });
61
- }
62
- }
63
- async loadFromTsedEngines() {
64
- const tsed = await tryImport("@tsed/engines");
65
- if (tsed) {
66
- tsed.requires.set("ejs", patchEJS(tsed.requires.get("ejs")));
67
- __classPrivateFieldGet(this, _PlatformViews_extensions, "f").forEach((engineType) => {
68
- if (tsed.engines.has(engineType)) {
69
- const options = this.getEngineOptions(engineType);
70
- if (options.requires) {
71
- tsed.requires.set(engineType, options.requires);
72
- }
73
- this.registerEngine(engineType, {
74
- options,
75
- render: tsed.getEngine(engineType)
76
- });
77
- }
78
- });
79
- }
50
+ this.registerEngine(engineType, {
51
+ options,
52
+ render: getEngine(engineType)
53
+ });
54
+ }
55
+ });
80
56
  }
81
57
  getEngines() {
82
- return [...__classPrivateFieldGet(this, _PlatformViews_extensions, "f").entries()].map(([extension, engineType]) => {
58
+ return [...this.#extensions.entries()].map(([extension, engineType]) => {
83
59
  const engine = this.getEngine(this.getExtension(engineType));
84
60
  return {
85
61
  extension,
@@ -88,17 +64,17 @@ let PlatformViews = class PlatformViews {
88
64
  });
89
65
  }
90
66
  registerEngine(engineType, engine) {
91
- __classPrivateFieldGet(this, _PlatformViews_engines, "f").set(engineType, engine);
67
+ this.#engines.set(engineType, engine);
92
68
  return this;
93
69
  }
94
70
  getEngine(type) {
95
- return __classPrivateFieldGet(this, _PlatformViews_engines, "f").get(__classPrivateFieldGet(this, _PlatformViews_extensions, "f").get(type) || type);
71
+ return this.#engines.get(this.#extensions.get(type) || type);
96
72
  }
97
73
  getEngineOptions(engineType) {
98
74
  return getValue(this.engineOptions, engineType, {});
99
75
  }
100
76
  async render(viewPath, options = {}) {
101
- const { path, extension } = __classPrivateFieldGet(this, _PlatformViews_cachePaths, "f").get(viewPath) || __classPrivateFieldGet(this, _PlatformViews_cachePaths, "f").set(viewPath, this.resolve(viewPath)).get(viewPath);
77
+ const { path, extension } = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath);
102
78
  const engine = this.getEngine(extension);
103
79
  if (!engine) {
104
80
  throw new Error(`Engine not found to render the following "${viewPath}"`);
@@ -123,7 +99,6 @@ let PlatformViews = class PlatformViews {
123
99
  };
124
100
  }
125
101
  };
126
- _PlatformViews_extensions = new WeakMap(), _PlatformViews_engines = new WeakMap(), _PlatformViews_cachePaths = new WeakMap();
127
102
  __decorate([
128
103
  Constant("env"),
129
104
  __metadata("design:type", String)
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformViews.js","sourceRoot":"","sources":["../../../src/services/PlatformViews.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAC,MAAM,MAAM,CAAC;AAC5C,OAAO,EACL,yBAAyB,EAI1B,MAAM,iCAAiC,CAAC;AAEzC,SAAS,QAAQ,CAAC,GAAQ;IACxB,OAAO;QACL,OAAO,CAAC,GAAW,EAAE,EAAC,MAAM,EAAE,GAAG,OAAO,EAAM;YAC5C,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACv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a,CAkJzB;SAlJY,aAAa","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {Constant, Module} from \"@tsed/di\";\nimport Fs from \"fs\";\nimport {extname, join, resolve} from \"path\";\nimport {\n PLATFORM_VIEWS_EXTENSIONS,\n PlatformViewEngine,\n PlatformViewsEngineOptions,\n PlatformViewsExtensionsTypes\n} from \"../domain/PlatformViewsSettings\";\n\nfunction patchEJS(ejs: any) {\n return {\n compile(str: string, {client, ...options}: any) {\n return (ejs || require(\"ejs\")).compile(str, options);\n }\n };\n}\n\nasync function tryImport(name: string) {\n try {\n return await import(name);\n } catch (er) {\n // istanbul ignore next\n }\n}\n\n/**\n * @platform\n */\n@Module({\n views: {\n exists: true\n }\n})\nexport class PlatformViews {\n @Constant(\"env\")\n env: Env;\n\n @Constant(\"views.root\", `${process.cwd()}/views`)\n readonly root: string;\n\n @Constant(\"views.cache\")\n readonly cache: boolean;\n\n @Constant(\"views.disabled\", false)\n readonly disabled: string;\n\n @Constant(\"views.viewEngine\", \"ejs\")\n readonly viewEngine: string;\n\n @Constant(\"views.extensions\", {})\n protected extensionsOptions: PlatformViewsExtensionsTypes;\n\n @Constant(\"views.options\", {})\n protected engineOptions: Record<string, PlatformViewsEngineOptions>;\n\n #extensions: Map<string, string>;\n #engines = new Map<string, PlatformViewEngine>();\n #cachePaths = new Map<string, {path: string; extension: string}>();\n\n async $onInit() {\n if (!this.disabled) {\n this.#extensions = new Map(\n Object.entries({\n ...PLATFORM_VIEWS_EXTENSIONS,\n ...this.extensionsOptions\n })\n );\n\n await this.loadFromConsolidate();\n await this.loadFromTsedEngines();\n }\n }\n\n /**\n * @deprecated\n */\n async loadFromConsolidate() {\n const cons = await tryImport(\"consolidate\");\n if (cons) {\n cons.requires.ejs = patchEJS(cons.requires.ejs);\n\n this.#extensions.forEach((engineType) => {\n if ((cons as any)[engineType]) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n (cons.requires as any)[engineType] = options.requires;\n }\n\n this.registerEngine(engineType, {\n options,\n render: (cons as any)[engineType]\n });\n }\n });\n }\n }\n\n async loadFromTsedEngines() {\n const tsed = await tryImport(\"@tsed/engines\");\n\n if (tsed) {\n tsed.requires.set(\"ejs\", patchEJS(tsed.requires.get(\"ejs\")));\n\n this.#extensions.forEach((engineType) => {\n if (tsed.engines.has(engineType)) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n tsed.requires.set(engineType, options.requires);\n }\n\n this.registerEngine(engineType, {\n options,\n render: tsed.getEngine(engineType)\n });\n }\n });\n }\n }\n\n getEngines() {\n return [...this.#extensions.entries()].map(([extension, engineType]) => {\n const engine = this.getEngine(this.getExtension(engineType))!;\n\n return {\n extension,\n engine\n };\n });\n }\n\n registerEngine(engineType: string, engine: PlatformViewEngine) {\n this.#engines.set(engineType, engine);\n\n return this;\n }\n\n getEngine(type: string) {\n return this.#engines.get(this.#extensions.get(type) || type);\n }\n\n getEngineOptions(engineType: string): PlatformViewsEngineOptions {\n return getValue(this.engineOptions, engineType, {});\n }\n\n async render(viewPath: string, options: any = {}): Promise<string> {\n const {path, extension} = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath)!;\n const engine = this.getEngine(extension);\n\n if (!engine) {\n throw new Error(`Engine not found to render the following \"${viewPath}\"`);\n }\n\n return engine.render(path, Object.assign({cache: this.cache || this.env === Env.PROD}, engine.options, options));\n }\n\n protected getExtension(viewPath: string) {\n return (extname(viewPath) || this.viewEngine).replace(/\\./, \"\");\n }\n\n protected resolve(viewPath: string) {\n const extension = this.getExtension(viewPath);\n\n viewPath = viewPath.replace(extname(viewPath), \"\") + \".\" + extension;\n\n const path =\n [\n viewPath,\n resolve(join(this.root, viewPath)),\n resolve(join(process.cwd(), \"views\", viewPath)),\n resolve(join(process.cwd(), \"public\", viewPath))\n ].find((file) => Fs.existsSync(file)) || viewPath;\n\n return {\n path,\n extension\n };\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformViews.js","sourceRoot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AAE,EAAE,CAAC,GAAG,GAAG,GAAG,SAAS,CAAC;QAErE,MAAM,IAAI,GACR;YACE,QAAQ;YACR,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAClC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;YAC/C,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;SACjD,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,QAAQ,CAAC;QAEpD,OAAO;YACL,IAAI;YACJ,SAAS;SACV,CAAC;IACJ,CAAC;CACF,CAAA;AAlHC;IADC,QAAQ,CAAC,KAAK,CAAC;;0CACP;AAGT;IADC,QAAQ,CAAC,YAAY,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,QAAQ,CAAC;;2CAC3B;AAGtB;IADC,QAAQ,CAAC,aAAa,CAAC;;4CACA;AAGxB;IADC,QAAQ,CAAC,gBAAgB,EAAE,KAAK,CAAC;;+CACR;AAG1B;IADC,QAAQ,CAAC,kBAAkB,EAAE,KAAK,CAAC;;iDACR;AAG5B;IADC,QAAQ,CAAC,kBAAkB,EAAE,EAAE,CAAC;;wDACyB;AAG1D;IADC,QAAQ,CAAC,eAAe,EAAE,EAAE,CAAC;;oDACsC;AApBzD,aAAa;IALzB,MAAM,CAAC;QACN,KAAK,EAAE;YACL,MAAM,EAAE,IAAI;SACb;KACF,CAAC;GACW,aAAa,CAoHzB;SApHY,aAAa","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {Constant, Module} from \"@tsed/di\";\nimport {engines, getEngine, requires} from \"@tsed/engines\";\nimport Fs from \"fs\";\nimport {extname, join, resolve} from \"path\";\nimport {\n PLATFORM_VIEWS_EXTENSIONS,\n PlatformViewEngine,\n PlatformViewsEngineOptions,\n PlatformViewsExtensionsTypes\n} from \"../domain/PlatformViewsSettings\";\n\nasync function patchEJS(ejs: any) {\n if (!ejs) {\n const mod = await import(\"ejs\");\n ejs = mod.default;\n }\n\n return {\n compile(str: string, {client, ...options}: any) {\n return ejs.compile(str, options);\n }\n };\n}\n\n/**\n * @platform\n */\n@Module({\n views: {\n exists: true\n }\n})\nexport class PlatformViews {\n @Constant(\"env\")\n env: Env;\n\n @Constant(\"views.root\", `${process.cwd()}/views`)\n readonly root: string;\n\n @Constant(\"views.cache\")\n readonly cache: boolean;\n\n @Constant(\"views.disabled\", false)\n readonly disabled: string;\n\n @Constant(\"views.viewEngine\", \"ejs\")\n readonly viewEngine: string;\n\n @Constant(\"views.extensions\", {})\n protected extensionsOptions: PlatformViewsExtensionsTypes;\n\n @Constant(\"views.options\", {})\n protected engineOptions: Record<string, PlatformViewsEngineOptions>;\n\n #extensions: Map<string, string>;\n #engines = new Map<string, PlatformViewEngine>();\n #cachePaths = new Map<string, {path: string; extension: string}>();\n\n async $onInit() {\n if (!this.disabled) {\n this.#extensions = new Map(\n Object.entries({\n ...PLATFORM_VIEWS_EXTENSIONS,\n ...this.extensionsOptions\n })\n );\n\n await this.loadEngines();\n }\n }\n\n async loadEngines() {\n requires.set(\"ejs\", await patchEJS(requires.get(\"ejs\")));\n\n this.#extensions.forEach((engineType) => {\n if (engines.has(engineType)) {\n const options = this.getEngineOptions(engineType);\n\n if (options.requires) {\n requires.set(engineType, options.requires);\n }\n\n this.registerEngine(engineType, {\n options,\n render: getEngine(engineType)\n });\n }\n });\n }\n\n getEngines() {\n return [...this.#extensions.entries()].map(([extension, engineType]) => {\n const engine = this.getEngine(this.getExtension(engineType))!;\n\n return {\n extension,\n engine\n };\n });\n }\n\n registerEngine(engineType: string, engine: PlatformViewEngine) {\n this.#engines.set(engineType, engine);\n\n return this;\n }\n\n getEngine(type: string) {\n return this.#engines.get(this.#extensions.get(type) || type);\n }\n\n getEngineOptions(engineType: string): PlatformViewsEngineOptions {\n return getValue(this.engineOptions, engineType, {});\n }\n\n async render(viewPath: string, options: any = {}): Promise<string> {\n const {path, extension} = this.#cachePaths.get(viewPath) || this.#cachePaths.set(viewPath, this.resolve(viewPath)).get(viewPath)!;\n const engine = this.getEngine(extension);\n\n if (!engine) {\n throw new Error(`Engine not found to render the following \"${viewPath}\"`);\n }\n\n return engine.render(path, Object.assign({cache: this.cache || this.env === Env.PROD}, engine.options, options));\n }\n\n protected getExtension(viewPath: string) {\n return (extname(viewPath) || this.viewEngine).replace(/\\./, \"\");\n }\n\n protected resolve(viewPath: string) {\n const extension = this.getExtension(viewPath);\n\n viewPath = viewPath.replace(extname(viewPath), \"\") + \".\" + extension;\n\n const path =\n [\n viewPath,\n resolve(join(this.root, viewPath)),\n resolve(join(process.cwd(), \"views\", viewPath)),\n resolve(join(process.cwd(), \"public\", viewPath))\n ].find((file) => Fs.existsSync(file)) || viewPath;\n\n return {\n path,\n extension\n };\n }\n}\n"]}
@@ -1,23 +1 @@
1
- /**
2
- * Use a view and sends the rendered HTML string to the client. Optional parameter:
3
- *
4
- * * viewOptions, an object whose properties define local variables for the view.
5
- *
6
- * The view argument is a string that is the file path of the view file to render.
7
- * This can be an absolute path, or a path relative to the views setting.
8
- * If the path does not contain a file extension, then the view engine setting determines the file extension.
9
- * If the path does contain a file extension, then Express will load the module for the specified template engine (via require())
10
- * and render it using the loaded module’s __express function.
11
- *
12
- * For more information, see [Using template engines with Express](http://expressjs.com/guide/using-template-engines.html).
13
- *
14
- * > NOTE: The view argument performs file system operations like reading a file from disk and evaluating Node.js modules,
15
- * and as so for security reasons should not contain input from the end-user.
16
- *
17
- * @param path Relative path to the view file.
18
- * @param options Additional options
19
- * @decorator
20
- * @operation
21
- * @response
22
- */
23
- export declare function View(path: string, options?: Object): MethodDecorator;
1
+ export { View } from "@tsed/schema";
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
1
4
  export * from "./decorators/view";
2
5
  export * from "./domain/PlatformViewsSettings";
3
6
  export * from "./services/PlatformViews";
@@ -13,11 +13,7 @@ export declare class PlatformViews {
13
13
  protected extensionsOptions: PlatformViewsExtensionsTypes;
14
14
  protected engineOptions: Record<string, PlatformViewsEngineOptions>;
15
15
  $onInit(): Promise<void>;
16
- /**
17
- * @deprecated
18
- */
19
- loadFromConsolidate(): Promise<void>;
20
- loadFromTsedEngines(): Promise<void>;
16
+ loadEngines(): Promise<void>;
21
17
  getEngines(): {
22
18
  extension: string;
23
19
  engine: PlatformViewEngine;
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@tsed/platform-views",
3
- "version": "7.0.0-beta.2",
3
+ "version": "7.0.0-beta.20",
4
4
  "description": "Views module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/cjs/index.js",
8
+ "module": "./lib/esm/index.js",
8
9
  "typings": "./lib/types/index.d.ts",
9
10
  "exports": {
10
11
  "types": "./lib/types/index.d.ts",
@@ -13,35 +14,54 @@
13
14
  "default": "./lib/esm/index.js"
14
15
  },
15
16
  "scripts": {
16
- "build": "yarn run build:esm && yarn run build:cjs",
17
+ "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
17
18
  "build:cjs": "tsc --build tsconfig.compile.json",
18
- "build:esm": "tsc --build tsconfig.compile.esm.json"
19
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
20
+ "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
21
+ "test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
19
22
  },
20
23
  "dependencies": {
21
- "consolidate": "^0.16.0",
24
+ "@tsed/engines": "7.0.0-beta.20",
22
25
  "ejs": "^3.1.5",
23
- "tslib": "2.2.0"
26
+ "tslib": "2.4.0"
24
27
  },
25
28
  "devDependencies": {
26
- "@tsed/core": "7.0.0-beta.2",
27
- "@tsed/di": "7.0.0-beta.2",
28
- "@tsed/engines": "^1.1.5",
29
- "@tsed/exceptions": "7.0.0-beta.2",
30
- "@tsed/schema": "7.0.0-beta.2",
31
- "@types/consolidate": "0.14.0"
29
+ "@tsed/core": "7.0.0-beta.20",
30
+ "@tsed/di": "7.0.0-beta.20",
31
+ "@tsed/engines": "7.0.0-beta.20",
32
+ "@tsed/exceptions": "7.0.0-beta.20",
33
+ "@tsed/schema": "7.0.0-beta.20",
34
+ "@types/ejs": "3.1.1"
32
35
  },
33
36
  "peerDependencies": {
34
- "@tsed/core": "^7.0.0-beta.2",
35
- "@tsed/di": "^7.0.0-beta.2",
36
- "@tsed/engines": "^1.1.5",
37
- "@tsed/exceptions": "^7.0.0-beta.2",
38
- "@tsed/schema": "^7.0.0-beta.2"
37
+ "@tsed/core": "^7.0.0-beta.20",
38
+ "@tsed/di": "^7.0.0-beta.20",
39
+ "@tsed/engines": "^7.0.0-beta.20",
40
+ "@tsed/exceptions": "^7.0.0-beta.20",
41
+ "@tsed/schema": "^7.0.0-beta.20"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "@tsed/core": {
45
+ "optional": false
46
+ },
47
+ "@tsed/di": {
48
+ "optional": false
49
+ },
50
+ "@tsed/exceptions": {
51
+ "optional": false
52
+ },
53
+ "@tsed/schema": {
54
+ "optional": false
55
+ }
39
56
  },
40
57
  "repository": "https://github.com/tsedio/tsed",
41
58
  "bugs": {
42
59
  "url": "https://github.com/tsedio/tsed/issues"
43
60
  },
44
- "homepage": "https://github.com/tsedio/tsed/tree/alpha/packages/platform/platform-views",
61
+ "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-views",
45
62
  "author": "Romain Lenzotti",
46
- "license": "MIT"
63
+ "license": "MIT",
64
+ "publishConfig": {
65
+ "tag": "beta"
66
+ }
47
67
  }
package/readme.md CHANGED
@@ -11,7 +11,8 @@
11
11
  [![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon)
12
12
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
13
13
  [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
14
- [![backers](https://opencollective.com/tsed/tiers/badge.svg)](https://opencollective.com/tsed)
14
+ [![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
15
+ [![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed)
15
16
 
16
17
  </div>
17
18
 
@@ -36,18 +37,17 @@ npm install --save @tsed/core
36
37
  ```
37
38
 
38
39
  ## Contributors
40
+
39
41
  Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html).
40
42
 
41
43
  <a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
42
44
 
43
-
44
45
  ## Backers
45
46
 
46
47
  Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
47
48
 
48
49
  <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
49
50
 
50
-
51
51
  ## Sponsors
52
52
 
53
53
  Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
@@ -56,7 +56,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
56
56
 
57
57
  The MIT License (MIT)
58
58
 
59
- Copyright (c) 2016 - 2018 Romain Lenzotti
59
+ Copyright (c) 2016 - 2022 Romain Lenzotti
60
60
 
61
61
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
62
62
 
@@ -8,16 +8,5 @@
8
8
  "declaration": true,
9
9
  "declarationDir": "./lib/types"
10
10
  },
11
- "exclude": [
12
- "node_modules",
13
- "test",
14
- "lib",
15
- "benchmark",
16
- "coverage",
17
- "spec",
18
- "**/*.benchmark.ts",
19
- "**/*.spec.ts",
20
- "keys",
21
- "jest.config.js"
22
- ]
11
+ "exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
23
12
  }