@tsed/platform-views 6.87.1 → 6.89.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/lib/{decorators → cjs/decorators}/view.d.ts +0 -0
- package/lib/cjs/decorators/view.js +31 -0
- package/lib/cjs/decorators/view.js.map +1 -0
- package/lib/{domain → cjs/domain}/PlatformViewsSettings.d.ts +1 -0
- package/lib/cjs/domain/PlatformViewsSettings.js +47 -0
- package/lib/cjs/domain/PlatformViewsSettings.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -2
- package/lib/cjs/index.js +7 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformViews.d.ts +1 -0
- package/lib/cjs/services/PlatformViews.js +166 -0
- package/lib/cjs/services/PlatformViews.js.map +1 -0
- package/lib/esm/decorators/view.js +27 -0
- package/lib/esm/decorators/view.js.map +1 -0
- package/lib/esm/domain/PlatformViewsSettings.js +44 -0
- package/lib/esm/domain/PlatformViewsSettings.js.map +1 -0
- package/lib/esm/index.js +4 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/services/PlatformViews.js +163 -0
- package/lib/esm/services/PlatformViews.js.map +1 -0
- package/lib/types/decorators/view.d.ts +23 -0
- package/lib/types/domain/PlatformViewsSettings.d.ts +89 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/services/PlatformViews.d.ts +34 -0
- package/package.json +18 -14
- package/tsconfig.compile.esm.json +23 -0
- package/lib/errors/TemplateRenderError.d.ts +0 -14
- package/lib/index.js +0 -375
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -348
- package/lib/index.modern.js.map +0 -1
- package/lib/utils/renderView.d.ts +0 -5
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
31
|
+
//# sourceMappingURL=view.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PLATFORM_VIEWS_EXTENSIONS = void 0;
|
|
4
|
+
exports.PLATFORM_VIEWS_EXTENSIONS = {
|
|
5
|
+
atpl: "atpl",
|
|
6
|
+
bracket: "bracket",
|
|
7
|
+
dot: "dot",
|
|
8
|
+
dust: "dust",
|
|
9
|
+
ect: "ect",
|
|
10
|
+
ejs: "ejs",
|
|
11
|
+
haml: "haml",
|
|
12
|
+
"haml-coffee": "haml-coffee",
|
|
13
|
+
hamlet: "hamlet",
|
|
14
|
+
hbs: "handlebars",
|
|
15
|
+
handlebars: "handlebars",
|
|
16
|
+
hogan: "hogan",
|
|
17
|
+
htmling: "htmling",
|
|
18
|
+
jazz: "jazz",
|
|
19
|
+
jqtpl: "jqtpl",
|
|
20
|
+
just: "just",
|
|
21
|
+
kernel: "kernel",
|
|
22
|
+
liquid: "liquid",
|
|
23
|
+
liquor: "liquor",
|
|
24
|
+
lodash: "lodash",
|
|
25
|
+
mote: "mote",
|
|
26
|
+
mustache: "mustache",
|
|
27
|
+
nunjucks: "nunjucks",
|
|
28
|
+
plates: "plates",
|
|
29
|
+
pug: "pug",
|
|
30
|
+
qejs: "qejs",
|
|
31
|
+
ractive: "ractive",
|
|
32
|
+
razor: "razor",
|
|
33
|
+
jsx: "react",
|
|
34
|
+
slm: "slm",
|
|
35
|
+
squirelly: "squirelly",
|
|
36
|
+
swig: "swig",
|
|
37
|
+
teacup: "teacup",
|
|
38
|
+
templayed: "templayed",
|
|
39
|
+
toffee: "toffee",
|
|
40
|
+
twig: "twig",
|
|
41
|
+
underscore: "underscore",
|
|
42
|
+
vash: "vash",
|
|
43
|
+
velocityjs: "velocityjs",
|
|
44
|
+
walrus: "walrus",
|
|
45
|
+
whiskers: "whiskers"
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=PlatformViewsSettings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformViewsSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformViewsSettings.ts"],"names":[],"mappings":";;;AAEa,QAAA,yBAAyB,GAAG;IACvC,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,aAAa,EAAE,aAAa;IAC5B,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,YAAY;IACjB,UAAU,EAAE,YAAY;IACxB,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,KAAK;IACV,SAAS,EAAE,WAAW;IACtB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,SAAS,EAAE,WAAW;IACtB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;CACrB,CAAC","sourcesContent":["import {BaseContext} from \"@tsed/di\";\n\nexport const PLATFORM_VIEWS_EXTENSIONS = {\n atpl: \"atpl\",\n bracket: \"bracket\",\n dot: \"dot\",\n dust: \"dust\",\n ect: \"ect\",\n ejs: \"ejs\",\n haml: \"haml\",\n \"haml-coffee\": \"haml-coffee\",\n hamlet: \"hamlet\",\n hbs: \"handlebars\",\n handlebars: \"handlebars\",\n hogan: \"hogan\",\n htmling: \"htmling\",\n jazz: \"jazz\",\n jqtpl: \"jqtpl\",\n just: \"just\",\n kernel: \"kernel\",\n liquid: \"liquid\",\n liquor: \"liquor\",\n lodash: \"lodash\",\n mote: \"mote\",\n mustache: \"mustache\",\n nunjucks: \"nunjucks\",\n plates: \"plates\",\n pug: \"pug\",\n qejs: \"qejs\",\n ractive: \"ractive\",\n razor: \"razor\",\n jsx: \"react\",\n slm: \"slm\",\n squirelly: \"squirelly\",\n swig: \"swig\",\n teacup: \"teacup\",\n templayed: \"templayed\",\n toffee: \"toffee\",\n twig: \"twig\",\n underscore: \"underscore\",\n vash: \"vash\",\n velocityjs: \"velocityjs\",\n walrus: \"walrus\",\n whiskers: \"whiskers\"\n};\n\nexport type PlatformViewsExtensionsTypes = Record<string, string>;\n\nexport interface PlatformViewsEngineOptions extends Record<string, unknown> {\n requires?: any;\n}\n\nexport interface PlatformRenderOptions extends Record<string, unknown> {\n $ctx: BaseContext;\n}\n\nexport interface PlatformViewEngine {\n options: PlatformViewsEngineOptions;\n\n render(path: string, options: PlatformRenderOptions): Promise<string>;\n}\n\nexport interface PlatformViewsSettings {\n disabled?: boolean;\n /**\n * Views directory.\n */\n root?: string;\n /**\n * Enable cache. Ts.ED enable cache in PRODUCTION profile by default.\n */\n cache?: boolean;\n /**\n * Provide extensions mapping to match the expected engines.\n */\n extensions?: Partial<PlatformViewsExtensionsTypes>;\n /**\n * Default view engine extension.\n * Allow omitting extension when using View decorator or render method.\n */\n viewEngine?: string;\n /**\n * Options mapping for each engine.\n */\n options?: Record<string, PlatformViewsEngineOptions>;\n}\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n /**\n * Object to configure Views engines with Consolidate. See more on [View engine](/docs/template-engine.md).\n */\n views: PlatformViewsSettings;\n }\n }\n}\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -8,6 +8,7 @@ export declare class PlatformViews {
|
|
|
8
8
|
env: Env;
|
|
9
9
|
readonly root: string;
|
|
10
10
|
readonly cache: boolean;
|
|
11
|
+
readonly disabled: string;
|
|
11
12
|
readonly viewEngine: string;
|
|
12
13
|
protected extensionsOptions: PlatformViewsExtensionsTypes;
|
|
13
14
|
protected engineOptions: Record<string, PlatformViewsEngineOptions>;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _PlatformViews_extensions, _PlatformViews_engines, _PlatformViews_cachePaths;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.PlatformViews = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const core_1 = require("@tsed/core");
|
|
7
|
+
const di_1 = require("@tsed/di");
|
|
8
|
+
const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
|
|
9
|
+
const path_1 = require("path");
|
|
10
|
+
const PlatformViewsSettings_1 = require("../domain/PlatformViewsSettings");
|
|
11
|
+
function patchEJS(ejs) {
|
|
12
|
+
return {
|
|
13
|
+
compile(str, { client, ...options }) {
|
|
14
|
+
return (ejs || require("ejs")).compile(str, options);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
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
|
+
/**
|
|
27
|
+
* @platform
|
|
28
|
+
*/
|
|
29
|
+
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
|
+
}
|
|
35
|
+
async $onInit() {
|
|
36
|
+
if (!this.disabled) {
|
|
37
|
+
(0, tslib_1.__classPrivateFieldSet)(this, _PlatformViews_extensions, new Map(Object.entries({
|
|
38
|
+
...PlatformViewsSettings_1.PLATFORM_VIEWS_EXTENSIONS,
|
|
39
|
+
...this.extensionsOptions
|
|
40
|
+
})), "f");
|
|
41
|
+
await this.loadFromConsolidate();
|
|
42
|
+
await this.loadFromTsedEngines();
|
|
43
|
+
}
|
|
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
|
+
});
|
|
62
|
+
}
|
|
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
|
+
}
|
|
83
|
+
}
|
|
84
|
+
getEngines() {
|
|
85
|
+
return [...(0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_extensions, "f").entries()].map(([extension, engineType]) => {
|
|
86
|
+
const engine = this.getEngine(this.getExtension(engineType));
|
|
87
|
+
return {
|
|
88
|
+
extension,
|
|
89
|
+
engine
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
registerEngine(engineType, engine) {
|
|
94
|
+
(0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_engines, "f").set(engineType, engine);
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
getEngine(type) {
|
|
98
|
+
return (0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_engines, "f").get((0, tslib_1.__classPrivateFieldGet)(this, _PlatformViews_extensions, "f").get(type) || type);
|
|
99
|
+
}
|
|
100
|
+
getEngineOptions(engineType) {
|
|
101
|
+
return (0, core_1.getValue)(this.engineOptions, engineType, {});
|
|
102
|
+
}
|
|
103
|
+
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);
|
|
105
|
+
const engine = this.getEngine(extension);
|
|
106
|
+
if (!engine) {
|
|
107
|
+
throw new Error(`Engine not found to render the following "${viewPath}"`);
|
|
108
|
+
}
|
|
109
|
+
return engine.render(path, Object.assign({ cache: this.cache || this.env === core_1.Env.PROD }, engine.options, options));
|
|
110
|
+
}
|
|
111
|
+
getExtension(viewPath) {
|
|
112
|
+
return ((0, path_1.extname)(viewPath) || this.viewEngine).replace(/\./, "");
|
|
113
|
+
}
|
|
114
|
+
resolve(viewPath) {
|
|
115
|
+
const extension = this.getExtension(viewPath);
|
|
116
|
+
viewPath = viewPath.replace((0, path_1.extname)(viewPath), "") + "." + extension;
|
|
117
|
+
const path = [
|
|
118
|
+
viewPath,
|
|
119
|
+
(0, path_1.resolve)((0, path_1.join)(this.root, viewPath)),
|
|
120
|
+
(0, path_1.resolve)((0, path_1.join)(process.cwd(), "views", viewPath)),
|
|
121
|
+
(0, path_1.resolve)((0, path_1.join)(process.cwd(), "public", viewPath))
|
|
122
|
+
].find((file) => fs_1.default.existsSync(file)) || viewPath;
|
|
123
|
+
return {
|
|
124
|
+
path,
|
|
125
|
+
extension
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
_PlatformViews_extensions = new WeakMap(), _PlatformViews_engines = new WeakMap(), _PlatformViews_cachePaths = new WeakMap();
|
|
130
|
+
(0, tslib_1.__decorate)([
|
|
131
|
+
(0, di_1.Constant)("env"),
|
|
132
|
+
(0, tslib_1.__metadata)("design:type", String)
|
|
133
|
+
], PlatformViews.prototype, "env", void 0);
|
|
134
|
+
(0, tslib_1.__decorate)([
|
|
135
|
+
(0, di_1.Constant)("views.root", `${process.cwd()}/views`),
|
|
136
|
+
(0, tslib_1.__metadata)("design:type", String)
|
|
137
|
+
], PlatformViews.prototype, "root", void 0);
|
|
138
|
+
(0, tslib_1.__decorate)([
|
|
139
|
+
(0, di_1.Constant)("views.cache"),
|
|
140
|
+
(0, tslib_1.__metadata)("design:type", Boolean)
|
|
141
|
+
], PlatformViews.prototype, "cache", void 0);
|
|
142
|
+
(0, tslib_1.__decorate)([
|
|
143
|
+
(0, di_1.Constant)("views.disabled", false),
|
|
144
|
+
(0, tslib_1.__metadata)("design:type", String)
|
|
145
|
+
], PlatformViews.prototype, "disabled", void 0);
|
|
146
|
+
(0, tslib_1.__decorate)([
|
|
147
|
+
(0, di_1.Constant)("views.viewEngine", "ejs"),
|
|
148
|
+
(0, tslib_1.__metadata)("design:type", String)
|
|
149
|
+
], PlatformViews.prototype, "viewEngine", void 0);
|
|
150
|
+
(0, tslib_1.__decorate)([
|
|
151
|
+
(0, di_1.Constant)("views.extensions", {}),
|
|
152
|
+
(0, tslib_1.__metadata)("design:type", Object)
|
|
153
|
+
], PlatformViews.prototype, "extensionsOptions", void 0);
|
|
154
|
+
(0, tslib_1.__decorate)([
|
|
155
|
+
(0, di_1.Constant)("views.options", {}),
|
|
156
|
+
(0, tslib_1.__metadata)("design:type", Object)
|
|
157
|
+
], PlatformViews.prototype, "engineOptions", void 0);
|
|
158
|
+
PlatformViews = (0, tslib_1.__decorate)([
|
|
159
|
+
(0, di_1.Module)({
|
|
160
|
+
views: {
|
|
161
|
+
exists: true
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
], PlatformViews);
|
|
165
|
+
exports.PlatformViews = PlatformViews;
|
|
166
|
+
//# sourceMappingURL=PlatformViews.js.map
|
|
@@ -0,0 +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,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,CAAC,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"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
}
|
|
27
|
+
//# sourceMappingURL=view.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const PLATFORM_VIEWS_EXTENSIONS = {
|
|
2
|
+
atpl: "atpl",
|
|
3
|
+
bracket: "bracket",
|
|
4
|
+
dot: "dot",
|
|
5
|
+
dust: "dust",
|
|
6
|
+
ect: "ect",
|
|
7
|
+
ejs: "ejs",
|
|
8
|
+
haml: "haml",
|
|
9
|
+
"haml-coffee": "haml-coffee",
|
|
10
|
+
hamlet: "hamlet",
|
|
11
|
+
hbs: "handlebars",
|
|
12
|
+
handlebars: "handlebars",
|
|
13
|
+
hogan: "hogan",
|
|
14
|
+
htmling: "htmling",
|
|
15
|
+
jazz: "jazz",
|
|
16
|
+
jqtpl: "jqtpl",
|
|
17
|
+
just: "just",
|
|
18
|
+
kernel: "kernel",
|
|
19
|
+
liquid: "liquid",
|
|
20
|
+
liquor: "liquor",
|
|
21
|
+
lodash: "lodash",
|
|
22
|
+
mote: "mote",
|
|
23
|
+
mustache: "mustache",
|
|
24
|
+
nunjucks: "nunjucks",
|
|
25
|
+
plates: "plates",
|
|
26
|
+
pug: "pug",
|
|
27
|
+
qejs: "qejs",
|
|
28
|
+
ractive: "ractive",
|
|
29
|
+
razor: "razor",
|
|
30
|
+
jsx: "react",
|
|
31
|
+
slm: "slm",
|
|
32
|
+
squirelly: "squirelly",
|
|
33
|
+
swig: "swig",
|
|
34
|
+
teacup: "teacup",
|
|
35
|
+
templayed: "templayed",
|
|
36
|
+
toffee: "toffee",
|
|
37
|
+
twig: "twig",
|
|
38
|
+
underscore: "underscore",
|
|
39
|
+
vash: "vash",
|
|
40
|
+
velocityjs: "velocityjs",
|
|
41
|
+
walrus: "walrus",
|
|
42
|
+
whiskers: "whiskers"
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=PlatformViewsSettings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformViewsSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformViewsSettings.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,aAAa,EAAE,aAAa;IAC5B,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,YAAY;IACjB,UAAU,EAAE,YAAY;IACxB,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,KAAK;IACV,SAAS,EAAE,WAAW;IACtB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,SAAS,EAAE,WAAW;IACtB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,YAAY;IACxB,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;CACrB,CAAC","sourcesContent":["import {BaseContext} from \"@tsed/di\";\n\nexport const PLATFORM_VIEWS_EXTENSIONS = {\n atpl: \"atpl\",\n bracket: \"bracket\",\n dot: \"dot\",\n dust: \"dust\",\n ect: \"ect\",\n ejs: \"ejs\",\n haml: \"haml\",\n \"haml-coffee\": \"haml-coffee\",\n hamlet: \"hamlet\",\n hbs: \"handlebars\",\n handlebars: \"handlebars\",\n hogan: \"hogan\",\n htmling: \"htmling\",\n jazz: \"jazz\",\n jqtpl: \"jqtpl\",\n just: \"just\",\n kernel: \"kernel\",\n liquid: \"liquid\",\n liquor: \"liquor\",\n lodash: \"lodash\",\n mote: \"mote\",\n mustache: \"mustache\",\n nunjucks: \"nunjucks\",\n plates: \"plates\",\n pug: \"pug\",\n qejs: \"qejs\",\n ractive: \"ractive\",\n razor: \"razor\",\n jsx: \"react\",\n slm: \"slm\",\n squirelly: \"squirelly\",\n swig: \"swig\",\n teacup: \"teacup\",\n templayed: \"templayed\",\n toffee: \"toffee\",\n twig: \"twig\",\n underscore: \"underscore\",\n vash: \"vash\",\n velocityjs: \"velocityjs\",\n walrus: \"walrus\",\n whiskers: \"whiskers\"\n};\n\nexport type PlatformViewsExtensionsTypes = Record<string, string>;\n\nexport interface PlatformViewsEngineOptions extends Record<string, unknown> {\n requires?: any;\n}\n\nexport interface PlatformRenderOptions extends Record<string, unknown> {\n $ctx: BaseContext;\n}\n\nexport interface PlatformViewEngine {\n options: PlatformViewsEngineOptions;\n\n render(path: string, options: PlatformRenderOptions): Promise<string>;\n}\n\nexport interface PlatformViewsSettings {\n disabled?: boolean;\n /**\n * Views directory.\n */\n root?: string;\n /**\n * Enable cache. Ts.ED enable cache in PRODUCTION profile by default.\n */\n cache?: boolean;\n /**\n * Provide extensions mapping to match the expected engines.\n */\n extensions?: Partial<PlatformViewsExtensionsTypes>;\n /**\n * Default view engine extension.\n * Allow omitting extension when using View decorator or render method.\n */\n viewEngine?: string;\n /**\n * Options mapping for each engine.\n */\n options?: Record<string, PlatformViewsEngineOptions>;\n}\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n /**\n * Object to configure Views engines with Consolidate. See more on [View engine](/docs/template-engine.md).\n */\n views: PlatformViewsSettings;\n }\n }\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
var _PlatformViews_extensions, _PlatformViews_engines, _PlatformViews_cachePaths;
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet, __decorate, __metadata } from "tslib";
|
|
3
|
+
import { Env, getValue } from "@tsed/core";
|
|
4
|
+
import { Constant, Module } from "@tsed/di";
|
|
5
|
+
import Fs from "fs";
|
|
6
|
+
import { extname, join, resolve } from "path";
|
|
7
|
+
import { PLATFORM_VIEWS_EXTENSIONS } from "../domain/PlatformViewsSettings";
|
|
8
|
+
function patchEJS(ejs) {
|
|
9
|
+
return {
|
|
10
|
+
compile(str, { client, ...options }) {
|
|
11
|
+
return (ejs || require("ejs")).compile(str, options);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
async function tryImport(name) {
|
|
16
|
+
try {
|
|
17
|
+
return await import(name);
|
|
18
|
+
}
|
|
19
|
+
catch (er) {
|
|
20
|
+
// istanbul ignore next
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @platform
|
|
25
|
+
*/
|
|
26
|
+
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
|
+
}
|
|
32
|
+
async $onInit() {
|
|
33
|
+
if (!this.disabled) {
|
|
34
|
+
__classPrivateFieldSet(this, _PlatformViews_extensions, new Map(Object.entries({
|
|
35
|
+
...PLATFORM_VIEWS_EXTENSIONS,
|
|
36
|
+
...this.extensionsOptions
|
|
37
|
+
})), "f");
|
|
38
|
+
await this.loadFromConsolidate();
|
|
39
|
+
await this.loadFromTsedEngines();
|
|
40
|
+
}
|
|
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
|
+
});
|
|
59
|
+
}
|
|
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
|
+
}
|
|
80
|
+
}
|
|
81
|
+
getEngines() {
|
|
82
|
+
return [...__classPrivateFieldGet(this, _PlatformViews_extensions, "f").entries()].map(([extension, engineType]) => {
|
|
83
|
+
const engine = this.getEngine(this.getExtension(engineType));
|
|
84
|
+
return {
|
|
85
|
+
extension,
|
|
86
|
+
engine
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
registerEngine(engineType, engine) {
|
|
91
|
+
__classPrivateFieldGet(this, _PlatformViews_engines, "f").set(engineType, engine);
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
getEngine(type) {
|
|
95
|
+
return __classPrivateFieldGet(this, _PlatformViews_engines, "f").get(__classPrivateFieldGet(this, _PlatformViews_extensions, "f").get(type) || type);
|
|
96
|
+
}
|
|
97
|
+
getEngineOptions(engineType) {
|
|
98
|
+
return getValue(this.engineOptions, engineType, {});
|
|
99
|
+
}
|
|
100
|
+
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);
|
|
102
|
+
const engine = this.getEngine(extension);
|
|
103
|
+
if (!engine) {
|
|
104
|
+
throw new Error(`Engine not found to render the following "${viewPath}"`);
|
|
105
|
+
}
|
|
106
|
+
return engine.render(path, Object.assign({ cache: this.cache || this.env === Env.PROD }, engine.options, options));
|
|
107
|
+
}
|
|
108
|
+
getExtension(viewPath) {
|
|
109
|
+
return (extname(viewPath) || this.viewEngine).replace(/\./, "");
|
|
110
|
+
}
|
|
111
|
+
resolve(viewPath) {
|
|
112
|
+
const extension = this.getExtension(viewPath);
|
|
113
|
+
viewPath = viewPath.replace(extname(viewPath), "") + "." + extension;
|
|
114
|
+
const path = [
|
|
115
|
+
viewPath,
|
|
116
|
+
resolve(join(this.root, viewPath)),
|
|
117
|
+
resolve(join(process.cwd(), "views", viewPath)),
|
|
118
|
+
resolve(join(process.cwd(), "public", viewPath))
|
|
119
|
+
].find((file) => Fs.existsSync(file)) || viewPath;
|
|
120
|
+
return {
|
|
121
|
+
path,
|
|
122
|
+
extension
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
_PlatformViews_extensions = new WeakMap(), _PlatformViews_engines = new WeakMap(), _PlatformViews_cachePaths = new WeakMap();
|
|
127
|
+
__decorate([
|
|
128
|
+
Constant("env"),
|
|
129
|
+
__metadata("design:type", String)
|
|
130
|
+
], PlatformViews.prototype, "env", void 0);
|
|
131
|
+
__decorate([
|
|
132
|
+
Constant("views.root", `${process.cwd()}/views`),
|
|
133
|
+
__metadata("design:type", String)
|
|
134
|
+
], PlatformViews.prototype, "root", void 0);
|
|
135
|
+
__decorate([
|
|
136
|
+
Constant("views.cache"),
|
|
137
|
+
__metadata("design:type", Boolean)
|
|
138
|
+
], PlatformViews.prototype, "cache", void 0);
|
|
139
|
+
__decorate([
|
|
140
|
+
Constant("views.disabled", false),
|
|
141
|
+
__metadata("design:type", String)
|
|
142
|
+
], PlatformViews.prototype, "disabled", void 0);
|
|
143
|
+
__decorate([
|
|
144
|
+
Constant("views.viewEngine", "ejs"),
|
|
145
|
+
__metadata("design:type", String)
|
|
146
|
+
], PlatformViews.prototype, "viewEngine", void 0);
|
|
147
|
+
__decorate([
|
|
148
|
+
Constant("views.extensions", {}),
|
|
149
|
+
__metadata("design:type", Object)
|
|
150
|
+
], PlatformViews.prototype, "extensionsOptions", void 0);
|
|
151
|
+
__decorate([
|
|
152
|
+
Constant("views.options", {}),
|
|
153
|
+
__metadata("design:type", Object)
|
|
154
|
+
], PlatformViews.prototype, "engineOptions", void 0);
|
|
155
|
+
PlatformViews = __decorate([
|
|
156
|
+
Module({
|
|
157
|
+
views: {
|
|
158
|
+
exists: true
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
], PlatformViews);
|
|
162
|
+
export { PlatformViews };
|
|
163
|
+
//# sourceMappingURL=PlatformViews.js.map
|
|
@@ -0,0 +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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AG,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,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"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
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;
|