Skip to content

Commit

Permalink
improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 1, 2019
1 parent 0466914 commit de5d4d6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 123 deletions.
21 changes: 7 additions & 14 deletions example/app.js
Expand Up @@ -4,8 +4,8 @@ const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const logger = require('morgan');
const http = require('http');
const pets = require('./pets.json');
const OpenApiValidator = require('express-openapi-validator').OpenApiValidator;
const { pets } = require('./pets');
const { OpenApiValidator } = require('express-openapi-validator');
const app = express();

// 1. Install bodyParsers for the request types your API will support
Expand All @@ -20,8 +20,6 @@ app.use(express.static(path.join(__dirname, 'public')));
const spec = path.join(__dirname, 'example.yaml');
app.use('/spec', express.static(spec));

let id = 3;

// 2. Install the OpenApiValidator on your express app
new OpenApiValidator({
apiSpec: './example.yaml',
Expand All @@ -34,22 +32,17 @@ new OpenApiValidator({
.then(() => {
// 3. Add routes
app.get('/v1/pets', function(req, res, next) {
res.json(pets);
res.json(pets.findAll(req.query));
});

app.post('/v1/pets', function(req, res, next) {
res.json({ id: id++, ...req.body });
res.json(pets.add({ ...req.body }));
});

app.get('/v1/pets/:id', function(req, res, next) {
const id = req.params.id;
const r = pets.filter(p => p.id === id);
if (id === 99) {
// return a response that does not match the spec
return res.json({ bad_format: 'bad format' });
}
return r.length > 0
? res.json(r)
const pet = pets.findById(req.params.id);
return pet
? res.json(pet)
: res.status(404).json({ message: 'not found', errors: [] });
});

Expand Down
21 changes: 8 additions & 13 deletions example/example.yaml
Expand Up @@ -60,7 +60,7 @@ paths:
$ref: '#/components/schemas/Error'

post:
description: Creates a new pet in the store. Duplicates are allowed
description: Creates a new pet in the store.
operationId: addPet
security:
- ApiKeyAuth: []
Expand All @@ -70,7 +70,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/NewPet'
$ref: '#/components/schemas/Pet'
responses:
'200':
description: pet response
Expand Down Expand Up @@ -170,27 +170,22 @@ paths:

components:
schemas:
NewPet:
Pet:
required:
- id
- name
- type
properties:
id:
readOnly: true
type: number
name:
type: string
tag:
type: string
type:
$ref: '#/components/schemas/PetType'

Pet:
allOf:
- $ref: '#/components/schemas/NewPet'
- required:
- id
properties:
id:
type: integer
format: int64

PetType:
type: string
enum:
Expand Down
41 changes: 41 additions & 0 deletions example/pets.js
@@ -0,0 +1,41 @@
const data = [
{
id: 1,
name: 'sparky',
type: 'dog',
tags: ['sweet'],
},
{
id: 2,
name: 'buzz',
type: 'cat',
tags: ['purrfect'],
},
{
id: 3,
name: 'max',
type: 'dog',
tags: [],
},
];

class Pets {
constructor() {
this.id = 4;
}
findAll({ type, limit }) {
return data.filter(d => d.type === type).slice(0, limit);
}

findById(id) {
return data.filter(p => p.id === id)[0];
}

add(pet) {
const npet = { id: this.id++, ...pet};
data.push(npet);
return npet;
}
}

exports.pets = new Pets();
20 changes: 0 additions & 20 deletions example/pets.json

This file was deleted.

76 changes: 0 additions & 76 deletions example/test.js

This file was deleted.

0 comments on commit de5d4d6

Please sign in to comment.