-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from yldrmzffr/middleware
feat(middleware): add support for function-based middleware
- Loading branch information
Showing
9 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type Middleware = ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
) => PropertyDescriptor; | ||
|
||
export type MiddlewareDecorator = (...middlewares: Function[]) => Middleware; | ||
export class MiddlewareFactory { | ||
Middleware = (...middlewares: Function[]): Middleware => { | ||
return ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
) => { | ||
Reflect.defineMetadata('middlewares', middlewares, target[propertyKey]); | ||
return descriptor; | ||
}; | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import {MuzuServer, Request, HttpException} from '../lib'; | ||
import * as request from 'supertest'; | ||
|
||
const muzuServer = new MuzuServer(); | ||
const {Controller, Get, Middleware} = muzuServer; | ||
|
||
function Logger() { | ||
console.log('Logger Middleware'); | ||
} | ||
|
||
function Auth(req: Request) { | ||
req.user = {name: 'Muzu User'}; | ||
} | ||
|
||
function AddId(req: Request) { | ||
req.userId = '2342'; | ||
} | ||
|
||
function Admin() { | ||
throw new HttpException(403, 'Unauthorized'); | ||
} | ||
|
||
async function AsyncLogger(): Promise<void> { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
console.log('Async Logger Middleware'); | ||
resolve(); | ||
}, 300); | ||
}); | ||
} | ||
|
||
@Controller('/api') | ||
class TestController { | ||
@Get('/hello') | ||
@Middleware(Logger) | ||
hello() { | ||
return {message: 'Get Method Called'}; | ||
} | ||
|
||
@Get('/hello-auth') | ||
@Middleware(Auth) | ||
helloAuth(req: Request) { | ||
return {message: `Hello ${req.user.name}`}; | ||
} | ||
|
||
@Get('/hello-admin') | ||
@Middleware(Admin) | ||
helloAdmin() { | ||
return {message: 'Hello Admin'}; | ||
} | ||
|
||
@Get('/hello-auth-id') | ||
@Middleware(Auth, AddId) | ||
helloAuthId(req: Request) { | ||
const {user, userId} = req; | ||
|
||
return {message: `Hello ${user.name}`, userId}; | ||
} | ||
|
||
@Get('/hello-async-logger') | ||
@Middleware(AsyncLogger) | ||
helloAsyncLogger() { | ||
return {message: 'Hello Async Logger'}; | ||
} | ||
} | ||
|
||
const port = 3001; | ||
muzuServer.listen(port); | ||
|
||
describe('MuzuServer', () => { | ||
it('should return 200 on GET /api/hello', async () => { | ||
const res = await request(muzuServer.server).get('/api/hello'); | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual( | ||
expect.objectContaining({ | ||
message: 'Get Method Called', | ||
}) | ||
); | ||
}); | ||
|
||
it('should return 200 on GET /api/hello-auth', async () => { | ||
const res = await request(muzuServer.server).get('/api/hello-auth'); | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual( | ||
expect.objectContaining({ | ||
message: 'Hello Muzu User', | ||
}) | ||
); | ||
}); | ||
|
||
it('should return 403 on GET /api/hello-admin', async () => { | ||
const res = await request(muzuServer.server).get('/api/hello-admin'); | ||
expect(res.status).toBe(403); | ||
expect(res.body).toEqual( | ||
expect.objectContaining({ | ||
status: 403, | ||
message: 'Unauthorized', | ||
}) | ||
); | ||
}); | ||
|
||
it('should return 200 on GET /api/hello-auth-id', async () => { | ||
const res = await request(muzuServer.server).get('/api/hello-auth-id'); | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual( | ||
expect.objectContaining({ | ||
userId: '2342', | ||
message: 'Hello Muzu User', | ||
}) | ||
); | ||
}); | ||
|
||
it('should return 200 on GET /api/hello-async-logger', async () => { | ||
const res = await request(muzuServer.server).get('/api/hello-async-logger'); | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual( | ||
expect.objectContaining({ | ||
message: 'Hello Async Logger', | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
muzuServer.stop(() => { | ||
console.log('Server stopped'); | ||
}); |