Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Render decorator for Koa Driver #1482

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,10 @@ getOne() {
```

To use rendering ability make sure to configure express / koa properly.
To use rendering ability with Koa you will need to use a rendering 3rd party such as [koa-views](https://github.com/queckezz/koa-views/),
koa-views is the only render middleware that has been tested.
To use rendering ability with Koa you will need to use a rendering 3rd party such as [@koa/ejs](https://github.com/koajs/ejs),
@koa/ejs is the only render middleware that has been tested.

See [the koa render test file](./test/functional/koa-render-decorator.spec.ts) as an example.

#### Throw HTTP errors

Expand Down
99 changes: 99 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.15.0",
"@koa/cors": "^5.0.0",
"@koa/ejs": "^5.1.0",
"@types/express": "^5.0.0",
"@types/express-session": "^1.18.0",
"@types/jest": "^29.5.13",
"@types/koa": "^2.15.0",
"@types/koa__ejs": "^5.1.0",
"@types/multer": "^1.4.12",
"@types/node": "^16.18.3",
"@types/serve-static": "^1.15.7",
Expand Down
9 changes: 4 additions & 5 deletions src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,11 @@ export class KoaDriver extends BaseDriver {
options.response.redirect(action.redirect);
}
} else if (action.renderedTemplate) {
// if template is set then render it // TODO: not working in koa
// if template is set then render it
const renderOptions = result && result instanceof Object ? result : {};

this.koa.use(async function (ctx: any, next: any) {
await ctx.render(action.renderedTemplate, renderOptions);
});
const ctxLocals = options.context.locals || {};
const oldNext = options.next;
options.next = () => options.context.render(action.renderedTemplate, {...ctxLocals, ...renderOptions}).then(oldNext);
} else if (result === undefined) {
// throw NotFoundError on undefined response
if (action.undefinedResultCode instanceof Function) {
Expand Down
84 changes: 84 additions & 0 deletions test/functional/koa-render-decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Render } from '../../src/decorator/Render';
import { Server as HttpServer } from 'http';
import HttpStatusCodes from 'http-status-codes';
import Koa from "koa";
import { Controller } from '../../src/decorator/Controller';
import { Get } from '../../src/decorator/Get';
import { createKoaServer, getMetadataArgsStorage, Ctx } from '../../src/index';
import { axios } from '../utilities/axios';
import koaEjs from "@koa/ejs";
import path from "path";
import DoneCallback = jest.DoneCallback;

describe(``, () => {
let koaServer: HttpServer;

describe('koa template rendering', () => {
beforeAll((done: DoneCallback) => {
getMetadataArgsStorage().reset();

@Controller()
class RenderController {
@Get('/index')
@Render('ejs-render-test-spec')
index(): any {
return {
name: 'Routing-controllers',
};
}

@Get('/locals')
@Render('ejs-render-test-locals-spec')
locals(@Ctx() ctx: any): any {
ctx.locals = {
myVariable: 'my-variable'
};

return {
name: 'Routing-controllers',
};
}
}

const resourcePath: string = path.resolve(__dirname, '../resources');

const koaApp = createKoaServer() as Koa;
koaEjs(koaApp, {
root: resourcePath,
layout: false,
viewExt: "html", // Auto-appended to template name
cache: false,
debug: true,
});

koaServer = koaApp.listen(3001, done);
});

afterAll((done: DoneCallback) => {
koaServer.close(done);
});

it('should render a template and use given variables', async () => {
expect.assertions(6);
const response = await axios.get('/index');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.data).toContain('<html>');
expect(response.data).toContain('<body>');
expect(response.data).toContain('Routing-controllers');
expect(response.data).toContain('</body>');
expect(response.data).toContain('</html>');
});

it('should render a template with given variables and locals variables', async () => {
expect.assertions(7);
const response = await axios.get('/locals');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.data).toContain('<html>');
expect(response.data).toContain('<body>');
expect(response.data).toContain('Routing-controllers');
expect(response.data).toContain('my-variable');
expect(response.data).toContain('</body>');
expect(response.data).toContain('</html>');
});
});
});
8 changes: 8 additions & 0 deletions test/resources/ejs-render-test-locals-spec.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>

<body>
<%= name %>
<%= myVariable %>
</body>

</html>
5 changes: 5 additions & 0 deletions test/resources/ejs-render-test-spec.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<%= name %>
</body>
</html>