From 31abff967c0a8b85c0ba51d5257bed831c403c58 Mon Sep 17 00:00:00 2001 From: foway Date: Wed, 22 Jan 2020 00:23:07 +0900 Subject: [PATCH] Fix example to work my feature --- example/api.yaml | 31 ++++++++++++++++ example/app.js | 37 ++----------------- example/package.json | 2 +- example/routes/pets.js | 80 ++++++++++++++++++++++++++++++++++++++++++ example/routes/ping.js | 7 ++++ 5 files changed, 121 insertions(+), 36 deletions(-) create mode 100644 example/routes/pets.js create mode 100644 example/routes/ping.js diff --git a/example/api.yaml b/example/api.yaml index 84b53a27..e5c92022 100644 --- a/example/api.yaml +++ b/example/api.yaml @@ -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 @@ -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: @@ -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 @@ -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 @@ -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 diff --git a/example/app.js b/example/app.js index fc527912..7e88a823 100644 --- a/example/app.js +++ b/example/app.js @@ -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({ diff --git a/example/package.json b/example/package.json index 1e50d6fe..37ef4557 100644 --- a/example/package.json +++ b/example/package.json @@ -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": { diff --git a/example/routes/pets.js b/example/routes/pets.js new file mode 100644 index 00000000..1cda3c02 --- /dev/null +++ b/example/routes/pets.js @@ -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(); + } +}; \ No newline at end of file diff --git a/example/routes/ping.js b/example/routes/ping.js new file mode 100644 index 00000000..9431b972 --- /dev/null +++ b/example/routes/ping.js @@ -0,0 +1,7 @@ +const get = (req, res) => { + res.status(200).send('pong'); +}; + +module.exports = { + 'ping#get': get, +}; \ No newline at end of file