Refactor API v3 components

pull/4530/head
Alex Dik 9 years ago
parent a728c9a8b9
commit 389efd0c3f
  1. 6
      frontend/app/components/api/api-v3/api-v3-cache.config.ts
  2. 8
      frontend/app/components/api/api-v3/api-v3.config.ts
  3. 14
      frontend/app/components/api/api-v3/api-v3.service.test.ts
  4. 5
      frontend/app/components/api/api-v3/api-v3.service.ts
  5. 12
      frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts
  6. 6
      frontend/app/components/api/api-v3/hal-link/hal-link.service.ts
  7. 16
      frontend/app/components/api/api-v3/hal-resources/collection-resource.service.test.ts
  8. 12
      frontend/app/components/api/api-v3/hal-resources/error-resource.service.ts
  9. 4
      frontend/app/components/api/api-v3/hal-resources/hal-resource.service.test.ts
  10. 8
      frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts
  11. 18
      frontend/app/components/api/api-v3/hal-resources/work-package-resource.service.ts
  12. 6
      frontend/app/components/api/api-v3/hal-transform/hal-transform.service.ts
  13. 5
      frontend/app/components/api/api-work-packages/api-work-packages.service.ts
  14. 6
      frontend/app/components/api/restangular.config.ts

@ -26,6 +26,8 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
// ++ // ++
import {opApiModule} from "../../../angular-modules";
function apiV3CacheConfig($provide) { function apiV3CacheConfig($provide) {
// Add caching wrapper around $http using angular-cache // Add caching wrapper around $http using angular-cache
$provide.decorator('$http', ($delegate:ng.IHttpService, CacheService:op.CacheService) => { $provide.decorator('$http', ($delegate:ng.IHttpService, CacheService:op.CacheService) => {
@ -66,6 +68,4 @@ function apiV3CacheConfig($provide) {
}); });
} }
angular opApiModule.config(apiV3CacheConfig);
.module('openproject.api')
.config(apiV3CacheConfig);

@ -26,6 +26,8 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
// ++ // ++
import {opApiModule} from "../../../angular-modules";
function apiV3Config(apiV3, halTransform) { function apiV3Config(apiV3, halTransform) {
apiV3.addResponseInterceptor((data, operation, what) => { apiV3.addResponseInterceptor((data, operation, what) => {
apiV3.addElementTransformer(what, halTransform); apiV3.addElementTransformer(what, halTransform);
@ -33,7 +35,7 @@ function apiV3Config(apiV3, halTransform) {
data._plain = angular.copy(data); data._plain = angular.copy(data);
if (data._type === 'Collection') { if (data._type === 'Collection') {
let resp = []; const resp = [];
angular.extend(resp, data); angular.extend(resp, data);
return resp; return resp;
} }
@ -46,6 +48,4 @@ function apiV3Config(apiV3, halTransform) {
}); });
} }
angular opApiModule.run(apiV3Config);
.module('openproject.api')
.run(apiV3Config);

@ -26,17 +26,17 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
// ++ // ++
import {opApiModule, opServicesModule} from "../../../angular-modules";
const expect = chai.expect;
describe('apiV3 service', () => { describe('apiV3 service', () => {
var apiV3:restangular.IService; var apiV3:restangular.IService;
var $httpBackend:ng.IHttpBackendService; var $httpBackend:ng.IHttpBackendService;
beforeEach(angular.mock.module('openproject.api')); beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name));
beforeEach(angular.mock.module('openproject.services')); beforeEach(angular.mock.inject(function (_$httpBackend_, _apiV3_) {
[$httpBackend, apiV3] = arguments;
beforeEach(angular.mock.inject((_apiV3_, _$httpBackend_) => {
apiV3 = _apiV3_;
$httpBackend = _$httpBackend_;
apiV3.setBaseUrl('/base'); apiV3.setBaseUrl('/base');
apiV3.setDefaultHttpFields({cache: false}); apiV3.setDefaultHttpFields({cache: false});
})); }));

@ -27,6 +27,7 @@
// ++ // ++
import {ApiPathsService} from "../api-paths/api-paths.service"; import {ApiPathsService} from "../api-paths/api-paths.service";
import {opApiModule} from "../../../angular-modules";
function apiV3Service(apiPaths:ApiPathsService, function apiV3Service(apiPaths:ApiPathsService,
Restangular:restangular.IService) { Restangular:restangular.IService) {
@ -36,6 +37,4 @@ function apiV3Service(apiPaths:ApiPathsService,
}); });
} }
angular opApiModule.factory('apiV3', apiV3Service);
.module('openproject.api')
.factory('apiV3', apiV3Service);

@ -26,16 +26,18 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
//++ //++
import {opApiModule, opServicesModule} from "../../../../angular-modules";
const expect = chai.expect;
describe('HalLink service', () => { describe('HalLink service', () => {
var $httpBackend:ng.IHttpBackendService; var $httpBackend:ng.IHttpBackendService;
var HalLink; var HalLink;
var apiV3; var apiV3;
beforeEach(angular.mock.module('openproject.api', 'openproject.services')); beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name));
beforeEach(angular.mock.inject((_apiV3_, _HalLink_, _$httpBackend_) => { beforeEach(angular.mock.inject(function (_$httpBackend_, _apiV3_, _HalLink_) {
apiV3 = _apiV3_; [$httpBackend, apiV3, HalLink] = arguments;
HalLink = _HalLink_;
$httpBackend = _$httpBackend_;
apiV3.setDefaultHttpFields({cache: false}); apiV3.setDefaultHttpFields({cache: false});
})); }));

@ -79,10 +79,8 @@ export class HalLink implements HalLinkInterface {
} }
} }
function halLinkService(_$q_:ng.IQService, _apiV3_:restangular.IService) { function halLinkService() {
$q = _$q_; [$q, apiV3] = arguments;
apiV3 = _apiV3_;
return HalLink; return HalLink;
} }

@ -26,27 +26,29 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
// ++ // ++
import {opApiModule, opServicesModule} from "../../../../angular-modules";
const expect = chai.expect;
describe('CollectionResource', () => { describe('CollectionResource', () => {
var CollectionResource; var CollectionResource;
beforeEach(angular.mock.module('openproject.api')); beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name));
beforeEach(angular.mock.module('openproject.services'));
beforeEach(angular.mock.inject((_CollectionResource_) => { beforeEach(angular.mock.inject((_CollectionResource_) => {
CollectionResource = _CollectionResource_; CollectionResource = _CollectionResource_;
})); }));
describe('getElements', () => { describe('getElements', () => {
it('returns the embedded elements', () => { it('returns the embedded elements', () => {
var plain = { var source = {
$embedded: { $embedded: {
elements: [1, 2, 3] 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);
}); });
}); });
}); });

@ -73,8 +73,8 @@ export class ErrorResource extends HalResource {
return columns.map(field => field.details.attribute); return columns.map(field => field.details.attribute);
} }
public getMessagesPerAttribute(): object { public getMessagesPerAttribute():any {
var perAttribute = {} var perAttribute = {};
if (this.details) { if (this.details) {
perAttribute[this.details.attribute] = [this.message]; perAttribute[this.details.attribute] = [this.message];
@ -94,9 +94,11 @@ export class ErrorResource extends HalResource {
} }
} }
function errorResource(_NotificationsService_) { function errorResourceService() {
NotificationsService = _NotificationsService_; [NotificationsService] = arguments;
return ErrorResource; return ErrorResource;
} }
opApiModule.factory('ErrorResource', ['NotificationsService', errorResource]); errorResourceService.$inject = ['NotificationsService'];
opApiModule.factory('ErrorResource', errorResourceService);

@ -37,9 +37,7 @@ describe('HalResource service', () => {
beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name)); beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name));
beforeEach(angular.mock.inject((_$httpBackend_, _HalResource_, apiV3) => { beforeEach(angular.mock.inject((_$httpBackend_, _HalResource_, apiV3) => {
$httpBackend = _$httpBackend_; [$httpBackend, HalResource] = arguments;
HalResource = _HalResource_;
apiV3.setDefaultHttpFields({cache: false}); apiV3.setDefaultHttpFields({cache: false});
})); }));

