Replace apiV3 dependency with halRequest in HalLink

pull/4734/head
Alex Dik 8 years ago
parent 00d15801d7
commit 3105acaaa6
  1. 32
      frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts
  2. 63
      frontend/app/components/api/api-v3/hal-link/hal-link.service.ts
  3. 25
      frontend/app/components/wp-edit/field-types/wp-edit-wiki-textarea-field.module.ts

@ -33,17 +33,13 @@ describe('HalLink service', () => {
var $httpBackend:ng.IHttpBackendService;
var $rootScope;
var HalLink;
var apiV3;
var link:HalLink;
beforeEach(angular.mock.module(opApiModule.name, opServicesModule.name));
beforeEach(angular.mock.inject(function (_$httpBackend_,
_$rootScope_,
_apiV3_,
_HalLink_) {
[$httpBackend, $rootScope, apiV3, HalLink] = _.toArray(arguments);
apiV3.setDefaultHttpFields({cache: false});
[$httpBackend, $rootScope, HalLink] = _.toArray(arguments);
}));
it('should exist', () => {
@ -119,25 +115,22 @@ describe('HalLink service', () => {
expect(result.hello).to.eq(response.hello);
});
it('should not return a restangularized result', () => {
expect(result.restangularized).to.not.be.ok;
});
it('should return a HalResource', () => {
expect(result.$isHal).to.be.true;
});
it('should perform a GET request by default', () => {
link.$fetch();
$httpBackend.expectGET('/api/link').respond(200);
$httpBackend.expectGET('/api/link').respond(200, {});
$httpBackend.flush();
});
it('should pass parameters as query params to the request', () => {
link.$fetch({
hello: 'world'
});
$httpBackend.expectGET('/api/link?hello=world').respond(200);
it('should send the provided data', () => {
const data = {hello: 'world'};
link.method = 'post';
link.$fetch(data);
$httpBackend.expect('POST', '/api/link', data).respond(200, {});
$httpBackend.flush();
});
@ -145,7 +138,7 @@ describe('HalLink service', () => {
link.method = 'post';
link.$fetch();
$httpBackend.expectPOST('/api/link').respond(200);
$httpBackend.expectPOST('/api/link').respond(200, {});
$httpBackend.flush();
});
@ -153,7 +146,7 @@ describe('HalLink service', () => {
link.method = 'put';
link.$fetch();
$httpBackend.expectPUT('/api/link').respond(200);
$httpBackend.expectPUT('/api/link').respond(200, {});
$httpBackend.flush();
});
@ -161,18 +154,17 @@ describe('HalLink service', () => {
link.method = 'patch';
link.$fetch();
$httpBackend.expectPATCH('/api/link').respond(200);
$httpBackend.expectPATCH('/api/link').respond(200, {});
$httpBackend.flush();
});
describe('when making the link callable', () => {
var func;
const runChecks = () => {
it('should return a function that fetches the data', () => {
func();
$httpBackend.expectPOST('foo').respond(200);
$httpBackend.expectPOST('foo').respond(200, {});
$httpBackend.flush();
});

@ -27,9 +27,12 @@
//++
import {opApiModule} from '../../../../angular-modules';
import {HalRequestService} from '../hal-request/hal-request.service';
import {HalResource} from '../hal-resources/hal-resource.service';
import FunctionBind = _.FunctionBind;
var $q:ng.IQService;
var apiV3:restangular.IService;
var halRequest:HalRequestService;
export interface HalLinkInterface {
href:string;
@ -39,20 +42,29 @@ export interface HalLinkInterface {
payload?:any;
}
interface CallableHalLink extends HalLinkInterface {
(data?):ng.IPromise<HalResource>;
}
export class HalLink implements HalLinkInterface {
/**
* Create the HalLink from an object with the HalLinkInterface.
*
* @param link
* @returns {HalLink}
*/
public static fromObject(link):HalLink {
return new HalLink(link.href, link.title, link.method, link.templated, link.payload);
}
public static callable(link) {
return HalLink.fromObject(link).$callable();
}
/**
* Return the restangular element.
* Return a function that fetches the resource.
*
* @param link
* @return {CallableHalLink}
*/
public get $route():restangular.IElement {
return apiV3.oneUrl('route', this.href);
public static callable(link):CallableHalLink {
return HalLink.fromObject(link).$callable();
}
constructor(public href:string = null,
@ -62,22 +74,25 @@ export class HalLink implements HalLinkInterface {
public payload?:any) {
}
public $fetch(...params) {
if (!this.href) {
return $q.when(null);
}
if (this.method === 'post') {
params.unshift('');
}
return this.$route[this.method === 'delete' && 'remove' || this.method](...params);
/**
* Fetch the resource.
*
* @param data
* @returns {ng.IPromise<HalResource>}
*/
public $fetch(data?:any):ng.IPromise<HalResource> {
return halRequest.request(this.method, this.href, data);
}
public $callable() {
const func:any = (...params) => this.$fetch(...params);
/**
* Return a function that fetches the resource.
*
* @returns {CallableHalLink}
*/
public $callable():CallableHalLink {
const linkFunc:any = data => this.$fetch(data);
_.extend(func, {
_.extend(linkFunc, {
$link: this,
href: this.href,
title: this.title,
@ -86,15 +101,15 @@ export class HalLink implements HalLinkInterface {
payload: this.payload
});
return func;
return linkFunc;
}
}
function halLinkService(...args) {
[$q, apiV3] = args;
[$q, halRequest] = args;
return HalLink;
}
halLinkService.$inject = ['$q', 'apiV3'];
halLinkService.$inject = ['$q', 'halRequest'];
opApiModule.factory('HalLink', halLinkService);

@ -26,8 +26,8 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {EditField} from "../wp-edit-field/wp-edit-field.module";
import {WorkPackageResource} from "../../api/api-v3/hal-resources/work-package-resource.service";
import {EditField} from '../wp-edit-field/wp-edit-field.module';
import {WorkPackageResource} from '../../api/api-v3/hal-resources/work-package-resource.service';
export class WikiTextareaEditField extends EditField {
@ -35,10 +35,11 @@ export class WikiTextareaEditField extends EditField {
public template:string = '/components/wp-edit/field-types/wp-edit-wiki-textarea-field.directive.html';
// Dependencies
protected $sce:ng.ISCEService = <ng.ISCEService> WikiTextareaEditField.$injector.get("$sce");
protected TextileService:ng.IServiceProvider = <ng.ISCEProvider> WikiTextareaEditField.$injector.get("TextileService");
protected $timeout:ng.ITimeoutService = <ng.ITimeoutService> WikiTextareaEditField.$injector.get("$timeout");
protected I18n:op.I18n = <op.I18n> WikiTextareaEditField.$injector.get("I18n");
protected $sce:ng.ISCEService = <ng.ISCEService> WikiTextareaEditField.$injector.get('$sce');
protected $http:ng.IHttpService = <ng.IHttpService> WikiTextareaEditField.$injector.get('$http');
protected TextileService:ng.IServiceProvider = <ng.ISCEProvider> WikiTextareaEditField.$injector.get('TextileService');
protected $timeout:ng.ITimeoutService = <ng.ITimeoutService> WikiTextareaEditField.$injector.get('$timeout');
protected I18n:op.I18n = <op.I18n> WikiTextareaEditField.$injector.get('I18n');
// wp resource
protected workPackage:WorkPackageResource;
@ -86,11 +87,15 @@ export class WikiTextareaEditField extends EditField {
if (this.isPreview) {
this.isBusy = true;
this.workPackage.getForm().then(form => {
var previewLink = form.$links.previewMarkup.$link.$route;
previewLink
.customPOST(this.fieldVal.raw, void 0, void 0, {'Content-Type': 'text/plain; charset=UTF-8'})
const link = form.previewMarkup.$link;
this.$http({
method: link.method,
url: link.href,
data: this.fieldVal.raw,
headers: {'Content-Type': 'text/plain; charset=UTF-8'}
})
.then(result => {
this.previewHtml = this.$sce.trustAsHtml(result);
this.previewHtml = this.$sce.trustAsHtml(result.data);
})
.finally(() => {
this.isBusy = false;

Loading…
Cancel
Save