Skip to content

Commit

Permalink
multi file spec example
Browse files Browse the repository at this point in the history
  • Loading branch information
carmine.dimacsio committed Jul 14, 2020
1 parent b114070 commit b662d51
Show file tree
Hide file tree
Showing 8 changed files with 1,446 additions and 0 deletions.
57 changes: 57 additions & 0 deletions examples/6-multi-file-spec/app.js
@@ -0,0 +1,57 @@
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const logger = require('morgan');
const http = require('http');
// const { Pets } = require('./services');
const { OpenApiValidator } = require('express-openapi-validator');

const port = 3000;
const app = express();
const apiSpec = path.join(__dirname, 'ems.yaml');

// 1. Install bodyParsers for the request types your API will support
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(bodyParser.json());

app.use(logger('dev'));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/spec', express.static(apiSpec));

// 2. Install the OpenApiValidator on your express app
new OpenApiValidator({
apiSpec,
validateResponses: true,
})
.install(app)
.then(() => {
// const pets = new Pets();
// 3. Add routes
app.put('/api/v1.0/queries', function (req, res, next) {
res.status(200).json([]);
});
app.post('/api/v1.0/queries', function (req, res, next) {
res.status(201).json({});
});
app.get('/api/v1.0/queries', function (req, res, next) {
res.json([]);
});

// 4. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});

http.createServer(app).listen(port);
console.log(`Listening on port ${port}`);
});

module.exports = app;
96 changes: 96 additions & 0 deletions examples/6-multi-file-spec/ems.yaml
@@ -0,0 +1,96 @@
openapi: 3.0.0
info:
title: EMS
description: Spec for EMS API
version: 1.0.0
servers:
- url: /api/v1.0
paths:
/queries:
get:
summary: Returns all queries
operationId: getQueries
parameters:
- name: offset
in: query
required: false
description: The record index to start returning (e.g. 1 = second record)
schema:
type: integer
responses:
'200':
description: An array of query objects
content:
'application/json':
schema:
$ref: 'schemas/queries.yaml'
'500':
description: Server error
# post:
# summary: Create a new query
# operationId: createQuery
# requestBody:
# description: A JSON object describing a query
# required: true
# content:
# 'application/json':
# schema:
# $ref: 'schemas/queryrequest.yaml'
# responses:
# '201':
# description: The created query object
# content:
# 'application/json':
# schema:
# $ref: 'schemas/queryresponse.yaml'
# '404':
# description: Query not found
# '500':
# description: Server error
post:
summary: Create a new query
operationId: createQuery
requestBody:
$ref: '#/components/requestBodies/QueryBody'
responses:
'201':
description: The created query object
content:
application/json:
schema:
$ref: 'schemas/queryresponse.yaml'
'404':
description: Query not found
'500':
description: Server error
put:
summary: Update queries in bulk
operationId: updateQueryBulk
requestBody:
$ref: '#/components/requestBodies/QueriesBody'
responses:
'200':
description: An array of updated query objects
content:
application/json:
schema:
$ref: 'schemas/queries.yaml'
'500':
description: Server error

components:
requestBodies:
QueryBody:
description: A JSON object describing a query request
required: true
content:
application/json:
schema:
$ref: 'schemas/queryrequest.yaml'
QueriesBody:
description: An array of JSON objects describing query requests
required: true
content:
application/json:
schema:
$ref: 'schemas/queryrequest.yaml'

0 comments on commit b662d51

Please sign in to comment.