@ -223,12 +223,8 @@ export class HalResource {
} }
} }
function halResourceService(_$q_, _lazy_, _halTransform_, _HalLink_) { function halResourceService() {
$q = _$q_; [$q, lazy, halTransform, HalLink] = arguments;
lazy = _lazy_;
halTransform = _halTransform_;
HalLink = _HalLink_;
return HalResource; return HalResource;
} }

@ -305,19 +305,11 @@ export class WorkPackageResource extends HalResource {
export interface WorkPackageResourceInterface extends WorkPackageResourceLinks, WorkPackageResourceEmbedded, WorkPackageResource { export interface WorkPackageResourceInterface extends WorkPackageResourceLinks, WorkPackageResourceEmbedded, WorkPackageResource {
} }
function wpResource(_$q_, _apiWorkPackages_, _wpCacheService_, _NotificationsService_) { function wpResource() {
$q = _$q_; [$q, apiWorkPackages, wpCacheService, NotificationsService] = arguments;
apiWorkPackages = _apiWorkPackages_;
wpCacheService = _wpCacheService_;
NotificationsService = _NotificationsService_;
return WorkPackageResource; return WorkPackageResource;
} }
opApiModule.factory('WorkPackageResource', [ wpResource.$inject = ['$q', 'apiWorkPackages', 'wpCacheService', 'NotificationsService'];
'$q',
'apiWorkPackages', opApiModule.factory('WorkPackageResource', wpResource);
'wpCacheService',
'NotificationsService',
wpResource
]);

@ -26,6 +26,8 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
// ++ // ++
import {opApiModule} from "../../../../angular-modules";
function halTransform(halTransformTypes) { function halTransform(halTransformTypes) {
return (element:op.ApiResult) => { return (element:op.ApiResult) => {
const resourceClass = halTransformTypes[element._type] || halTransformTypes.default; const resourceClass = halTransformTypes[element._type] || halTransformTypes.default;
@ -43,6 +45,4 @@ function halTransform(halTransformTypes) {
}; };
} }
angular opApiModule.factory('halTransform', halTransform);
.module('openproject.api')
.factory('halTransform', halTransform);

@ -27,6 +27,7 @@
//++ //++
import {HalResource} from '../api-v3/hal-resources/hal-resource.service'; import {HalResource} from '../api-v3/hal-resources/hal-resource.service';
import {opApiModule} from "../../../angular-modules";
export class ApiWorkPackagesService { export class ApiWorkPackagesService {
protected wpBaseApi; protected wpBaseApi;
@ -113,6 +114,4 @@ export class ApiWorkPackagesService {
} }
} }
angular opApiModule.service('apiWorkPackages', ApiWorkPackagesService);
.module('openproject.api')
.service('apiWorkPackages', ApiWorkPackagesService);

@ -26,10 +26,10 @@
// See doc/COPYRIGHT.rdoc for more details. // See doc/COPYRIGHT.rdoc for more details.
//++ //++
import {opApiModule} from "../../angular-modules";
function restangularConfiguration(RestangularProvider: restangular.IProvider) { function restangularConfiguration(RestangularProvider: restangular.IProvider) {
RestangularProvider.setDefaultHttpFields({cache: true}); RestangularProvider.setDefaultHttpFields({cache: true});
} }
angular opApiModule.config(restangularConfiguration);
.module('openproject.api')
.config(restangularConfiguration);

Loading…
Cancel
Save