Skip to content

Commit 5573afc

Browse files
authored
feat(http): Allow registering global middleware (#71)
* chore: add global code owner for repository * chore(app): extract `createRouter()` method * feat(app): add support for global middleware registration * test(app): use type assertion * chore(test): fix broken test * chore: bump version to 2.2.0
1 parent ff1f4cf commit 5573afc

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Global code owners
2+
* @imdhemy

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@koala-ts/framework",
33
"main": "dist/index.js",
44
"type": "module",
5-
"version": "2.1.2",
5+
"version": "2.2.0",
66
"description": "A NodeJS framework for lazy developers",
77
"keywords": [
88
"koala",

src/Application/ApplicationFactory.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Koa from 'koa';
22
import { beforeEach, describe, expect, test, vi } from 'vitest';
33
import { create } from '@/Application/ApplicationFactory';
4-
import { koalaDefaultConfig } from '@/Config';
4+
import { KoalaConfig, koalaDefaultConfig } from '@/Config';
55
import type { HttpScope, NextMiddleware, UploadedFile } from '@/Http';
66
import { Route } from '@/Routing';
77
import { createTestAgent, TestAgent } from '@/Testing';
@@ -136,3 +136,18 @@ describe('Application', () => {
136136
expect(response.text).toBe('avatar.png');
137137
});
138138
});
139+
140+
describe('Global Middleware', () => {
141+
test('it should register configured global middleware', async () => {
142+
const middlewareFn = vi.fn();
143+
const config = {
144+
controllers: [],
145+
globalMiddleware: [middlewareFn],
146+
};
147+
const testAgent = createTestAgent(config as KoalaConfig);
148+
149+
await testAgent.get('/');
150+
151+
expect(middlewareFn).toHaveBeenCalled();
152+
});
153+
});

src/Application/ApplicationFactory.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ import { koaBody } from 'koa-body';
44
import { type Application } from './types';
55
import { extendResponse } from '@/Application/Response';
66
import { type KoalaConfig } from '@/Config';
7-
import { type HttpScope } from '@/Http';
7+
import { type HttpMiddleware, type HttpScope } from '@/Http';
88
import { getRoutes } from '@/Routing';
99

10-
export function create(_: KoalaConfig): Application {
10+
export function create(config: KoalaConfig): Application {
1111
const app = new Koa() as Application;
1212
app.scope = app.context;
1313

14+
app.use(extendResponse);
15+
if (undefined !== config.globalMiddleware) registerGlobalMiddleware(app, config.globalMiddleware);
16+
app.use(createRouter().routes());
17+
18+
return app;
19+
}
20+
21+
function createRouter(): Router {
1422
const router = new Router();
1523

1624
for (const route of getRoutes()) {
@@ -22,8 +30,11 @@ export function create(_: KoalaConfig): Application {
2230
}
2331
}
2432

25-
app.use(extendResponse);
26-
app.use(router.routes());
33+
return router;
34+
}
2735

28-
return app;
36+
function registerGlobalMiddleware(app: Application, middleware: HttpMiddleware[]): void {
37+
for (const mw of middleware) {
38+
app.use(mw);
39+
}
2940
}

src/Config/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { type HttpMiddleware } from '@/Http';
2+
13
export type Controller = new(...args: unknown[]) => unknown;
24

35
export interface KoalaConfig {
46
controllers: Controller[];
7+
globalMiddleware?: HttpMiddleware[];
58
}

0 commit comments

Comments
 (0)