Skip to content

Commit e7e3d85

Browse files
authored
Merge pull request #34 from deligenius/0.114.0
Remove factory, support oak@10.5.1
2 parents bc8d312 + 72d8869 commit e7e3d85

32 files changed

+159
-594
lines changed

.github/workflows/CI.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ jobs:
1313
# This workflow contains a single job called "build"
1414
test:
1515
# The type of runner that the job will run on
16-
runs-on: ubuntu-16.04
16+
runs-on: ubuntu-20.04
1717

1818
# Steps represent a sequence of tasks that will be executed as part of the job
1919
steps:
2020
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
2121
- uses: actions/checkout@v2
22+
2223
- name: Start Deno test
23-
uses: denolib/setup-deno@v2
24+
uses: denoland/setup-deno@v1
2425
with:
25-
deno-version: v1.x
26+
deno-version: v1.20.5
27+
2628
- run: deno --version
2729
- run: deno test -A

.vscode/settings.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
{
22
"deno.enable": true,
3-
"workbench.colorCustomizations": {
4-
"activityBar.background": "#2D3005",
5-
"titleBar.activeBackground": "#3F4306",
6-
"titleBar.activeForeground": "#FAFCE2"
7-
},
83
}

deps.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
export * from "https://deno.land/x/oak@v6.5.0/mod.ts";
1+
export { existsSync } from "https://deno.land/std@0.131.0/fs/exists.ts";
2+
export * as path from "https://deno.land/std@0.131.0/path/mod.ts";
23

4+
// test dependencies
5+
export { green, blue } from "https://deno.land/std@0.131.0/fmt/colors.ts";
6+
export { assertEquals, assert } from "https://deno.land/std@0.131.0/testing/asserts.ts";
37

4-
export {existsSync} from 'https://deno.land/std@0.84.0/fs/exists.ts'
5-
export * as path from 'https://deno.land/std@0.84.0/path/mod.ts'
8+
export { Application } from "https://deno.land/x/oak@v10.5.1/mod.ts";

lib/adapterFactory.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/adapters/oak.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

lib/adapters/oak/oak.adapter.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type {
2+
Context,
3+
RouteParams,
4+
State,
5+
} from "https://deno.land/x/oak@v10.5.1/mod.ts";
6+
7+
import type { ViewConfig,Adapter,Engine } from "../../viewEngine.type.ts";
8+
import { getTemplate } from "./oak.utils.ts";
9+
10+
declare module "https://deno.land/x/oak@v10.5.1/mod.ts" {
11+
// App level Context
12+
interface Context {
13+
render: (fileName: string, data?: object) => void;
14+
}
15+
16+
// Router level Context
17+
interface RouterContext<
18+
R extends string,
19+
P extends RouteParams<R> = RouteParams<R>,
20+
// deno-lint-ignore no-explicit-any
21+
S extends State = Record<string, any>
22+
> {
23+
render: (fileName: string, data?: object) => void;
24+
}
25+
26+
// add viewConfig to Application interface
27+
interface Application {
28+
viewConcig: ViewConfig;
29+
}
30+
}
31+
32+
//! Add `render` function to Context
33+
export const oakAdapter: Adapter = (
34+
renderEngine: Engine,
35+
config: ViewConfig = <ViewConfig>{}
36+
) => {
37+
return async function (ctx: Context, next: Function) {
38+
// load default view setting
39+
if (!ctx.app.viewConcig) {
40+
ctx.app.viewConcig = {
41+
viewEngine: config.viewEngine,
42+
viewRoot: config.viewRoot ?? "./",
43+
};
44+
}
45+
46+
ctx.render = async function (fileName: string, data?: object) {
47+
try {
48+
const viewConfig = ctx.app.viewConcig;
49+
50+
// find template file path, either on internet or in local file system
51+
let template = await getTemplate(viewConfig.viewRoot!, fileName);
52+
53+
ctx.response.headers.set("Content-Type", "text/html; charset=utf-8");
54+
ctx.response.body = await renderEngine(
55+
template,
56+
data ?? {},
57+
ctx.app.viewConcig,
58+
fileName
59+
);
60+
} catch (e) {
61+
ctx.response.status = 404;
62+
console.error("View-Engine: ", e.message);
63+
}
64+
};
65+
66+
await next();
67+
};
68+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { path } from "../../deps.ts";
1+
import { path } from "../../../deps.ts";
22

33
const urlRegex =
44
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;

lib/adapters/oak/oak_test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// app.ts
2+
import { Application, green, assertEquals, assert } from "../../../deps.ts";
3+
import {
4+
viewEngine,
5+
oakAdapter,
6+
ejsEngine,
7+
handlebarsEngine,
8+
} from "../../../mod.ts";
9+
10+
const removeRegex = /\r?\n|\r|\s/g;
11+
12+
Deno.test({
13+
name: green("Testing Oak - EjsEngine"),
14+
async fn() {
15+
const controller = new AbortController();
16+
const { signal } = controller;
17+
const app = new Application();
18+
app.use(viewEngine(oakAdapter, ejsEngine, { viewRoot: "./views/ejs" }));
19+
20+
app.use(async (ctx, next) => {
21+
ctx.render("index.ejs", { data: { name: "John" } });
22+
});
23+
setTimeout(async () => {
24+
const actual = await fetch("http://localhost:8000").then((res) =>
25+
res.text()
26+
);
27+
28+
assert(actual.includes("John"));
29+
controller.abort();
30+
}, 500);
31+
32+
await app.listen({ port: 8000, signal });
33+
},
34+
});

0 commit comments

Comments
 (0)