Merge pull request #4594 from furinvader/refactoring/hal/hal-link

Refactor HalLink
pull/4598/head
Alex Dik 8 years ago committed by GitHub
commit 29cde57927
  1. 37
      frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts
  2. 16
      frontend/app/components/api/api-v3/hal-link/hal-link.service.ts
  3. 4
      frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts
  4. 2
      frontend/app/components/wp-edit/field-types/wp-edit-wiki-textarea-field.module.ts
  5. 1
      frontend/app/typings/open-project.typings.ts

@ -166,14 +166,14 @@ describe('HalLink service', () => {
}); });
describe('when using the link function wrapper', () => { 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.expectGET('/api/link').respond(200); $httpBackend.expectPOST('foo').respond(200);
$httpBackend.flush() $httpBackend.flush();
}); });
it('should pass the params to $fetch', () => { it('should pass the params to $fetch', () => {
@ -182,18 +182,41 @@ describe('HalLink service', () => {
expect($fetch.calledWith('hello')).to.be.true; expect($fetch.calledWith('hello')).to.be.true;
}); });
it('should have the href property of the link', () => {
expect(func.href).to.equal(link.href);
});
it('should have the title property of the link', () => {
expect(func.title).to.equal(link.title);
});
it('should have the method property of the link', () => {
expect(func.method).to.equal(link.method);
});
it('should have the templated property of the link', () => {
expect(func.templated).to.equal(link.templated);
});
}; };
describe('when using $toFunc', () => { beforeEach(() => {
link.href = 'foo';
link.title = 'title';
link.method = 'post';
link.templated = true;
});
describe('when using the instance method', () => {
beforeEach(() => { beforeEach(() => {
func = link.$toFunc(); func = link.$callable();
}); });
runChecks(); runChecks();
}); });
describe('when using the static factory function', () => { describe('when using the static factory method', () => {
beforeEach(() => { beforeEach(() => {
func = HalLink.asFunc(link); func = HalLink.callable(link);
link = func.$link; link = func.$link;
}); });
runChecks(); runChecks();

@ -43,8 +43,8 @@ export class HalLink implements HalLinkInterface {
return new HalLink(link.href, link.title, link.method, link.templated); return new HalLink(link.href, link.title, link.method, link.templated);
} }
public static asFunc(link) { public static callable(link) {
return HalLink.fromObject(link).$toFunc(); return HalLink.fromObject(link).$callable();
} }
/** /**
@ -72,10 +72,16 @@ export class HalLink implements HalLinkInterface {
return this.$route[this.method === 'delete' && 'remove' || this.method](...params); return this.$route[this.method === 'delete' && 'remove' || this.method](...params);
} }
public $toFunc() { public $callable() {
const func:any = (...params) => this.$fetch(...params); const func:any = (...params) => this.$fetch(...params);
func.$link = this;
func.$route = this.$route; _.extend(func, {
$link: this,
href: this.href,
title: this.title,
method: this.method,
templated: this.templated
});
return func; return func;
} }

@ -184,7 +184,7 @@ function initializeResource(halResource:HalResource) {
if (link.href) { if (link.href) {
if (link.method !== 'get') { if (link.method !== 'get') {
return HalLink.asFunc(link); return HalLink.callable(link);
} }
return createLinkedResource(linkName, link); return createLinkedResource(linkName, link);
@ -218,7 +218,7 @@ function initializeResource(halResource:HalResource) {
function setupLinks() { function setupLinks() {
setupProperty('links', setupProperty('links',
link => Array.isArray(link) ? link.map(HalLink.asFunc) : HalLink.asFunc(link)); link => Array.isArray(link) ? link.map(HalLink.callable) : HalLink.callable(link));
} }
function setupEmbedded() { function setupEmbedded() {

@ -82,7 +82,7 @@ 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.$route; var previewLink = form.$links.previewMarkup.$link.$route;
previewLink previewLink
.customPOST(this.fieldVal.raw, void 0, void 0, {'Content-Type': 'text/plain; charset=UTF-8'}) .customPOST(this.fieldVal.raw, void 0, void 0, {'Content-Type': 'text/plain; charset=UTF-8'})
.then(result => { .then(result => {

@ -153,7 +153,6 @@ declare namespace api {
interface Function { interface Function {
$link?:any; $link?:any;
$route?:restangular.IService;
name:string; name:string;
_type:string; _type:string;
} }

Loading…
Cancel
Save