Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tuckerconnelly/micro-open-api
Add OpenAPI schemas and validation to your Node.js micro server
https://github.com/tuckerconnelly/micro-open-api
Last synced: 23 days ago
JSON representation
Add OpenAPI schemas and validation to your Node.js micro server
- Host: GitHub
- URL: https://github.com/tuckerconnelly/micro-open-api
- Owner: tuckerconnelly
- License: mit
- Created: 2019-08-08T23:55:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:57:09.000Z (almost 2 years ago)
- Last Synced: 2024-08-10T21:45:03.483Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 855 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
***Early Version***
# micro-open-api
Add OpenAPI schemas and validation to your [micro](https://github.com/zeit/micro) server.
## Features
* Automatic parameter validation
* Automatic DRY documentation for your api
* Built on web standards## Usage
```
npm i micro-open-api
```Then where you define your HTTP server:
```js
const path = require('path');
const micro = require('micro');
const microOpenApi = require('micro-open-api');module.exports = () =>
micro(
microOpenApi(
`
openapi: "3.0.0"
info:
version: 1.0.0
`,
path.join(__dirname, './modules')
)((req, res) => micro.send(res, 404))
);
```Then in `modules/test-endpoint.js` add:
```js
exports.schema = `
paths:
/test-endpoint:
post:
operationId: testEndpoint
requestBody:
required: true
content:
application/json:
schema:
myRequiredParam: [required]
properties:
required:
type: string
`// Must match operationId above
exports.testEndpoint = async () => {
return { ok: true };
};
```And then you can enjoy free request-body and parameter validation!
```
> http -b POST localhost:3000/test-endpoint{"errors":{"myRequiredParam":"My required param is required."}}
```
You also get free documentation by pointing a [Swagger UI](https://swagger.io/tools/swagger-ui/) instance at `/open-api`.
## Other notes
You can point `micro-open-api` at any folder.
You can also define multiple endpoint paths if you like in any file--it'll just merge any `exports.schema` values that it finds.
---
> Whatever you do, work at it with all your heart, as working for the Lord, not for human masters, since you know that you will receive an inheritance from the Lord as a reward.
*Colossians 3:23*