Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

Commit

Permalink
Allow for swagger document to be passed in custom.documentation (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
linead authored and tchock committed Apr 8, 2019
1 parent 0450b58 commit 78a81be
Show file tree
Hide file tree
Showing 3 changed files with 823 additions and 1 deletion.
87 changes: 86 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const documentation = require('./documentation');
const models = require('./models');
const swagger = require('./swagger');
const fs = require('fs');
const downloadDocumentation = require('./downloadDocumentation');

Expand All @@ -12,6 +13,7 @@ class ServerlessAWSDocumentation {
this.fs = fs;

Object.assign(this, models);
Object.assign(this, swagger);
Object.assign(this, documentation());
Object.assign(this, downloadDocumentation);

Expand Down Expand Up @@ -52,9 +54,92 @@ class ServerlessAWSDocumentation {

beforeDeploy() {
this.customVars = this.serverless.variables.service.custom;

if (!(this.customVars && this.customVars.documentation)) return;

if (this.customVars.documentation.swagger) {
// Handle references to models
this.replaceSwaggerDefinitions(this.customVars.documentation.definitions)
//Map swagger into documentation models
const swaggerDefs = this.customVars.documentation.definitions
if (swaggerDefs) {
const swaggerModels = Object.keys(swaggerDefs).map(definitionName => {
return {
name: definitionName,
description: swaggerDefs[definitionName].description,
contentType: 'application/json',
schema: swaggerDefs[definitionName]
}
})
this.customVars.documentation.models = swaggerModels
} else {
this.customVars.documentation.models = []
}

//Find http events and map the swagger across
this.serverless.service.getAllFunctions().forEach(functionName => {
const func = this.serverless.service.getFunction(functionName)
if (func.events) {
func.events.forEach(event => {
if (event.http) {
// look up the path in the swagger
const path = this.customVars.documentation.paths['/' + event.http.path]
if (path) {
const method = path[event.http.method]
const methodDoc = {'requestHeaders': [], 'pathParams': [], 'queryParams': [],
'requestModels': {}}
if ( method.parameters ) {
method.parameters.forEach(param => {
if (param.in === 'header') {
methodDoc['requestHeaders'].push({
name: param.name,
description: param.description,
required: param.required
})
} else if (param.in === 'path') {
methodDoc['pathParams'].push({
name: param.name,
description: param.description,
required: param.required
})
} else if (param.in === 'query') {
methodDoc['queryParams'].push({
name: param.name,
description: param.description,
required: param.required
})
} else if (param.in === 'body') {
methodDoc['requestModels']['application/json'] =
this.extractModel(param, this.customVars.documentation.models);
}
})
}

if ( method.responses ) {
methodDoc['methodResponses'] = []
Object.keys(method.responses).map(statusCode => {
const response = method.responses[statusCode];
const methodResponse = {
statusCode: ""+statusCode,
};

if ( response.schema ) {
const responseModels = {};
responseModels['application/json'] =
this.extractModel(response, this.customVars.documentation.models);
methodResponse['responseModels'] = responseModels;
}
methodDoc['methodResponses'].push(methodResponse);
});
}

event.http.documentation = methodDoc
}
}
})
}
})
}

this.cfTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;

// The default rest API reference
Expand Down
Loading

0 comments on commit 78a81be

Please sign in to comment.