-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐇 Altera api mock para usar openapi. Issue #23
- Loading branch information
Showing
12 changed files
with
18,500 additions
and
164 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.