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

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

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

Loading…
Cancel
Save