You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
297 lines
11 KiB
297 lines
11 KiB
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
var Joi = require('joi');
|
|
|
|
function _interopNamespace(e) {
|
|
if (e && e.__esModule) return e;
|
|
var n = Object.create(null);
|
|
if (e) {
|
|
Object.keys(e).forEach(function (k) {
|
|
if (k !== 'default') {
|
|
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
Object.defineProperty(n, k, d.get ? d : {
|
|
enumerable: true,
|
|
get: function () {
|
|
return e[k];
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
n['default'] = e;
|
|
return Object.freeze(n);
|
|
}
|
|
|
|
var Joi__namespace = /*#__PURE__*/_interopNamespace(Joi);
|
|
|
|
/*! *****************************************************************************
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
***************************************************************************** */
|
|
|
|
function __values(o) {
|
|
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
if (m) return m.call(o);
|
|
if (o && typeof o.length === "number") return {
|
|
next: function () {
|
|
if (o && i >= o.length) o = void 0;
|
|
return { value: o && o[i++], done: !o };
|
|
}
|
|
};
|
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
}
|
|
|
|
var path$1 = require("path");
|
|
var fs$1 = require("fs");
|
|
function removeIndex(ss) {
|
|
var remove = function (str) {
|
|
if (str.endsWith("/index")) {
|
|
return str.slice(0, -6);
|
|
}
|
|
if (str.endsWith("index")) {
|
|
return str.slice(0, -5);
|
|
}
|
|
return str ? str : "/";
|
|
};
|
|
var r = true;
|
|
var rr = ss;
|
|
while (r) {
|
|
if (rr.endsWith("/index")) {
|
|
rr = remove(rr);
|
|
}
|
|
else {
|
|
r = false;
|
|
}
|
|
}
|
|
return rr ? rr : "/";
|
|
}
|
|
function isIndexEnd(str) {
|
|
return str.length == 1 && str.endsWith("/");
|
|
}
|
|
function walkDir(filePath, exclude) {
|
|
if (exclude === void 0) { exclude = ["node_modules", "^_", ".git", ".idea", ".gitignore", "client", "\.txt$"]; }
|
|
var files = [];
|
|
function Data(opts) {
|
|
this.relativeDir = opts.relativeDir;
|
|
this.relativeFile = opts.relativeFile;
|
|
this.filename = opts.filename;
|
|
this.file = opts.file;
|
|
this.absoluteFile = opts.absoluteFile;
|
|
this.relativeFileNoExt = opts.relativeFileNoExt;
|
|
this.absoluteDir = opts.absoluteDir;
|
|
}
|
|
function readDir(filePath, dirname) {
|
|
if (dirname === void 0) { dirname = "."; }
|
|
var res = fs$1.readdirSync(filePath);
|
|
res.forEach(function (filename) {
|
|
var filepath = path$1.resolve(filePath, filename);
|
|
var stat = fs$1.statSync(filepath);
|
|
var name = filepath.split(path$1.sep).slice(-1)[0];
|
|
if (typeof exclude === "string" && new RegExp(exclude).test(name)) {
|
|
return;
|
|
}
|
|
if (Array.isArray(exclude)) {
|
|
for (var i = 0; i < exclude.length; i++) {
|
|
var excludeItem = exclude[i];
|
|
if (new RegExp(excludeItem).test(name)) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (!stat.isFile()) {
|
|
readDir(filepath, dirname + path$1.sep + name);
|
|
}
|
|
else {
|
|
var data = new Data({
|
|
relativeDir: dirname,
|
|
relativeFile: dirname + path$1.sep + path$1.parse(filepath).base,
|
|
relativeFileNoExt: dirname + path$1.sep + path$1.parse(filepath).name,
|
|
file: path$1.parse(filepath).base,
|
|
filename: path$1.parse(filepath).name,
|
|
absoluteFile: filepath,
|
|
absoluteDir: path$1.parse(filepath).dir,
|
|
});
|
|
files.push(data);
|
|
}
|
|
});
|
|
}
|
|
readDir(filePath);
|
|
return files;
|
|
}
|
|
|
|
function method(opts) {
|
|
return function (target, propertyKey, descriptor) {
|
|
target[propertyKey].$method = opts;
|
|
};
|
|
}
|
|
function route(route) {
|
|
return function (target, propertyKey, descriptor) {
|
|
target[propertyKey].$route = route;
|
|
};
|
|
}
|
|
function config(options) {
|
|
return function (target, propertyKey, descriptor) {
|
|
target[propertyKey].$options = options;
|
|
};
|
|
}
|
|
function auth(isAuth) {
|
|
if (isAuth === void 0) { isAuth = true; }
|
|
return function (target, propertyKey, descriptor) {
|
|
target[propertyKey].$auth = isAuth;
|
|
};
|
|
}
|
|
function validate(validate) {
|
|
return function (target, propertyKey, descriptor) {
|
|
target[propertyKey].$validate = validate;
|
|
};
|
|
}
|
|
function swagger(desc, notes, tags) {
|
|
return function (target, propertyKey, descriptor) {
|
|
target[propertyKey].$swagger = [desc, notes, tags];
|
|
};
|
|
}
|
|
|
|
var path = require("path");
|
|
var fs = require("fs");
|
|
var routes = ["所有路由路径:"];
|
|
var routePlugin = (function () {
|
|
function routePlugin() {
|
|
this.name = "routePlugin";
|
|
this.version = "0.0.1";
|
|
}
|
|
routePlugin.prototype.register = function (server, options) {
|
|
var sourceDir = options.sourceDir;
|
|
var type = options.type || "jwt";
|
|
var files = walkDir(sourceDir);
|
|
files.forEach(function (file) {
|
|
var e_1, _a;
|
|
var filename = file.relativeFileNoExt;
|
|
var array = filename.split(path.sep).slice(1);
|
|
var fileNoExt = removeIndex("/" + array.join("/"));
|
|
var moduleName = path.resolve(sourceDir, filename);
|
|
var obj = require(moduleName);
|
|
if (obj.default) {
|
|
var func = new (obj.default || obj)();
|
|
var prototype = Object.getPrototypeOf(func);
|
|
var keys = Reflect.ownKeys(prototype);
|
|
try {
|
|
for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
|
|
var key = keys_1_1.value;
|
|
if (key !== "constructor") {
|
|
var ff = func[key];
|
|
var method = ff.$method || "GET";
|
|
var route = "";
|
|
if (ff.$route) {
|
|
if (isIndexEnd(fileNoExt)) {
|
|
route = ff.$route;
|
|
}
|
|
else {
|
|
route = fileNoExt + ff.$route;
|
|
}
|
|
}
|
|
else {
|
|
if (isIndexEnd(fileNoExt)) {
|
|
route = fileNoExt + key.toString();
|
|
}
|
|
else {
|
|
route = fileNoExt + "/" + key.toString();
|
|
}
|
|
}
|
|
route = removeIndex(route);
|
|
var options_1 = ff.$options ? ff.$options : {};
|
|
if (!options_1.auth) {
|
|
if (ff.$auth == undefined) {
|
|
if (route.startsWith("/api")) {
|
|
options_1.auth = type;
|
|
}
|
|
else {
|
|
options_1.auth = false;
|
|
}
|
|
}
|
|
else if (ff.$auth) {
|
|
options_1.auth = type;
|
|
}
|
|
else {
|
|
options_1.auth = false;
|
|
}
|
|
}
|
|
if (!options_1.validate) {
|
|
var validateObj = ff.$validate || {};
|
|
if (options_1.auth && type === "jwt") {
|
|
if (validateObj.headers) {
|
|
validateObj.headers = validateObj.headers.keys({
|
|
Authorization: Joi__namespace.string(),
|
|
});
|
|
}
|
|
else {
|
|
validateObj.headers = Joi__namespace.object({
|
|
headers: Joi__namespace.object({
|
|
Authorization: Joi__namespace.string(),
|
|
}).unknown(),
|
|
});
|
|
}
|
|
}
|
|
if (validateObj) {
|
|
options_1.validate = validateObj;
|
|
}
|
|
}
|
|
if (ff.$swagger) {
|
|
options_1.description = ff.$swagger[0];
|
|
options_1.notes = ff.$swagger[1];
|
|
options_1.tags = ff.$swagger[2];
|
|
}
|
|
var str = route;
|
|
if (options_1.auth) {
|
|
str += " 该路由需要权限";
|
|
}
|
|
else {
|
|
str += " 该路由不需要权限";
|
|
}
|
|
routes.push(str);
|
|
server.route({
|
|
method: method,
|
|
path: route,
|
|
handler: ff,
|
|
options: options_1,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
}
|
|
});
|
|
fs.writeFileSync(path.resolve(sourceDir, "route.txt"), routes.join("\n"), {
|
|
encoding: "utf-8",
|
|
});
|
|
};
|
|
return routePlugin;
|
|
}());
|
|
var plugin = new routePlugin();
|
|
|
|
exports.auth = auth;
|
|
exports.config = config;
|
|
exports.method = method;
|
|
exports.plugin = plugin;
|
|
exports.route = route;
|
|
exports.swagger = swagger;
|
|
exports.validate = validate;
|
|
//# sourceMappingURL=hapi-router.cjs.js.map
|
|
|