@h3ravel/view 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 h3ravel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # @h3ravel/view
2
+
3
+ A view rendering system for the H3ravel framework, providing template rendering capabilities using Edge.js.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @h3ravel/view
9
+ # or
10
+ pnpm add @h3ravel/view
11
+ # or
12
+ yarn add @h3ravel/view
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Usage
18
+
19
+ ```typescript
20
+ import { ViewManager } from '@h3ravel/view'
21
+
22
+ const viewManager = new ViewManager({
23
+ viewsPath: './resources/views',
24
+ cache: process.env.NODE_ENV === 'production'
25
+ })
26
+
27
+ // Render a template
28
+ const html = await viewManager.render('welcome', { name: 'John' })
29
+ ```
30
+
31
+ ### With H3ravel Framework
32
+
33
+ The view package integrates seamlessly with the H3ravel framework:
34
+
35
+ ```typescript
36
+ // In your controller
37
+ export class HomeController extends Controller {
38
+ public async index() {
39
+ return await view('home', {
40
+ title: 'Welcome to H3ravel',
41
+ user: { name: 'John Doe' }
42
+ })
43
+ }
44
+ }
45
+ ```
46
+
47
+ ### Service Provider
48
+
49
+ The package includes a service provider that automatically registers the view system:
50
+
51
+ ```typescript
52
+ import { ViewServiceProvider } from '@h3ravel/view'
53
+
54
+ // The provider is automatically registered when the package is installed
55
+ ```
56
+
57
+ ## Features
58
+
59
+ - **Edge.js Integration**: Built on top of the powerful Edge.js template engine
60
+ - **Laravel-like Syntax**: Familiar template syntax for Laravel developers
61
+ - **Caching**: Production-ready template caching
62
+ - **Global Helpers**: Access to framework helpers within templates
63
+ - **Pluggable**: Can be used independently or as part of H3ravel
64
+
65
+ ## Template Syntax
66
+
67
+ The view system uses Edge.js syntax:
68
+
69
+ ```edge
70
+ {{-- resources/views/welcome.edge --}}
71
+ <!DOCTYPE html>
72
+ <html>
73
+ <head>
74
+ <title>{{ title }}</title>
75
+ </head>
76
+ <body>
77
+ <h1>Hello, {{ user.name }}!</h1>
78
+
79
+ @if(user.isAdmin)
80
+ <p>Welcome, admin!</p>
81
+ @endif
82
+
83
+ @each(item in items)
84
+ <div>{{ item.name }}</div>
85
+ @endeach
86
+ </body>
87
+ </html>
88
+ ```
89
+
90
+ ## Configuration
91
+
92
+ ```typescript
93
+ interface ViewConfig {
94
+ viewsPath: string // Path to views directory
95
+ cache?: boolean // Enable/disable caching
96
+ globals?: object // Global variables available in all templates
97
+ }
98
+ ```
99
+
100
+ ## API Reference
101
+
102
+ ### ViewManager
103
+
104
+ The main class for managing view rendering.
105
+
106
+ #### Methods
107
+
108
+ - `render(template: string, data?: object): Promise<string>` - Render a template
109
+ - `exists(template: string): boolean` - Check if template exists
110
+ - `mount(path: string): void` - Mount additional view directory
111
+ - `global(key: string, value: any): void` - Add global variable
112
+
113
+ ### ViewServiceProvider
114
+
115
+ Service provider for automatic integration with H3ravel framework.
116
+
117
+ ## License
118
+
119
+ MIT License. See [LICENSE](../../LICENSE) for more information.
@@ -0,0 +1,102 @@
1
+ /// <reference path="./app.globals.d.ts" />
2
+ import { Edge } from "edge.js";
3
+ import { ServiceProvider } from "@h3ravel/core";
4
+
5
+ //#region src/Contracts/ViewContract.d.ts
6
+ /**
7
+ * Contract for view rendering engines
8
+ */
9
+ interface ViewContract {
10
+ /**
11
+ * Render a template with the given data
12
+ *
13
+ * @param template - Template name/path
14
+ * @param data - Data to pass to the template
15
+ * @returns Promise resolving to rendered HTML string
16
+ */
17
+ render(template: string, data?: Record<string, any>): Promise<string>;
18
+ /**
19
+ * Check if a template exists
20
+ *
21
+ * @param template - Template name/path
22
+ * @returns True if template exists
23
+ */
24
+ exists(template: string): boolean;
25
+ /**
26
+ * Mount a directory for template lookup
27
+ *
28
+ * @param path - Path to mount
29
+ */
30
+ mount(path: string): void;
31
+ /**
32
+ * Register a global variable/helper
33
+ *
34
+ * @param key - Global variable name
35
+ * @param value - Value or function
36
+ */
37
+ global(key: string, value: any): void;
38
+ }
39
+ //#endregion
40
+ //#region src/EdgeViewEngine.d.ts
41
+ /**
42
+ * Edge.js implementation of the ViewContract
43
+ */
44
+ declare class EdgeViewEngine implements ViewContract {
45
+ private edge;
46
+ constructor(options?: {
47
+ viewsPath?: string;
48
+ cache?: boolean;
49
+ });
50
+ /**
51
+ * Render a template with the given data
52
+ */
53
+ render(template: string, data?: Record<string, any>): Promise<string>;
54
+ /**
55
+ * Check if a template exists
56
+ */
57
+ exists(_template: string): boolean;
58
+ /**
59
+ * Mount a directory for template lookup
60
+ */
61
+ mount(path: string): void;
62
+ /**
63
+ * Register a global variable/helper
64
+ */
65
+ global(key: string, value: any): void;
66
+ /**
67
+ * Get the underlying Edge instance
68
+ */
69
+ getEdge(): Edge;
70
+ }
71
+ //#endregion
72
+ //#region src/Providers/ViewServiceProvider.d.ts
73
+ /**
74
+ * View Service Provider
75
+ *
76
+ * Registers the view engine with the application container
77
+ */
78
+ declare class ViewServiceProvider extends ServiceProvider {
79
+ static priority: number;
80
+ register(): Promise<void>;
81
+ boot(): Promise<void>;
82
+ }
83
+ //#endregion
84
+ //#region src/Commands/MakeViewCommand.d.ts
85
+ /**
86
+ * Command to create new view files
87
+ */
88
+ declare class MakeViewCommand {
89
+ /**
90
+ * Create a new view file
91
+ *
92
+ * @param name - View name (can include directories like 'auth/login')
93
+ * @param options - Command options
94
+ */
95
+ static make(name: string, options?: {
96
+ force?: boolean;
97
+ basePath?: string;
98
+ }): Promise<void>;
99
+ }
100
+ //#endregion
101
+ export { EdgeViewEngine, MakeViewCommand, type ViewContract, EdgeViewEngine as ViewManager, ViewServiceProvider };
102
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1,102 @@
1
+ /// <reference path="./app.globals.d.ts" />
2
+ import { Edge } from "edge.js";
3
+ import { ServiceProvider } from "@h3ravel/core";
4
+
5
+ //#region src/Contracts/ViewContract.d.ts
6
+ /**
7
+ * Contract for view rendering engines
8
+ */
9
+ interface ViewContract {
10
+ /**
11
+ * Render a template with the given data
12
+ *
13
+ * @param template - Template name/path
14
+ * @param data - Data to pass to the template
15
+ * @returns Promise resolving to rendered HTML string
16
+ */
17
+ render(template: string, data?: Record<string, any>): Promise<string>;
18
+ /**
19
+ * Check if a template exists
20
+ *
21
+ * @param template - Template name/path
22
+ * @returns True if template exists
23
+ */
24
+ exists(template: string): boolean;
25
+ /**
26
+ * Mount a directory for template lookup
27
+ *
28
+ * @param path - Path to mount
29
+ */
30
+ mount(path: string): void;
31
+ /**
32
+ * Register a global variable/helper
33
+ *
34
+ * @param key - Global variable name
35
+ * @param value - Value or function
36
+ */
37
+ global(key: string, value: any): void;
38
+ }
39
+ //#endregion
40
+ //#region src/EdgeViewEngine.d.ts
41
+ /**
42
+ * Edge.js implementation of the ViewContract
43
+ */
44
+ declare class EdgeViewEngine implements ViewContract {
45
+ private edge;
46
+ constructor(options?: {
47
+ viewsPath?: string;
48
+ cache?: boolean;
49
+ });
50
+ /**
51
+ * Render a template with the given data
52
+ */
53
+ render(template: string, data?: Record<string, any>): Promise<string>;
54
+ /**
55
+ * Check if a template exists
56
+ */
57
+ exists(_template: string): boolean;
58
+ /**
59
+ * Mount a directory for template lookup
60
+ */
61
+ mount(path: string): void;
62
+ /**
63
+ * Register a global variable/helper
64
+ */
65
+ global(key: string, value: any): void;
66
+ /**
67
+ * Get the underlying Edge instance
68
+ */
69
+ getEdge(): Edge;
70
+ }
71
+ //#endregion
72
+ //#region src/Providers/ViewServiceProvider.d.ts
73
+ /**
74
+ * View Service Provider
75
+ *
76
+ * Registers the view engine with the application container
77
+ */
78
+ declare class ViewServiceProvider extends ServiceProvider {
79
+ static priority: number;
80
+ register(): Promise<void>;
81
+ boot(): Promise<void>;
82
+ }
83
+ //#endregion
84
+ //#region src/Commands/MakeViewCommand.d.ts
85
+ /**
86
+ * Command to create new view files
87
+ */
88
+ declare class MakeViewCommand {
89
+ /**
90
+ * Create a new view file
91
+ *
92
+ * @param name - View name (can include directories like 'auth/login')
93
+ * @param options - Command options
94
+ */
95
+ static make(name: string, options?: {
96
+ force?: boolean;
97
+ basePath?: string;
98
+ }): Promise<void>;
99
+ }
100
+ //#endregion
101
+ export { EdgeViewEngine, MakeViewCommand, type ViewContract, EdgeViewEngine as ViewManager, ViewServiceProvider };
102
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,159 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ let edge_js = require("edge.js");
25
+ edge_js = __toESM(edge_js);
26
+ let __h3ravel_core = require("@h3ravel/core");
27
+ __h3ravel_core = __toESM(__h3ravel_core);
28
+ let node_fs_promises = require("node:fs/promises");
29
+ node_fs_promises = __toESM(node_fs_promises);
30
+ let node_path = require("node:path");
31
+ node_path = __toESM(node_path);
32
+
33
+ //#region src/EdgeViewEngine.ts
34
+ /**
35
+ * Edge.js implementation of the ViewContract
36
+ */
37
+ var EdgeViewEngine = class {
38
+ edge;
39
+ constructor(options = {}) {
40
+ this.edge = edge_js.Edge.create({ cache: options.cache ?? false });
41
+ if (options.viewsPath) this.edge.mount(options.viewsPath);
42
+ }
43
+ /**
44
+ * Render a template with the given data
45
+ */
46
+ async render(template, data = {}) {
47
+ return await this.edge.render(template, data);
48
+ }
49
+ /**
50
+ * Check if a template exists
51
+ */
52
+ exists(_template) {
53
+ try {
54
+ return true;
55
+ } catch {
56
+ return false;
57
+ }
58
+ }
59
+ /**
60
+ * Mount a directory for template lookup
61
+ */
62
+ mount(path) {
63
+ this.edge.mount(path);
64
+ }
65
+ /**
66
+ * Register a global variable/helper
67
+ */
68
+ global(key, value) {
69
+ this.edge.global(key, value);
70
+ }
71
+ /**
72
+ * Get the underlying Edge instance
73
+ */
74
+ getEdge() {
75
+ return this.edge;
76
+ }
77
+ };
78
+
79
+ //#endregion
80
+ //#region src/Providers/ViewServiceProvider.ts
81
+ /**
82
+ * View Service Provider
83
+ *
84
+ * Registers the view engine with the application container
85
+ */
86
+ var ViewServiceProvider = class extends __h3ravel_core.ServiceProvider {
87
+ static priority = 995;
88
+ async register() {
89
+ const viewEngine = new EdgeViewEngine({
90
+ viewsPath: this.app.getPath("views"),
91
+ cache: process.env.NODE_ENV === "production"
92
+ });
93
+ viewEngine.global("app", this.app);
94
+ const edge = viewEngine.getEdge();
95
+ /**
96
+ * Bind the view engine to the container
97
+ */
98
+ this.app.bind("edge", () => edge);
99
+ }
100
+ async boot() {
101
+ /**
102
+ * Initialize the view handler method
103
+ *
104
+ * @param template
105
+ * @param params
106
+ * @returns
107
+ */
108
+ const view = async (template, data) => {
109
+ return this.app.make("http.response").html(await this.app.make("edge").render(template, data));
110
+ };
111
+ /**
112
+ * Bind the view method to the global variable space
113
+ */
114
+ globalThis.view = view;
115
+ /**
116
+ * Dynamically bind the view renderer to the service container.
117
+ * This allows any part of the request lifecycle to render templates using Edge.
118
+ */
119
+ this.app.bind("view", () => view);
120
+ }
121
+ };
122
+
123
+ //#endregion
124
+ //#region src/Commands/MakeViewCommand.ts
125
+ /**
126
+ * Command to create new view files
127
+ */
128
+ var MakeViewCommand = class {
129
+ /**
130
+ * Create a new view file
131
+ *
132
+ * @param name - View name (can include directories like 'auth/login')
133
+ * @param options - Command options
134
+ */
135
+ static async make(name, options = {}) {
136
+ const { force = false, basePath = "src/resources/views" } = options;
137
+ const path = `${basePath}/${name}.edge`;
138
+ if (name.includes("/")) await (0, node_fs_promises.mkdir)((0, node_path.dirname)(path), { recursive: true });
139
+ if (!force) try {
140
+ const { FileSystem } = await import("@h3ravel/shared");
141
+ if (await FileSystem.fileExists(path)) throw new Error(`View ${name} already exists`);
142
+ } catch (error) {
143
+ if (error instanceof Error && error.message.includes("already exists")) throw error;
144
+ }
145
+ const content = `{{-- ${path} --}}
146
+ <div>
147
+ <!-- Your view content here -->
148
+ <h1>{{ title ?? 'Welcome' }}</h1>
149
+ </div>`;
150
+ await (0, node_fs_promises.writeFile)(path, content);
151
+ }
152
+ };
153
+
154
+ //#endregion
155
+ exports.EdgeViewEngine = EdgeViewEngine;
156
+ exports.MakeViewCommand = MakeViewCommand;
157
+ exports.ViewManager = EdgeViewEngine;
158
+ exports.ViewServiceProvider = ViewServiceProvider;
159
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["Edge","ServiceProvider"],"sources":["../src/EdgeViewEngine.ts","../src/Providers/ViewServiceProvider.ts","../src/Commands/MakeViewCommand.ts"],"sourcesContent":["import { Edge } from 'edge.js'\nimport { ViewContract } from './Contracts/ViewContract'\n\n/**\n * Edge.js implementation of the ViewContract\n */\nexport class EdgeViewEngine implements ViewContract {\n private edge: Edge\n\n constructor(options: {\n viewsPath?: string\n cache?: boolean\n } = {}) {\n this.edge = Edge.create({\n cache: options.cache ?? false\n })\n\n if (options.viewsPath) {\n this.edge.mount(options.viewsPath)\n }\n }\n\n /**\n * Render a template with the given data\n */\n async render (template: string, data: Record<string, any> = {}): Promise<string> {\n return await this.edge.render(template, data)\n }\n\n /**\n * Check if a template exists\n */\n exists (_template: string): boolean {\n try {\n // Edge doesn't have a direct exists method, so we try to render with empty data\n // This is a simple approach - in production you might want to implement proper template discovery\n return true // For now, assume template exists - Edge will throw if it doesn't during render\n } catch {\n return false\n }\n }\n\n /**\n * Mount a directory for template lookup\n */\n mount (path: string): void {\n this.edge.mount(path)\n }\n\n /**\n * Register a global variable/helper\n */\n global (key: string, value: any): void {\n this.edge.global(key, value)\n }\n\n /**\n * Get the underlying Edge instance\n */\n getEdge (): Edge {\n return this.edge\n }\n}\n","import { EdgeViewEngine } from '../EdgeViewEngine'\nimport { ServiceProvider } from '@h3ravel/core'\n\n/**\n * View Service Provider\n * \n * Registers the view engine with the application container\n */\nexport class ViewServiceProvider extends ServiceProvider {\n public static priority = 995\n\n async register () {\n // Create the view engine instance\n const viewEngine = new EdgeViewEngine({\n viewsPath: this.app.getPath('views'),\n cache: process.env.NODE_ENV === 'production'\n })\n\n // Register the app instance if available\n viewEngine.global('app', this.app)\n\n const edge = viewEngine.getEdge()\n\n /**\n * Bind the view engine to the container\n */\n this.app.bind('edge', () => edge)\n }\n\n async boot () {\n /**\n * Initialize the view handler method\n * \n * @param template \n * @param params \n * @returns \n */\n const view = async (template: string, data?: Record<string, any>) => {\n const response = this.app.make('http.response')\n\n return response.html(await this.app.make('edge').render(template, data))\n }\n\n /**\n * Bind the view method to the global variable space\n */\n globalThis.view = view\n\n /**\n * Dynamically bind the view renderer to the service container.\n * This allows any part of the request lifecycle to render templates using Edge.\n */\n this.app.bind('view', () => view)\n }\n}\n","import { mkdir, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\n\n/**\n * Command to create new view files\n */\nexport class MakeViewCommand {\n /**\n * Create a new view file\n * \n * @param name - View name (can include directories like 'auth/login')\n * @param options - Command options\n */\n static async make(\n name: string, \n options: {\n force?: boolean\n basePath?: string\n } = {}\n ): Promise<void> {\n const { force = false, basePath = 'src/resources/views' } = options\n \n const path = `${basePath}/${name}.edge`\n\n // The view is scoped to a path make sure to create the associated directories\n if (name.includes('/')) {\n await mkdir(dirname(path), { recursive: true })\n }\n\n // Check if the view already exists\n if (!force) {\n try {\n const { FileSystem } = await import('@h3ravel/shared')\n if (await FileSystem.fileExists(path)) {\n throw new Error(`View ${name} already exists`)\n }\n } catch (error) {\n if (error instanceof Error && error.message.includes('already exists')) {\n throw error\n }\n // FileSystem not available, continue\n }\n }\n\n // Create the view file\n const content = `{{-- ${path} --}}\n<div>\n <!-- Your view content here -->\n <h1>{{ title ?? 'Welcome' }}</h1>\n</div>`\n\n await writeFile(path, content)\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAa,iBAAb,MAAoD;CAClD,AAAQ;CAER,YAAY,UAGR,EAAE,EAAE;AACN,OAAK,OAAOA,a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,MAAM,CAAC;AAIjD,MAAI,CAAC,MACH,KAAI;GACF,MAAM,EAAE,eAAe,MAAM,OAAO;AACpC,OAAI,MAAM,WAAW,WAAW,KAAK,CACnC,OAAM,IAAI,MAAM,QAAQ,KAAK,iBAAiB;WAEzC,OAAO;AACd,OAAI,iBAAiB,SAAS,MAAM,QAAQ,SAAS,iBAAiB,CACpE,OAAM;;EAOZ,MAAM,UAAU,QAAQ,KAAK;;;;;AAM7B,wCAAgB,MAAM,QAAQ"}
package/dist/index.mjs ADDED
@@ -0,0 +1,129 @@
1
+ import { Edge } from "edge.js";
2
+ import { ServiceProvider } from "@h3ravel/core";
3
+ import { mkdir, writeFile } from "node:fs/promises";
4
+ import { dirname } from "node:path";
5
+
6
+ //#region src/EdgeViewEngine.ts
7
+ /**
8
+ * Edge.js implementation of the ViewContract
9
+ */
10
+ var EdgeViewEngine = class {
11
+ edge;
12
+ constructor(options = {}) {
13
+ this.edge = Edge.create({ cache: options.cache ?? false });
14
+ if (options.viewsPath) this.edge.mount(options.viewsPath);
15
+ }
16
+ /**
17
+ * Render a template with the given data
18
+ */
19
+ async render(template, data = {}) {
20
+ return await this.edge.render(template, data);
21
+ }
22
+ /**
23
+ * Check if a template exists
24
+ */
25
+ exists(_template) {
26
+ try {
27
+ return true;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
32
+ /**
33
+ * Mount a directory for template lookup
34
+ */
35
+ mount(path) {
36
+ this.edge.mount(path);
37
+ }
38
+ /**
39
+ * Register a global variable/helper
40
+ */
41
+ global(key, value) {
42
+ this.edge.global(key, value);
43
+ }
44
+ /**
45
+ * Get the underlying Edge instance
46
+ */
47
+ getEdge() {
48
+ return this.edge;
49
+ }
50
+ };
51
+
52
+ //#endregion
53
+ //#region src/Providers/ViewServiceProvider.ts
54
+ /**
55
+ * View Service Provider
56
+ *
57
+ * Registers the view engine with the application container
58
+ */
59
+ var ViewServiceProvider = class extends ServiceProvider {
60
+ static priority = 995;
61
+ async register() {
62
+ const viewEngine = new EdgeViewEngine({
63
+ viewsPath: this.app.getPath("views"),
64
+ cache: process.env.NODE_ENV === "production"
65
+ });
66
+ viewEngine.global("app", this.app);
67
+ const edge = viewEngine.getEdge();
68
+ /**
69
+ * Bind the view engine to the container
70
+ */
71
+ this.app.bind("edge", () => edge);
72
+ }
73
+ async boot() {
74
+ /**
75
+ * Initialize the view handler method
76
+ *
77
+ * @param template
78
+ * @param params
79
+ * @returns
80
+ */
81
+ const view = async (template, data) => {
82
+ return this.app.make("http.response").html(await this.app.make("edge").render(template, data));
83
+ };
84
+ /**
85
+ * Bind the view method to the global variable space
86
+ */
87
+ globalThis.view = view;
88
+ /**
89
+ * Dynamically bind the view renderer to the service container.
90
+ * This allows any part of the request lifecycle to render templates using Edge.
91
+ */
92
+ this.app.bind("view", () => view);
93
+ }
94
+ };
95
+
96
+ //#endregion
97
+ //#region src/Commands/MakeViewCommand.ts
98
+ /**
99
+ * Command to create new view files
100
+ */
101
+ var MakeViewCommand = class {
102
+ /**
103
+ * Create a new view file
104
+ *
105
+ * @param name - View name (can include directories like 'auth/login')
106
+ * @param options - Command options
107
+ */
108
+ static async make(name, options = {}) {
109
+ const { force = false, basePath = "src/resources/views" } = options;
110
+ const path = `${basePath}/${name}.edge`;
111
+ if (name.includes("/")) await mkdir(dirname(path), { recursive: true });
112
+ if (!force) try {
113
+ const { FileSystem } = await import("@h3ravel/shared");
114
+ if (await FileSystem.fileExists(path)) throw new Error(`View ${name} already exists`);
115
+ } catch (error) {
116
+ if (error instanceof Error && error.message.includes("already exists")) throw error;
117
+ }
118
+ const content = `{{-- ${path} --}}
119
+ <div>
120
+ <!-- Your view content here -->
121
+ <h1>{{ title ?? 'Welcome' }}</h1>
122
+ </div>`;
123
+ await writeFile(path, content);
124
+ }
125
+ };
126
+
127
+ //#endregion
128
+ export { EdgeViewEngine, MakeViewCommand, EdgeViewEngine as ViewManager, ViewServiceProvider };
129
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/EdgeViewEngine.ts","../src/Providers/ViewServiceProvider.ts","../src/Commands/MakeViewCommand.ts"],"sourcesContent":["import { Edge } from 'edge.js'\nimport { ViewContract } from './Contracts/ViewContract'\n\n/**\n * Edge.js implementation of the ViewContract\n */\nexport class EdgeViewEngine implements ViewContract {\n private edge: Edge\n\n constructor(options: {\n viewsPath?: string\n cache?: boolean\n } = {}) {\n this.edge = Edge.create({\n cache: options.cache ?? false\n })\n\n if (options.viewsPath) {\n this.edge.mount(options.viewsPath)\n }\n }\n\n /**\n * Render a template with the given data\n */\n async render (template: string, data: Record<string, any> = {}): Promise<string> {\n return await this.edge.render(template, data)\n }\n\n /**\n * Check if a template exists\n */\n exists (_template: string): boolean {\n try {\n // Edge doesn't have a direct exists method, so we try to render with empty data\n // This is a simple approach - in production you might want to implement proper template discovery\n return true // For now, assume template exists - Edge will throw if it doesn't during render\n } catch {\n return false\n }\n }\n\n /**\n * Mount a directory for template lookup\n */\n mount (path: string): void {\n this.edge.mount(path)\n }\n\n /**\n * Register a global variable/helper\n */\n global (key: string, value: any): void {\n this.edge.global(key, value)\n }\n\n /**\n * Get the underlying Edge instance\n */\n getEdge (): Edge {\n return this.edge\n }\n}\n","import { EdgeViewEngine } from '../EdgeViewEngine'\nimport { ServiceProvider } from '@h3ravel/core'\n\n/**\n * View Service Provider\n * \n * Registers the view engine with the application container\n */\nexport class ViewServiceProvider extends ServiceProvider {\n public static priority = 995\n\n async register () {\n // Create the view engine instance\n const viewEngine = new EdgeViewEngine({\n viewsPath: this.app.getPath('views'),\n cache: process.env.NODE_ENV === 'production'\n })\n\n // Register the app instance if available\n viewEngine.global('app', this.app)\n\n const edge = viewEngine.getEdge()\n\n /**\n * Bind the view engine to the container\n */\n this.app.bind('edge', () => edge)\n }\n\n async boot () {\n /**\n * Initialize the view handler method\n * \n * @param template \n * @param params \n * @returns \n */\n const view = async (template: string, data?: Record<string, any>) => {\n const response = this.app.make('http.response')\n\n return response.html(await this.app.make('edge').render(template, data))\n }\n\n /**\n * Bind the view method to the global variable space\n */\n globalThis.view = view\n\n /**\n * Dynamically bind the view renderer to the service container.\n * This allows any part of the request lifecycle to render templates using Edge.\n */\n this.app.bind('view', () => view)\n }\n}\n","import { mkdir, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\n\n/**\n * Command to create new view files\n */\nexport class MakeViewCommand {\n /**\n * Create a new view file\n * \n * @param name - View name (can include directories like 'auth/login')\n * @param options - Command options\n */\n static async make(\n name: string, \n options: {\n force?: boolean\n basePath?: string\n } = {}\n ): Promise<void> {\n const { force = false, basePath = 'src/resources/views' } = options\n \n const path = `${basePath}/${name}.edge`\n\n // The view is scoped to a path make sure to create the associated directories\n if (name.includes('/')) {\n await mkdir(dirname(path), { recursive: true })\n }\n\n // Check if the view already exists\n if (!force) {\n try {\n const { FileSystem } = await import('@h3ravel/shared')\n if (await FileSystem.fileExists(path)) {\n throw new Error(`View ${name} already exists`)\n }\n } catch (error) {\n if (error instanceof Error && error.message.includes('already exists')) {\n throw error\n }\n // FileSystem not available, continue\n }\n }\n\n // Create the view file\n const content = `{{-- ${path} --}}\n<div>\n <!-- Your view content here -->\n <h1>{{ title ?? 'Welcome' }}</h1>\n</div>`\n\n await writeFile(path, content)\n }\n}"],"mappings":";;;;;;;;;AAMA,IAAa,iBAAb,MAAoD;CAClD,AAAQ;CAER,YAAY,UAGR,EAAE,EAAE;AACN,OAAK,OAAO,KAAK,OAAO,EACtB,OAAO,QAAQ,SAAS,OACzB,CAAC;AAEF,MAAI,QAAQ,UACV,MAAK,KAAK,MAAM,QAAQ,UAAU;;;;;CAOtC,MAAM,OAAQ,UAAkB,OAA4B,EAAE,EAAmB;AAC/E,SAAO,MAAM,KAAK,KAAK,OAAO,UAAU,KAAK;;;;;CAM/C,OAAQ,WAA4B;AAClC,MAAI;AAGF,UAAO;UACD;AACN,UAAO;;;;;;CAOX,MAAO,MAAoB;AACzB,OAAK,KAAK,MAAM,KAAK;;;;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ED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@h3ravel/view",
3
+ "version": "0.1.0",
4
+ "description": "View rendering system for H3ravel framework",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "keywords": [
19
+ "h3ravel",
20
+ "view",
21
+ "template",
22
+ "edge",
23
+ "rendering"
24
+ ],
25
+ "author": "H3ravel Team",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/tusharshah21/framework.git",
30
+ "directory": "packages/view"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/tusharshah21/framework/issues"
34
+ },
35
+ "homepage": "https://h3ravel.toneflix.net",
36
+ "dependencies": {
37
+ "edge.js": "^6.3.0",
38
+ "@h3ravel/core": "1.12.0",
39
+ "@h3ravel/http": "11.3.2"
40
+ },
41
+ "devDependencies": {
42
+ "typescript": "^5.0.0",
43
+ "vitest": "^2.0.0"
44
+ },
45
+ "peerDependencies": {
46
+ "@h3ravel/shared": "0.20.11"
47
+ },
48
+ "scripts": {
49
+ "dev": "tsdown --watch --config-loader unconfig",
50
+ "typecheck": "tsc --noEmit",
51
+ "barrel": "barrelsby --directory src --delete --singleQuotes",
52
+ "build": "tsdown --config-loader unconfig",
53
+ "start": "node dist/index.js",
54
+ "lint": "eslint . --ext .ts",
55
+ "test": "jest --passWithNoTests",
56
+ "version-patch": "pnpm version patch"
57
+ }
58
+ }