@foal/core
Advanced tools
Comparing version 1.9.0 to 1.11.0
@@ -7,1 +7,9 @@ /** | ||
export declare type Class<T = any> = new (...args: any[]) => T; | ||
/** | ||
* Interface of a concrete or abstract class from its class definition. | ||
* | ||
* @export | ||
*/ | ||
export interface ClassOrAbstractClass<T = any> extends Function { | ||
prototype: T; | ||
} |
@@ -277,3 +277,2 @@ "use strict"; | ||
} | ||
exports.Config = Config; | ||
Config.cache = { | ||
@@ -284,1 +283,2 @@ dotEnv: undefined, | ||
}; | ||
exports.Config = Config; |
import 'reflect-metadata'; | ||
import { Class } from './class.interface'; | ||
import { Class, ClassOrAbstractClass } from './class.interface'; | ||
export interface IDependency { | ||
@@ -24,3 +24,3 @@ propertyKey: string; | ||
* @template Service | ||
* @param {Class<Service>} serviceClass - The service class. | ||
* @param {ClassOrAbstractClass<Service>} serviceClass - The service class. | ||
* @param {(object|ServiceManager)} [dependencies] - Either a ServiceManager or an | ||
@@ -30,4 +30,4 @@ * object which key/values are the service properties/instances. | ||
*/ | ||
export declare function createService<Service>(serviceClass: Class<Service>, dependencies?: object | ServiceManager): Service; | ||
export declare function createControllerOrService<T>(serviceClass: Class<T>, dependencies?: object | ServiceManager): T; | ||
export declare function createService<Service>(serviceClass: ClassOrAbstractClass<Service>, dependencies?: object | ServiceManager): Service; | ||
export declare function createControllerOrService<T>(serviceClass: ClassOrAbstractClass<T>, dependencies?: object | ServiceManager): T; | ||
/** | ||
@@ -48,11 +48,11 @@ * Identity Mapper that instantiates and returns service singletons. | ||
* | ||
* @param {(string|Class)} [identifier] - The service ID or the service class. | ||
* @param {(string|ClassOrAbstractClass)} [identifier] - The service ID or the service class. | ||
* @returns {Promise<void>} | ||
* @memberof ServiceManager | ||
*/ | ||
boot(identifier?: string | Class): Promise<void>; | ||
boot(identifier?: string | ClassOrAbstractClass): Promise<void>; | ||
/** | ||
* Add manually a service to the identity mapper. | ||
* | ||
* @param {string|Class} identifier - The service ID or the service class. | ||
* @param {string|ClassOrAbstractClass} identifier - The service ID or the service class. | ||
* @param {*} service - The service object (or mock). | ||
@@ -64,3 +64,3 @@ * @param {{ boot: boolean }} [options={ boot: false }] If `boot` is true, the service method "boot" | ||
*/ | ||
set(identifier: string | Class, service: any, options?: { | ||
set(identifier: string | ClassOrAbstractClass, service: any, options?: { | ||
boot: boolean; | ||
@@ -71,9 +71,11 @@ }): this; | ||
* | ||
* @param {string|Class} identifier - The service ID or the service class. | ||
* @param {string|ClassOrAbstractClass} identifier - The service ID or the service class. | ||
* @returns {*} - The service instance. | ||
* @memberof ServiceManager | ||
*/ | ||
get<T>(identifier: Class<T>): T; | ||
get<T>(identifier: ClassOrAbstractClass<T>): T; | ||
get(identifier: string): any; | ||
private bootService; | ||
private getConcreteClassFromConfig; | ||
private getProperty; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// std | ||
const path_1 = require("path"); | ||
// 3p | ||
require("reflect-metadata"); | ||
const config_1 = require("./config"); | ||
/** | ||
@@ -34,3 +38,3 @@ * Decorator injecting a service inside a controller or another service. | ||
* @template Service | ||
* @param {Class<Service>} serviceClass - The service class. | ||
* @param {ClassOrAbstractClass<Service>} serviceClass - The service class. | ||
* @param {(object|ServiceManager)} [dependencies] - Either a ServiceManager or an | ||
@@ -78,3 +82,3 @@ * object which key/values are the service properties/instances. | ||
* | ||
* @param {(string|Class)} [identifier] - The service ID or the service class. | ||
* @param {(string|ClassOrAbstractClass)} [identifier] - The service ID or the service class. | ||
* @returns {Promise<void>} | ||
@@ -100,3 +104,3 @@ * @memberof ServiceManager | ||
* | ||
* @param {string|Class} identifier - The service ID or the service class. | ||
* @param {string|ClassOrAbstractClass} identifier - The service ID or the service class. | ||
* @param {*} service - The service object (or mock). | ||
@@ -130,2 +134,6 @@ * @param {{ boot: boolean }} [options={ boot: false }] If `boot` is true, the service method "boot" | ||
} | ||
if (identifier.hasOwnProperty('concreteClassConfigPath')) { | ||
const concreteClass = this.getConcreteClassFromConfig(identifier); | ||
return this.get(concreteClass); | ||
} | ||
// If the service has not been instantiated yet then do it. | ||
@@ -151,3 +159,48 @@ const dependencies = Reflect.getMetadata('dependencies', identifier.prototype) || []; | ||
} | ||
getConcreteClassFromConfig(cls) { | ||
const concreteClassConfigPath = this.getProperty(cls, 'concreteClassConfigPath', 'string'); | ||
const concreteClassName = this.getProperty(cls, 'concreteClassName', 'string'); | ||
let concreteClassPath; | ||
if (cls.hasOwnProperty('defaultConcreteClassPath')) { | ||
concreteClassPath = config_1.Config.get2(concreteClassConfigPath, 'string', 'local'); | ||
} | ||
else { | ||
concreteClassPath = config_1.Config.getOrThrow(concreteClassConfigPath, 'string'); | ||
} | ||
let prettyConcreteClassPath; | ||
if (concreteClassPath === 'local') { | ||
concreteClassPath = this.getProperty(cls, 'defaultConcreteClassPath', 'string', `[CONFIG] ${cls.name} does not support the "local" option in ${concreteClassConfigPath}.`); | ||
} | ||
else if (concreteClassPath.startsWith('./')) { | ||
prettyConcreteClassPath = concreteClassPath; | ||
concreteClassPath = path_1.join(process.cwd(), 'build', concreteClassPath); | ||
} | ||
prettyConcreteClassPath = prettyConcreteClassPath || concreteClassPath; | ||
let pkg; | ||
try { | ||
pkg = require(concreteClassPath); | ||
} | ||
catch (err) { | ||
// TODO: test this line. | ||
if (err.code !== 'MODULE_NOT_FOUND') { | ||
throw err; | ||
} | ||
throw new Error(`[CONFIG] The package or file ${prettyConcreteClassPath} was not found.`); | ||
} | ||
const concreteClass = this.getProperty(pkg, concreteClassName, 'function', `[CONFIG] ${prettyConcreteClassPath} is not a valid package or file for ${cls.name}:` | ||
+ ` class ${concreteClassName} not found.`, `[CONFIG] ${prettyConcreteClassPath} is not a valid package or file for ${cls.name}:` | ||
+ ` ${concreteClassName} is not a class.`); | ||
return concreteClass; | ||
} | ||
getProperty(obj, propertyKey, type, notFoundMsg, typeMsg) { | ||
if (!obj.hasOwnProperty(propertyKey)) { | ||
throw new Error(notFoundMsg || `[CONFIG] ${obj.name}.${propertyKey} is missing.`); | ||
} | ||
const property = obj[propertyKey]; | ||
if (typeof property !== type) { | ||
throw new Error(typeMsg || `[CONFIG] ${obj.name}.${propertyKey} should be a ${type}.`); | ||
} | ||
return property; | ||
} | ||
} | ||
exports.ServiceManager = ServiceManager; |
@@ -42,3 +42,3 @@ "use strict"; | ||
const subPathItem = subPaths[subPath]; | ||
paths[subControllerPath + subPath] = Object.assign(Object.assign({}, paths[subControllerPath + subPath]), subPathItem); | ||
paths[subControllerPath + subPath] = Object.assign({}, paths[subControllerPath + subPath], subPathItem); | ||
} | ||
@@ -57,3 +57,3 @@ } | ||
.replace(/\:\w*/g, $1 => `{${$1.slice(1)}}`); | ||
paths[path] = Object.assign(Object.assign({}, paths[path]), { [httpMethod.toLowerCase()]: utils_2.mergeOperations(operation, metadata_getters_1.getApiCompleteOperation(controllerClass, controllers.get(controllerClass), propertyKey)) }); | ||
paths[path] = Object.assign({}, paths[path], { [httpMethod.toLowerCase()]: utils_2.mergeOperations(operation, metadata_getters_1.getApiCompleteOperation(controllerClass, controllers.get(controllerClass), propertyKey)) }); | ||
} | ||
@@ -60,0 +60,0 @@ return { components, paths, tags }; |
export { removeSessionCookie } from './remove-session-cookie'; | ||
export { SessionStore, SessionOptions } from './session-store'; | ||
export { Store, SessionStore, SessionOptions } from './session-store'; | ||
export { Session } from './session'; | ||
@@ -4,0 +4,0 @@ export { setSessionCookie } from './set-session-cookie'; |
@@ -6,2 +6,3 @@ "use strict"; | ||
var session_store_1 = require("./session-store"); | ||
exports.Store = session_store_1.Store; | ||
exports.SessionStore = session_store_1.SessionStore; | ||
@@ -8,0 +9,0 @@ var session_1 = require("./session"); |
@@ -11,9 +11,11 @@ import { Session } from './session'; | ||
* | ||
* Examples of SessionStore: TypeORMStore, RedisStore, MongoDBStore. | ||
* Examples of Store: TypeORMStore, RedisStore, MongoDBStore. | ||
* | ||
* @export | ||
* @abstract | ||
* @class SessionStore | ||
* @class Store | ||
*/ | ||
export declare abstract class SessionStore { | ||
export declare abstract class Store { | ||
static concreteClassConfigPath: string; | ||
static concreteClassName: string; | ||
/** | ||
@@ -35,3 +37,3 @@ * Read session expiration timeouts from the configuration. | ||
* @returns {{ inactivity: number , absolute: number }} The expiration timeouts | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -49,3 +51,3 @@ static getExpirationTimeouts(): { | ||
* @returns {Promise<Session>} The created session. | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -66,3 +68,3 @@ createAndSaveSessionFromUser(user: { | ||
* @returns {Promise<Session>} The created session. | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -78,3 +80,3 @@ abstract createAndSaveSession(sessionContent: object, options?: SessionOptions): Promise<Session>; | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -88,3 +90,3 @@ abstract update(session: Session): Promise<void>; | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -100,3 +102,3 @@ abstract destroy(sessionID: string): Promise<void>; | ||
* @returns {(Promise<Session|undefined>)} The Session object. | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -113,3 +115,3 @@ abstract read(sessionID: string): Promise<Session | undefined>; | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -122,3 +124,3 @@ abstract extendLifeTime(sessionID: string): Promise<void>; | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -133,3 +135,3 @@ abstract clear(): Promise<void>; | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -142,3 +144,3 @@ abstract cleanUpExpiredSessions(): Promise<void>; | ||
* @returns {Promise<string>} - The session ID. | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -154,5 +156,6 @@ protected generateSessionID(): Promise<string>; | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
protected applySessionOptions(content: object, options: SessionOptions): Promise<void>; | ||
} | ||
export { Store as SessionStore }; |
@@ -13,9 +13,9 @@ "use strict"; | ||
* | ||
* Examples of SessionStore: TypeORMStore, RedisStore, MongoDBStore. | ||
* Examples of Store: TypeORMStore, RedisStore, MongoDBStore. | ||
* | ||
* @export | ||
* @abstract | ||
* @class SessionStore | ||
* @class Store | ||
*/ | ||
class SessionStore { | ||
class Store { | ||
/** | ||
@@ -37,3 +37,3 @@ * Read session expiration timeouts from the configuration. | ||
* @returns {{ inactivity: number , absolute: number }} The expiration timeouts | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -63,3 +63,3 @@ static getExpirationTimeouts() { | ||
* @returns {Promise<Session>} The created session. | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -74,3 +74,3 @@ createAndSaveSessionFromUser(user, options) { | ||
* @returns {Promise<string>} - The session ID. | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -88,3 +88,3 @@ async generateSessionID() { | ||
* @returns {Promise<void>} | ||
* @memberof SessionStore | ||
* @memberof Store | ||
*/ | ||
@@ -97,2 +97,5 @@ async applySessionOptions(content, options) { | ||
} | ||
exports.SessionStore = SessionStore; | ||
Store.concreteClassConfigPath = 'settings.session.store'; | ||
Store.concreteClassName = 'ConcreteSessionStore'; | ||
exports.Store = Store; | ||
exports.SessionStore = Store; |
import { HookDecorator } from '../core'; | ||
import { TokenOptions } from './token.hook'; | ||
export declare function TokenOptional(options: TokenOptions): HookDecorator; | ||
export declare function TokenOptional(options?: TokenOptions): HookDecorator; |
@@ -9,3 +9,3 @@ "use strict"; | ||
// TODO: Add missing documentation. | ||
function TokenOptional(options) { | ||
function TokenOptional(options = {}) { | ||
return (target, propertyKey) => { | ||
@@ -12,0 +12,0 @@ token_hook_1.Token(false, options)(target, propertyKey); |
import { HookDecorator } from '../core'; | ||
import { TokenOptions } from './token.hook'; | ||
export declare function TokenRequired(options: TokenOptions): HookDecorator; | ||
export declare function TokenRequired(options?: TokenOptions): HookDecorator; |
@@ -9,3 +9,3 @@ "use strict"; | ||
// TODO: Add missing documentation. | ||
function TokenRequired(options) { | ||
function TokenRequired(options = {}) { | ||
return (target, propertyKey) => { | ||
@@ -12,0 +12,0 @@ token_hook_1.Token(true, options)(target, propertyKey); |
@@ -5,3 +5,3 @@ import { Class, HookDecorator } from '../core'; | ||
user?: (id: string | number) => Promise<any | undefined>; | ||
store: Class<SessionStore>; | ||
store?: Class<SessionStore>; | ||
cookie?: boolean; | ||
@@ -8,0 +8,0 @@ redirectTo?: string; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const session_1 = require("./session"); | ||
const session_store_1 = require("./session-store"); | ||
const set_session_cookie_1 = require("./set-session-cookie"); | ||
@@ -23,2 +24,4 @@ // TODO: Add missing documentation. | ||
return core_1.Hook(async (ctx, services) => { | ||
const ConcreteSessionStore = options.store || session_store_1.SessionStore; | ||
const store = services.get(ConcreteSessionStore); | ||
const cookieName = core_1.Config.get2('settings.session.cookie.name', 'string', constants_1.SESSION_DEFAULT_COOKIE_NAME); | ||
@@ -73,3 +76,2 @@ /* Validate the request */ | ||
/* Verify the session ID */ | ||
const store = services.get(options.store); | ||
const session = await store.read(sessionID); | ||
@@ -76,0 +78,0 @@ if (!session) { |
{ | ||
"name": "@foal/core", | ||
"version": "1.9.0", | ||
"version": "1.11.0", | ||
"description": "A Node.js and TypeScript framework, all-inclusive.", | ||
@@ -91,3 +91,4 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@foal/ejs": "^1.9.0", | ||
"@foal/ejs": "^1.11.0", | ||
"@foal/internal-test": "^1.10.0", | ||
"@types/mocha": "~2.2.43", | ||
@@ -113,3 +114,3 @@ "@types/node": "~10.1.2", | ||
}, | ||
"gitHead": "c0e648721ef9e8cd5a3f645d06ea660605f44ddb" | ||
"gitHead": "7b5068974b99fbfcbc4514321b2870d02bd4076a" | ||
} |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
353614
8948
21
12