Refactor apiPaths service using the pathBuilder

pull/4716/head
Alex Dik 8 years ago
parent b4e79a7809
commit 8eceb2744d
  1. 10
      frontend/app/components/api/api-experimental/api-experimental.service.ts
  2. 42
      frontend/app/components/api/api-paths/api-paths.config.ts
  3. 52
      frontend/app/components/api/api-paths/api-paths.service.test.ts
  4. 48
      frontend/app/components/api/api-paths/api-paths.service.ts
  5. 7
      frontend/app/components/api/api-v3/api-v3.service.ts

@ -26,15 +26,13 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {ApiPathsService} from "../api-paths/api-paths.service";
import {opApiModule} from '../../../angular-modules';
function apiExperimentalService(apiPaths:ApiPathsService, Restangular: restangular.IService) {
function apiExperimentalService(apiPaths, Restangular: restangular.IService) {
return Restangular.withConfig((RestangularConfigurer) => {
RestangularConfigurer.setBaseUrl(apiPaths.experimental);
RestangularConfigurer.setBaseUrl(apiPaths.ex());
});
}
angular
.module('openproject.api')
.factory('apiExperimental', apiExperimentalService);
opApiModule.factory('apiExperimental', apiExperimentalService);

@ -0,0 +1,42 @@
//-- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
//++
import {opApiModule} from '../../../angular-modules';
import {ApiPathsServiceProvider} from './api-paths.service';
function apiPathsProviderConfig(apiPathsProvider:ApiPathsServiceProvider) {
const ex = {};
const v3 = {};
apiPathsProvider.pathConfig = {
ex: ['api/experimental', ex],
v3: ['api/v3', v3]
};
}
opApiModule.config(apiPathsProviderConfig);

@ -26,51 +26,29 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {ApiPathsService} from './api-paths.service';
import {opApiModule, opServicesModule} from '../../../angular-modules';
describe('apiPaths', () => {
var apiPaths:ApiPathsService;
var $document:ng.IDocumentService;
var apiPaths:any;
beforeEach(angular.mock.module('openproject.api'));
beforeEach(angular.mock.module('openproject.services'));
beforeEach(angular.mock.module(
opApiModule.name,
opServicesModule.name
));
beforeEach(angular.mock.inject((_$document_, _apiPaths_) => {
$document = _$document_;
beforeEach(angular.mock.inject(function (_apiPaths_) {
apiPaths = _apiPaths_;
}));
describe('when without app_base_path', () => {
describe('when using path()', () => {
it('should return an api experimental path', () => {
expect(apiPaths.path('experimental')).to.eq('/api/experimental/');
});
it('should return an api v2 path', () => {
expect(apiPaths.path('v2')).to.eq('/api/v2/');
});
it('should return an api v3 path', () => {
expect(apiPaths.path('v3')).to.eq('/api/v3/');
});
});
describe('when using v3', () => {
it('should return an api v3 path', () => {
expect(apiPaths.v3).to.eq('/api/v3/');
});
});
it('should exist', () => {
expect(apiPaths).to.exist;
});
describe('when using v2', () => {
it('should return an api v3 path', () => {
expect(apiPaths.v2).to.eq('/api/v2/');
});
});
it('should have a ex property', () => {
expect(apiPaths).to.have.property('ex');
});
describe('when using experimental', () => {
it('should return an api experimental path', () => {
expect(apiPaths.experimental).to.eq('/api/experimental/');
});
});
it('should have a v3 property', () => {
expect(apiPaths).to.have.property('v3');
});
});

@ -27,37 +27,29 @@
// ++
import {opApiModule} from '../../../angular-modules';
import {PathBuilderService} from '../path-builder/path-builder.service';
/**
* Replaces the PathHelper service in its function, providing a way to generate safe paths
* without having to store them directly in a service.
* Provide paths for the API requests.
*/
export class ApiPathsService {
protected paths:{[name:string]:string};
constructor(protected $document, protected appBasePath) {
this.paths = {
v3: 'api/v3/',
v2: 'api/v2/',
experimental: 'api/experimental/'
};
}
public path(name:string):string {
return this.appBasePath + '/' + this.paths[name];
}
public get v3():string {
return this.path('v3');
}
public get v2():string {
return this.path('v2');
}
public get experimental():string {
return this.path('experimental');
export class ApiPathsServiceProvider {
/**
* Configuration object for the pathBuilder service
* @type {any}
*/
public pathConfig:any = {};
/**
* Return the service.
*
* @param appBasePath
* @param pathBuilder
* @return {Array}
*/
public $get(appBasePath:string, pathBuilder:PathBuilderService) {
const config:any = pathBuilder.buildPaths({base: [appBasePath, this.pathConfig]});
return config.base;
}
}
opApiModule.service('apiPaths', ApiPathsService);
opApiModule.provider('apiPaths', ApiPathsServiceProvider);

@ -26,14 +26,13 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {ApiPathsService} from "../api-paths/api-paths.service";
import {opApiModule} from "../../../angular-modules";
import {opApiModule} from '../../../angular-modules';
function apiV3Service(apiPaths:ApiPathsService,
function apiV3Service(apiPaths,
Restangular:restangular.IService) {
return Restangular.withConfig(RestangularConfigurer => {
RestangularConfigurer.setBaseUrl(apiPaths.v3);
RestangularConfigurer.setBaseUrl(apiPaths.v3());
});
}

Loading…
Cancel
Save