From 389efd0c3fcbb51d1f3d6eaf09bf57e18c2a7b93 Mon Sep 17 00:00:00 2001 From: Alex Dik Date: Tue, 14 Jun 2016 13:26:08 +0200 Subject: [PATCH] Refactor API v3 components --- .../api/api-v3/api-v3-cache.config.ts | 6 ++--- .../components/api/api-v3/api-v3.config.ts | 8 +++---- .../api/api-v3/api-v3.service.test.ts | 14 ++++++------ .../components/api/api-v3/api-v3.service.ts | 5 ++--- .../api-v3/hal-link/hal-link.service.test.ts | 12 +++++----- .../api/api-v3/hal-link/hal-link.service.ts | 6 ++--- .../collection-resource.service.test.ts | 22 ++++++++++--------- .../hal-resources/error-resource.service.ts | 12 +++++----- .../hal-resource.service.test.ts | 4 +--- .../hal-resources/hal-resource.service.ts | 8 ++----- .../work-package-resource.service.ts | 18 +++++---------- .../hal-transform/hal-transform.service.ts | 6 ++--- .../api-work-packages.service.ts | 5 ++--- .../app/components/api/restangular.config.ts | 6 ++--- 14 files changed, 60 insertions(+), 72 deletions(-) diff --git a/frontend/app/components/api/api-v3/api-v3-cache.config.ts b/frontend/app/components/api/api-v3/api-v3-cache.config.ts index 6e350bf071..85d6e2d03a 100644 --- a/frontend/app/components/api/api-v3/api-v3-cache.config.ts +++ b/frontend/app/components/api/api-v3/api-v3-cache.config.ts @@ -26,6 +26,8 @@ // See doc/COPYRIGHT.rdoc for more details. // ++ +import {opApiModule} from "../../../angular-modules"; + function apiV3CacheConfig($provide) { // Add caching wrapper around $http using angular-cache $provide.decorator('$http', ($delegate:ng.IHttpService, CacheService:op.CacheService) => { @@ -66,6 +68,4 @@ function apiV3CacheConfig($provide) { }); } -angular - .module('openproject.api') - .config(apiV3CacheConfig); +opApiModule.config(apiV3CacheConfig); diff --git a/frontend/app/components/api/api-v3/api-v3.config.ts b/frontend/app/components/api/api-v3/api-v3.config.ts index b3e09d199d..63a6685de3 100644 --- a/frontend/app/components/api/api-v3/api-v3.config.ts +++ b/frontend/app/components/api/api-v3/api-v3.config.ts @@ -26,6 +26,8 @@ // See doc/COPYRIGHT.rdoc for more details. // ++ +import {opApiModule} from "../../../angular-modules"; + function apiV3Config(apiV3, halTransform) { apiV3.addResponseInterceptor((data, operation, what) => { apiV3.addElementTransformer(what, halTransform); @@ -33,7 +35,7 @@ function apiV3Config(apiV3, halTransform) { data._plain = angular.copy(data); if (data._type === 'Collection') { - let resp = []; + const resp = []; angular.extend(resp, data); return resp; } @@ -46,6 +48,4 @@ function apiV3Config(apiV3, halTransform) { }); } -angular - .module('openproject.api') - .run(apiV3Config); +opApiModule.run(apiV3Config); diff --git a/frontend/app/components/api/api-v3/api-v3.service.test.ts b/frontend/app/components/api/api-v3/api-v3.service.test.ts index 262b73bdd6..ece2c914bc 100644 --- a/frontend/app/components/api/api-v3/api-v3.service.test.ts +++ b/frontend/app/components/api/api-v3/api-v3.service.test.ts @@ -26,17 +26,17 @@ // See doc/COPYRIGHT.rdoc for more details. // ++ +import {opApiModule, opServicesModule} from "../../../angular-modules"; + +const expect = chai.expect; + describe('apiV3 service', () => { var apiV3:restangular.IService; var $httpBackend:ng.IHttpBackendService; - beforeEach(angular.mock.module('openproject.api')); - beforeEach(angular.mock.module('openproject.services')); - - beforeEach(angular.mock.inject((_apiV3_, _$httpBackend_) => { - apiV3 = _apiV3_; - $httpBackend = _$httpBackend_; - + beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name)); + beforeEach(angular.mock.inject(function (_$httpBackend_, _apiV3_) { + [$httpBackend, apiV3] = arguments; apiV3.setBaseUrl('/base'); apiV3.setDefaultHttpFields({cache: false}); })); diff --git a/frontend/app/components/api/api-v3/api-v3.service.ts b/frontend/app/components/api/api-v3/api-v3.service.ts index 8dd52a76b3..af668f90db 100644 --- a/frontend/app/components/api/api-v3/api-v3.service.ts +++ b/frontend/app/components/api/api-v3/api-v3.service.ts @@ -27,6 +27,7 @@ // ++ import {ApiPathsService} from "../api-paths/api-paths.service"; +import {opApiModule} from "../../../angular-modules"; function apiV3Service(apiPaths:ApiPathsService, Restangular:restangular.IService) { @@ -36,6 +37,4 @@ function apiV3Service(apiPaths:ApiPathsService, }); } -angular - .module('openproject.api') - .factory('apiV3', apiV3Service); +opApiModule.factory('apiV3', apiV3Service); diff --git a/frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts b/frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts index 896a40bb1a..0f90bb7a96 100644 --- a/frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts +++ b/frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts @@ -26,16 +26,18 @@ // See doc/COPYRIGHT.rdoc for more details. //++ +import {opApiModule, opServicesModule} from "../../../../angular-modules"; + +const expect = chai.expect; + describe('HalLink service', () => { var $httpBackend:ng.IHttpBackendService; var HalLink; var apiV3; - beforeEach(angular.mock.module('openproject.api', 'openproject.services')); - beforeEach(angular.mock.inject((_apiV3_, _HalLink_, _$httpBackend_) => { - apiV3 = _apiV3_; - HalLink = _HalLink_; - $httpBackend = _$httpBackend_; + beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name)); + beforeEach(angular.mock.inject(function (_$httpBackend_, _apiV3_, _HalLink_) { + [$httpBackend, apiV3, HalLink] = arguments; apiV3.setDefaultHttpFields({cache: false}); })); diff --git a/frontend/app/components/api/api-v3/hal-link/hal-link.service.ts b/frontend/app/components/api/api-v3/hal-link/hal-link.service.ts index a7f85ef885..fc6ac0603f 100644 --- a/frontend/app/components/api/api-v3/hal-link/hal-link.service.ts +++ b/frontend/app/components/api/api-v3/hal-link/hal-link.service.ts @@ -79,10 +79,8 @@ export class HalLink implements HalLinkInterface { } } -function halLinkService(_$q_:ng.IQService, _apiV3_:restangular.IService) { - $q = _$q_; - apiV3 = _apiV3_; - +function halLinkService() { + [$q, apiV3] = arguments; return HalLink; } diff --git a/frontend/app/components/api/api-v3/hal-resources/collection-resource.service.test.ts b/frontend/app/components/api/api-v3/hal-resources/collection-resource.service.test.ts index af203b3ee1..2a151c388e 100644 --- a/frontend/app/components/api/api-v3/hal-resources/collection-resource.service.test.ts +++ b/frontend/app/components/api/api-v3/hal-resources/collection-resource.service.test.ts @@ -26,27 +26,29 @@ // See doc/COPYRIGHT.rdoc for more details. // ++ +import {opApiModule, opServicesModule} from "../../../../angular-modules"; + +const expect = chai.expect; + describe('CollectionResource', () => { var CollectionResource; - beforeEach(angular.mock.module('openproject.api')); - beforeEach(angular.mock.module('openproject.services')); - + beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name)); beforeEach(angular.mock.inject((_CollectionResource_) => { CollectionResource = _CollectionResource_; })); describe('getElements', () => { it('returns the embedded elements', () => { - var plain = { - $embedded: { - elements: [1,2,3] - } - } + var source = { + $embedded: { + elements: [1, 2, 3] + } + }; - var resource = new CollectionResource(plain); + var resource = new CollectionResource(source); - expect(resource.getElements()).to.eql(plain.$embedded.elements); + expect(resource.getElements()).to.eql(source.$embedded.elements); }); }); }); diff --git a/frontend/app/components/api/api-v3/hal-resources/error-resource.service.ts b/frontend/app/components/api/api-v3/hal-resources/error-resource.service.ts index 7d95d9a721..87308bf245 100644 --- a/frontend/app/components/api/api-v3/hal-resources/error-resource.service.ts +++ b/frontend/app/components/api/api-v3/hal-resources/error-resource.service.ts @@ -73,8 +73,8 @@ export class ErrorResource extends HalResource { return columns.map(field => field.details.attribute); } - public getMessagesPerAttribute(): object { - var perAttribute = {} + public getMessagesPerAttribute():any { + var perAttribute = {}; if (this.details) { perAttribute[this.details.attribute] = [this.message]; @@ -94,9 +94,11 @@ export class ErrorResource extends HalResource { } } -function errorResource(_NotificationsService_) { - NotificationsService = _NotificationsService_; +function errorResourceService() { + [NotificationsService] = arguments; return ErrorResource; } -opApiModule.factory('ErrorResource', ['NotificationsService', errorResource]); +errorResourceService.$inject = ['NotificationsService']; + +opApiModule.factory('ErrorResource', errorResourceService); diff --git a/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.test.ts b/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.test.ts index b032babec1..3f79a80c25 100644 --- a/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.test.ts +++ b/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.test.ts @@ -37,9 +37,7 @@ describe('HalResource service', () => { beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name)); beforeEach(angular.mock.inject((_$httpBackend_, _HalResource_, apiV3) => { - $httpBackend = _$httpBackend_; - HalResource = _HalResource_; - + [$httpBackend, HalResource] = arguments; apiV3.setDefaultHttpFields({cache: false}); })); diff --git a/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts b/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts index bcfba69ad0..af76a0dc3a 100644 --- a/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts +++ b/frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts @@ -223,12 +223,8 @@ export class HalResource { } } -function halResourceService(_$q_, _lazy_, _halTransform_, _HalLink_) { - $q = _$q_; - lazy = _lazy_; - halTransform = _halTransform_; - HalLink = _HalLink_; - +function halResourceService() { + [$q, lazy, halTransform, HalLink] = arguments; return HalResource; } diff --git a/frontend/app/components/api/api-v3/hal-resources/work-package-resource.service.ts b/frontend/app/components/api/api-v3/hal-resources/work-package-resource.service.ts index 10e1e6adab..1417c90fae 100644 --- a/frontend/app/components/api/api-v3/hal-resources/work-package-resource.service.ts +++ b/frontend/app/components/api/api-v3/hal-resources/work-package-resource.service.ts @@ -305,19 +305,11 @@ export class WorkPackageResource extends HalResource { export interface WorkPackageResourceInterface extends WorkPackageResourceLinks, WorkPackageResourceEmbedded, WorkPackageResource { } -function wpResource(_$q_, _apiWorkPackages_, _wpCacheService_, _NotificationsService_) { - $q = _$q_; - apiWorkPackages = _apiWorkPackages_; - wpCacheService = _wpCacheService_; - NotificationsService = _NotificationsService_; - +function wpResource() { + [$q, apiWorkPackages, wpCacheService, NotificationsService] = arguments; return WorkPackageResource; } -opApiModule.factory('WorkPackageResource', [ - '$q', - 'apiWorkPackages', - 'wpCacheService', - 'NotificationsService', - wpResource -]); +wpResource.$inject = ['$q', 'apiWorkPackages', 'wpCacheService', 'NotificationsService']; + +opApiModule.factory('WorkPackageResource', wpResource); diff --git a/frontend/app/components/api/api-v3/hal-transform/hal-transform.service.ts b/frontend/app/components/api/api-v3/hal-transform/hal-transform.service.ts index df6adca897..05d7f4ae67 100644 --- a/frontend/app/components/api/api-v3/hal-transform/hal-transform.service.ts +++ b/frontend/app/components/api/api-v3/hal-transform/hal-transform.service.ts @@ -26,6 +26,8 @@ // See doc/COPYRIGHT.rdoc for more details. // ++ +import {opApiModule} from "../../../../angular-modules"; + function halTransform(halTransformTypes) { return (element:op.ApiResult) => { const resourceClass = halTransformTypes[element._type] || halTransformTypes.default; @@ -43,6 +45,4 @@ function halTransform(halTransformTypes) { }; } -angular - .module('openproject.api') - .factory('halTransform', halTransform); +opApiModule.factory('halTransform', halTransform); diff --git a/frontend/app/components/api/api-work-packages/api-work-packages.service.ts b/frontend/app/components/api/api-work-packages/api-work-packages.service.ts index a6bdb06a89..240263730f 100644 --- a/frontend/app/components/api/api-work-packages/api-work-packages.service.ts +++ b/frontend/app/components/api/api-work-packages/api-work-packages.service.ts @@ -27,6 +27,7 @@ //++ import {HalResource} from '../api-v3/hal-resources/hal-resource.service'; +import {opApiModule} from "../../../angular-modules"; export class ApiWorkPackagesService { protected wpBaseApi; @@ -113,6 +114,4 @@ export class ApiWorkPackagesService { } } -angular - .module('openproject.api') - .service('apiWorkPackages', ApiWorkPackagesService); +opApiModule.service('apiWorkPackages', ApiWorkPackagesService); diff --git a/frontend/app/components/api/restangular.config.ts b/frontend/app/components/api/restangular.config.ts index 7879a082a5..0b7de6b0af 100644 --- a/frontend/app/components/api/restangular.config.ts +++ b/frontend/app/components/api/restangular.config.ts @@ -26,10 +26,10 @@ // See doc/COPYRIGHT.rdoc for more details. //++ +import {opApiModule} from "../../angular-modules"; + function restangularConfiguration(RestangularProvider: restangular.IProvider) { RestangularProvider.setDefaultHttpFields({cache: true}); } -angular - .module('openproject.api') - .config(restangularConfiguration); +opApiModule.config(restangularConfiguration);