'use strict' ; import { Request } from 'express' ; import { IConfig } from 'config' ; import { Cache , ImageCacheConstructor } from './abstract' ; export default class CacheFactory { private request : Request ; private config : IConfig ; public static adapter : Record < string , any > = { } ; public constructor ( app_config , req ) { this . config = app_config ; this . request = req ; } public static addAdapter ( type , object ) { CacheFactory . adapter [ type ] = object ; } public getAdapter ( type , ... constructorParams ) { let AdapterClass = CacheFactory . adapter [ type ] ; if ( ! AdapterClass ) { AdapterClass = require ( `./${ type }` ) . default ; } if ( ! AdapterClass ) { throw new Error ( `Invalid adapter ${ type }` ) ; } else { const adapterInstance = new AdapterClass ( this . config , this . request ) ; if ( ( typeof adapterInstance . isValidFor === 'function' ) && ! adapterInstance . isValidFor ( type ) ) { throw new Error ( `Not valid adapter class or adapter is not valid for ${ type }` ) ; } return adapterInstance ; } } } export { CacheFactory } ;