Skip to content

Commit

Permalink
fix: add /v1 prefix to the all paths and change 200 response of /gene…
Browse files Browse the repository at this point in the history
…rate path (#35)
  • Loading branch information
magicmatatjahu authored Jan 18, 2022
1 parent af9e609 commit 98573a4
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 18 deletions.
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 successfully generated.
content:
application/json:
application/zip:
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 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 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 request(app.getServer())
.post('/generate')
.post('/v1/generate')
.send({
asyncapi: {
asyncapi: '2.2.0',
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/tests/validate.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('ValidateController', () => {
const app = new App([new ValidateController()]);

return request(app.getServer())
.post('/validate')
.post('/v1/validate')
.send({
asyncapi: validJSONAsyncAPI
})
Expand All @@ -93,7 +93,7 @@ describe('ValidateController', () => {
const app = new App([new ValidateController()]);

return request(app.getServer())
.post('/validate')
.post('/v1/validate')
.send({
asyncapi: validYAMLAsyncAPI
})
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('ValidateController', () => {
const app = new App([new ValidateController()]);

return request(app.getServer())
.post('/validate')
.post('/v1/validate')
.send({
asyncapi: invalidJSONAsyncAPI
})
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
6 changes: 3 additions & 3 deletions src/middlewares/tests/document-validation.middleware.test.ts
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

0 comments on commit 98573a4

Please sign in to comment.