janja-koa 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 +21 -0
- package/README.md +34 -2
- package/dist/index.d.ts +30 -0
- package/dist/index.js +26 -0
- package/package.json +49 -3
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
|
-
#
|
|
1
|
+
# janja-koa
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Janja](https://github.com/vvenv/janja) view rendering for Koa.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add janja janja-koa
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import Koa from 'koa';
|
|
11
|
+
import { views } from 'janja-koa';
|
|
12
|
+
|
|
13
|
+
const app = new Koa();
|
|
14
|
+
|
|
15
|
+
app.use(views('./views'));
|
|
16
|
+
|
|
17
|
+
app.use(async (ctx) => {
|
|
18
|
+
await ctx.render('home', { title: 'Hello' });
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
- `ctx.render(view, data)` renders `views/home.janja` as an HTML response. The view sees `ctx.state` overlaid with `data`.
|
|
23
|
+
- Views and `{{ include "partials/footer" }}` are resolved inside the views directory, with or without the extension, and cannot point outside of it.
|
|
24
|
+
- Each view is compiled once and recompiled only when it, or a template it includes, changes on disk.
|
|
25
|
+
- Errors in a view reject `ctx.render`, so Koa's error handling applies.
|
|
26
|
+
|
|
27
|
+
## Options
|
|
28
|
+
|
|
29
|
+
`views(root, options)` accepts every Janja renderer option except `loader` (`globals`, `filters`, `plugins`, `autoEscape`, `cache`, `debug`, ...), plus:
|
|
30
|
+
|
|
31
|
+
| Option | Default | Description |
|
|
32
|
+
| ----------- | ---------- | ----------- |
|
|
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type RendererOptions } from 'janja';
|
|
2
|
+
import type { Middleware } from 'koa';
|
|
3
|
+
export interface ViewsOptions extends Omit<RendererOptions, 'loader'> {
|
|
4
|
+
/**
|
|
5
|
+
* Extension of the views, used when a view or include is named without one.
|
|
6
|
+
* @default '.janja'
|
|
7
|
+
*/
|
|
8
|
+
extension?: string;
|
|
9
|
+
}
|
|
10
|
+
declare module 'koa' {
|
|
11
|
+
interface ExtendableContext {
|
|
12
|
+
/**
|
|
13
|
+
* Render a view from the views directory as the response body. The view
|
|
14
|
+
* sees `ctx.state` overlaid with `data`.
|
|
15
|
+
*/
|
|
16
|
+
render: (view: string, data?: Record<string, unknown>) => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Middleware that adds `ctx.render(view, data)`:
|
|
21
|
+
*
|
|
22
|
+
* app.use(views('./views'));
|
|
23
|
+
* app.use(async (ctx) => ctx.render('home', { title: 'Hello' }));
|
|
24
|
+
*
|
|
25
|
+
* Views and `{{ include "..." }}` are resolved inside `root` and cannot leave
|
|
26
|
+
* it. One renderer serves all requests, so each view is compiled once and
|
|
27
|
+
* recompiled only when it, or a template it includes, changes on disk.
|
|
28
|
+
*/
|
|
29
|
+
export declare function views(root: string, { extension, ...options }?: ViewsOptions): Middleware;
|
|
30
|
+
export default views;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createFileLoader, Renderer } from 'janja';
|
|
2
|
+
/**
|
|
3
|
+
* Middleware that adds `ctx.render(view, data)`:
|
|
4
|
+
*
|
|
5
|
+
* app.use(views('./views'));
|
|
6
|
+
* app.use(async (ctx) => ctx.render('home', { title: 'Hello' }));
|
|
7
|
+
*
|
|
8
|
+
* Views and `{{ include "..." }}` are resolved inside `root` and cannot leave
|
|
9
|
+
* it. One renderer serves all requests, so each view is compiled once and
|
|
10
|
+
* recompiled only when it, or a template it includes, changes on disk.
|
|
11
|
+
*/
|
|
12
|
+
export function views(root, { extension = '.janja', ...options } = {}) {
|
|
13
|
+
const renderer = new Renderer({
|
|
14
|
+
...options,
|
|
15
|
+
loader: createFileLoader(root, { extensions: [extension] }),
|
|
16
|
+
});
|
|
17
|
+
return async (ctx, next) => {
|
|
18
|
+
ctx.render = async (view, data = {}) => {
|
|
19
|
+
const html = await renderer.renderFile(view, { ...ctx.state, ...data });
|
|
20
|
+
ctx.type = 'html';
|
|
21
|
+
ctx.body = html;
|
|
22
|
+
};
|
|
23
|
+
await next();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export default views;
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "janja-koa",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Janja view rendering for Koa",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/vvenv/janja.git",
|
|
10
|
+
"directory": "packages/janja-koa"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://janja.wenfu.dev",
|
|
13
|
+
"bugs": "https://github.com/vvenv/janja/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"koa",
|
|
16
|
+
"middleware",
|
|
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
|
+
"peerDependencies": {
|
|
37
|
+
"janja": "^4.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/koa": "^2.15.0",
|
|
41
|
+
"@types/node": "^24.10.3",
|
|
42
|
+
"koa": "^3.0.0",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
44
|
+
"vitest": "^4.0.16",
|
|
45
|
+
"janja": "4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
49
|
+
"test": "vitest",
|
|
50
|
+
"typecheck": "tsc --noEmit"
|
|
51
|
+
}
|
|
6
52
|
}
|