Skip to content

Commit

Permalink
Merge pull request #264 from daniloab/feat/openapi-channels
Browse files Browse the repository at this point in the history
feat(openapi): add support for channels
  • Loading branch information
daniloab authored Aug 26, 2021
2 parents 54e887a + 3830c42 commit e1b8902
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/eventDriven/openapiConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
info: {
title: 'User Event API',
version: '1.0.0',
},
description: ' User Event API Specification',
openapi: '3.0.0',
};
52 changes: 52 additions & 0 deletions examples/eventDriven/src/customSpec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"info": {
"title": "User Event API",
"version": "1.0.0"
},
"description": "User Event API Specification",
"openapi": "3.0.0",
"paths": {},
"components": {
"messages": {
"UserSignedIn": {
"payload": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Id of the user"
}
}
}
},
"UserSignedUp": {
"payload": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Id of the user"
}
}
}
}
}
},
"channels": {
"user/signedin": {
"subscribe": {
"message": {
"$ref": "#/components/messages/UserSignedIn"
}
}
},
"user/signedup": {
"subscribe": {
"message": {
"$ref": "#/components/messages/UserSignedUp"
}
}
}
},
"tags": []
}
32 changes: 32 additions & 0 deletions examples/eventDriven/src/customSpec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
info:
title: User Event API
version: 1.0.0
description: 'User Event API Specification'
openapi: 3.0.0
paths: {}
components:
messages:
UserSignedIn:
payload:
type: object
properties:
id:
type: string
description: Id of the user
UserSignedUp:
payload:
type: object
properties:
id:
type: string
description: Id of the user
channels:
user/signedin:
subscribe:
message:
$ref: '#/components/messages/UserSignedIn'
user/signedup:
subscribe:
message:
$ref: '#/components/messages/UserSignedUp'
tags: []
6 changes: 6 additions & 0 deletions examples/eventDriven/src/modules/userSignIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ou usersignin event it will be placed here
export const userSignIn = (job: Job<{ userId: string }>) => {
const { userId } = job.data;

console.log("User Logged", userId);
};
14 changes: 14 additions & 0 deletions examples/eventDriven/src/modules/userSignIn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
channels:
user/signedin:
subscribe:
message:
$ref: '#/components/messages/UserSignedIn'
components:
messages:
UserSignedIn:
payload:
type: object
properties:
id:
type: string
description: Id of the user
5 changes: 5 additions & 0 deletions examples/eventDriven/src/modules/userSignUp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const userSignUp = (job: Job<{ userId: string }>) => {
const { userId } = job.data;

console.log("User Registered", userId);
};
14 changes: 14 additions & 0 deletions examples/eventDriven/src/modules/userSignUp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
channels:
user/signedup:
subscribe:
message:
$ref: '#/components/messages/UserSignedUp'
components:
messages:
UserSignedUp:
payload:
type: object
properties:
id:
type: string
description: Id of the user
2 changes: 2 additions & 0 deletions src/specification.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function prepare(definition) {
'parameters',
'securityDefinitions',
'components',
'channels',
],
};

Expand Down Expand Up @@ -136,6 +137,7 @@ function organize(swaggerObject, annotation, property) {
'responses',
'parameters',
'definitions',
'channels',
];
if (commonProperties.includes(property)) {
for (const definition of Object.keys(annotation[property])) {
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/cli.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Options:
"
`;
exports[`CLI module should generate json final file from separated files 1`] = `
"Swagger specification is ready.
"
`;
exports[`CLI module should generate yml final file from separated files 1`] = `
"Swagger specification is ready.
"
`;
exports[`CLI module should reject definition file with invalid JSON syntax 1`] = `
"Error while loading definition file 'test/files/v2/wrong_syntax.json':
Unexpected token t in JSON at position 18
Expand Down
16 changes: 16 additions & 0 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ describe('CLI module', () => {
expect(result.stderr).toMatchSnapshot();
});

it('should generate json final file from separated files', async () => {
const result = await sh(
`${bin} -d examples/eventDriven/openapiConfig.js examples/eventDriven/src/modules/**/*.yml -o examples/eventDriven/src/customSpec.json`
);

expect(result.stdout).toMatchSnapshot();
});

it('should generate yml final file from separated files', async () => {
const result = await sh(
`${bin} -d examples/eventDriven/openapiConfig.js examples/eventDriven/src/modules/**/*.yml -o examples/eventDriven/src/customSpec.yml`
);

expect(result.stdout).toMatchSnapshot();
});

afterAll(() => {
fs.unlinkSync(`${dir}/swagger.json`);
fs.unlinkSync(`${dir}/customSpec.json`);
Expand Down

0 comments on commit e1b8902

Please sign in to comment.