在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:oaijs/koa-oai-router开源软件地址:https://github.com/oaijs/koa-oai-router开源编程语言:JavaScript 100.0%开源软件介绍:Koa-OAI-RouterI have used markdown and wiki to manage api doc, from which I have suffered a lot. It wastes too much time and is very boring. The document should be repaired when you change the api. It's very unconvenient to test and debug. The management of api doc totally depends on people. As a result, it is hard to make document have high quality. Meanwhile, the developers will spend more time on testing, which may have a bad effect on project. What's worse, it will affect our mood, which is unbearable for me : ( So I try my best to solve this problem. When there is a will, there is a way. Finally, I find The OpenAPI Specification. And its ecological circle is perfect. Swagger includes lots of tool chain. According to the Specification, Swagger UI can produce the document. The data types and models of OpenAPI are based on the JSON-Schema Draft 4. I truly hope that this library can help those who are in the same trouble. Happy coding. BTW,PR & Issue & Star are welcome! : ) Features
MigrationIf you are not a 1.x user, please skip this section directly. If you are a 1.x user and want to upgrade to version 2.0, I'm sorry that you will not upgrade to version 2.0 easily. Please read this Migration carefully and follow the operation manual to upgrade. Installation# required koa 2.x
> npm install koa-oai-router --save Getting StartedThe following will teach you how to use the router to build a web server with a good organizational structure and In this case, it basically covers all the key points of the router, including:
our target is:
Here we go. Creating web server// ./app.js
const Koa = require('koa');
const logger = require('koa-logger');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-oai-router');
const middleware = require('koa-oai-router-middleware');
const app = new Koa();
// *configure router - load api doc from directory api
const router = new Router({
apiDoc: './api',
});
// *configure plugin - identify x-oai-middleware in the api file and load the appropriate middleware from controllers
// *mount plugin to router
router.mount(middleware, './controllers');
app.use(logger());
app.use(bodyParser());
// *mount router to app
app.use(router.routes());
app.listen(3000); Creating business middlewareCreate business middleware directory // ./controllers/pets.js
const database = [];
// Query pets in the database according to tags, and limit the query result according to the limit.
async function get(ctx, next) {
const { tags = '', limit = 999 } = ctx.request.query;
const tagsArray = tags.split(',');
const docs = [];
database.forEach((item, idx) => {
if (tagsArray.indexOf(item.tag) !== -1 && docs.length < limit) {
item.id = idx + 1;
docs.push(item);
}
});
ctx.response.body = docs;
}
// Create a pet store to the database.
async function post(ctx, next) {
const body = ctx.request.body;
database.push(body);
ctx.response.body = {
id: database.length,
name: body.name,
tag: body.tag,
};
}
module.exports = {
get,
post,
}; Creating api docIf you know nothing about OpenAPI, please read OpenAPI carefully. Writing api base infoYou can describe the basic information of the service, such as the version of the service, the basic path, transmission protocol, author and permission license. # ./api/api.yaml
swagger: '2.0'
info:
version: 1.0.0
title: Swagger Petstore
description: >-
A sample API that uses a petstore as an example to demonstrate features in
the swagger-2.0 specification
termsOfService: 'http://swagger.io/terms/'
contact:
name: Swagger API Team
license:
name: MIT
basePath: /api
schemes:
- http
consumes:
- application/json
produces:
- application/json
Writing |
name | description | status |
---|---|---|
koa-oai-router-middleware | middleware loader | Done |
koa-oai-router-correction | form correction | Done |
koa-oai-router-parameters | form validator | Done |
koa-oai-router-responses | response handler | Done |
koa-oai-router-cache | request cache | Done |
koa-oai-router-rbac | request rbac | Planning |
koa-oai-router-controller-mongo | MongoDB REST server | Developing |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论