Comparing version 2.0.3 to 2.0.5
@@ -1,9 +0,34 @@ | ||
import { SwaggerDefalut, travelersApis } from "./lib/api"; | ||
import { SwaggerConfig, travelersApis } from "./lib/api"; | ||
import { Code } from "./lib/code"; | ||
import { Request, Response, NextFunction, RequestHandler, Express, ErrorRequestHandler } from "express"; | ||
declare type travelersConfig<T> = { | ||
host?: String; | ||
declare global { | ||
namespace Travelers { | ||
interface Srvs { | ||
[k: string]: any; | ||
} | ||
interface Config { | ||
[k: string]: any; | ||
} | ||
} | ||
} | ||
interface Srvs extends Travelers.Srvs { | ||
} | ||
interface Req extends Request { | ||
srvs: Travelers.Srvs; | ||
$config: Travelers.Config; | ||
} | ||
interface Res extends Response { | ||
} | ||
interface Config extends Travelers.Config { | ||
host?: string; | ||
port: number; | ||
} & T; | ||
interface Args { | ||
swaggerConfig: SwaggerConfig; | ||
swaggerPath: string; | ||
} | ||
interface TravelersOption { | ||
config: Config; | ||
before?: (app: Express) => void; | ||
security?: { | ||
[key: string]: (req: Req, res: Res) => Promise<any>; | ||
}; | ||
apis: { | ||
@@ -13,35 +38,25 @@ [key: string]: travelersApis; | ||
controllers: { | ||
[key: string]: (req: Request, res: Response) => Promise<any>; | ||
[key: string]: (req: Req, res: Res) => Promise<any>; | ||
}; | ||
} | ||
interface travelersOption { | ||
config: travelersConfig<any>; | ||
before?: (app: Express) => void; | ||
args: Args; | ||
srvs?: { | ||
[key: string]: any; | ||
}; | ||
swaggerDefalut?: SwaggerDefalut; | ||
after?: (app: Express, obj?: object) => void; | ||
after?: (app: Express, srvs: Travelers.Srvs) => void; | ||
} | ||
export declare function travelers(option: travelersOption): Promise<void>; | ||
declare global { | ||
namespace travelers { | ||
interface Srvs { | ||
[k: string]: any; | ||
} | ||
interface $config { | ||
} | ||
} | ||
} | ||
declare module "express" { | ||
interface Request { | ||
srvs: travelers.Srvs; | ||
$config: travelers.$config; | ||
} | ||
} | ||
interface App extends Express { | ||
srvs: travelers.Srvs; | ||
$config: travelers.$config; | ||
} | ||
export { Request, Response, NextFunction, RequestHandler, Express, ErrorRequestHandler, travelersApis, travelersOption, Code, App }; | ||
export declare function travelers(option: TravelersOption): Promise<{ | ||
swagger: { | ||
paths: {}; | ||
swagger: string; | ||
info: { | ||
title: string; | ||
description: string; | ||
version: string; | ||
}; | ||
host: string; | ||
basePath: string; | ||
schemes: string[]; | ||
produces: string[]; | ||
security: any[]; | ||
}; | ||
}>; | ||
export { Req, Res, NextFunction, RequestHandler, Express, ErrorRequestHandler, travelersApis, TravelersOption, Code, Config, Srvs }; |
@@ -10,5 +10,4 @@ "use strict"; | ||
const app = express(); | ||
let { config, before, args, swaggerDefalut, after, srvs } = option; | ||
const { host = "0.0.0.0", port = "3000" } = config; | ||
const { apis, controllers } = args; | ||
let { config, before, security = {}, apis, controllers, after, srvs } = option; | ||
const { host = "0.0.0.0", port = 3000 } = config; | ||
srvs = code_1.srvsCode(srvs); | ||
@@ -21,11 +20,13 @@ if (before) | ||
req["srvs"] = srvs; | ||
req["$config"] = config; | ||
next(); | ||
}); | ||
await api_1.apiManage(app, apis, controllers, swaggerDefalut, config); | ||
await api_1.apiManage(app, security, apis, controllers, config); | ||
if (after) | ||
after(app, { swagger: api_1.swagger }); | ||
after(app, srvs); | ||
app.listen(port, `${host}`); | ||
console.log(chalk.bold.red(`travelers start host:${host} prot:${port}`)); | ||
return { swagger: api_1.swagger }; | ||
} | ||
exports.travelers = travelers; | ||
//# sourceMappingURL=index.js.map |
/// <reference types="express" /> | ||
import * as joi from "joi"; | ||
import { Request, Response, Express } from "../index"; | ||
interface Info { | ||
title: string; | ||
description: string; | ||
version: string; | ||
} | ||
interface SwaggerDefalut { | ||
swagger?: string; | ||
info?: Info; | ||
host?: string; | ||
basePath?: string; | ||
schemes?: string[]; | ||
produces?: string[]; | ||
} | ||
import { Req, Res, Express, Config } from "../index"; | ||
declare const swaggerConfigDefalut: { | ||
swagger: string; | ||
info: { | ||
title: string; | ||
description: string; | ||
version: string; | ||
}; | ||
host: string; | ||
basePath: string; | ||
schemes: string[]; | ||
produces: string[]; | ||
security: any[]; | ||
}; | ||
declare type SwaggerConfig = typeof swaggerConfigDefalut; | ||
interface Reqeust { | ||
@@ -36,2 +37,3 @@ params?: { | ||
operationId: string; | ||
security?: string[]; | ||
produces?: string[]; | ||
@@ -54,5 +56,3 @@ req?: Reqeust; | ||
produces: string[]; | ||
security: { | ||
"bearerAuth": any[]; | ||
}[]; | ||
security: any[]; | ||
}; | ||
@@ -63,5 +63,7 @@ interface ManageApis { | ||
interface ManageControllers { | ||
[key: string]: (req: Request, res: Response) => Promise<any>; | ||
[key: string]: (req: Req, res: Res) => Promise<any>; | ||
} | ||
declare function apiManage(app: Express, apis: ManageApis, controllers: ManageControllers, swaggerDefalut: SwaggerDefalut, config: any): Promise<void>; | ||
export { SwaggerDefalut, apiManage, travelersApis, swagger, ManageApis, ManageControllers }; | ||
declare function apiManage(app: Express, security: { | ||
[key: string]: (req: Req, res: Res) => Promise<any>; | ||
}, apis: ManageApis, controllers: ManageControllers, config: Config): Promise<void>; | ||
export { SwaggerConfig, apiManage, travelersApis, swagger, ManageApis, ManageControllers }; |
@@ -6,6 +6,7 @@ "use strict"; | ||
const path = require("path"); | ||
const express = require("express"); | ||
const chalk = require("chalk"); | ||
const verify = require("./verify"); | ||
const express = require("express"); | ||
const swaggerDefalutSwagger = { | ||
const fs = require("fs"); | ||
const swaggerConfigDefalut = { | ||
swagger: "2.0", | ||
@@ -21,21 +22,20 @@ info: { | ||
produces: ["application/json"], | ||
security: [ | ||
{ | ||
"bearerAuth": [] | ||
} | ||
], | ||
security: [], | ||
}; | ||
let swagger = { | ||
...swaggerDefalutSwagger, | ||
...swaggerConfigDefalut, | ||
paths: {} | ||
}; | ||
exports.swagger = swagger; | ||
async function apiManage(app, apis, controllers, swaggerDefalut = {}, config) { | ||
const { port = "3000" } = config; | ||
const host = "127.0.0.1"; | ||
async function apiManage(app, security, apis, controllers, config) { | ||
verify.apiVerify(apis, controllers); | ||
let selfSecurity = security; | ||
Object.keys(security).forEach(_key => { | ||
swagger.security.push({ [_key]: [] }); | ||
}); | ||
const { swaggerPath, swaggerConfig, port = "3000" } = config; | ||
Object.keys(apis).forEach(apiItem => { | ||
const items = apis[apiItem]; | ||
items.forEach(item => { | ||
const { path, method, summary = "默认", tags = [apiItem], description, operationId, req, res } = item; | ||
const { path, method, summary = "默认", tags = [apiItem], security = [], description, operationId, req, res } = item; | ||
const { query, body, params } = req; | ||
@@ -52,2 +52,3 @@ const resBody = res.body; | ||
tags, | ||
security, | ||
responses: { | ||
@@ -92,2 +93,6 @@ "200": { | ||
// 验证 | ||
for (const item of security) { | ||
if (selfSecurity[item]) | ||
await selfSecurity[item](req, res); | ||
} | ||
const _query = item.req.query || {}; | ||
@@ -130,11 +135,12 @@ const _body = item.req.body || {}; | ||
}); | ||
swaggerDefalut.host = `${host}:${port}`; | ||
exports.swagger = swagger = { ...swagger, ...swaggerDefalut }; | ||
app.use("/document", express.static(path.join(__dirname, "../../swagger"))); | ||
console.log(chalk.bold.red(`document you can click: http://${host}:${port}/document`)); | ||
app.get("/swagger", (req, res, next) => { | ||
exports.swagger = swagger = { ...swagger, ...swaggerConfig }; | ||
app.use(swaggerPath, express.static(path.join(__dirname, "../../swagger"))); | ||
console.log(chalk.bold.red(`document you can click: http://127.0.0.1${port == 80 ? "" : `:${port}`}${swaggerPath}`)); | ||
app.get(`${swaggerPath}/json`, (req, res, next) => { | ||
res.send(swagger); | ||
}); | ||
let swaggerUrlFile = `let swaggerUrl = "${swaggerPath}/json"`; | ||
fs.writeFileSync(path.join(__dirname, "../../swagger/url.js"), swaggerUrlFile); | ||
} | ||
exports.apiManage = apiManage; | ||
//# sourceMappingURL=api.js.map |
@@ -30,3 +30,3 @@ "use strict"; | ||
code: 400, | ||
msg: "$message" | ||
msg: "${message}" | ||
} | ||
@@ -47,7 +47,7 @@ }; | ||
result[key] = { | ||
resJson: function (res, args) { | ||
resJson: function (res, args = {}) { | ||
let obj = codeAll[key]; | ||
Object.keys(args).forEach(objKey => { | ||
let replaceObj = `$${objKey}`; | ||
obj.msg = obj.msg.replace(replaceObj, args[objKey]); | ||
let replaceObj = "${" + objKey + "}"; | ||
obj.msg = obj.msg.replace(replaceObj, `${args[objKey]}`); | ||
}); | ||
@@ -54,0 +54,0 @@ res.status(codeAll[key].code).send(obj); |
{ | ||
"name": "travelers", | ||
"version": "2.0.3", | ||
"version": "2.0.5", | ||
"description": "I wanna write code like travelers", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc", | ||
"lint": "npx eslint --ext .ts src", | ||
"start": "tsc && node dist/index.js", | ||
"watch-update": "nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/index.ts" | ||
"fix": "npx eslint --ext .ts src --fix", | ||
"start": "ts-node src/index.ts" | ||
}, | ||
@@ -13,0 +12,0 @@ "repository": { |
@@ -38,33 +38,48 @@ | ||
### src/apis/index.ts | ||
```js | ||
import api from "./api"; | ||
export { api }; | ||
``` | ||
### src/controllers/controller.ts | ||
```js | ||
import { Request, Response } from "travelers"; | ||
export async function everyDay_list(req: Request, res: Response) { | ||
import { Req, Res } from "travelers"; | ||
export async function operationId(req: Req, res: Res) { | ||
let { body, srvs } = req; | ||
const { knex } = srvs; | ||
res.json({ | ||
code: 200 | ||
}); | ||
const { knex, codes } = srvs; | ||
codes.ok.resJson(res); | ||
} | ||
``` | ||
### src/controllers/index.ts | ||
```js | ||
import * as controller from "./controller"; | ||
const controllers = { | ||
...controller | ||
}; | ||
export default controllers; | ||
``` | ||
### src/index.ts | ||
```js | ||
import { travelers, travelersOption,Response,Request,NextFunction } from "travelers"; | ||
import { travelers, TravelersOption, Req, Res, NextFunction, Config } from "travelers"; | ||
import * as apis from "./apis/index"; | ||
import * as srvs from "./srvs/index"; | ||
import * as controllers from "./controllers/index"; | ||
import controllers from "./controllers/index"; | ||
import config from "./config/index"; | ||
const option: travelersOption = { | ||
import * as security from "./security"; | ||
const option: TravelersOption = { | ||
config, | ||
before: function (app) { | ||
}, | ||
srvs, | ||
args: { | ||
apis, | ||
controllers, | ||
}, | ||
after: function (app, obj: any) { | ||
app.use((req: Request, res: Response) => { | ||
security, | ||
apis, | ||
controllers, | ||
after: function (app, srvs) { | ||
app.use((req: Req, res: Res) => { | ||
const { codes } = req.srvs; | ||
@@ -75,3 +90,5 @@ return codes.notfind.resJson(res); | ||
}; | ||
travelers(option); | ||
travelers(option).then(data => { | ||
// console.log(JSON.stringify(data, null, 4)); | ||
}); | ||
@@ -78,0 +95,0 @@ ``` |
@@ -1,2 +0,2 @@ | ||
import { SwaggerDefalut, apiManage, travelersApis, swagger } from "./lib/api"; | ||
import { SwaggerConfig, apiManage, travelersApis, swagger } from "./lib/api"; | ||
import * as chalk from "chalk"; | ||
@@ -7,9 +7,39 @@ import { srvsCode, Code } from "./lib/code"; | ||
type travelersConfig<T> = { | ||
host?: String, | ||
port: number, | ||
} & T; | ||
declare global { | ||
namespace Travelers { | ||
interface Srvs { | ||
[k: string]: any; | ||
} | ||
interface Config { | ||
[k: string]: any; | ||
} | ||
} | ||
} | ||
interface Srvs extends Travelers.Srvs { | ||
interface Args { | ||
} | ||
interface Req extends Request { | ||
srvs: Travelers.Srvs | ||
$config: Travelers.Config | ||
} | ||
interface Res extends Response { | ||
} | ||
interface Config extends Travelers.Config { | ||
host?: string | ||
port: number | ||
swaggerConfig: SwaggerConfig | ||
swaggerPath: string | ||
} | ||
interface TravelersOption { | ||
config: Config, | ||
before?: (app: Express) => void, | ||
security?: { | ||
[key: string]: (req: Req, res: Res) => Promise<any> | ||
}, | ||
apis: { | ||
@@ -19,26 +49,17 @@ [key: string]: travelersApis | ||
controllers: { | ||
[key: string]: (req: Request, res: Response) => Promise<any> | ||
[key: string]: (req: Req, res: Res) => Promise<any> | ||
} | ||
srvs?: { [key: string]: any }, | ||
after?: (app: Express, srvs: Travelers.Srvs) => void | ||
} | ||
interface travelersOption { | ||
config: travelersConfig<any>, | ||
before?: (app: Express) => void, | ||
args: Args, | ||
srvs?: { | ||
[key: string]: any | ||
}, | ||
swaggerDefalut?: SwaggerDefalut, | ||
after?: (app: Express, obj?: object) => void | ||
} | ||
export async function travelers(option: travelersOption) { | ||
export async function travelers(option: TravelersOption) { | ||
const app = express(); | ||
let { config, before, security = {}, apis, controllers, after, srvs } = option; | ||
const { host = "0.0.0.0", port = 3000 } = config; | ||
let { config, before, args, swaggerDefalut, after, srvs } = option; | ||
const { host = "0.0.0.0", port = "3000" } = config; | ||
const { apis, controllers } = args; | ||
srvs = srvsCode(srvs); | ||
srvs = srvsCode(srvs); | ||
if (before) before(app); | ||
@@ -49,39 +70,17 @@ app.use(express.json()); | ||
req["srvs"] = srvs; | ||
req["$config"] = config; | ||
next(); | ||
}); | ||
await apiManage(app, apis, controllers, swaggerDefalut, config); | ||
if (after) after(app, { swagger }); | ||
await apiManage(app, security, apis, controllers, config); | ||
if (after) after(app, srvs); | ||
app.listen(port, `${host}`); | ||
console.log(chalk.bold.red(`travelers start host:${host} prot:${port}`)); | ||
return { swagger }; | ||
} | ||
export { Req, Res, NextFunction, RequestHandler, Express, ErrorRequestHandler, travelersApis, TravelersOption, Code, Config, Srvs }; | ||
declare global { | ||
namespace travelers { | ||
interface Srvs { | ||
[k: string]: any; | ||
} | ||
interface $config { | ||
} | ||
} | ||
} | ||
declare module "express" { | ||
interface Request { | ||
srvs: travelers.Srvs | ||
$config: travelers.$config | ||
} | ||
} | ||
interface App extends Express { | ||
srvs: travelers.Srvs | ||
$config: travelers.$config | ||
} | ||
export { Request, Response, NextFunction, RequestHandler, Express, ErrorRequestHandler, travelersApis, travelersOption, Code, App }; | ||
@@ -5,10 +5,10 @@ | ||
import * as convert from "joi-to-json-schema"; | ||
import { join } from "path"; | ||
import * as path from "path"; | ||
import { Request, Response, Express, NextFunction } from "../index"; | ||
import * as express from "express"; | ||
import { Req, Res, Express, NextFunction, Config } from "../index"; | ||
import * as chalk from "chalk"; | ||
import * as verify from "./verify"; | ||
import * as express from "express"; | ||
import * as fs from "fs"; | ||
const swaggerDefalutSwagger = { | ||
const swaggerConfigDefalut = { | ||
swagger: "2.0", | ||
@@ -25,22 +25,8 @@ info: { | ||
security: [ | ||
{ | ||
"bearerAuth": [] | ||
} | ||
], | ||
}; | ||
interface Info { | ||
title: string, | ||
description: string, | ||
version: string, | ||
} | ||
interface SwaggerDefalut { | ||
swagger?: string, | ||
info?: Info, | ||
host?: string, | ||
basePath?: string, | ||
schemes?: string[], | ||
produces?: string[], | ||
} | ||
type SwaggerConfig = typeof swaggerConfigDefalut | ||
@@ -68,2 +54,3 @@ interface Reqeust { | ||
operationId: string; | ||
security?: string[]; | ||
produces?: string[]; | ||
@@ -77,3 +64,3 @@ req?: Reqeust; | ||
let swagger = { | ||
...swaggerDefalutSwagger, | ||
...swaggerConfigDefalut, | ||
paths: {} | ||
@@ -87,3 +74,3 @@ }; | ||
interface ManageControllers { | ||
[key: string]: (req: Request, res: Response) => Promise<any> | ||
[key: string]: (req: Req, res: Res) => Promise<any> | ||
} | ||
@@ -93,15 +80,22 @@ | ||
app: Express, | ||
security:{ | ||
[key: string]: (req: Req, res: Res) => Promise<any> | ||
}, | ||
apis: ManageApis, | ||
controllers: ManageControllers, | ||
swaggerDefalut: SwaggerDefalut = {}, | ||
config) { | ||
const { port = "3000" } = config; | ||
const host = "127.0.0.1"; | ||
config: Config | ||
) { | ||
verify.apiVerify(apis, controllers); | ||
let selfSecurity = security; | ||
Object.keys(security).forEach(_key => { | ||
swagger.security.push({[_key]:[]}); | ||
}); | ||
const { swaggerPath, swaggerConfig,port = "3000" } = config; | ||
Object.keys(apis).forEach(apiItem => { | ||
const items: travelersApis = apis[apiItem]; | ||
items.forEach(item => { | ||
const { path, method, summary = "默认", tags = [apiItem], description, operationId, req, res } = item; | ||
const { path, method, summary = "默认", tags = [apiItem], security = [], description, operationId, req, res } = item; | ||
const { query, body, params } = req; | ||
const resBody = res.body; | ||
if (!swagger.paths[path]) { | ||
@@ -116,2 +110,3 @@ swagger.paths[path] = {}; | ||
tags, | ||
security, | ||
responses: { | ||
@@ -158,5 +153,8 @@ "200": { | ||
koaPath = koaPath.replace(/{/g, ":"); | ||
app[method](koaPath, (req: Request, res: Response, next: NextFunction) => { | ||
app[method](koaPath, (req: Req, res: Res, next: NextFunction) => { | ||
(async function () { | ||
// 验证 | ||
for (const item of security) { | ||
if(selfSecurity[item]) await selfSecurity[item](req,res); | ||
} | ||
const _query = item.req.query || {}; | ||
@@ -196,13 +194,14 @@ const _body = item.req.body || {}; | ||
swaggerDefalut.host = `${host}:${port}`; | ||
swagger = { ...swagger, ...swaggerDefalut }; | ||
app.use("/document", express.static(path.join(__dirname, "../../swagger"))); | ||
swagger = { ...swagger, ...swaggerConfig }; | ||
app.use(swaggerPath, express.static(path.join(__dirname, "../../swagger"))); | ||
console.log(chalk.bold.red(`document you can click: http://${host}:${port}/document`)); | ||
app.get("/swagger", (req, res, next) => { | ||
console.log(chalk.bold.red(`document you can click: http://127.0.0.1${port == 80?"":`:${port}`}${swaggerPath}`)); | ||
app.get(`${swaggerPath}/json`, (req, res, next) => { | ||
res.send(swagger); | ||
}); | ||
let swaggerUrlFile = `let swaggerUrl = "${swaggerPath}/json"`; | ||
fs.writeFileSync(path.join(__dirname, "../../swagger/url.js"), swaggerUrlFile); | ||
} | ||
export { SwaggerDefalut, apiManage, travelersApis, swagger, ManageApis, ManageControllers }; | ||
export { SwaggerConfig, apiManage, travelersApis, swagger, ManageApis, ManageControllers }; |
@@ -28,7 +28,7 @@ const Code = { | ||
code: 400, | ||
msg: "$message" | ||
msg: "${message}" | ||
} | ||
}; | ||
import { Request, Response, Express, NextFunction } from "../index"; | ||
import { Req, Res, Express, NextFunction } from "../index"; | ||
interface CodeType { [key: string]: { code: number, msg: string } } | ||
@@ -38,3 +38,3 @@ | ||
[key: string]: { | ||
resJson: (res: Response, args?: { [key: string]: string }) => void | ||
resJson: (res: Res, args?: { [key: string]: string | number }) => void | ||
} | ||
@@ -56,7 +56,7 @@ } | ||
result[key] = { | ||
resJson: function (res, args) { | ||
resJson: function (res, args = {}) { | ||
let obj = codeAll[key]; | ||
Object.keys(args).forEach(objKey => { | ||
let replaceObj = `$${objKey}`; | ||
obj.msg = obj.msg.replace(replaceObj, args[objKey]); | ||
let replaceObj = "${" + objKey + "}"; | ||
obj.msg = obj.msg.replace(replaceObj, `${args[objKey]}`); | ||
}); | ||
@@ -63,0 +63,0 @@ res.status(codeAll[key].code).send(obj); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
1473770
6366
0
96
1