baasic-sdk-javascript
Version:
JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).
91 lines (90 loc) • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var inversify_1 = require("inversify");
var httpApi_1 = require("./httpApi");
var contracts_1 = require("./core/contracts");
var jQuery_1 = require("./httpApi/jQuery");
var localStorage_1 = require("./core/localStorage");
var browserEvents_1 = require("./core/browserEvents");
var core_1 = require("core");
var DIModule = /** @class */ (function () {
function DIModule() {
this.diModules = [];
this.kernel = new inversify_1.Container();
}
DIModule.prototype.init = function (app, modules) {
var _this = this;
var _a;
var diModule = new inversify_1.ContainerModule(function (bind) {
var apiKey = app.getApiKey();
var urlBuilder = [(app.settings.useSSL ? 'https' : 'http') + "://" + app.settings.apiRootUrl];
if (app.settings.apiVersion) {
urlBuilder.push(app.settings.apiVersion);
}
urlBuilder.push(apiKey);
urlBuilder.push('');
if (app.settings) {
var appOptions = {
apiKey: apiKey,
apiUrl: urlBuilder.join('/'),
enableHALJSON: app.settings.enableHALJSON
};
app.settings.apiUrl = appOptions.apiUrl;
_this.kernel.bind(contracts_1.TYPES.IAppOptions).toConstantValue(appOptions);
_this.kernel.bind(contracts_1.TYPES.IBaasicAppOptions).toConstantValue(app.settings);
}
_this.bindHandler(httpApi_1.httpTYPES.IHttpClient, app.settings.httpClient, jQuery_1.JQueryHttpClient);
_this.bindHandlerWithOptions(contracts_1.TYPES.IStorageHandler, contracts_1.TYPES.IDefaultStorageConfig, app.settings.storageHandler, localStorage_1.LocalStorageHandler);
if (app.settings.TokenHandler) {
_this.kernel.bind(contracts_1.TYPES.ITokenHandler).to(app.settings.TokenHandler).inSingletonScope();
}
else {
_this.kernel.bind(contracts_1.TYPES.ITokenHandler).to(core_1.TokenHandler).inSingletonScope();
}
_this.bindHandler(contracts_1.TYPES.IEventHandler, app.settings.eventHandler, browserEvents_1.BrowserEventHandler);
if (app.settings.abortSignal) {
_this.kernel.bind(httpApi_1.httpTYPES.IAbortSignal).toConstantValue(app.settings.abortSignal());
}
_this.kernel.bind(contracts_1.TYPES.IBaasicApp).toConstantValue(app);
});
this.diModules.push(diModule);
for (var _i = 0, modules_1 = modules; _i < modules_1.length; _i++) {
var m = modules_1[_i];
this.addModule(m);
}
(_a = this.kernel).load.apply(_a, this.diModules);
};
DIModule.prototype.bindHandler = function (type, value, defaultBinding) {
if (value) {
this.kernel.bind(type).toConstantValue(value());
}
else {
this.kernel.bind(type).to(defaultBinding);
}
};
DIModule.prototype.bindHandlerWithOptions = function (type, optionType, value, defaultBinding) {
if (value) {
if (value instanceof Function) {
this.kernel.bind(type).toConstantValue(value());
return;
}
else {
this.kernel.bind(optionType).toConstantValue(value);
}
}
this.kernel.bind(type).to(defaultBinding);
};
DIModule.prototype.addModule = function (module) {
if (module instanceof inversify_1.ContainerModule) {
this.diModules.push(module);
}
else if (module instanceof Object && !(module instanceof Function)) {
for (var mod in module) {
this.addModule(module[mod]);
}
}
};
return DIModule;
}());
exports.DIModule = DIModule;
;