deligenius / view-engine

🚀A Template View Engine for Deno frameworks
MIT License
54 stars 15 forks source link

Support Oak 7.4.0 #25

Closed MisterJimson closed 2 years ago

MisterJimson commented 3 years ago

When Oak 7.4.0 is used, the render method cannot be found.

import {
  Application,
  HttpError,
  Router,
  send,
  Status,
} from "https://deno.land/x/oak@v7.4.0/mod.ts";

import {
  adapterFactory,
  engineFactory,
  viewEngine,
} from "https://deno.land/x/view_engine@v1.5.0/mod.ts";

ctx.render("index.ejs"......

Error: Property 'render' does not exist on type 'RouterContext<RouteParams, Record<string, any>>'.

renaudfractale commented 3 years ago

I have a same probleme ....

My version of Deno is v1.10.1

mtso commented 3 years ago

Adding the following in the scope where ctx.render(...) is used solved it for me:

import { ViewConfig } from "https://deno.land/x/view_engine@v1.5.0/mod.ts";

declare module "https://deno.land/x/oak@v7.5.0/mod.ts" {
  interface Context {
    render: (fileName: string, data?: object) => void;
  }

  interface RouterContext {
    render: (fileName: string, data?: object) => void;
  }

  interface Application {
    view: ViewConfig;
  }
}

...

router.get("/", async (ctx) => {
  ctx.render(...);
});

Notice specific oak version in the module string: declare module "https://deno.land/x/oak@<oak version>/mod.ts" {

Reference: https://github.com/deligenius/view-engine/blob/349d4c0c82cb0499d507b88790f9ce16452730a4/lib/adapters/oak.ts#L5-L16

reedspool commented 2 years ago

@mtso's fix worked for me for Oak v10.2.0 with the change that RouterContext now accepts a type argument that must be matched:

  interface RouterContext<R extends string> {
    render: (fileName: string, data?: object) => void;
  }
gjuoun commented 2 years ago

Please check out the newest version, thanks!