Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@injex/express-plugin

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@injex/express-plugin - npm Package Compare versions

Comparing version 3.5.1 to 4.0.0-alpha.0

15

lib/createConfig.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.noop = void 0;
var tslib_1 = require("tslib");
// tslint:disable-next-line
function noop() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
}
function noop(...args) { }
exports.noop = noop;
function createConfig(config) {
return tslib_1.__assign({ name: "expressApp", app: null, createAppCallback: noop }, config);
return {
name: "expressApp",
app: null,
createAppCallback: noop,
...config,
};
}
exports.default = createConfig;
//# sourceMappingURL=createConfig.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.controller = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function controller() {

@@ -6,0 +6,0 @@ return function (targetConstructor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.del = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function del(path) {

@@ -6,0 +6,0 @@ return function (targetPrototype, methodName, propertyDescriptor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.get = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function get(path) {

@@ -6,0 +6,0 @@ return function (targetPrototype, methodName, propertyDescriptor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.middleware = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function middleware(middlewareNameOrType) {

@@ -6,0 +6,0 @@ return function (targetPrototype, methodName, propertyDescriptor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.patch = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function patch(path) {

@@ -6,0 +6,0 @@ return function (targetPrototype, methodName, propertyDescriptor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.post = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function post(path) {

@@ -6,0 +6,0 @@ return function (targetPrototype, methodName, propertyDescriptor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.put = void 0;
var metadataHandlers_1 = require("../metadataHandlers");
const metadataHandlers_1 = require("../metadataHandlers");
function put(path) {

@@ -6,0 +6,0 @@ return function (targetPrototype, methodName, propertyDescriptor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressPlugin = void 0;
var tslib_1 = require("tslib");
var express = require("express");
var metadataHandlers_1 = require("./metadataHandlers");
var createConfig_1 = require("./createConfig");
var ExpressPlugin = /** @class */ (function () {
function ExpressPlugin(config) {
const express = require("express");
const metadataHandlers_1 = require("./metadataHandlers");
const createConfig_1 = require("./createConfig");
class ExpressPlugin {
constructor(config) {
this.config = createConfig_1.default(config);

@@ -14,38 +13,25 @@ this.controllerModules = [];

}
ExpressPlugin.prototype.apply = function (container) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, app, createAppCallback;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.config, app = _a.app, createAppCallback = _a.createAppCallback;
this.container = container;
if (!app) return [3 /*break*/, 1];
this.app = app;
return [3 /*break*/, 3];
case 1:
this.app = express();
if (!(typeof createAppCallback === "function")) return [3 /*break*/, 3];
return [4 /*yield*/, createAppCallback(this.app)];
case 2:
_b.sent();
_b.label = 3;
case 3:
// save the express app instance for later use
this.container.addObject(this.app, this.config.name);
this.container.hooks.afterModuleCreation.tap(this.handleModule, null, this);
this.container.hooks.afterCreateModules.tap(this.initializeControllers, null, this);
return [2 /*return*/];
}
});
});
};
ExpressPlugin.prototype.initializeControllers = function () {
for (var _i = 0, _a = this.controllerModules; _i < _a.length; _i++) {
var controllerModule = _a[_i];
var _b = metadataHandlers_1.default.getMetadata(controllerModule.metadata.item), _c = _b.routes, routes = _c === void 0 ? [] : _c, _d = _b.middlewares, middlewares = _d === void 0 ? [] : _d;
async apply(container) {
const { app, createAppCallback } = this.config;
this.container = container;
if (app) {
this.app = app;
}
else {
this.app = express();
if (typeof createAppCallback === "function") {
await createAppCallback(this.app);
}
}
// save the express app instance for later use
this.container.addObject(this.app, this.config.name);
this.container.hooks.afterModuleCreation.tap(this.handleModule, null, this);
this.container.hooks.afterCreateModules.tap(this.initializeControllers, null, this);
}
initializeControllers() {
for (const controllerModule of this.controllerModules) {
const { routes = [], middlewares = [] } = metadataHandlers_1.default.getMetadata(controllerModule.metadata.item);
// convert controller handlers to express route handlers
for (var _e = 0, routes_1 = routes; _e < routes_1.length; _e++) {
var route = routes_1[_e];
var handlerMiddlewares = this.getMiddlewareModulesForRoute(route, middlewares);
for (const route of routes) {
const handlerMiddlewares = this.getMiddlewareModulesForRoute(route, middlewares);
controllerModule.metadata.singleton

@@ -58,7 +44,7 @@ ? this.createSingletonRouteHandler(route, controllerModule.module, handlerMiddlewares)

this.controllerModules.length = 0;
};
ExpressPlugin.prototype.handleModule = function (module) {
}
handleModule(module) {
// check if this is a @controller module
if (metadataHandlers_1.default.hasMetadata(module.metadata.item)) {
var controller = metadataHandlers_1.default.getMetadata(module.metadata.item).controller;
const { controller } = metadataHandlers_1.default.getMetadata(module.metadata.item);
// module must be decorated with @controller()

@@ -70,11 +56,11 @@ if (!controller) {

}
};
ExpressPlugin.prototype.getMiddlewareModulesForRoute = function (route, middlewares) {
var routeMiddlewares = [];
var config;
for (var i = 0, len = middlewares.length; i < len; i++) {
}
getMiddlewareModulesForRoute(route, middlewares) {
const routeMiddlewares = [];
let config;
for (let i = 0, len = middlewares.length; i < len; i++) {
config = middlewares[i];
if (config.handler === route.handler) {
// @ts-ignore
var middlewareModule = this.container.getModuleDefinition(config.middleware);
const middlewareModule = this.container.getModuleDefinition(config.middleware);
if (middlewareModule) {

@@ -86,8 +72,6 @@ routeMiddlewares.push(middlewareModule);

return routeMiddlewares;
};
ExpressPlugin.prototype.createSingletonRouteHandler = function (route, controller, middlewares) {
var _a;
if (middlewares === void 0) { middlewares = []; }
var self = this;
var args = [route.path];
}
createSingletonRouteHandler(route, controller, middlewares = []) {
const self = this;
const args = [route.path];
if (middlewares.length) {

@@ -99,69 +83,34 @@ args.push(this.createRouteMiddleware(middlewares));

});
(_a = this.app)[route.method].apply(_a, args);
};
ExpressPlugin.prototype.createFactoryRouteHandler = function (route, controllerFactory, middlewares) {
var _a;
if (middlewares === void 0) { middlewares = []; }
var self = this;
var args = [route.path];
this.app[route.method](...args);
}
createFactoryRouteHandler(route, controllerFactory, middlewares = []) {
const self = this;
const args = [route.path];
if (middlewares.length) {
args.push(this.createRouteMiddleware(middlewares));
}
args.push(function injexExpressPluginRouteHandler(req, res) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var controller;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, controllerFactory()];
case 1:
controller = _a.sent();
self.handleRoute(controller, route.handler, req, res);
return [2 /*return*/];
}
});
});
args.push(async function injexExpressPluginRouteHandler(req, res) {
const controller = await controllerFactory();
self.handleRoute(controller, route.handler, req, res);
});
(_a = this.app)[route.method].apply(_a, args);
};
ExpressPlugin.prototype.handleRoute = function (controller, handler, req, res) {
this.app[route.method](...args);
}
handleRoute(controller, handler, req, res) {
controller[handler](req, res);
};
ExpressPlugin.prototype.createRouteMiddleware = function (middlewares) {
var self = this;
return function (req, res, next) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var chain, i, len, _a, _b, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
chain = [];
i = 0, len = middlewares.length;
_d.label = 1;
case 1:
if (!(i < len)) return [3 /*break*/, 6];
_b = (_a = chain).push;
if (!middlewares[i].metadata.singleton) return [3 /*break*/, 2];
_c = middlewares[i].module;
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, middlewares[i].module()];
case 3:
_c = _d.sent();
_d.label = 4;
case 4:
_b.apply(_a, [_c]);
_d.label = 5;
case 5:
i++;
return [3 /*break*/, 1];
case 6:
self.executeMiddlewareChain(chain, req, res, next);
return [2 /*return*/];
}
});
});
}
createRouteMiddleware(middlewares) {
const self = this;
return async function (req, res, next) {
const chain = [];
for (let i = 0, len = middlewares.length; i < len; i++) {
chain.push(middlewares[i].metadata.singleton
? middlewares[i].module
: await middlewares[i].module());
}
self.executeMiddlewareChain(chain, req, res, next);
};
};
ExpressPlugin.prototype.executeMiddlewareChain = function (middlewares, req, res, next) {
var self = this;
var middleware = middlewares.shift();
}
executeMiddlewareChain(middlewares, req, res, next) {
const self = this;
const middleware = middlewares.shift();
if (middleware) {

@@ -185,6 +134,5 @@ // call each middleware `handle` fn. the middleware next fn

}
};
return ExpressPlugin;
}());
}
}
exports.ExpressPlugin = ExpressPlugin;
//# sourceMappingURL=ExpressPlugin.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressPlugin = exports.middleware = exports.patch = exports.put = exports.del = exports.post = exports.get = exports.controller = void 0;
var tslib_1 = require("tslib");
const tslib_1 = require("tslib");
// decorators

@@ -6,0 +6,0 @@ var controller_1 = require("./decorators/controller");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRouteMetadata = void 0;
var stdlib_1 = require("@injex/stdlib");
var expressMetadataSymbol = Symbol("expressMetadata");
var metadataHandlers = stdlib_1.createMetadataHandlers(expressMetadataSymbol);
const stdlib_1 = require("@injex/stdlib");
const expressMetadataSymbol = Symbol("expressMetadata");
const metadataHandlers = stdlib_1.createMetadataHandlers(expressMetadataSymbol);
function createRouteMetadata(targetConstructor, path, method, handler) {
metadataHandlers.pushMetadata(targetConstructor, "routes", {
path: path,
method: method,
handler: handler
path,
method,
handler
});

@@ -13,0 +13,0 @@ }

{
"name": "@injex/express-plugin",
"version": "3.5.1",
"version": "4.0.0-alpha.0",
"description": "",

@@ -41,4 +41,4 @@ "keywords": [],

"devDependencies": {
"@injex/core": "^3.5.1",
"@injex/node": "^3.5.1",
"@injex/core": "^4.0.0-alpha.0",
"@injex/node": "^4.0.0-alpha.0",
"@types/express": "^4.17.8",

@@ -53,5 +53,5 @@ "express": "^4.17.1",

"dependencies": {
"@injex/stdlib": "^3.5.1"
"@injex/stdlib": "^4.0.0-alpha.0"
},
"gitHead": "32ef46a3a5f5447e1320598e4bc71e03903d0a00"
"gitHead": "c573d14be7e2530d19be7ebd7a6d4a8faf45b507"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc