phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
501 lines (311 loc) • 15.7 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]}); }); } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7;
var _chunkJTSD2LIGjs = require('./chunk-JTSD2LIG.js');
var _chunkDPPDWBYOjs = require('./chunk-DPPDWBYO.js');
var _chunkOV3UJLGPjs = require('./chunk-OV3UJLGP.js');
var _chunk4LLLQOMFjs = require('./chunk-4LLLQOMF.js');
// src/types.ts
var CustomResponse = class {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "CustomResponse");
}
};
// src/modules/base.ts
var _phecdacore = require('phecda-core'); _createStarExport(_phecdacore);
function _ts_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;
}
_chunk4LLLQOMFjs.__name.call(void 0, _ts_decorate, "_ts_decorate");
function _ts_metadata(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
_chunk4LLLQOMFjs.__name.call(void 0, _ts_metadata, "_ts_metadata");
var ServerBase = (_class = class extends _phecdacore.Base {constructor(...args2) { super(...args2); _class.prototype.__init.call(this); }
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "ServerBase");
}
__init() {this.emitter = exports.emitter = _chunkJTSD2LIGjs.emitter}
log(msg, level = "log") {
_chunk4LLLQOMFjs.log.call(void 0, msg, level, this.tag);
}
}, _class);
var HttpBase = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "HttpBase");
}
};
_ts_decorate([
_chunkDPPDWBYOjs.Ctx,
_ts_metadata("design:type", typeof Ctx === "undefined" ? Object : Ctx)
], HttpBase.prototype, "context", void 0);
var RpcBase = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "RpcBase");
}
};
_ts_decorate([
_chunkDPPDWBYOjs.Ctx,
_ts_metadata("design:type", typeof Ctx === "undefined" ? Object : Ctx)
], RpcBase.prototype, "context", void 0);
// src/modules/filter.ts
var PFilter = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "PFilter");
}
constructor(tag) {
super();
this.key = tag || _phecdacore.getTag.call(void 0, this);
_chunkOV3UJLGPjs.addFilter.call(void 0, this.key, this.use.bind(this));
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.filterRecord[this.key];
});
}
};
// src/modules/guard.ts
var PGuard = (_class2 = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "PGuard");
}
__init2() {this.priority = 0}
constructor(tag) {
super();_class2.prototype.__init2.call(this);;
this.key = tag || _phecdacore.getTag.call(void 0, this);
_chunkOV3UJLGPjs.addGuard.call(void 0, this.key, this.use.bind(this), this.priority);
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.guardRecord[this.key];
});
}
}, _class2);
// src/modules/pipe.ts
var PPipe = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "PPipe");
}
constructor(tag) {
super();
this.key = tag || _phecdacore.getTag.call(void 0, this);
_chunkOV3UJLGPjs.addPipe.call(void 0, this.key, this.use.bind(this));
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.pipeRecord[this.key];
});
}
};
// src/modules/addon.ts
var PAddon = (_class3 = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "PAddon");
}
__init3() {this.priority = 0}
constructor(tag) {
super();_class3.prototype.__init3.call(this);;
this.key = tag || _phecdacore.getTag.call(void 0, this);
_chunkOV3UJLGPjs.addAddon.call(void 0, this.key, this.use.bind(this), this.priority);
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.addonRecord[this.key];
});
}
}, _class3);
// src/modules/extension.ts
var PExtension = class extends ServerBase {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "PExtension");
}
constructor(tag) {
super();
const key = this.key = tag || _phecdacore.getTag.call(void 0, this);
if (this.pipe) {
_chunkOV3UJLGPjs.addPipe.call(void 0, key, this.pipe.bind(this));
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.pipeRecord[key];
});
}
if (this.addon) {
_chunkOV3UJLGPjs.addAddon.call(void 0, key, this.addon.bind(this), this.addonPriority);
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.addonRecord[key];
});
}
if (this.guard) {
_chunkOV3UJLGPjs.addGuard.call(void 0, key, this.guard.bind(this), this.guardPriority);
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.guardRecord[key];
});
}
if (this.filter) {
_chunkOV3UJLGPjs.addFilter.call(void 0, key, this.filter.bind(this));
this.onUnmount(() => {
delete _chunkOV3UJLGPjs.Context.filterRecord[key];
});
}
}
};
// src/generator/utils.ts
var _fsextra = require('fs-extra'); var _fsextra2 = _interopRequireDefault(_fsextra);
var Generator = class {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "Generator");
}
constructor(path) {
if (path) this._path = path;
}
get path() {
return this._path || `.ps/${this.name.toLowerCase()}.js`;
}
async output(meta) {
await _fsextra2.default.outputFile(this.path, this.generateCode(meta));
}
};
// src/generator/rpc.ts
var RPCGenerator = (_class4 = class extends Generator {constructor(...args3) { super(...args3); _class4.prototype.__init4.call(this);_class4.prototype.__init5.call(this); }
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "RPCGenerator");
}
__init4() {this.name = "RPC"}
__init5() {this.classMap = {}}
getContent() {
let content = "";
for (const name in this.classMap) {
content += `
export class ${name}{
${Object.values(this.classMap[name]).reduce((p, c) => p + c)}
}`;
}
return content;
}
addMethod(args) {
const { rpc, name, func, tag } = args;
if (!rpc) return;
if (!this.classMap[name]) this.classMap[name] = {};
this.classMap[name][func] = `
${func}(){
return {tag:'${tag}',func:"${func}",isEvent:${!!rpc.isEvent},queue:"${rpc.queue || ""}"}
}
`;
}
generateCode(meta) {
meta.forEach(({ data }) => {
if (data.controller === "rpc") this.addMethod(data);
});
return this.getContent();
}
}, _class4);
// src/generator/http.ts
var HTTPGenerator = (_class5 = class extends Generator {constructor(...args4) { super(...args4); _class5.prototype.__init6.call(this);_class5.prototype.__init7.call(this); }
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "HTTPGenerator");
}
__init6() {this.name = "HTTP"}
__init7() {this.classMap = {}}
getContent() {
let content = "";
for (const name in this.classMap) {
content += `
export class ${name}{
${Object.values(this.classMap[name]).reduce((p, c) => p + c)}
}`;
}
return content;
}
addMethod(args) {
const { http, name, func, params, tag } = args;
if (!_optionalChain([http, 'optionalAccess', _2 => _2.type])) return;
const url = _chunkOV3UJLGPjs.joinUrl.call(void 0, http.prefix, http.route).replace(/\/\:([^\/]*)/g, (_, js) => `/{{${js}}}`);
if (!this.classMap[name]) this.classMap[name] = {};
this.classMap[name][func] = `
${func}(...args){
const ret={tag:"${tag}",func:"${func}",body:{},headers:{},query:{},params:{},method:"${http.type}",url:"${url}"}
${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ""}=args[${i}]
${c.type === "params" ? `ret.url=ret.url.replace('{{${c.key}}}',args[${i}])` : ""}
`, "")}
return ret
}
`;
}
generateCode(meta) {
meta.forEach(({ data }) => {
if (data.controller === "http") this.addMethod(data);
});
return this.getContent();
}
}, _class5);
// src/generator/openapi.ts
var OpenAPIGenerator = (_class6 = class extends Generator {constructor(...args5) { super(...args5); _class6.prototype.__init8.call(this);_class6.prototype.__init9.call(this); }
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "OpenAPIGenerator");
}
__init8() {this.name = "OpenAPI"}
__init9() {this.paths = {}}
getContent() {
return JSON.stringify({
openapi: "3.0.0",
info: {
title: "API Documentation",
version: "1.0.0",
description: "API documentation generated by phecda-server"
},
paths: this.paths
});
}
addMethod(args) {
const { http, tag, func } = args;
if (!_optionalChain([http, 'optionalAccess', _3 => _3.type])) return;
const path = _chunkOV3UJLGPjs.joinUrl.call(void 0, http.prefix, http.route);
if (!this.paths[path]) this.paths[path] = {};
const config = _phecdacore.getMergedMeta.call(void 0, _chunkJTSD2LIGjs.useS.call(void 0, ).getModel(tag), func).openapi;
this.paths[path][http.type] = {
summary: config.summary,
description: config.description,
tags: config.tags || [
tag
],
deprecated: config.deprecated,
parameters: config.parameters,
requestBody: config.requestBody,
responses: config.responses
};
}
generateCode(meta) {
meta.forEach(({ data }) => {
if (data.controller === "http") this.addMethod(data);
});
return this.getContent();
}
}, _class6);
// src/generator/doc.ts
var DocGenerator = (_class7 = class extends Generator {
static {
_chunk4LLLQOMFjs.__name.call(void 0, this, "DocGenerator");
}
__init10() {this.name = "DOC"}
__init11() {this.classMap = {}}
constructor(path) {
super(path || ".ps/doc.json");_class7.prototype.__init10.call(this);_class7.prototype.__init11.call(this);;
}
getContent() {
return JSON.stringify(this.classMap);
}
addMethod(data) {
const { name, func } = data;
if (!data.rawMeta.doc) return;
if (!this.classMap[name]) this.classMap[name] = {};
this.classMap[name][func] = {
doc: data.rawMeta.doc,
params: data.params.filter((item) => item.rawMeta.doc).map((item) => {
return {
doc: item.rawMeta.doc,
index: item.index
};
})
};
}
generateCode(meta) {
meta.forEach(({ data }) => {
if (data.controller) this.addMethod(data);
});
return this.getContent();
}
}, _class7);
// src/index.ts
exports.Addon = _chunkDPPDWBYOjs.Addon; exports.ApiDoc = _chunkDPPDWBYOjs.ApiDoc; exports.Arg = _chunkDPPDWBYOjs.Arg; exports.BadGatewayException = _chunkOV3UJLGPjs.BadGatewayException; exports.BadRequestException = _chunkOV3UJLGPjs.BadRequestException; exports.BaseParam = _chunkDPPDWBYOjs.BaseParam; exports.Body = _chunkDPPDWBYOjs.Body; exports.ConflictException = _chunkOV3UJLGPjs.ConflictException; exports.Context = _chunkOV3UJLGPjs.Context; exports.Controller = _chunkDPPDWBYOjs.Controller; exports.Ctx = _chunkDPPDWBYOjs.Ctx; exports.CustomResponse = CustomResponse; exports.Define = _chunkDPPDWBYOjs.Define; exports.Delete = _chunkDPPDWBYOjs.Delete; exports.DocGenerator = DocGenerator; exports.ERROR_SYMBOL = _chunk4LLLQOMFjs.ERROR_SYMBOL; exports.Exception = _chunkOV3UJLGPjs.Exception; exports.Factory = _chunkJTSD2LIGjs.Factory; exports.Filter = _chunkDPPDWBYOjs.Filter; exports.ForbiddenException = _chunkOV3UJLGPjs.ForbiddenException; exports.FrameworkException = _chunkOV3UJLGPjs.FrameworkException; exports.Generator = Generator; exports.Get = _chunkDPPDWBYOjs.Get; exports.Guard = _chunkDPPDWBYOjs.Guard; exports.HTTPGenerator = HTTPGenerator; exports.Head = _chunkDPPDWBYOjs.Head; exports.Header = _chunkDPPDWBYOjs.Header; exports.HttpBase = HttpBase; exports.IS_DEV = _chunk4LLLQOMFjs.IS_DEV; exports.IS_ONLY_GENERATE = _chunk4LLLQOMFjs.IS_ONLY_GENERATE; exports.IS_PURE = _chunk4LLLQOMFjs.IS_PURE; exports.IS_STRICT = _chunk4LLLQOMFjs.IS_STRICT; exports.InvalidInputException = _chunkOV3UJLGPjs.InvalidInputException; exports.LOG_LEVEL = _chunk4LLLQOMFjs.LOG_LEVEL; exports.ManyFiles = _chunkDPPDWBYOjs.ManyFiles; exports.Meta = _chunkJTSD2LIGjs.Meta; exports.Mixin = _chunk4LLLQOMFjs.Mixin; exports.NotFoundException = _chunkOV3UJLGPjs.NotFoundException; exports.OneFile = _chunkDPPDWBYOjs.OneFile; exports.OpenAPIGenerator = OpenAPIGenerator; exports.PAddon = PAddon; exports.PExtension = PExtension; exports.PFilter = PFilter; exports.PGuard = PGuard; exports.PPipe = PPipe; exports.PS_EXIT_CODE = _chunk4LLLQOMFjs.PS_EXIT_CODE; exports.Param = _chunkDPPDWBYOjs.Param; exports.Patch = _chunkDPPDWBYOjs.Patch; exports.PayloadLargeException = _chunkOV3UJLGPjs.PayloadLargeException; exports.Pipe = _chunkDPPDWBYOjs.Pipe; exports.Post = _chunkDPPDWBYOjs.Post; exports.Put = _chunkDPPDWBYOjs.Put; exports.Query = _chunkDPPDWBYOjs.Query; exports.Queue = _chunkDPPDWBYOjs.Queue; exports.RPCGenerator = RPCGenerator; exports.Route = _chunkDPPDWBYOjs.Route; exports.Rpc = _chunkDPPDWBYOjs.Rpc; exports.RpcBase = RpcBase; exports.Search = _chunkDPPDWBYOjs.Search; exports.ServerBase = ServerBase; exports.ServerPhecda = _chunkJTSD2LIGjs.ServerPhecda; exports.ServiceUnavailableException = _chunkOV3UJLGPjs.ServiceUnavailableException; exports.TimeoutException = _chunkOV3UJLGPjs.TimeoutException; exports.TimerException = _chunkOV3UJLGPjs.TimerException; exports.UnauthorizedException = _chunkOV3UJLGPjs.UnauthorizedException; exports.UndefinedException = _chunkOV3UJLGPjs.UndefinedException; exports.UnsupportedMediaTypeException = _chunkOV3UJLGPjs.UnsupportedMediaTypeException; exports.ValidateException = _chunkOV3UJLGPjs.ValidateException; exports.WorkerException = _chunkOV3UJLGPjs.WorkerException; exports.addAddon = _chunkOV3UJLGPjs.addAddon; exports.addFilter = _chunkOV3UJLGPjs.addFilter; exports.addGuard = _chunkOV3UJLGPjs.addGuard; exports.addPipe = _chunkOV3UJLGPjs.addPipe; exports.defaultPipe = _chunkOV3UJLGPjs.defaultPipe; exports.defaultServerInject = _chunkJTSD2LIGjs.defaultServerInject; exports.emitter = _chunkJTSD2LIGjs.emitter; exports.getLogger = _chunk4LLLQOMFjs.getLogger; exports.log = _chunk4LLLQOMFjs.log; exports.phecdaNamespace = _chunkJTSD2LIGjs.phecdaNamespace; exports.runMiddleware = _chunk4LLLQOMFjs.runMiddleware; exports.setLogger = _chunk4LLLQOMFjs.setLogger; exports.useS = _chunkJTSD2LIGjs.useS;