janja-fastify 0.0.0-stage → 0.1.2

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 李文富
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 CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # janja-fastify
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [Janja](https://github.com/vvenv/janja) view rendering for Fastify 5.
4
+
5
+ ```bash
6
+ pnpm add janja janja-fastify
7
+ ```
8
+
9
+ ```js
10
+ import Fastify from 'fastify';
11
+ import { janjaView } from 'janja-fastify';
12
+
13
+ const fastify = Fastify();
14
+
15
+ await fastify.register(janjaView, { root: './views' });
16
+
17
+ fastify.get('/', (request, reply) => reply.view('home', { title: 'Hello' }));
18
+ ```
19
+
20
+ - `reply.view(view, data)` renders `views/home.janja` and sends it as an HTML response. The view sees `reply.locals`, if you set it in a hook, overlaid with `data`.
21
+ - `fastify.view(view, data)` renders to a string, for emails and the like.
22
+ - Views and `{{ include "partials/footer" }}` are resolved inside the views directory, with or without the extension, and cannot point outside of it.
23
+ - Each view is compiled once and recompiled only when it, or a template it includes, changes on disk.
24
+ - Errors in a view reject the handler, so Fastify's error handling applies.
25
+
26
+ ## Options
27
+
28
+ Every Janja renderer option except `loader` (`globals`, `filters`, `plugins`, `autoEscape`, `cache`, `debug`, ...), plus:
29
+
30
+ | Option | Default | Description |
31
+ | ----------- | ---------- | ----------- |
32
+ | `root` | required | Directory of the views |
33
+ | `extension` | `'.janja'` | Extension of the views, used when a view or include is named without one |
34
+
35
+ If views never change while the server runs, pass `cache: { revalidate: false }` to skip re-reading included templates on each request.
@@ -0,0 +1,33 @@
1
+ import type { FastifyPluginAsync } from 'fastify';
2
+ import { type RendererOptions } from 'janja';
3
+ export interface JanjaViewOptions extends Omit<RendererOptions, 'loader'> {
4
+ /** Directory of the views */
5
+ root: string;
6
+ /**
7
+ * Extension of the views, used when a view or include is named without one.
8
+ * @default '.janja'
9
+ */
10
+ extension?: string;
11
+ }
12
+ declare module 'fastify' {
13
+ interface FastifyInstance {
14
+ /** Render a view to a string */
15
+ view: (view: string, data?: Record<string, unknown>) => Promise<string>;
16
+ }
17
+ interface FastifyReply {
18
+ /**
19
+ * Render a view and send it as an HTML response. The view sees
20
+ * `reply.locals`, if set, overlaid with `data`.
21
+ */
22
+ view: (view: string, data?: Record<string, unknown>) => Promise<this>;
23
+ locals?: Record<string, unknown>;
24
+ }
25
+ }
26
+ /**
27
+ * Fastify plugin that adds `reply.view(view, data)`:
28
+ *
29
+ * await fastify.register(janjaView, { root: './views' });
30
+ * fastify.get('/', (request, reply) => reply.view('home', { title: 'Hi' }));
31
+ */
32
+ export declare const janjaView: FastifyPluginAsync<JanjaViewOptions>;
33
+ export default janjaView;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ import fp from 'fastify-plugin';
2
+ import { createFileLoader, Renderer } from 'janja';
3
+ const plugin = async (fastify, { root, extension = '.janja', ...options }) => {
4
+ if (!root) {
5
+ throw new Error('janja-fastify: the "root" option is required');
6
+ }
7
+ // One renderer for all requests: each view is compiled once and recompiled
8
+ // only when it, or a template it includes, changes on disk
9
+ const renderer = new Renderer({
10
+ ...options,
11
+ loader: createFileLoader(root, { extensions: [extension] }),
12
+ });
13
+ fastify.decorate('view', (view, data = {}) => renderer.renderFile(view, data));
14
+ fastify.decorateReply('view', async function view(name, data = {}) {
15
+ const html = await renderer.renderFile(name, { ...this.locals, ...data });
16
+ return this.type('text/html; charset=utf-8').send(html);
17
+ });
18
+ };
19
+ /**
20
+ * Fastify plugin that adds `reply.view(view, data)`:
21
+ *
22
+ * await fastify.register(janjaView, { root: './views' });
23
+ * fastify.get('/', (request, reply) => reply.view('home', { title: 'Hi' }));
24
+ */
25
+ export const janjaView = fp(plugin, {
26
+ fastify: '5.x',
27
+ name: 'janja-fastify',
28
+ });
29
+ export default janjaView;
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "janja-fastify",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.2",
4
+ "description": "Janja view rendering for Fastify",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/vvenv/janja.git",
10
+ "directory": "packages/janja-fastify"
11
+ },
12
+ "homepage": "https://janja.wenfu.dev",
13
+ "bugs": "https://github.com/vvenv/janja/issues",
14
+ "keywords": [
15
+ "fastify",
16
+ "fastify-plugin",
17
+ "views",
18
+ "janja",
19
+ "template"
20
+ ],
21
+ "engines": {
22
+ "node": ">=18"
23
+ },
24
+ "main": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "README.md"
35
+ ],
36
+ "dependencies": {
37
+ "fastify-plugin": "^5.0.0"
38
+ },
39
+ "peerDependencies": {
40
+ "janja": "^4.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^24.10.3",
44
+ "fastify": "^5.0.0",
45
+ "typescript": "^5.9.3",
46
+ "vitest": "^4.0.16",
47
+ "janja": "4.0.0"
48
+ },
49
+ "scripts": {
50
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
51
+ "test": "vitest",
52
+ "typecheck": "tsc --noEmit"
53
+ }
6
54
  }