const express = require("express");
const middleware = require("swagger-express-middleware");
const app = express();
app.get('/', wrongMethod);
exports.endpoint = function (req, res) {
const schema = {
swagger: '2.0',
info: {
version: '1.0.0',
title: 'test'
},
consumes: ['application/json'],
produces: ['application/json'],
paths: {
'/': {
post: {
parameters: [
{
name: 'body',
in: 'body',
required: true,
schema: {
type: 'array',
items: {
type: 'object',
properties: {
tour_id: {
type: 'integer',
format: 'int32'
},
date: {
type: 'string',
format: 'date'
}
}
}
}
}
],
responses: {
default: {
description: 'Purchase successful'
}
}
}
}
}
};
middleware(schema, app, (err, middleware) => {
app.use(
middleware.metadata(),
middleware.CORS(),
middleware.parseRequest()
);
app.use(displayParsedBodyProps);
app.use(errorHandler);
app(req, res);
});
};
function wrongMethod (req, res) {
res.send(
'<p>You need to send an HTTP POST, not GET</p>\n\n' +
'<a href="http://getpostman.com">Try using Postman</a>'
);
}
function displayParsedBodyProps (req, res) {
let body = 'The request was parsed successfully.\n\n';
Object.keys(req.body).forEach(key => {
let value = req.body[key];
let type = typeof value;
if (value instanceof Date) {
type = 'Date';
}
body += `The "${key}" property is a ${type} with a value of ${JSON.stringify(value)}\n`;
});
res.send(body);
}
function errorHandler (err, req, res, next) {
res.status(err.status).send(err.stack || err.message);
}