@feroomjs/vue-routes
Version:
@feroomjs/vue-routes
112 lines (102 loc) • 4.8 kB
JavaScript
;
var feroom = require('feroom');
var eventHttp = require('@moostjs/event-http');
var eventHttp$1 = require('@wooksjs/event-http');
var moost = require('moost');
/******************************************************************************
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 __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function __metadata(metadataKey, metadataValue) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
}
const banner = () => `[${"@feroomjs/vue-routes"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
/* istanbul ignore file */
function logError(error) {
console.error('[91m' + '[1m' + banner() + error + '[0m');
}
exports.VueRoutesController = class VueRoutesController {
constructor(_registry) {
this._registry = _registry;
}
getModuleRoutes(module) {
var _a;
return ((_a = module.config.extensions) === null || _a === void 0 ? void 0 : _a.vueRoutes) || [];
}
renderProp(name, value, indent = '') {
return `${indent} ${JSON.stringify(name)}: ${JSON.stringify(value)}`;
}
renderFunc(name, value, indent = '') {
if (typeof value === 'boolean') {
return this.renderProp(name, value, indent);
}
if (typeof value !== 'string') {
logError(`Prop "${name}" has unsupported type "${typeof value}"`);
return `${indent} ${JSON.stringify(name)}: null`;
}
if (value.startsWith('(') || value.startsWith('async ') || value.startsWith('function')) {
return `${indent} ${JSON.stringify(name)}: ${value}`;
}
return this.renderProp(name, value, indent);
}
renderRoute(route, indent = '') {
let s = `${indent}{\n`;
for (const [name, value] of Object.entries(route)) {
switch (name) {
case 'path':
case 'name':
s += this.renderProp(name, value, indent) + ',\n';
break;
case 'children':
if (value && Array.isArray(value)) {
s += `${indent} "children": [\n`;
for (const child of value) {
s += this.renderRoute(child, indent + ' ') + ',\n';
}
s += `${indent} ],\n`;
}
break;
default: s += this.renderFunc(name, value, indent) + ',\n';
}
}
return s + `${indent}}`;
}
routes() {
try {
eventHttp$1.useSetHeader('content-type').value = 'application/javascript';
}
catch (e) {
//
}
const routes = this._registry.getAllModules().map(m => this.getModuleRoutes(m)).flat(1);
let content = '';
for (const route of routes) {
content += this.renderRoute(route) + ',\n';
}
return `export const vueRoutes = [${content}]\nexport default vueRoutes`;
}
};
__decorate([
eventHttp.Get('/feroom-dynamic/vue-routes.js'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], exports.VueRoutesController.prototype, "routes", null);
exports.VueRoutesController = __decorate([
moost.Controller(),
__metadata("design:paramtypes", [feroom.FeRegistry])
], exports.VueRoutesController);