Skip to content

Commit

Permalink
Fix example to work my feature
Browse files Browse the repository at this point in the history
  • Loading branch information
foway0 committed Jan 21, 2020
1 parent 90e699d commit 31abff9
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 36 deletions.
31 changes: 31 additions & 0 deletions example/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,34 @@ info:
servers:
- url: /v1
paths:
/ping:
get:
description: |
ping then pong!
operationId: ping
x-eov-operationId: ping#get
x-eov-controller: ping
responses:
'200':
description: OK
content:
text/plain:
schema:
type: string
example: pong
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets:
get:
description: |
Returns all pets
operationId: findPets
x-eov-operationId: pets#list
x-eov-controller: pets
parameters:
- name: type
in: query
Expand Down Expand Up @@ -62,6 +85,8 @@ paths:
post:
description: Creates a new pet in the store.
operationId: addPet
x-eov-operationId: pets#create
x-eov-controller: pets
security:
- ApiKeyAuth: []
requestBody:
Expand Down Expand Up @@ -89,6 +114,8 @@ paths:
get:
description: Returns a user based on a single ID, if the user does not have access to the pet
operationId: find pet by id
x-eov-operationId: pets#pet
x-eov-controller: pets
parameters:
- name: id
in: path
Expand All @@ -113,6 +140,8 @@ paths:
delete:
description: deletes a single pet based on the ID supplied
operationId: deletePet
x-eov-operationId: pets#delete
x-eov-controller: pets
parameters:
- name: id
in: path
Expand All @@ -135,6 +164,8 @@ paths:
post:
description: upload a photo of the pet
operationId: uploadPetPhoto
x-eov-operationId: pets#petPhotos
x-eov-controller: pets
parameters:
- name: id
in: path
Expand Down
37 changes: 2 additions & 35 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,11 @@ app.use('/spec', express.static(apiSpec));
new OpenApiValidator({
apiSpec,
validateResponses: true, // default false
controller: path.join(__dirname, 'routes'), // default false
})
.install(app)
.then(() => {
// 3. Add routes
app.get('/v1/pets', function(req, res, next) {
res.json(pets.findAll(req.query));
});

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

app.get('/v1/pets/:id', function(req, res, next) {
const pet = pets.findById(req.params.id);
return pet
? res.json(pet)
: res.status(404).json({ message: 'not found' });
});

// 3a. Add a route upload file(s)
app.post('/v1/pets/:id/photos', function(req, res, next) {
// DO something with the file
// files are found in req.files
// non file multipar params are in req.body['my-param']
console.log(req.files);

res.json({
files_metadata: req.files.map(f => ({
originalname: f.originalname,
encoding: f.encoding,
mimetype: f.mimetype,
// Buffer of file conents
// buffer: f.buffer,
})),
});
});

// 4. Create a custom error handler
// 3. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
res.status(err.status || 500).json({
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.4",
"express-openapi-validator": "^3.x",
"express-openapi-validator": "file:../",
"morgan": "^1.9.1"
},
"devDependencies": {
Expand Down
80 changes: 80 additions & 0 deletions example/routes/pets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
let 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;
}

delete(id) {
return data.filter(e => e.id !== id);
}
}

const pets = new Pets();

module.exports = {
'pets#list': (req, res) => {
return res.json(pets.findAll(req.query));
},
'pets#create': (req, res) => {
return res.json(pets.add({ ...req.body }));
},
'pets#pet': (req, res) => {
const pet = pets.findById(req.params.id);
return pet
? res.json(pet)
: res.status(404).json({ message: 'not found' });
},
'pets#petPhotos': (req, res) => {
// DO something with the file
// files are found in req.files
// non file multipar params are in req.body['my-param']
console.log(req.files);

return res.status(201).json({
files_metadata: req.files.map(f => ({
originalname: f.originalname,
encoding: f.encoding,
mimetype: f.mimetype,
// Buffer of file conents
// buffer: f.buffer,
})),
});
},
'pets#delete': (req, res) => {
data = pets.delete(req.params.id);
return res.status(204).end();
}
};
7 changes: 7 additions & 0 deletions example/routes/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const get = (req, res) => {
res.status(200).send('pong');
};

module.exports = {
'ping#get': get,
};

0 comments on commit 31abff9

Please sign in to comment.