Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
export { existsSync } from "https://deno.land/std@0.131.0/fs/exists.ts";
export * as path from "https://deno.land/std@0.131.0/path/mod.ts";
export { existsSync } from "https://deno.land/std@0.214.0/fs/exists.ts";
export * as path from "https://deno.land/std@0.214.0/path/mod.ts";

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

export { Application } from "https://deno.land/x/oak@v10.6.0/mod.ts";
export { Application, Context, type MiddlewareOrMiddlewareObject } from "https://deno.land/x/oak@v13.0.0/mod.ts";

export * as eta from "https://deno.land/x/eta@v1.12.3/mod.ts";
export * as dejs from "https://deno.land/x/dejs@0.10.2/mod.ts";
export * as pug from "https://deno.land/x/pug@v0.1.3/mod.ts";
import { Eta } from "npm:eta";
export const eta = new Eta();
export * as dejs from "https://deno.land/x/dejs@0.10.3/mod.ts";
export * as pug from "npm:pug";
23 changes: 10 additions & 13 deletions lib/adapters/oak/oak.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type {
Context,
MiddlewareOrMiddlewareObject,
RouteParams,
State,
} from "https://deno.land/x/oak@v10.6.0/mod.ts";
} from "https://deno.land/x/oak@v13.0.0/mod.ts";

import type { ViewConfig,Adapter,Engine } from "../../viewEngine.type.ts";
import { getTemplate } from "./oak.utils.ts";
import type { ViewConfig,Engine } from "../../viewEngine.type.ts";

