oaijs / koa-oai-router Goto Github PK
View Code? Open in Web Editor NEWKoa Router, based on OpenAPI, Swagger and Json Schema.
Koa Router, based on OpenAPI, Swagger and Json Schema.
what I did:
const routerV0 = new Router({
apiDoc: path.join(__dirname, 'api-v0'),
apiExplorerVisible: true,
})
routerV0.mount(middleware, path.join(__dirname, 'controllers-v0'))
app.use(routerV0.routes())
const routerDash = new Router({
apiDoc: path.join(__dirname, 'api-dash'),
apiExplorerVisible: true,
})
routerDash.mount(middleware, path.join(__dirname, 'controllers-dash'))
app.use(routerDash.routes())
what I got:
$ curl http://localhost:8081/api-explorer
{"result":"<!-- HTML for static distribution bundle build -->\n<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title>Swagger UI</title>\n <link rel=\"stylesheet\" type=\"text/css\" href=\"./swagger-ui.css\" >\n <link rel=\"icon\" type=\"image/png\" href=\"./favicon-32x32.png\" sizes=\"32x32\" />\n <link rel=\"icon\" type=\"image/png\" href=\"./favicon-16x16.png\" sizes=\"16x16\" />\n <style>\n html\n {\n box-sizing: border-box;\n overflow: -moz-scrollbars-vertical;\n overflow-y: scroll;\n }\n\n *,\n *:before,\n *:after\n {\n box-sizing: inherit;\n }\n\n body\n {\n margin:0;\n background: #fafafa;\n }\n </style>\n </head>\n\n <body>\n <div id=\"swagger-ui\"></div>\n\n <script src=\"./swagger-ui-bundle.js\"> </script>\n <script src=\"./swagger-ui-standalone-preset.js\"> </script>\n <script>\n window.onload = function() {\n\n // Build a system\n const ui = SwaggerUIBundle({\n configUrl: \"./api-explorer-config.json\",\n dom_id: '#swagger-ui',\n deepLinking: true,\n presets: [\n SwaggerUIBundle.presets.apis,\n SwaggerUIStandalonePreset\n ],\n plugins: [\n SwaggerUIBundle.plugins.DownloadUrl\n ],\n layout: \"StandaloneLayout\"\n })\n\n window.ui = ui\n }\n </script>\n </body>\n</html>\n","statusCode":200}⏎
Was it I made some mistake or two-router is not supported yet?
I configured the router according to document.
const router = new Router({
apiDoc: path.join(__dirname, "api"),
apiExplorerVisible: true
});
router.mount(middleware, path.join(__dirname, "controllers"));
All the APIs (http://localhost:8080/api/...
) works fine.
The problem is, the API Explorer shows Petstore instead of my API.
like /users?detail=true
, now return:
error: 'Invalid type: string (expected boolean)'
Hi,
First, I wanted to say that your router is really great !
But I have a question about x-oai-controller and controllerDir names.
Why didnt you name them "x-oai-middleware" and "middlewareDir" ?
Hello,
First of all, I wanted to excuse myself for my terrible english and the quality of this issue.
While testing your module for my work project I bumped into this error, while requesting the docs/swagger-ui page:
> [email protected] start /home/marco/Projects/pollerExpress
> node --harmony api/server.js
ApiExplorer: http://127.0.0.1:9000/docs
TypeError: ctx.renderSwaggerApiExplorer is not a function
at /home/marco/Projects/pollerExpress/node_modules/koa-oai-router/dist/oai-explorer.js:53:17
at Generator.next (<anonymous>)
at step (/home/marco/Projects/pollerExpress/node_modules/koa-oai-router/dist/oai-explorer.js:33:191)
at /home/marco/Projects/pollerExpress/node_modules/koa-oai-router/dist/oai-explorer.js:33:437
at Promise (<anonymous>)
at /home/marco/Projects/pollerExpress/node_modules/koa-oai-router/dist/oai-explorer.js:33:99
at /home/marco/Projects/pollerExpress/node_modules/koa-oai-router/dist/oai-explorer.js:57:19
at /home/marco/Projects/pollerExpress/node_modules/koa-mount/index.js:58:28
at dispatch (/home/marco/Projects/pollerExpress/node_modules/koa-compose/index.js:44:32)
at next (/home/marco/Projects/pollerExpress/node_modules/koa-compose/index.js:45:18)
at /home/marco/Projects/pollerExpress/node_modules/koa-mount/index.js:52:26
at dispatch (/home/marco/Projects/pollerExpress/node_modules/koa-compose/index.js:44:32)
at next (/home/marco/Projects/pollerExpress/node_modules/koa-compose/index.js:45:18)
at /home/marco/Projects/pollerExpress/node_modules/koa-mount/index.js:52:26
at dispatch (/home/marco/Projects/pollerExpress/node_modules/koa-compose/index.js:44:32)
at next (/home/marco/Projects/pollerExpress/node_modules/koa-compose/index.js:45:18)
the error is generated at ./koa-oai-router/lib/oai-explorer.js:24: await tx.renderSwaggerApiExplorer('index', options);
ctx.renderSwaggerApiExplorer
should be provided by the koa-views-swagger package
but looking at it, the function exposed is, instead, ctx.render
In fact after correcting the name of the function everything was working correctly.
Please ad support for OpenAPI v3.
npmjs.org serves only version 1.0.0. Is it possible to publish latest version?
How can I protect a specific route using koa-oai-router?
That's too bad!
I'm trying to manage file upload in my application. I successfully implemented your lib for all "standard" routes, but now I would like, for one single route, to add the middleware koa-body
to manage file upload.
And I don't find out where to put this middleware? Can you post an example? Thanks.
errorHandler function only can handle the error before parameters verification. So can you give me a API to handle the error in the top of one request
An error occurred while I was testing a REST server with jest
and supertest
. It seems a stuck Promise
in koa-oai-router
was detected.
Jest has detected the following 3 open handles potentially keeping Jest from exiting:
● DNSCHANNEL
at dns.js:338:23
● PROMISE
at Promise.then (<anonymous>)
at OAIRouter.routes (node_modules/koa-oai-router/lib/router.js:100:40)
at Object.<anonymous> (src/index.ts:141:16)
at Object.<anonymous> (test/app.test.ts:3:1)
● PROMISE
at Promise.then (<anonymous>)
at Promise.catch (<anonymous>)
at OAIRouter.routes (node_modules/koa-oai-router/lib/router.js:114:17)
at Object.<anonymous> (src/index.ts:141:16)
at Object.<anonymous> (test/app.test.ts:3:1)
allOf is support for swagger-ui 3.x to split paths into multi files. I think this is a very import function for us.
paths:
allOf:
- $ref: /api/users.yaml
- $ref: /api/items.yaml
https://github.com/OAI/OpenAPI-Specification/blob/master/guidelines/REUSE.md
swagger-api/swagger-ui#1098
swagger-api/swagger-editor#233
I specified array parmeter as multi string:
- in: query
name: keywords
type: array
collectionFormat: multi
items:
type: string
but when just single value is passed
curl localhost:3035/endpoint?keywords=mykeyword"
it fails with error
{
"detail": {
"dataPath": ".keywords",
"keyword": "type",
"message": "should be array",
"params": {
"type": "array"
},
"schemaPath": "#/properties/keywords/type"
},
"error": "should be array",
"path": ".keywords",
"status": 400,
"type": "query"
}
New v3 available.
It looks like it's not parsing the parameter in path in any way.
Example: in the route /user/{userId}, the userID value is not in request.query or request.param
Could not find anywhere.
Hi,
General setup works fine and i can see the swagger doc with all the details of my api. However when I try to call an API with below swagger format:
'/user/{id}':
get:
x-oai-middleware:
- file: users
handler: get
parameters:
- name: Authorization
in: header
type: string
- name: id
in: path
type: integer
I get a 404. in swagger doc, my base path is set to /api. So in swagger explorer this particular api is listed as(after giving sample data);
curl -X GET "https://localhost:8000/api/user/1" -H "accept: application/json" -H "Authorization: sfasdff"
My /controller directory has users.js file with:
import koa from 'koa';
async function get(ctx:koa.Context, next:Function) {
ctx.response.body = ctx.params.id; //expects to echo the received id via path.
}
App didn't complain about whether it managed to load the controllers etc.. So i think below has worked fine;
router.mount(new routerMiddleware(), path.resolve(__dirname, 'controllers'));
Any idea why it gets a 404 ?
Is it possible to add a middleware as the first one of the chain for any route ? (without setting it mannually in the chain in the x-oai-controller section).
My plan is to easily wrap everything into a try catch.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.