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

Migrates to AH 18 #28

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/log/*
/node_modules/
/node_modules/*
.vscode
129 changes: 64 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,25 @@ Generate Swagger-UI documentation from Actionhero

## Install & Setup

- `npm install ah-swagger-plugin --save`
- run `actionhero link --name=ah-swagger-plugin` to register the Swagger Plugin in ActionHero v13
* more information [here](http://www.actionherojs.com/docs/#including-plugins)

Then finally, so the UI can read the swagger data, ensure that the route `GET /api/swagger` is configured to point to the `swagger` action.
```js
// config/routes.js
exports.default = {
routes: function(api){
- Install this plugin: `npm install ah-swagger-plugin --save`
- Add plugin to your project's `./config/plugins.js`:
```
exports['default'] = {
plugins: (api) => {
return {
get: [
{ path: '/swagger', action: 'swagger' }
]
'ah-swagger-plugin': { path: __dirname + '/../node_modules/ah-swagger-plugin' }
}
}
};
}
```

## More information

Checkout the [Actionhero docs](http://www.actionherojs.com/docs/#plugins).
Checkout the [Actionhero docs](https://docs.actionherojs.com/tutorial-plugins.html).

For Actionhero v17 to v13, specify ah-swagger-plugin **v1.0.1** in your package.json - Documentation is captured on github with [tag v1.0.1](https://github.com/supamii/ah-swagger-plugin/tree/v1.0.1)

Stuck with Actionhero 12 and below? Go ahead and specify ah-swagger-plugin **v0.0.15** in your package.json - Documentation is captured on github with [tag v0.0.16](https://github.com/supamii/ah-swagger-plugin/tree/v0.0.16)
For Actionhero v12 and below, specify ah-swagger-plugin **v0.0.16** in your package.json - Documentation is captured on github with [tag v0.0.16](https://github.com/supamii/ah-swagger-plugin/tree/v0.0.16)

## Overview
This plugin will create an end-point that analyzes your Actionhero routes and provides JSON for swagger to consume.
Expand All @@ -42,58 +38,61 @@ For simplicity, a default index.html is provided under the ./public/swagger fold
Below is an example of how an action can be defined:

```javascript
exports.myAction = {
name: 'myAction',
summary: 'A simple summary of my action',
description: 'A detailed description of my action.',
responseSchemas: {
// By default set this 200 property to provide a sample response in the form of a JSON schema
// object. Since schemas can get pretty bulky, consider requiring a file instead of having
// everything in-line. E.g. '200': require('myResponseSchema.js')
//
// It's also possible to automate schema generation with json-schema-generator with json-patch.
'200': {
description: 'Sample http 200 response',
schema: {
type: 'object',
properties: {
'marco': {
type: 'string',
example: 'polo'
const { Action, api } = require('actionhero')

module.exports = class MyAction extends Action {
constructor () {
super()
this.name = 'myAction'
this.description = 'A detailed description of my action.'
this.responseSchemas = {
// By default set this 200 property to provide a sample response in the form of a JSON schema
// object. Since schemas can get pretty bulky, consider requiring a file instead of having
// everything in-line. E.g. '200': require('myResponseSchema.js')
//
// It's also possible to automate schema generation with json-schema-generator with json-patch.
'200': {
description: 'Sample http 200 response',
schema: {
type: 'object',
properties: {
'marco': {
type: 'string',
example: 'polo'
}
}
}
}
}
},
inputs: {
required: [ 'myParam' ],
// Each input parameter needs to be defined as a property, including input parameters for routes.
myParam: {
description: 'A detailed description of myParam',
required: true,
// Define this as an enum if you want to specify the list of possible values.
enum: [ 'value1', 'value2', 'value3' ]
this.inputs = {
required: ['myParam'],
// Each input parameter needs to be defined as a property, including input parameters for routes.
myParam: {
description: 'A detailed description of myParam',
required: true,
// Define this as an enum if you want to specify the list of possible values.
enum: ['value1', 'value2', 'value3']
}
}
},
// For post/put http requests, describing the body is set here in JSON schema form.
modelSchema: {
myParam: {
type: 'string',
example: 'value1'
},
otherData: {
type: 'string',
example: '-data1'
// For post/put http requests, describing the body is set here in JSON schema form.
this.modelSchema = {
myParam: {
type: 'string',
example: 'value1'
},
otherData: {
type: 'string',
example: '-data1'
}
}
},
// A tag will group/organize actions together
tags: [ 'Examples' ],
run: function(api, data, next) {
next();
// A tag will group/organize actions together
this.tags = ['Examples']
}
};

async run (data) { }
}
```
- You can access the `http://127.0.0.1:8080/public/swagger` and see your project's documentation:
- You can access the `http://127.0.0.1:8080/swagger` and see your project's documentation:

![alt tag](https://raw.github.com/supamii/ah-swagger-plugin/master/screenshot.png)

Expand All @@ -107,20 +106,20 @@ TODOs:

## Advanced Configuration

To override default configurations, define the namespace api.config.swagger:
A `./config/swagger.js` file will be created which will store your configuration.

```javascript
exports['default'] = {
swagger: function(api){
exports.default = {
swagger: (api) => {
return {
// Should be changed to hit www.yourserver.com. If this is null, defaults to ip:port from
// internal values or from hostOverride and portOverride.
baseUrl: '127.0.0.1:8080',
// Specify routes that don't need to be displayed
ignoreRoutes: [ '/swagger' ],
ignoreRoutes: ['/swagger'],
// Specify how routes are grouped
routeTags : {
'basics' : [ 'showDocumentation', 'status' ]
routeTags: {
'basics': ['showDocumentation', 'status']
},
// Generate documentation for simple actions specified by action-name
documentSimpleRoutes: true,
Expand Down
5 changes: 5 additions & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
## Version 0.0.8 (05-Jul-2015)
* Various updates, mostly documentation
* Swagger v2.1.5-M2

## Version 2.0.0 (08-Jan-2018)
* Migrated to Actionhero v18
* Swagger v2.1.5-M2

44 changes: 16 additions & 28 deletions actions/swagger.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
exports.swagger = {
name: 'swagger',
description: 'Returns Swagger JSON',
summary: 'Swagger Data',
responseSchemas: {
'200': {
description: 'Sample response',
schema: {
type: 'object',
properties: {
'swagger': {
type: 'string',
example: 'Swagger 2.0'
}
}
const { Action, api } = require('actionhero')

module.exports = class Swagger extends Action {
constructor () {
super()
this.name = 'swagger'
this.description = 'Returns Swagger JSON'
this.inputs = {
secure: {
required: false
}
}
},
inputs: {
secure: {
required: false
}
},
run: function(api, data, next) {
// Built by the swagger initializer.
data.response = api.swagger.documentation;
}

async run (data) {
data.response = api.swagger.documentation

// Allows us to toggle on the swagger docs an support http vs https.
if (data.params.secure && (data.params.secure === true || data.params.secure === 'true')) {
data.response.schemes = [ 'https' ];
data.response.schemes = ['https']
}
next();
}
};
}
10 changes: 5 additions & 5 deletions config/swagger.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
exports['default'] = {
swagger: function(api){
exports.default = {
swagger: (api) => {
return {
// Should be changed to hit www.yourserver.com. If this is null, defaults to ip:port from
// internal values or from hostOverride and portOverride.
baseUrl: '127.0.0.1:8080',
// Specify routes that don't need to be displayed
ignoreRoutes: [ '/swagger' ],
ignoreRoutes: ['/swagger'],
// Specify how routes are grouped
routeTags : {
'basics' : [ 'showDocumentation', 'status' ]
routeTags: {
'basics': ['showDocumentation', 'status']
},
// Generate documentation for simple actions specified by action-name
documentSimpleRoutes: true,
Expand Down
Loading