Refactor class loading system

Signed-off-by: Marius David Wieschollek's avatarMarius David Wieschollek <passwords.public@mdns.eu>
parent a64a98d9
This diff is collapsed.
import ConfigurationError from "../Exception/ConfigruationError";
export default class BaseApi {
/**
*
* @param server
* @param config
* @param classes
*/
constructor(server, config, classes) {
this._setConfig(config);
this._classes = classes;
this._classes.setInstance('model.server', server);
this._classes.setInstance('api', this);
this._server = server;
this._session = this._classes.getInstance('model.session', server.getUser(), server.getToken());
this._events = this._classes.getInstance('event.event');
}
/**
*
* @param {String} event
* @param {Function} listener
*/
on(event, listener) {
this._events.on(event, listener);
}
/**
*
* @param {String} event
* @param {Function} listener
*/
once(event, listener) {
this._events.once(event, listener);
}
/**
*
* @param {String} event
* @param {Function} listener
*/
off(event, listener) {
this._events.off(event, listener);
}
/**
*
* @param {String} event
* @param {Object} data
*/
emit(event, data) {
this._events.emit(event, data);
}
/**
*
* @return {Server}
*/
getServer() {
return this._server;
}
/**
*
* @return {ApiRequest}
*/
getRequest() {
/** @type {ApiRequest} **/
let request = this.getClass('network.request', this, this._server.getApiUrl(), this.getSession());
if(this._config.userAgent !== null) {
request.setUserAgent(this._config.userAgent);
}
return request;
}
/**
* @returns {Session}
*/
getSession() {
return this._session
.setUser(this._server.getUser())
.setToken(this._server.getToken());
}
/**
*
* @returns {SessionAuthorization}
*/
getSessionAuthorization() {
return this.getInstance('authorization.session');
}
/**
*
* @returns {PasswordRepository}
*/
getPasswordRepository() {
return this.getInstance('repository.password');
}
/**
*
* @returns {FolderRepository}
*/
getFolderRepository() {
return this.getInstance('repository.folder');
}
/**
*
* @returns {TagRepository}
*/
getTagRepository() {
return this.getInstance('repository.tag');
}
/**
*
* @returns {CSEv1Encryption}
*/
getCseV1Encryption() {
return this.getInstance('encryption.csev1');
}
/**
*
* @returns {CSEv1Encryption}
*/
getDefaultEncryption() {
let mode = 'auto';
if(this._config.hasOwnProperty('defaultEncryption')) {
mode = this._config.defaultEncryption;
}
if(mode === 'none') {
return this.getInstance('encryption.none');
}
if(mode === 'csev1') {
return this.getInstance('encryption.csev1');
}
let csev1 = this.getInstance('encryption.csev1');
if(csev1.enabled()) return csev1;
return this.getInstance('encryption.none');
}
/**
*
* @param parameters
* @return {*}
*/
getInstance(...parameters) {
return this._classes.getInstance(...parameters)
}
/**
*
* @param parameters
* @return {*}
*/
setInstance(...parameters) {
return this._classes.setInstance(...parameters)
}
/**
*
* @param parameters
* @return {*}
*/
getClass(...parameters) {
return this._classes.getClass(...parameters)
}
/**
*
* @param config
* @private
*/
_setConfig(config) {
if(!config.hasOwnProperty('userAgent')) {
config.userAgent = null;
}
if(config.hasOwnProperty('defaultEncryption') && ['auto', 'none', 'csev1'].indexOf(config.defaultEncryption) === -1) {
throw new ConfigurationError('Invalid default encryption');
} else {
config.defaultEncryption = 'auto';
}
this._config = config;
}
}
\ No newline at end of file
import ObjectMerger from "../Utility/ObjectMerger";
export default class BaseClassLoader {
constructor(classes = {}) {
this._classes = ObjectMerger.merge(this._getDefaultClasses(), classes);
this._instances = {};
}
/**
*
* @param {String} name
* @param {*} properties
* @return {Object}
* @api
*/
getInstance(name, ...properties) {
if(!this._instances.hasOwnProperty(name) || !this._instances[name]) {
this._instances[name] = this.getClass(name, ...properties);
}
return this._instances[name];
}
/**
*
* @param {String} name
* @param {Object} object
* @return {BaseClassLoader}
* @api
*/
setInstance(name, object) {
this._instances[name] = object;
return this;
}
/**
*
* @param {String} name
* @param {*} properties
* @return {Object}
*/
getClass(name, ...properties) {
let path = name.split('.');
if(!this._classes.hasOwnProperty(path[0]) || !this._classes[path[0]].hasOwnProperty(path[1])) {
throw new Error(`The class ${name} does not exist`);
}
let creator = this._classes[path[0]][path[1]];
if(creator instanceof Function) {
if(!creator.prototype || creator.hasOwnProperty('arguments') && creator.hasOwnProperty('caller')) {
return creator(...properties);
}
return new creator(...properties);
} else {
return creator;
}
}
/**
*
* @param {String} name
* @param {Object} constructor
* @return {BaseClassLoader}
* @api
*/
registerClass(name, constructor) {
let path = name.split('.');
if(!this._classes.hasOwnProperty(path[0])) {
this._classes[path[0]] = {};
}
if(!this._classes[path[0]].hasOwnProperty(path[1])) {
this._classes[path[1]] = {};
}
this._classes[path[0]][path[1]] = constructor;
return this;
}
/**
*
* @return {Object}
* @protected
*/
_getDefaultClasses() {
return {};
}
}
\ No newline at end of file
import PasswordRepository from "../Repositories/PasswordRepository";
import FolderRepository from "../Repositories/FolderRepository";
import TagRepository from "../Repositories/TagRepository";
import SettingRepository from "../Repositories/SettingRepository";
import PasswordCollection from "../Collection/PasswordCollection";
import FolderCollection from "../Collection/FolderCollection";
import CustomFieldCollection from "../Collection/CustomFieldCollection";
import TagCollection from "../Collection/TagCollection";
import SettingCollection from "../Collection/SettingCollection";
import PasswordConverter from "../Converter/PasswordConverter";
import FolderConverter from "../Converter/FolderConverter";
import CustomFieldConverter from "../Converter/CustomFieldConverter";
import TagConverter from "../Converter/TagConverter";
import SettingConverter from "../Converter/SettingConverter";
import Password from "../Model/Password/Password";
import Folder from "../Model/Folder/Folder";
import Tag from "../Model/Tag/Tag";
import Server from "../Model/Server/Server";
import Session from "../Model/Session/Session";
import DataField from "../Model/CustomField/DataField";
import EmailField from "../Model/CustomField/EmailField";
import FileField from "../Model/CustomField/FileField";
import SecretField from "../Model/CustomField/SecretField";
import TextField from "../Model/CustomField/TextField";
import UrlField from "../Model/CustomField/UrlField";
import Setting from "../Model/Setting/Setting";
import ApiRequest from "../Network/ApiRequest";
import ApiResponse from "../Network/ApiResponse";
import SessionAuthorization from "../Authorization/SessionAuthorization";
import PWDv1Challenge from "../Authorization/Challenge/PWDv1Challenge";
import UserToken from "../Authorization/Token/UserToken";
import RequestToken from "../Authorization/Token/RequestToken";
import NoEncryption from "../Encryption/NoEncryption";
import CSEv1Encryption from "../Encryption/CSEv1Encryption";
import ExportV1Encryption from "../Encryption/ExportV1Encryption";
import CSEv1Keychain from "../Encryption/Keychain/CSEv1Keychain";
import Cache from "../Cache/Cache";
import BooleanState from "../State/BooleanState";
import EventEmitter from "eventemitter3";
import ConfigurationError from "../Exception/ConfigruationError";
import ResponseContentTypeError from "../Exception/ResponseContentTypeError";
import ResponseDecodingError from "../Exception/ResponseDecodingError";
import UnknownPropertyError from "../Exception/UnknownPropertyError";
import TokenTypeNotSupported from "../Exception/TokenTypeNotSupported";
import EncryptionTypeNotSupported from "../Exception/EncryptionTypeNotSupported";
import NetworkError from "../Exception/NetworkError";
import HttpError from "../Exception/Http/HttpError";
import BadRequestError from "../Exception/Http/BadRequestError";
import UnauthorizedError from "../Exception/Http/UnauthorizedError";
import ForbiddenError from "../Exception/Http/ForbiddenError";
import NotFoundError from "../Exception/Http/NotFoundError";
import MethodNotAllowedError from "../Exception/Http/MethodNotAllowedError";
import TooManyRequestsError from "../Exception/Http/TooManyRequestsError";
import InternalServerError from "../Exception/Http/InternalServerError";
import BadGatewayError from "../Exception/Http/BadGatewayError";
import ServiceUnavailableError from "../Exception/Http/ServiceUnavailableError";
import GatewayTimeoutError from "../Exception/Http/GatewayTimeoutError";
import BaseClassLoader from "./BaseClassLoader";
import ModelService from "../Services/ModelService";
export default class DefaultClassLoader extends BaseClassLoader {
/**
*
* @return {Object}
* @protected
*/
_getDefaultClasses() {
return {
repository : {
password: () => { return new PasswordRepository(this.getInstance('api')); },
folder : () => { return new FolderRepository(this.getInstance('api')); },
tag : () => { return new TagRepository(this.getInstance('api')); },
setting : () => { return new SettingRepository(this.getInstance('api')); }
},
collection : {
password: (...e) => { return new PasswordCollection(this.getInstance('converter.password'), ...e); },
folder : (...e) => { return new FolderCollection(this.getInstance('converter.folder'), ...e); },
field : (...e) => { return new CustomFieldCollection(this.getInstance('converter.field'), ...e); },
tag : (...e) => { return new TagCollection(this.getInstance('converter.tag'), ...e); },
setting : (...e) => { return new SettingCollection(this.getInstance('converter.setting'), ...e); }
},
converter : {
password: () => { return new PasswordConverter(this.getInstance('api')); },
folder : () => { return new FolderConverter(this.getInstance('api')); },
field : () => { return new CustomFieldConverter(this.getInstance('api')); },
tag : () => { return new TagConverter(this.getInstance('api')); },
setting : () => { return new SettingConverter(this.getInstance('api')); }
},
model : {
password : Password,
folder : Folder,
tag : Tag,
server : Server,
session : Session,
dataField : DataField,
emailField : EmailField,
fileField : FileField,
secretField: SecretField,
textField : TextField,
urlField : UrlField,
setting : Setting
},
network : {
request : ApiRequest,
response: ApiResponse
},
authorization: {
session: () => { return new SessionAuthorization(this.getInstance('api')); }
},
challenge : {
pwdv1: PWDv1Challenge
},
token : {
user : UserToken,
request: RequestToken
},
encryption : {
none : NoEncryption,
csev1: CSEv1Encryption,
expv1: ExportV1Encryption
},
keychain : {
csev1: CSEv1Keychain
},
service : {
model: () => { return new ModelService(this); }
},
cache : {
cache: Cache
},
state : {
boolean: BooleanState
},
event : {
event: EventEmitter
},
exception : {
configuration: ConfigurationError,
contenttype : ResponseContentTypeError,
decoding : ResponseDecodingError,
property : UnknownPropertyError,
challenge : TokenTypeNotSupported,
token : TokenTypeNotSupported,
encryption : EncryptionTypeNotSupported,
network : NetworkError,
http : HttpError,
400 : BadRequestError,
401 : UnauthorizedError,
403 : ForbiddenError,
404 : NotFoundError,
405 : MethodNotAllowedError,
429 : TooManyRequestsError,
500 : InternalServerError,
502 : BadGatewayError,
503 : ServiceUnavailableError,
504 : GatewayTimeoutError
}
};
}
}
\ No newline at end of file
import DefaultClassLoader from "./DefaultClassLoader";
import EnhancedPassword from "../Model/Password/EnhancedPassword";
import EnhancedFolder from "../Model/Folder/EnhancedFolder";
import EnhancedTag from "../Model/Tag/EnhancedTag";
export default class EnhancedClassLoader extends DefaultClassLoader {
/**
*
* @return {Object}
* @protected
*/
_getDefaultClasses() {
let classes = super._getDefaultClasses();
classes.model.password = EnhancedPassword;
classes.model.folder = EnhancedFolder;
classes.model.tag = EnhancedTag;
return classes;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment