UNPKG

phecda-server

Version:

server framework that provide IOC/type-reuse/http&rpc-adaptor

622 lines (430 loc) 22.2 kB
"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 _class8; var _class9; var _chunkSKGOTR4Tjs = require('./chunk-SKGOTR4T.js'); var _chunk5ZZAOOKEjs = require('./chunk-5ZZAOOKE.js'); var _chunk76UDKZOJjs = require('./chunk-76UDKZOJ.js'); var _chunkLLF55NZPjs = require('./chunk-LLF55NZP.js'); // src/types.ts var CustomResponse = class { static { _chunkLLF55NZPjs.__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; } _chunkLLF55NZPjs.__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); } _chunkLLF55NZPjs.__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 { _chunkLLF55NZPjs.__name.call(void 0, this, "ServerBase"); } __init() {this.emitter = exports.emitter = _chunkSKGOTR4Tjs.emitter} log(msg, level = "log") { _chunkLLF55NZPjs.log.call(void 0, msg, level, this.tag); } }, _class); var HttpBase = class extends ServerBase { static { _chunkLLF55NZPjs.__name.call(void 0, this, "HttpBase"); } }; _ts_decorate([ _chunk5ZZAOOKEjs.Ctx, _ts_metadata("design:type", typeof Ctx === "undefined" ? Object : Ctx) ], HttpBase.prototype, "context", void 0); var RpcBase = class extends ServerBase { static { _chunkLLF55NZPjs.__name.call(void 0, this, "RpcBase"); } }; _ts_decorate([ _chunk5ZZAOOKEjs.Ctx, _ts_metadata("design:type", typeof Ctx === "undefined" ? Object : Ctx) ], RpcBase.prototype, "context", void 0); // src/modules/filter.ts function _ts_decorate2(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; } _chunkLLF55NZPjs.__name.call(void 0, _ts_decorate2, "_ts_decorate"); function _ts_metadata2(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } _chunkLLF55NZPjs.__name.call(void 0, _ts_metadata2, "_ts_metadata"); var PFilter = class extends ServerBase { static { _chunkLLF55NZPjs.__name.call(void 0, this, "PFilter"); } _init() { this.key = _phecdacore.getTag.call(void 0, this); _chunk76UDKZOJjs.addFilter.call(void 0, this.key, this.use.bind(this)); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.filterRecord[this.key]; }); } }; _ts_decorate2([ _phecdacore.Init, _ts_metadata2("design:type", Function), _ts_metadata2("design:paramtypes", []), _ts_metadata2("design:returntype", void 0) ], PFilter.prototype, "_init", null); // src/modules/guard.ts function _ts_decorate3(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; } _chunkLLF55NZPjs.__name.call(void 0, _ts_decorate3, "_ts_decorate"); function _ts_metadata3(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } _chunkLLF55NZPjs.__name.call(void 0, _ts_metadata3, "_ts_metadata"); var PGuard = (_class2 = class extends ServerBase {constructor(...args3) { super(...args3); _class2.prototype.__init2.call(this); } static { _chunkLLF55NZPjs.__name.call(void 0, this, "PGuard"); } __init2() {this.priority = 0} _init() { this.key = _phecdacore.getTag.call(void 0, this); _chunk76UDKZOJjs.addGuard.call(void 0, this.key, this.use.bind(this), this.priority); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.guardRecord[this.key]; }); } }, _class2); _ts_decorate3([ _phecdacore.Init, _ts_metadata3("design:type", Function), _ts_metadata3("design:paramtypes", []), _ts_metadata3("design:returntype", void 0) ], PGuard.prototype, "_init", null); // src/modules/pipe.ts function _ts_decorate4(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; } _chunkLLF55NZPjs.__name.call(void 0, _ts_decorate4, "_ts_decorate"); function _ts_metadata4(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } _chunkLLF55NZPjs.__name.call(void 0, _ts_metadata4, "_ts_metadata"); var PPipe = class extends ServerBase { static { _chunkLLF55NZPjs.__name.call(void 0, this, "PPipe"); } _init() { this.key = _phecdacore.getTag.call(void 0, this); _chunk76UDKZOJjs.addPipe.call(void 0, this.key, this.use.bind(this)); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.pipeRecord[this.key]; }); } }; _ts_decorate4([ _phecdacore.Init, _ts_metadata4("design:type", Function), _ts_metadata4("design:paramtypes", []), _ts_metadata4("design:returntype", void 0) ], PPipe.prototype, "_init", null); // src/modules/addon.ts function _ts_decorate5(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; } _chunkLLF55NZPjs.__name.call(void 0, _ts_decorate5, "_ts_decorate"); function _ts_metadata5(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } _chunkLLF55NZPjs.__name.call(void 0, _ts_metadata5, "_ts_metadata"); var PAddon = (_class3 = class extends ServerBase {constructor(...args4) { super(...args4); _class3.prototype.__init3.call(this); } static { _chunkLLF55NZPjs.__name.call(void 0, this, "PAddon"); } __init3() {this.priority = 0} _init() { this.key = _phecdacore.getTag.call(void 0, this); _chunk76UDKZOJjs.addAddon.call(void 0, this.key, this.use.bind(this), this.priority); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.addonRecord[this.key]; }); } }, _class3); _ts_decorate5([ _phecdacore.Init, _ts_metadata5("design:type", Function), _ts_metadata5("design:paramtypes", []), _ts_metadata5("design:returntype", void 0) ], PAddon.prototype, "_init", null); // src/modules/extension.ts function _ts_decorate6(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; } _chunkLLF55NZPjs.__name.call(void 0, _ts_decorate6, "_ts_decorate"); function _ts_metadata6(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } _chunkLLF55NZPjs.__name.call(void 0, _ts_metadata6, "_ts_metadata"); var PExtension = class extends ServerBase { static { _chunkLLF55NZPjs.__name.call(void 0, this, "PExtension"); } _init() { this.key = _phecdacore.getTag.call(void 0, this); if (this.pipe) { _chunk76UDKZOJjs.addPipe.call(void 0, this.key, this.pipe.bind(this)); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.pipeRecord[this.key]; }); } if (this.addon) { _chunk76UDKZOJjs.addAddon.call(void 0, this.key, this.addon.bind(this), this.addonPriority); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.addonRecord[this.key]; }); } if (this.guard) { _chunk76UDKZOJjs.addGuard.call(void 0, this.key, this.guard.bind(this), this.guardPriority); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.guardRecord[this.key]; }); } if (this.filter) { _chunk76UDKZOJjs.addFilter.call(void 0, this.key, this.filter.bind(this)); this.onUnmount(() => { delete _chunk76UDKZOJjs.Context.filterRecord[this.key]; }); } } }; _ts_decorate6([ _phecdacore.Init, _ts_metadata6("design:type", Function), _ts_metadata6("design:paramtypes", []), _ts_metadata6("design:returntype", void 0) ], PExtension.prototype, "_init", null); // src/generator/utils.ts var _fsextra = require('fs-extra'); var _fsextra2 = _interopRequireDefault(_fsextra); var Generator = (_class4 = class { static { _chunkLLF55NZPjs.__name.call(void 0, this, "Generator"); } constructor(path) {;_class4.prototype.__init4.call(this); if (path) this._path = path; } __init4() {this.ext = ".js"} get path() { return this._path || `.ps/${this.name.toLowerCase()}${this.ext}`; } async output(meta) { await _fsextra2.default.outputFile(this.path, this.generateCode(meta)); } }, _class4); // src/generator/rpc.ts var RPCGenerator = (_class5 = class extends Generator {constructor(...args5) { super(...args5); _class5.prototype.__init5.call(this);_class5.prototype.__init6.call(this); } static { _chunkLLF55NZPjs.__name.call(void 0, this, "RPCGenerator"); } __init5() {this.name = "RPC"} __init6() {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, method, tag } = args; if (!rpc) return; if (!this.classMap[name]) this.classMap[name] = {}; this.classMap[name][method] = ` ${method}(){ return {tag:'${tag}',method:"${method}",isEvent:${!!rpc.isEvent},queue:"${rpc.queue || ""}"} } `; } generateCode(meta) { meta.forEach(({ data }) => { if (data.controller === "rpc") this.addMethod(data); }); return this.getContent(); } }, _class5); // src/generator/http.ts var HTTPGenerator = (_class6 = class extends Generator {constructor(...args6) { super(...args6); _class6.prototype.__init7.call(this);_class6.prototype.__init8.call(this); } static { _chunkLLF55NZPjs.__name.call(void 0, this, "HTTPGenerator"); } __init7() {this.name = "HTTP"} __init8() {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, method, params, tag } = args; if (!_optionalChain([http, 'optionalAccess', _2 => _2.method])) return; const url = _chunk76UDKZOJjs.joinUrl.call(void 0, http.prefix, http.route).replace(/\/\:([^\/]*)/g, (_, js) => `/{{${js}}}`); if (!this.classMap[name]) this.classMap[name] = {}; this.classMap[name][method] = ` ${method}(...args){ let url="${url}" const ret={tag:"${tag}",method:"${method}",body:{},headers:{},query:{},params:{}} ${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ""}=args[${i}] ${c.type === "params" ? `url=url.replace('{{${c.key}}}',args[${i}])` : ""} `, "")} ret.http={method:"${http.method}",url} return ret } `; } generateCode(meta) { meta.forEach(({ data }) => { if (data.controller === "http") this.addMethod(data); }); return this.getContent(); } }, _class6); // src/generator/openapi.ts var OpenAPIGenerator = (_class7 = class extends Generator {constructor(...args7) { super(...args7); _class7.prototype.__init9.call(this);_class7.prototype.__init10.call(this);_class7.prototype.__init11.call(this); } static { _chunkLLF55NZPjs.__name.call(void 0, this, "OpenAPIGenerator"); } __init9() {this.ext = ".json"} __init10() {this.name = "OpenAPI"} __init11() {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, model) { const { http, tag, method } = args; if (!_optionalChain([http, 'optionalAccess', _3 => _3.method])) return; const config = _phecdacore.getMergedMeta.call(void 0, model, method).openapi; if (!config) return; const path = _chunk76UDKZOJjs.joinUrl.call(void 0, http.prefix, http.route); if (!this.paths[path]) this.paths[path] = {}; this.paths[path][http.method] = { 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, model }) => { if (data.controller === "http") this.addMethod(data, model); }); return this.getContent(); } }, _class7); // src/generator/doc.ts var DocGenerator = (_class8 = class extends Generator { static { _chunkLLF55NZPjs.__name.call(void 0, this, "DocGenerator"); } __init12() {this.name = "DOC"} __init13() {this.classMap = {}} constructor(path) { super(path || ".ps/doc.json");_class8.prototype.__init12.call(this);_class8.prototype.__init13.call(this);; } getContent() { return JSON.stringify(this.classMap); } addMethod(data) { const { name, method, meta } = data; if (!meta.doc) return; if (!this.classMap[name]) this.classMap[name] = {}; this.classMap[name][method] = { doc: meta.doc, params: data.params.filter((item) => item.meta.doc).map((item) => { return { doc: item.meta.doc, index: item.index }; }) }; } generateCode(meta) { meta.forEach(({ data }) => { if (data.controller) this.addMethod(data); }); return this.getContent(); } }, _class8); // src/generator/graph.ts var GraphGenerator = (_class9 = class extends Generator {constructor(...args8) { super(...args8); _class9.prototype.__init14.call(this);_class9.prototype.__init15.call(this); } static { _chunkLLF55NZPjs.__name.call(void 0, this, "GraphGenerator"); } __init14() {this.ext = ".mmd"} __init15() {this.name = "Graph"} generateCode(meta) { const edges = /* @__PURE__ */ new Set(); const nodes = /* @__PURE__ */ new Set(); meta.forEach(({ model }) => { const from = String(_phecdacore.getTag.call(void 0, model)); nodes.add(from); const paramTypes = Reflect.getMetadata("design:paramtypes", model) || []; paramTypes.forEach((dep) => { const to = String(_phecdacore.getTag.call(void 0, dep)); if (to && to !== from) { nodes.add(to); edges.add(`${to} --> ${from}`); } }); }); let result = "graph TD\n"; nodes.forEach((n) => { result += ` ${String(n)} `; }); edges.forEach((e) => { result += ` ${e} `; }); return result; } }, _class9); // src/index.ts exports.Addon = _chunk5ZZAOOKEjs.Addon; exports.ApiDoc = _chunk5ZZAOOKEjs.ApiDoc; exports.Arg = _chunk5ZZAOOKEjs.Arg; exports.BadGatewayException = _chunk76UDKZOJjs.BadGatewayException; exports.BadRequestException = _chunk76UDKZOJjs.BadRequestException; exports.BaseParam = _chunk5ZZAOOKEjs.BaseParam; exports.Body = _chunk5ZZAOOKEjs.Body; exports.ConflictException = _chunk76UDKZOJjs.ConflictException; exports.Context = _chunk76UDKZOJjs.Context; exports.Controller = _chunk5ZZAOOKEjs.Controller; exports.Ctx = _chunk5ZZAOOKEjs.Ctx; exports.CustomResponse = CustomResponse; exports.Define = _chunk5ZZAOOKEjs.Define; exports.Delete = _chunk5ZZAOOKEjs.Delete; exports.DocGenerator = DocGenerator; exports.ERROR_SYMBOL = _chunkLLF55NZPjs.ERROR_SYMBOL; exports.Exception = _chunk76UDKZOJjs.Exception; exports.Factory = _chunkSKGOTR4Tjs.Factory; exports.Filter = _chunk5ZZAOOKEjs.Filter; exports.ForbiddenException = _chunk76UDKZOJjs.ForbiddenException; exports.FrameworkException = _chunk76UDKZOJjs.FrameworkException; exports.Generator = Generator; exports.Get = _chunk5ZZAOOKEjs.Get; exports.GraphGenerator = GraphGenerator; exports.Guard = _chunk5ZZAOOKEjs.Guard; exports.HTTPGenerator = HTTPGenerator; exports.Head = _chunk5ZZAOOKEjs.Head; exports.Header = _chunk5ZZAOOKEjs.Header; exports.HttpBase = HttpBase; exports.IS_DEV = _chunkLLF55NZPjs.IS_DEV; exports.IS_ONLY_GENERATE = _chunkLLF55NZPjs.IS_ONLY_GENERATE; exports.IS_PURE = _chunkLLF55NZPjs.IS_PURE; exports.IS_STRICT = _chunkLLF55NZPjs.IS_STRICT; exports.InvalidInputException = _chunk76UDKZOJjs.InvalidInputException; exports.LOG_LEVEL = _chunkLLF55NZPjs.LOG_LEVEL; exports.Meta = _chunkSKGOTR4Tjs.Meta; exports.Mixin = _chunkLLF55NZPjs.Mixin; exports.NotFoundException = _chunk76UDKZOJjs.NotFoundException; exports.OpenAPIGenerator = OpenAPIGenerator; exports.PAddon = PAddon; exports.PExtension = PExtension; exports.PFilter = PFilter; exports.PGuard = PGuard; exports.PPipe = PPipe; exports.PS_EXIT_CODE = _chunkLLF55NZPjs.PS_EXIT_CODE; exports.Param = _chunk5ZZAOOKEjs.Param; exports.Patch = _chunk5ZZAOOKEjs.Patch; exports.PayloadLargeException = _chunk76UDKZOJjs.PayloadLargeException; exports.Pipe = _chunk5ZZAOOKEjs.Pipe; exports.Post = _chunk5ZZAOOKEjs.Post; exports.Put = _chunk5ZZAOOKEjs.Put; exports.Query = _chunk5ZZAOOKEjs.Query; exports.Queue = _chunk5ZZAOOKEjs.Queue; exports.RPCGenerator = RPCGenerator; exports.Route = _chunk5ZZAOOKEjs.Route; exports.Rpc = _chunk5ZZAOOKEjs.Rpc; exports.RpcBase = RpcBase; exports.Search = _chunk5ZZAOOKEjs.Search; exports.ServerBase = ServerBase; exports.ServerPhecda = _chunkSKGOTR4Tjs.ServerPhecda; exports.ServiceUnavailableException = _chunk76UDKZOJjs.ServiceUnavailableException; exports.TimeoutException = _chunk76UDKZOJjs.TimeoutException; exports.TimerException = _chunk76UDKZOJjs.TimerException; exports.UnauthorizedException = _chunk76UDKZOJjs.UnauthorizedException; exports.UndefinedException = _chunk76UDKZOJjs.UndefinedException; exports.UnsupportedMediaTypeException = _chunk76UDKZOJjs.UnsupportedMediaTypeException; exports.UploadFile = _chunk5ZZAOOKEjs.UploadFile; exports.ValidateException = _chunk76UDKZOJjs.ValidateException; exports.WorkerException = _chunk76UDKZOJjs.WorkerException; exports.addAddon = _chunk76UDKZOJjs.addAddon; exports.addFilter = _chunk76UDKZOJjs.addFilter; exports.addGuard = _chunk76UDKZOJjs.addGuard; exports.addPipe = _chunk76UDKZOJjs.addPipe; exports.defaultFilter = _chunk76UDKZOJjs.defaultFilter; exports.defaultPipe = _chunk76UDKZOJjs.defaultPipe; exports.defaultServerInject = _chunkSKGOTR4Tjs.defaultServerInject; exports.emitter = _chunkSKGOTR4Tjs.emitter; exports.getLogger = _chunkLLF55NZPjs.getLogger; exports.log = _chunkLLF55NZPjs.log; exports.phecdaNamespace = _chunkSKGOTR4Tjs.phecdaNamespace; exports.runMiddleware = _chunkLLF55NZPjs.runMiddleware; exports.setLogger = _chunkLLF55NZPjs.setLogger; exports.useS = _chunkSKGOTR4Tjs.useS;