-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/cdimascio/express-openapi…
- Loading branch information
Showing
21 changed files
with
7,583 additions
and
367 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -60,4 +60,4 @@ See the [full documentation](https://github.com/cdimascio/express-openapi-valida | |
|
||
## License | ||
|
||
MIT | ||
MIT |
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,27 @@ | ||
# example | ||
|
||
## Install | ||
|
||
```shell | ||
npm run deps && npm i | ||
``` | ||
|
||
## Run | ||
|
||
From this `7-schema-object-mapper` directory, run: | ||
|
||
```shell | ||
npm start | ||
``` | ||
|
||
## Try | ||
|
||
```shell | ||
|
||
## call pets | ||
## the call below should return 400 since it requires additional parameters | ||
curl http://localhost:3000/v1/pets?type=dog&limit=3 | ||
|
||
## Get the first item id | ||
curl http://localhost:3000/v1/pets/<first item id> | ||
``` |
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,50 @@ | ||
openapi: '3.0.0' | ||
info: | ||
version: 1.0.0 | ||
title: Swagger Petstore schemaObjectMapper variant | ||
description: A sample API | ||
termsOfService: http://swagger.io/terms/ | ||
license: | ||
name: Apache 2.0 | ||
url: https://www.apache.org/licenses/LICENSE-2.0.html | ||
servers: | ||
- url: /v1 | ||
paths: | ||
/date-time: | ||
get: | ||
responses: | ||
200: | ||
description: date-time handler | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
created_at: | ||
type: string | ||
format: date-time | ||
id: | ||
type: number | ||
/date: | ||
get: | ||
responses: | ||
200: | ||
description: date handler | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/User' | ||
|
||
components: | ||
schemas: | ||
Date: | ||
type: string | ||
format: date | ||
User: | ||
type: object | ||
properties: | ||
id: | ||
type: number | ||
created_at: | ||
$ref: "#/components/schemas/Date" | ||
|
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,57 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const bodyParser = require('body-parser'); | ||
const http = require('http'); | ||
const OpenApiValidator = require('express-openapi-validator'); | ||
|
||
const port = 3000; | ||
const app = express(); | ||
const apiSpec = path.join(__dirname, 'api.yaml'); | ||
|
||
// 1. Install bodyParsers for the request types your API will support | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.text()); | ||
app.use(bodyParser.json()); | ||
|
||
// Optionally serve the API spec | ||
app.use('/spec', express.static(apiSpec)); | ||
|
||
// 2. Install the OpenApiValidator on your express app | ||
app.use( | ||
OpenApiValidator.middleware({ | ||
apiSpec, | ||
validateResponses: true, | ||
}), | ||
); | ||
// 3. Add routes | ||
app.get('/v1/ping', function (req, res, next) { | ||
res.send('pong'); | ||
}); | ||
|
||
app.get('/v1/date-time', function (req, res, next) { | ||
res.json({ | ||
id: 1, | ||
created_at: new Date(), | ||
}); | ||
}); | ||
|
||
app.get('/v1/date', function (req, res, next) { | ||
res.json({ | ||
id: 1, | ||
created_at: new Date(), | ||
}); | ||
}); | ||
|
||
// 4. Create a custom error handler | ||
app.use((err, req, res, next) => { | ||
// format errors | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
errors: err.errors, | ||
}); | ||
}); | ||
|
||
http.createServer(app).listen(port); | ||
console.log(`Listening on port ${port}`); | ||
|
||
module.exports = app; |
Oops, something went wrong.