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
14 changes: 11 additions & 3 deletions lib/createHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { parseRequestUrl } from './internals/parseRequestUrl';
* Prepares a router for the given class.
*
* @param cls Router class
* @param builder Router instance builder
*
* @example
* ```ts
Expand All @@ -24,15 +25,22 @@ import { parseRequestUrl } from './internals/parseRequestUrl';
* export default createHandler(Events);
* ```
*/
export function createHandler(cls: new (...args: any[]) => any): NextApiHandler {
const instance = new cls();
export function createHandler<T extends any>(
cls: new (...args: any[]) => T,
builder: () => T | Promise<T> = () => new cls()
): NextApiHandler {
let instance: any;
const [directory, fileName] = getCallerInfo();

return (req: NextApiRequest, res: NextApiResponse) => {
return async (req: NextApiRequest, res: NextApiResponse) => {
if (!req.url || !req.method) {
return notFound(req, res);
}

if (!instance) {
instance = await builder();
}

const path = parseRequestUrl(req, directory, fileName);
const [keys, match, method] = findRoute(cls, req.method, path);
if (!method) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"express": "^4.17.1",
"express-rate-limit": "^6.0.4",
"husky": "8.0.3",
"inversify": "5.1.1",
"jest": "26.6.3",
"lint-staged": "13.1.0",
"multer": "^1.4.2",
Expand Down
56 changes: 56 additions & 0 deletions test/e2e-ioc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'reflect-metadata';
import { Container, injectable } from 'inversify';
import request from 'supertest';
import { createHandler, Get } from '../lib';
import { setupServer } from './setupServer';

// Create service
const container = new Container({
autoBindInjectable: true
});

interface TestData {
test: string;
}

@injectable()
export class TestService {
public getTest(): TestData {
return {
test: 'successful'
};
}
}

// Create handler with injection
@injectable()
class TestHandler {
public constructor(private readonly service: TestService) {}

@Get()
public testData() {
return this.service.getTest();
}
}

describe('E2E - ioc', () => {
let server: ReturnType<typeof setupServer>;
beforeAll(() => (server = setupServer(createHandler(TestHandler, () => container.get(TestHandler)))));
afterAll(() => {
if ('close' in server && typeof server.close === 'function') {
server.close();
}
});

it('Should successfully return data from service', () =>
request(server)
.get('/')
.expect(200)
.then(res =>
expect(res).toMatchObject({
body: {
test: 'successful'
}
})
));
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4264,6 +4264,11 @@ into-stream@^6.0.0:
from2 "^2.3.0"
p-is-promise "^3.0.0"

[email protected]:
version "5.1.1"
resolved "https://registry.yarnpkg.com/inversify/-/inversify-5.1.1.tgz#6fbd668c591337404e005a1946bfe0d802c08730"
integrity sha512-j8grHGDzv1v+8T1sAQ+3boTCntFPfvxLCkNcxB1J8qA0lUN+fAlSyYd+RXKvaPRL4AGyPxViutBEJHNXOyUdFQ==

ip-regex@^4.1.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-4.3.0.tgz#687275ab0f57fa76978ff8f4dddc8a23d5990db5"
Expand Down