{"size": 3808, "ext": "js", "max_stars_repo_path": "src/core/services/baasicApp.js", "max_stars_repo_name": "VlatkaFundak/baasic-sdk-angularjs", "max_stars_repo_stars_event_min_datetime": "2016-03-26T13:38:56.000Z", "max_stars_repo_stars_event_max_datetime": "2016-03-26T13:38:56.000Z", "max_issues_repo_path": "src/core/services/baasicApp.js", "max_issues_repo_name": "VlatkaFundak/baasic-sdk-angularjs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/core/services/baasicApp.js", "max_forks_repo_name": "VlatkaFundak/baasic-sdk-angularjs", "max_forks_repo_forks_event_min_datetime": "2019-09-26T12:06:49.000Z", "max_forks_repo_forks_event_max_datetime": "2019-09-26T12:06:49.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 24.1012658228, "max_line_length": 236, "alphanum_fraction": 0.6045168067}
﻿/* globals module, MonoSoftware */
/**
 * @module baasicApp
 * @description  `baasicApp` service is used to manage Baasic application instances. Multiple AngularJS application instances can be created and coexist at the same time (each will communicate with its corresponding Baasic application).
 */

(function (angular, module, undefined) {
	'use strict';
	module.provider('baasicApp', function baasicAppService() {
		var apps = {};
		var defaultAppKey;
		/**
        * Create an application.
        * @method create
        * @example
		var app = baasicApp.create('<api-key>', {
			apiRootUrl : 'api.baasic.com',
			apiVersion : '<version>'
		});
        **/
		this.create = function create(apiKey, config) {

			apps[apiKey] = function (httpClient) {
				var cfg = angular.extend({
					httpClient: function () {
						return httpClient;
					}
				}, config);
				var app = new baasicSdkJavaScript.BaasicApp(apiKey, cfg);
				apps[apiKey] = function () {
					return app;
				};

				return app;
			};

			if (!defaultAppKey) {
				defaultAppKey = apiKey;
			}
		};

		this.$get = ["$http", "$q", function ($http, $q) {
			var httpClient = getHttpClient($http, $q);

			return {
				/**
				 * Returns a list of all applications.
				 * @method
				 * @example baasicApp.all();
				 **/
				all: function () {
					var list = [];
					for (var key in apps) {
						list.push(apps[key](httpClient));
					}

					return list;
				},
				/**
				 * Returns a specified application.
				 * @method
				 * @example baasicApp.get('<api-key>');
				 **/
				get: function getBaasicApplication(apiKey) {
					var appFactory;
					if (apiKey) {
						appFactory = apps[apiKey];
					} else {
						appFactory = apps[defaultAppKey];
					}

					return appFactory(httpClient);
				}
			};
		}];
	});

	function getHttpClient($http, $q) {
		return {
			createPromise: function (deferFn) {
				var deferred = $q.defer();
				deferFn(deferred.resolve, deferred.reject);
				var promise = deferred.promise;

				promise.success = function (fn) {
					promise.then(function (response) {
						resolveHttpPromise(fn, response);
					}, null);
					return promise;
				};

				promise.error = function (fn) {
					promise.then(null, function (response) {
						resolveHttpPromise(fn, response);
					});
					return promise;
				};

				return promise;
			},
			request: function (request) {
				var config = {
					withCredentials: true,
					method: request.method,
					url: request.url.toString()
				};
				if (request.responseType) {
					config.responseType = request.responseType;
				}

				if (request.headers) config.headers = request.headers;
				if (request.data) config.data = request.data;

				var promise = $http(config)
					.then(function (value) {
						return {
							headers: value.headers(),
							data: value.data,
							statusCode: value.status,
							statusText: value.statusText,
							request: request
						};
					}, function (data) {
						throw {
							headers: data.headers(),
							data: data.data,
							statusCode: data.status,
							statusText: data.statusText,
							request: request
						};
					});

				promise.success = function (fn) {
					promise.then(function (response) {
						fn(response.data, response.statusCode, response.headers, request);
					}, null);
					return promise;
				};

				promise.error = function (fn) {
					promise.then(null, function (response) {
						fn(response.data, response.statusCode, response.headers, request);
					});
					return promise;
				};

				return promise;
			}
		};
	}

	function resolveHttpPromise(fn, response) {
		var config = angular.extend({}, response.request);
		if (config.url) config.url = config.url.toString();
		fn(response.data, response.statusCode, response.headers, config);
	}

}(angular, module));