@hyperstackjs/hypernight
Advanced tools
+485
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/index.ts | ||
| var src_exports = {}; | ||
| __export(src_exports, { | ||
| All: () => All, | ||
| Checkout: () => Checkout, | ||
| ChildControllers: () => ChildControllers, | ||
| Children: () => Children, | ||
| ClassErrorMiddleware: () => ClassErrorMiddleware, | ||
| ClassMiddleware: () => ClassMiddleware, | ||
| ClassOptions: () => ClassOptions, | ||
| ClassWrapper: () => ClassWrapper, | ||
| Controller: () => Controller, | ||
| Copy: () => Copy, | ||
| Delete: () => Delete, | ||
| ErrorMiddleware: () => ErrorMiddleware, | ||
| Get: () => Get, | ||
| Head: () => Head, | ||
| Lock: () => Lock, | ||
| MSearch: () => MSearch, | ||
| Merge: () => Merge, | ||
| Middleware: () => Middleware, | ||
| Mkactivity: () => Mkactivity, | ||
| Mkcol: () => Mkcol, | ||
| Move: () => Move, | ||
| Notify: () => Notify, | ||
| Options: () => Options, | ||
| Patch: () => Patch, | ||
| Post: () => Post, | ||
| Purge: () => Purge, | ||
| Put: () => Put, | ||
| Report: () => Report, | ||
| Search: () => Search, | ||
| Server: () => Server, | ||
| Subscribe: () => Subscribe, | ||
| Trace: () => Trace, | ||
| Unlock: () => Unlock, | ||
| Unsubscribe: () => Unsubscribe, | ||
| Wrapper: () => Wrapper, | ||
| addBasePathToClassMetadata: () => addBasePathToClassMetadata, | ||
| addChildControllersToClassMetadata: () => addChildControllersToClassMetadata, | ||
| addClassOptionsToClassMetadata: () => addClassOptionsToClassMetadata, | ||
| addErrorMiddlewareToMetadata: () => addErrorMiddlewareToMetadata, | ||
| addHttpVerbToMethodMetadata: () => addHttpVerbToMethodMetadata, | ||
| addMiddlewareToMetadata: () => addMiddlewareToMetadata, | ||
| addWrapperToMetadata: () => addWrapperToMetadata | ||
| }); | ||
| module.exports = __toCommonJS(src_exports); | ||
| // src/decorators/types.ts | ||
| var classMetadataKey = Symbol("Class Metadata Key"); | ||
| // src/decorators/class.ts | ||
| function Controller(path) { | ||
| return (target) => { | ||
| addBasePathToClassMetadata(target.prototype, `/${path}`); | ||
| }; | ||
| } | ||
| function ClassOptions(options) { | ||
| return (target) => { | ||
| addClassOptionsToClassMetadata(target.prototype, options); | ||
| }; | ||
| } | ||
| function Children(children) { | ||
| console.log("Warning: @Children decorator is deprecated. Use ChildControllers instead."); | ||
| return (target) => { | ||
| addChildControllersToClassMetadata(target.prototype, children); | ||
| }; | ||
| } | ||
| function ChildControllers(children) { | ||
| return (target) => { | ||
| addChildControllersToClassMetadata(target.prototype, children); | ||
| }; | ||
| } | ||
| function addBasePathToClassMetadata(target, basePath) { | ||
| let metadata = Reflect.getOwnMetadata(classMetadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| metadata.basePath = basePath; | ||
| Reflect.defineMetadata(classMetadataKey, metadata, target); | ||
| } | ||
| function addClassOptionsToClassMetadata(target, options) { | ||
| let metadata = Reflect.getOwnMetadata(classMetadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| metadata.options = options; | ||
| Reflect.defineMetadata(classMetadataKey, metadata, target); | ||
| } | ||
| function addChildControllersToClassMetadata(target, childControllers) { | ||
| let metadata = Reflect.getOwnMetadata(classMetadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| if (!metadata.childControllers) { | ||
| metadata.childControllers = []; | ||
| } | ||
| let newArr; | ||
| if (childControllers instanceof Array) { | ||
| newArr = childControllers.slice(); | ||
| } else { | ||
| newArr = [childControllers]; | ||
| } | ||
| newArr.push(...metadata.childControllers); | ||
| metadata.childControllers = newArr; | ||
| Reflect.defineMetadata(classMetadataKey, metadata, target); | ||
| } | ||
| // src/decorators/method.ts | ||
| function All(path) { | ||
| return helperForRoutes("all", path); | ||
| } | ||
| function Checkout(path) { | ||
| return helperForRoutes("checkout" /* CHECKOUT */, path); | ||
| } | ||
| function Copy(path) { | ||
| return helperForRoutes("copy" /* COPY */, path); | ||
| } | ||
| function Delete(path) { | ||
| return helperForRoutes("delete" /* DELETE */, path); | ||
| } | ||
| function Get(path) { | ||
| return helperForRoutes("get" /* GET */, path); | ||
| } | ||
| function Head(path) { | ||
| return helperForRoutes("head" /* HEAD */, path); | ||
| } | ||
| function Lock(path) { | ||
| return helperForRoutes("lock" /* LOCK */, path); | ||
| } | ||
| function Merge(path) { | ||
| return helperForRoutes("merge" /* MERGE */, path); | ||
| } | ||
| function Mkactivity(path) { | ||
| return helperForRoutes("mkactivity" /* MKACTIVITY */, path); | ||
| } | ||
| function Mkcol(path) { | ||
| return helperForRoutes("mkcol" /* MKCOL */, path); | ||
| } | ||
| function Move(path) { | ||
| return helperForRoutes("move" /* MOVE */, path); | ||
| } | ||
| function MSearch(path) { | ||
| return helperForRoutes("m-search" /* MSEARCH */, path); | ||
| } | ||
| function Notify(path) { | ||
| return helperForRoutes("notify" /* NOTIFY */, path); | ||
| } | ||
| function Options(path) { | ||
| return helperForRoutes("options" /* OPTIONS */, path); | ||
| } | ||
| function Patch(path) { | ||
| return helperForRoutes("patch" /* PATCH */, path); | ||
| } | ||
| function Post(path) { | ||
| return helperForRoutes("post" /* POST */, path); | ||
| } | ||
| function Purge(path) { | ||
| return helperForRoutes("purge" /* PURGE */, path); | ||
| } | ||
| function Put(path) { | ||
| return helperForRoutes("put" /* PUT */, path); | ||
| } | ||
| function Report(path) { | ||
| return helperForRoutes("report" /* REPORT */, path); | ||
| } | ||
| function Search(path) { | ||
| return helperForRoutes("search" /* SEARCH */, path); | ||
| } | ||
| function Subscribe(path) { | ||
| return helperForRoutes("subscribe" /* SUBSCRIBE */, path); | ||
| } | ||
| function Trace(path) { | ||
| return helperForRoutes("trace" /* TRACE */, path); | ||
| } | ||
| function Unlock(path) { | ||
| return helperForRoutes("unlock" /* UNLOCK */, path); | ||
| } | ||
| function Unsubscribe(path) { | ||
| return helperForRoutes("unsubscribe" /* UNSUBSCRIBE */, path); | ||
| } | ||
| function helperForRoutes(httpVerb, path) { | ||
| return (target, propertyKey) => { | ||
| let newPath; | ||
| if (path === void 0) { | ||
| newPath = ""; | ||
| } else if (path instanceof RegExp) { | ||
| newPath = addForwardSlashToFrontOfRegex(path); | ||
| } else { | ||
| newPath = `/${path}`; | ||
| } | ||
| addHttpVerbToMethodMetadata(target, propertyKey, httpVerb, newPath); | ||
| }; | ||
| } | ||
| function addForwardSlashToFrontOfRegex(regex) { | ||
| if (regex.toString().charAt(1) === "^") { | ||
| return RegExp(`/${regex.toString().slice(2).replace(/\/$/, "")}`); | ||
| } else { | ||
| return new RegExp(`/.*${regex.toString().slice(1).replace(/\/$/, "")}`); | ||
| } | ||
| } | ||
| function addHttpVerbToMethodMetadata(target, metadataKey, httpDecorator, path) { | ||
| let metadata = Reflect.getOwnMetadata(metadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| if (!metadata.httpRoutes) { | ||
| metadata.httpRoutes = []; | ||
| } | ||
| const newArr = [ | ||
| { | ||
| httpDecorator, | ||
| path | ||
| } | ||
| ]; | ||
| newArr.push(...metadata.httpRoutes); | ||
| metadata.httpRoutes = newArr; | ||
| Reflect.defineMetadata(metadataKey, metadata, target); | ||
| } | ||
| // src/decorators/middleware.ts | ||
| function Middleware(middleware) { | ||
| return (target, propertyKey) => { | ||
| addMiddlewareToMetadata(target, propertyKey, middleware); | ||
| }; | ||
| } | ||
| function ErrorMiddleware(errorMiddleware) { | ||
| return (target, propertyKey) => { | ||
| addErrorMiddlewareToMetadata(target, propertyKey, errorMiddleware); | ||
| }; | ||
| } | ||
| function ClassMiddleware(middleware) { | ||
| return (target) => { | ||
| addMiddlewareToMetadata(target.prototype, classMetadataKey, middleware); | ||
| }; | ||
| } | ||
| function ClassErrorMiddleware(errorMiddleware) { | ||
| return (target) => { | ||
| addErrorMiddlewareToMetadata(target.prototype, classMetadataKey, errorMiddleware); | ||
| }; | ||
| } | ||
| function addMiddlewareToMetadata(target, metadataKey, middlewares) { | ||
| let metadata = Reflect.getOwnMetadata(metadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| if (!metadata.middlewares) { | ||
| metadata.middlewares = []; | ||
| } | ||
| let newArr; | ||
| if (middlewares instanceof Array) { | ||
| newArr = middlewares.slice(); | ||
| } else { | ||
| newArr = [middlewares]; | ||
| } | ||
| newArr.push(...metadata.middlewares); | ||
| metadata.middlewares = newArr; | ||
| Reflect.defineMetadata(metadataKey, metadata, target); | ||
| } | ||
| function addErrorMiddlewareToMetadata(target, metadataKey, errorMiddlewares) { | ||
| let metadata = Reflect.getOwnMetadata(metadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| if (!metadata.errorMiddlewares) { | ||
| metadata.errorMiddlewares = []; | ||
| } | ||
| let newArr; | ||
| if (errorMiddlewares instanceof Array) { | ||
| newArr = errorMiddlewares.slice(); | ||
| } else { | ||
| newArr = [errorMiddlewares]; | ||
| } | ||
| newArr.push(...metadata.errorMiddlewares); | ||
| metadata.errorMiddlewares = newArr; | ||
| Reflect.defineMetadata(metadataKey, metadata, target); | ||
| } | ||
| // src/decorators/wrapper.ts | ||
| function Wrapper(wrapper) { | ||
| return (target, propertyKey) => { | ||
| addWrapperToMetadata(target, propertyKey, wrapper); | ||
| }; | ||
| } | ||
| function ClassWrapper(wrapper) { | ||
| return (target) => { | ||
| addWrapperToMetadata(target.prototype, classMetadataKey, wrapper); | ||
| }; | ||
| } | ||
| function addWrapperToMetadata(target, metadataKey, wrapper) { | ||
| let metadata = Reflect.getOwnMetadata(metadataKey, target); | ||
| if (!metadata) { | ||
| metadata = {}; | ||
| } | ||
| metadata.wrapper = wrapper; | ||
| Reflect.defineMetadata(metadataKey, metadata, target); | ||
| } | ||
| // src/Server.ts | ||
| var import_reflect_metadata = require("reflect-metadata"); | ||
| var _Server = class { | ||
| constructor(showLogs) { | ||
| this._showLogs = false; | ||
| const express = require("express"); | ||
| this._app = express(); | ||
| this._showLogs = showLogs || false; | ||
| } | ||
| get app() { | ||
| return this._app; | ||
| } | ||
| get showLogs() { | ||
| return this._showLogs; | ||
| } | ||
| set showLogs(showLogs) { | ||
| this._showLogs = showLogs; | ||
| } | ||
| addControllers(controllers, routerLib, globalMiddleware) { | ||
| controllers = Array.isArray(controllers) ? controllers : [controllers]; | ||
| const { Router } = require("express"); | ||
| const routerLibrary = routerLib || Router; | ||
| controllers.forEach((controller) => { | ||
| if (controller) { | ||
| const routerAndPath = this.getRouter(routerLibrary, controller); | ||
| if (routerAndPath) { | ||
| if (globalMiddleware) { | ||
| this.app.use(routerAndPath.basePath, globalMiddleware, routerAndPath.router); | ||
| } else { | ||
| this.app.use(routerAndPath.basePath, routerAndPath.router); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| wrapErrorMiddleware(errorMiddleware, requestHandler) { | ||
| return (req, res, next) => { | ||
| try { | ||
| requestHandler(req, res, next); | ||
| } catch (error) { | ||
| errorMiddleware(error, req, res, next); | ||
| } | ||
| }; | ||
| } | ||
| getRouter(routerLibrary, controller) { | ||
| const prototype = Object.getPrototypeOf(controller); | ||
| const classMetadata = Reflect.getOwnMetadata(classMetadataKey, prototype); | ||
| if (!classMetadata) { | ||
| return null; | ||
| } | ||
| const { | ||
| basePath, | ||
| childControllers: children, | ||
| errorMiddlewares: classErrorMiddleware, | ||
| middlewares: classMiddleware, | ||
| options, | ||
| wrapper: classWrapper | ||
| } = classMetadata; | ||
| if (!basePath) { | ||
| return null; | ||
| } | ||
| const router = routerLibrary(options); | ||
| if (this.showLogs) { | ||
| console.log(`${_Server.LOG_STR} "${controller.constructor.name}"`); | ||
| } | ||
| if (classMiddleware) { | ||
| router.use(classMiddleware); | ||
| } | ||
| let members = Object.getOwnPropertyNames(controller); | ||
| members = members.concat(Object.getOwnPropertyNames(prototype)); | ||
| members.forEach((member) => { | ||
| const methodMetadata = Reflect.getOwnMetadata(member, prototype); | ||
| if (methodMetadata) { | ||
| const { | ||
| httpRoutes, | ||
| middlewares, | ||
| errorMiddlewares, | ||
| wrapper | ||
| } = methodMetadata; | ||
| let callBack = (...args) => { | ||
| return controller[member](...args); | ||
| }; | ||
| if (classWrapper) { | ||
| callBack = classWrapper(callBack); | ||
| } | ||
| if (wrapper) { | ||
| callBack = wrapper(callBack); | ||
| } | ||
| if (errorMiddlewares) { | ||
| errorMiddlewares.forEach((errorMiddleware) => { | ||
| callBack = this.wrapErrorMiddleware(errorMiddleware, callBack); | ||
| }); | ||
| } | ||
| if (httpRoutes) { | ||
| httpRoutes.forEach((route) => { | ||
| const { httpDecorator, path } = route; | ||
| if (middlewares) { | ||
| router[httpDecorator](path, middlewares, callBack); | ||
| } else { | ||
| router[httpDecorator](path, callBack); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| if (children) { | ||
| children.forEach((child) => { | ||
| const childRouterAndPath = this.getRouter(routerLibrary, child); | ||
| if (childRouterAndPath) { | ||
| router.use(childRouterAndPath.basePath, childRouterAndPath.router); | ||
| } | ||
| }); | ||
| } | ||
| if (classErrorMiddleware) { | ||
| router.use(classErrorMiddleware); | ||
| } | ||
| return { | ||
| basePath, | ||
| router | ||
| }; | ||
| } | ||
| }; | ||
| var Server = _Server; | ||
| Server.LOG_STR = "Setting up controller:"; | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| All, | ||
| Checkout, | ||
| ChildControllers, | ||
| Children, | ||
| ClassErrorMiddleware, | ||
| ClassMiddleware, | ||
| ClassOptions, | ||
| ClassWrapper, | ||
| Controller, | ||
| Copy, | ||
| Delete, | ||
| ErrorMiddleware, | ||
| Get, | ||
| Head, | ||
| Lock, | ||
| MSearch, | ||
| Merge, | ||
| Middleware, | ||
| Mkactivity, | ||
| Mkcol, | ||
| Move, | ||
| Notify, | ||
| Options, | ||
| Patch, | ||
| Post, | ||
| Purge, | ||
| Put, | ||
| Report, | ||
| Search, | ||
| Server, | ||
| Subscribe, | ||
| Trace, | ||
| Unlock, | ||
| Unsubscribe, | ||
| Wrapper, | ||
| addBasePathToClassMetadata, | ||
| addChildControllersToClassMetadata, | ||
| addClassOptionsToClassMetadata, | ||
| addErrorMiddlewareToMetadata, | ||
| addHttpVerbToMethodMetadata, | ||
| addMiddlewareToMetadata, | ||
| addWrapperToMetadata | ||
| }); |
+1
-1
| { | ||
| "name": "@hyperstackjs/hypernight", | ||
| "version": "1.0.30", | ||
| "version": "1.0.31", | ||
| "description": "An updated and tweaked fork of @overnight/core to be used in hyperstack", | ||
@@ -5,0 +5,0 @@ "author": "Sean Maxwell & Hyperstackjs Authors", |
83004
19.76%20
5.26%2106
29.36%