declare module "https://deno.land/x/oak@v10.6.0/mod.ts" {
declare module "https://deno.land/x/oak@v13.0.0/mod.ts" {
// App level Context
interface Context {
render: (fileName: string, data?: object) => void;
Expand All @@ -28,13 +28,12 @@ declare module "https://deno.land/x/oak@v10.6.0/mod.ts" {
viewConfig: ViewConfig;
}
}

//! Add `render` function to Context
export const oakAdapter: Adapter = (
export const oakAdapter = <T extends MiddlewareOrMiddlewareObject>(
renderEngine: Engine,
config: ViewConfig = <ViewConfig>{}
) => {
return async function (ctx: Context, next: Function) {
return async function (ctx: Context, next: () => Promise<unknown>) {
// load default view setting
if (!ctx.app.viewConfig) {
ctx.app.viewConfig = {
Expand All @@ -43,15 +42,13 @@ export const oakAdapter: Adapter = (
};
}

ctx.render = async (fileName: string, data?: object) =>{
ctx.render = (fileName: string, data?: object) =>{
try {
const viewConfig = ctx.app.viewConfig;

ctx.response.headers.set("Content-Type", "text/html; charset=utf-8");

ctx.response.body = async () => {
ctx.response.body = () => {
return renderEngine(
await getTemplate(viewConfig.viewRoot ??"./", fileName),
fileName,
data ?? {},
ctx.app.viewConfig,
fileName
Expand All @@ -65,5 +62,5 @@ export const oakAdapter: Adapter = (
};

await next();
};
} as T;
};
4 changes: 2 additions & 2 deletions lib/adapters/oak/oak.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { path } from "../../../deps.ts";

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


export async function getTemplate(viewRoot: string, fileName: string) {
Expand Down
9 changes: 5 additions & 4 deletions lib/adapters/oak/oak_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// app.ts
import { Application, green, assertEquals, assert } from "../../../deps.ts";
import { Application, green, assert } from "../../../deps.ts";

import {
viewEngine,
oakAdapter,
dejsEngine
} from "../../../mod.ts";

const removeRegex = /\r?\n|\r|\s/g;
//import declaration from the oak.adapter.ts file
// const removeRegex = /\r?\n|\r|\s/g;

Deno.test({
name: green("Testing Oak - dejsEngine"),
Expand All @@ -16,7 +17,7 @@ Deno.test({
const app = new Application();
app.use(viewEngine(oakAdapter, dejsEngine, { viewRoot: "./views/ejs" }));

app.use(async (ctx, next) => {
app.use((ctx, _) => {
ctx.render("index.ejs", { data: { name: "John" } });
});
setTimeout(async () => {
Expand Down
10 changes: 5 additions & 5 deletions lib/engines/dejs/dejs.engine.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { dejs } from "../../../deps.ts";
import type { Engine, ViewConfig } from "../../viewEngine.type.ts";

export const dejsEngine: Engine = async (
export const dejsEngine: Engine = (
template: string,
data: object = {},
config: ViewConfig = {},
filename: string = ""
_config: ViewConfig = {},
_filename = ""
): Promise<string> => {

return new Promise<string>(async (resolve, reject) => {
return new Promise<string>( (resolve, reject) => {
try{
const result = await dejs.renderToString(template, data)
const result = dejs.renderToString(template, data)
resolve(result)
}catch(e){
reject(e)
Expand Down
4 changes: 2 additions & 2 deletions lib/engines/denjuck/denjuck.engine.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @deno-types="https://deno.land/x/denjucks/mod.d.ts"
// @deno-types="https://deno.land/x/denjucks@1.1.1/mod.d.ts"
import denjucks from "https://raw.githubusercontent.com/lumeland/denjucks/v2.0.0/mod.js";
import type { Engine,ViewConfig } from "../../viewEngine.type.ts";

export const denjuckEngine: Engine = (
template: string,
data: object = {},
config: ViewConfig = {},
filename: string = "",
_filename = "",
) => {

if (config.viewRoot) {
Expand Down
9 changes: 4 additions & 5 deletions lib/engines/eta/eta.engine.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { eta } from "../../../deps.ts";
import type { Engine, ViewConfig } from "../../viewEngine.type.ts";

export const etaEngine: Engine = async (
export const etaEngine: Engine = (
template: string,
data: object = {},
config: ViewConfig = {},
filename: string = ""
_filename = ""
): Promise<string> => {

if (config.viewRoot) {
eta.configure({ views: config.viewRoot });
}

return new Promise<string>(async (resolve, reject) => {
return new Promise<string>((resolve, reject) => {
try{
const result = await eta.render( template, data) as string
const result = eta.render(template, data) as string
resolve(result)
}catch(e){
reject(e)
Expand Down
8 changes: 4 additions & 4 deletions lib/engines/handlebars/handlebars.engine.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import handlebars from "https://jspm.dev/handlebars@4.7.6";
import handlebars from "npm:handlebars";
import type { Engine,ViewConfig } from "../../viewEngine.type.ts";

export const hbs = <any> handlebars;
export const hbs = handlebars;

export const handlebarsEngine: Engine = (
template: string,
data: object = {},
config: ViewConfig = {},
filename: string = "",
_config: ViewConfig = {},
_filename = "",

) => {
return new Promise<string>((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/engines/pug/pug.engine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { path, pug } from "../../../deps.ts";
import { Engine, ViewConfig } from "../../viewEngine.type.ts";

export const pugEngine: Engine = async (
export const pugEngine: Engine = (
template: string,
data: object = {},
config: ViewConfig = {},
Expand Down
6 changes: 3 additions & 3 deletions lib/viewEngine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Adapter, Engine, ViewConfig } from "./viewEngine.type.ts";

export function viewEngine(
adapter: Adapter,
export function viewEngine<TAdapterMiddleware>(
adapter: Adapter<TAdapterMiddleware>,
engine: Engine,
config: ViewConfig = <ViewConfig>{}
): any {
): TAdapterMiddleware {
return adapter(engine, config);
}
11 changes: 9 additions & 2 deletions lib/viewEngine.type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { MiddlewareOrMiddlewareObject } from "../deps.ts";

export interface ViewConfig {
viewRoot?: string;
viewEngine?: Engine | undefined;
}

export type Adapter = (
export type AdapterMiddleware<T, TContext> = (
context: TContext,
next: () => Promise<unknown>,
) => Promise<T>;

export type Adapter<T = MiddlewareOrMiddlewareObject> = (
renderEngine: Engine,
config: ViewConfig,
) => void;
) => T;

export type Engine = (
template: string,
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ Suppose you have a folder like this:
```
```ts
// app.ts
import { Application } from "https://deno.land/x/oak@v10.5.1/mod.ts";
import { viewEngine, ejsEngine, oakAdapter } from "https://deno.land/x/view_engine@v10.5.1c/mod.ts"
import { Application } from "https://deno.land/x/oak@v13.0.0/mod.ts";
import { viewEngine, etaEngine, oakAdapter } from "https://deno.land/x/view_engine@v11.0.0/mod.ts"

const app = new Application();

Expand Down