Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Commit

Permalink
feat(hal client) : support of link without href attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
deblockt committed Mar 26, 2017
1 parent 76f72f7 commit 306730d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/hal-rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,18 @@ export class HalRestClient {
const links = json._links;
resource.links = Object.keys(links)
.filter((item) => item !== "self")
.reduce((prev, curentKey) => {
const type =
Reflect.getMetadata("halClient:specificType", c.prototype, curentKey)
.reduce((prev, currentKey) => {
if ("string" === typeof links[currentKey]) {
links[currentKey] = {href : links[currentKey]};
}
const type = Reflect.getMetadata("halClient:specificType", c.prototype, currentKey)
|| HalResource;
const propKey = halToTs[curentKey] || curentKey;
prev[propKey] = createResource(this, type, links[curentKey].href);
const propKey = halToTs[currentKey] || currentKey;
prev[propKey] = createResource(this, type, links[currentKey].href);
return prev;
}, {});
resource.uri = links.self.href;

resource.uri = "string" === typeof links.self ? links.self : links.self.href;
} else if ("_embedded" === key) {
const embedded = json._embedded;
for (const prop of Object.keys(embedded)) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/hal-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ function initTests() {
name : "Thomas",
});

const project3 = {
_links : {
self : "http://test.fr/projects/3",
subResource : {
href : "http://test.fr/projects/3/subResource",
},
versions : "http://test.fr/projects/3/versions",
},
name : "Project 3",
};
testNock
.get("/project/3")
.reply(200, project3);
}

test("fetch contains list", async (t) => {
Expand Down Expand Up @@ -256,3 +269,11 @@ test("fetch non hal object throw exception", async (t) => {
t.ok("throwed exception");
}
});

test("can read link without href", async (t) => {
initTests();
const project = await createClient("http://test.fr").fetchResource("/project/3");
t.equals(project.uri, "http://test.fr/projects/3");
t.equals(project.prop("subResource").uri, "http://test.fr/projects/3/subResource");
t.equals(project.link("versions").uri, "http://test.fr/projects/3/versions");
});

0 comments on commit 306730d

Please sign in to comment.