Gathering detailed insights and metrics for @lxsbw/koa-joi-swagger-ts
Gathering detailed insights and metrics for @lxsbw/koa-joi-swagger-ts
npm install @lxsbw/koa-joi-swagger-ts
Typescript
Module System
Node Version
NPM Version
59.3
Supply Chain
91.5
Quality
70.2
Maintenance
50
Vulnerability
97
License
TypeScript (99.2%)
JavaScript (0.8%)
Total Downloads
2,119
Last Day
2
Last Week
3
Last Month
35
Last Year
211
134 Commits
1 Watching
4 Branches
Minified
Minified + Gzipped
Latest Version
1.0.10
Package Id
@lxsbw/koa-joi-swagger-ts@1.0.10
Unpacked Size
85.16 kB
Size
18.45 kB
File Count
77
NPM Version
6.14.8
Node Version
14.15.1
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
-72.7%
3
Compared to previous week
Last month
1,650%
35
Compared to previous month
Last year
-49.5%
211
Compared to previous year
npm install @lxsbw/koa-joi-swagger-ts --save
Each controller can use 5 predefined rest methods GET, PUT, POST, PATCH, DELETE
@controller("/user")
class UserController extends BaseController {
@put("/{userId}")
@parameter("userId", joi.string().min(2).description("userId"), ENUM_PARAM_IN.path)
addSomeUser(ctx) {
ctx.body = "cant add user";
}
@del("/{userId}")
@parameter("userId", joi.string().min(2).description("userId"), ENUM_PARAM_IN.path)
deleteSomeUser(ctx) {
ctx.body = "user not found";
}
}
We have 3 type of resolvers (aka middlewares, but without next() functions) - functions which can be called before (or after) your controller method:
This decorator can wrap on your method into try-catch, and if something wrong - call function which specifiyed into parameter fn with Error argument
Parameters:
import {safe, parameter, del, controller, ENUM_PARAM_IN} from "@lxsbw/koa-joi-swagger-ts";
// ...somecodehere...
@controller("/user")
class UserController extends BaseController {
@del("/{userId}")
@parameter("userId", joi.string().min(2).description("userId"), ENUM_PARAM_IN.path)
@safe( (err) => { console.log(err) } )
deleteSomeUser() {
throw Error("Some bad error");
}
}
This decorator can add additional functions which will called before your controller method.
Parameters:
import {before, parameter, del, controller, ENUM_PARAM_IN} from "@lxsbw/koa-joi-swagger-ts";
// ...somecodehere...
@controller("/user")
class UserController extends BaseController {
@del("/{userId}")
@parameter("userId", joi.string().min(2).description("userId"), ENUM_PARAM_IN.path)
@before( (ctx) => { console.log("first resolver") } )
@before( (ctx) => { console.log("second resolver") }, (ctx) => { console.log("third resolver") } )
deleteSomeUser(ctx) {
ctx.body = "user not found";
}
}
This decorator can add additional functions which will called after your controller method.
ATTENTION! Specific of decorators calling - is reversed order of calls, thats why, if you use few @after decorators - last after will be called as first, and first as last, thats why I recommend to use list of functions as multiple arguments if order matters something for your logic
Parameters:
import {after, parameter, del, controller, ENUM_PARAM_IN} from "@lxsbw/koa-joi-swagger-ts";
// ...somecodehere...
@controller("/user")
class UserController extends BaseController {
@del("/{userId}")
@parameter("userId", joi.string().min(2).description("userId"), ENUM_PARAM_IN.path)
@after( (ctx) => { console.log("called THIRD afetr method") } )
@after( (ctx) => { console.log("called FIRST after method") }, (ctx) => { console.log("called SECOND after method") } )
deleteSomeUser(ctx) {
ctx.body = "user not found";
}
}
import {parameter, get, post, del, controller, definition, KoaSwaggerRouter, summary, response, tag, ENUM_PARAM_IN} from "@lxsbw/koa-joi-swagger-ts";
import * as joi from "joi";
import * as fs from "fs";
import {array, string} from "joi";
import * as koa from "koa";
@definition("User", "User Entity")
class UserSchema {
userName = joi.string().min(6).description("username").required();
userPass = joi.string().min(6).description("password").required();
}
@controller("/v3/api")
class BaseController {
@get("/")
index() {
}
}
/**
* This method will be called by middleware instead of controller
*/
const baseControllerFunction = async (controller, ctx, next, summary): Promise<void> => {
console.log(`${ctx.request.method} ${ctx.request.url}`);
try {
await controller(ctx);
} catch (e) {
console.log(e, `Error while executing "${summary}"`);
}
};
@controller("/user")
class UserController extends BaseController {
@del("/{userId}")
@parameter("userId", joi.string().min(2).description("userId"), ENUM_PARAM_IN.path)
index() {
}
@get("/")
@parameter("userId", joi.string().required(), ENUM_PARAM_IN.query)
doGet(ctx) {
ctx.body = Date.now();
}
@get("/{userId}")
@parameter("userId", joi.number().min(2).description("userId"), ENUM_PARAM_IN.path)
@response(200, {$ref: UserSchema})
getUser(ctx) {
ctx.body = {userName: ctx.params.userId.toString(), userPass: Date.now().toString()};
}
@post("/upload")
@parameter("file1", {type: "file"}, ENUM_PARAM_IN.formData)
doUpload(ctx) {
ctx.body = { fileObj: ctx.body.file1};
}
@post("/")
doPost() {
}
@get("s")
@response(200, {type: "array", items: {$ref: UserSchema}})
getUsers() {
}
}
@definition("Admin", "Admin Entity")
class AdminSchema {
userName = joi.string().required().min(6).uppercase();
userPass = joi.string();
}
@controller("/admin")
class AdminController extends UserController {
@post("/login")
@parameter("name", joi.string().description("name"))
@parameter("list", array().items(string()).required(), ENUM_PARAM_IN.query)
@summary("AdminController.index")
@response(200, {$ref: AdminSchema})
@response(202, joi.string().description("aaa"))
@tag("Admin")
@tag("User")
index() {
}
}
const router = new KoaSwaggerRouter({
swagger: '2.0',
info: {
description:
'This is a sample server Koa2 server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.',
title: 'Koa2 TypeScript Swagger',
version: '1.0.0',
concat: {
email: 'xxx@outlook.com'
},
// 开源协议
license: {
name: 'Apache 2.0',
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
}
},
// host: '',
basePath: '',
schemes: ['http', 'https'],
paths: {},
definitions: {},
securityDefinitions: {
JWT: {
type: 'apiKey',
in: 'header',
name: 'Authorization'
}
}
});
router.loadDefinition(UserSchema);
router.loadDefinition(AdminSchema);
// Or you can:
// router.loadDefinition([UserSchema, AdminSchema]);
router.loadController(BaseController);
// Process controller through pattern Decorator
router.loadController(UserController, baseControllerFunction);
router.loadController(AdminController);
router.setSwaggerFile("swagger.json");
router.loadSwaggerUI("/");
fs.writeFileSync("./swagger.json", JSON.stringify(router.swagger));
// console.log(router.getRouter());
const app = new koa();
app.use(router.getRouter().routes());
app.use(router.getRouter().allowedMethods());
app.listen(3002);
You can quickly test @lxsbw/koa-joi-swagger-ts with the project example koa-base-ts.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
58 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More