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: add /v1 prefix to the all paths and change 200 response of /generate path #35

Merged
merged 10 commits into from
Jan 18, 2022
5 changes: 3 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ paths:
'200':
description: Template succesfully generated.
content:
application/json:
application/octet-stream:
BOLT04 marked this conversation as resolved.
Show resolved Hide resolved
schema:
$ref: '#/components/schemas/Template'
type: string
format: binary
'400':
description: Failed to generate the given template due to invalid AsyncAPI document.
content:
Expand Down
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class App {

private initializeControllers(controller: Controller[]) {
controller.forEach(controller => {
this.app.use('/', controller.boot());
// in the `openapi.yaml` we have prefix `v1` for all paths
this.app.use('/v1/', controller.boot());
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/controllers/tests/generator.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('GeneratorController', () => {
const app = new App([new GenerateController()]);

return await request(app.getServer())
.post('/generate')
.post('/v1/generate')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand All @@ -33,7 +33,7 @@ describe('GeneratorController', () => {
const app = new App([new GenerateController()]);

return await request(app.getServer())
.post('/generate')
.post('/v1/generate')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand All @@ -52,7 +52,7 @@ describe('GeneratorController', () => {
const app = new App([new GenerateController()]);

return await request(app.getServer())
.post('/generate')
.post('/v1/generate')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand Down
4 changes: 3 additions & 1 deletion src/middlewares/request-body-validation.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const ajv = new Ajv({
* Retrieve proper AJV's validator function, create or reuse it.
*/
async function getValidator(req: Request) {
const { path: reqPath, method } = req;
const method = req.method;
let reqPath = req.path;
reqPath = reqPath.startsWith('/v1') ? reqPath.replace('/v1', '') : reqPath;
const schemaName = `${reqPath}->${method}`;

const validate = ajv.getSchema(schemaName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('documentValidationMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/test')
.post('/v1/test')
.send({})
.expect(200, {
success: true,
Expand All @@ -40,7 +40,7 @@ describe('documentValidationMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/test')
.post('/v1/test')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('documentValidationMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/test')
.post('/v1/test')
.send({
// without title, version and channels
asyncapi: {
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/tests/problem.middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('problemMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/test')
.post('/v1/test')
.send({})
.expect(422, {
type: ProblemException.createType('custom-problem'),
Expand All @@ -43,7 +43,7 @@ describe('problemMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/test')
.post('/v1/test')
.send({})
.expect(200, {
success: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('requestBodyValidationMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/generate')
.post('/v1/generate')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand All @@ -39,7 +39,7 @@ describe('requestBodyValidationMiddleware', () => {

it('should throw error when request body is invalid', async () => {
const TestController = createTestController({
path: '/test',
path: '/generate',
method: 'post',
callback: (_, res) => {
res.status(200).send({ success: true });
Expand All @@ -48,7 +48,7 @@ describe('requestBodyValidationMiddleware', () => {
const app = new App([new TestController()]);

return await request(app.getServer())
.post('/generate')
.post('/v1/generate')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand Down