Skip to content

Commit

Permalink
🐇 Altera api mock para usar openapi. Issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
huogerac committed Oct 19, 2021
1 parent df8f755 commit afb575b
Show file tree
Hide file tree
Showing 12 changed files with 18,500 additions and 164 deletions.
20 changes: 0 additions & 20 deletions ApiMock/db.json

This file was deleted.

3 changes: 0 additions & 3 deletions ApiMock/server/config.js

This file was deleted.

19 changes: 0 additions & 19 deletions ApiMock/server/index.js

This file was deleted.

5 changes: 5 additions & 0 deletions api/mock/server/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
PORT: process.env.API_PORT || 5001,
ORIGIN_URL: process.env.ORIGIN_URL || '*',
OPENAPI_SPEC: 'api/mock/server/openapi.yaml',
}
8 changes: 8 additions & 0 deletions api/mock/server/controllers/news.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const data = require('../data')

module.exports = {
listNews: (req, res) => {
const result = data.news
res.send({ result })
},
}
5 changes: 5 additions & 0 deletions api/mock/server/data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const datahelper = require('../datahelper')

module.exports = {
news: datahelper.parseJson('./data/news.json'),
}
18 changes: 18 additions & 0 deletions api/mock/server/data/news.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"id": 1,
"title": "Confraria DevPro faz setup de projeto VueJS",
"author": "huogerac",
"reactions": 42,
"comments": 42,
"date": "2021-10-05 21:42:26"
},
{
"id": 2,
"title": "Image Sharing. No Bullshit",
"author": "vyrotek",
"reactions": 149,
"comments": 112,
"date": "2021-10-05 21:32:23"
}
]
10 changes: 10 additions & 0 deletions api/mock/server/datahelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs');
const path = require('path');

module.exports = {
parseJson: (jsonPath) => {
return JSON.parse(
fs.readFileSync(path.resolve(__dirname, jsonPath), 'utf8')
);
}
};
43 changes: 43 additions & 0 deletions api/mock/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const express = require('express')
const swaggerUi = require('swagger-ui-express')
const YAML = require('yamljs')
const cors = require('cors')
const logger = require('morgan')
const bodyParser = require('body-parser')
const { OpenApiValidator } = require('express-openapi-validator')

const config = require('./config')
const swaggerDocument = YAML.load(config.OPENAPI_SPEC)

const news = require('./controllers/news')

const YELLOW = '\x1b[33m%s\x1b[0m'
const WHITE = '\x1b[37m'

const app = express()

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.text())
app.use(bodyParser.json())

app.use(logger('dev'))
app.use(cors({ credentials: true, origin: config.ORIGIN_URL }))
app.use('/api/docs/', swaggerUi.serve, swaggerUi.setup(swaggerDocument))

new OpenApiValidator({
apiSpec: config.OPENAPI_SPEC,
validateRequests: true,
validateResponses: false,
})
.install(app)
.then(() => {
app.get('/api/news', news.listNews)

http: app.listen(config.PORT, () => {
console.log(
YELLOW,
'🆙 JSON Server is running on port: ' + config.PORT,
WHITE
)
})
})
43 changes: 43 additions & 0 deletions api/mock/server/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
openapi: 3.0.2

info:
version: 0.0.1
title: Hackernews Mock API
description: Mock API

paths:
/api/news:
get:
operationId: hackernews.api.news.list_news
summary: Returns the latest news
tags:
- News

responses:
200:
description: The lastest news
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/News'

components:
schemas:
News:
type: object
properties:
id:
type: integer
example: 42
title:
type: string
example: Python is a great option for backend and APIs
description:
type: string
nullable: true
created_at:
type: string
format: date-time
example: 2021-01-11T11:32:28Z
Loading

0 comments on commit afb575b

Please sign in to comment.