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

Commit

Permalink
Merge branch 'master' into fix/listFetchResetCache
Browse files Browse the repository at this point in the history
  • Loading branch information
deblockt committed Jan 7, 2019
2 parents bbac4e5 + dea9865 commit c521570
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 55 deletions.
17 changes: 10 additions & 7 deletions src/hal-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ function getTransco(transconame: string, target: any): object {
}

export function HalProperty<T extends HalResource>(nameOrType ?: string|Function, maybeType ?: Function) {
let type;
let propType;
let propName;
let error = false;
if (nameOrType) {
if (typeof nameOrType === "string") {
propName = nameOrType;
} else {
type = nameOrType;
propType = nameOrType;
}

if (maybeType) {
if (typeof nameOrType === "function") {
error = true;
} else {
type = maybeType;
propType = maybeType;
}
}
}
Expand All @@ -39,8 +39,8 @@ export function HalProperty<T extends HalResource>(nameOrType ?: string|Function
if (error) {
throw new Error(`${target.constructor.name}.${key} @HalProperty parameters are 'name' and 'type', not reverse`);
}
const propertyType = Reflect.getMetadata("design:type", target, key);
if (propertyType === Array && type === undefined) {
const baseType = Reflect.getMetadata("design:type", target, key);
if (baseType === Array && propType === undefined) {
throw new Error(`${target.constructor.name}.${key} for Array you need to specify a type on @HalProperty.` +
"Example : @HalProperty(HalResource) or @HalProperty(ClassOfArrayContent)");
}
Expand All @@ -50,8 +50,11 @@ export function HalProperty<T extends HalResource>(nameOrType ?: string|Function
halToTs[propName || key] = key;
tsToHal[key] = propName || key;

const toUseType = type || propertyType;
Reflect.defineMetadata("halClient:specificType", toUseType, target, key);
const type = propType || baseType;
Reflect.defineMetadata("halClient:specificType", type, target, key);
if (propName && propType) {
Reflect.defineMetadata("halClient:specificType", type, target, propName);
}

// Delete property.
if (delete target[key]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HalProperty, HalResource } from "../../";

export class ContactInfos extends HalResource {
export class Contacts extends HalResource {
@HalProperty()
public phone: string;
}
6 changes: 6 additions & 0 deletions src/test/model/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { HalProperty, HalResource } from "../../";

export class Location extends HalResource {
@HalProperty()
public address: string;
}
11 changes: 9 additions & 2 deletions src/test/model/person.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HalProperty, HalResource } from "../../";

import { ContactInfos } from "./contact-infos";
import { Contacts } from "./contacts";
import { Location } from "./location";

export class Person extends HalResource {
@HalProperty()
Expand All @@ -16,8 +17,14 @@ export class Person extends HalResource {
public father: any;

@HalProperty()
public contactInfos: ContactInfos;
public contacts: Contacts;

@HalProperty("best-friend")
public bestFriend: Person;

@HalProperty(Location)
public home: Location;

@HalProperty("place-of-employment", Location)
public work: Location;
}
14 changes: 7 additions & 7 deletions src/test/rest-create-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createClient, createResource, HalProperty, HalResource, resetCache } fr
import * as nock from "nock";
import { test } from "tape-async";

import { ContactInfos } from "./model/contact-infos";
import { Contacts } from "./model/contacts";

let testNock;
const basePath = "http://test.fr/";
Expand Down Expand Up @@ -33,8 +33,8 @@ function initTests() {
},
},
_links : {
contactInfos : {
href : "http://test.fr/person/2/contactInfos",
contacts : {
href : "http://test.fr/person/2/contacts",
},
project: {
href: "http://test.fr/project/4",
Expand All @@ -55,10 +55,10 @@ function initTests() {
name : "Project 5",
};

const contactInfos = {
const contacts = {
_links : {
self : {
href : "http://test.fr/person/2/contactInfos",
href : "http://test.fr/person/2/contacts",
},
},
phone : "xxxxxxxxxx",
Expand All @@ -75,8 +75,8 @@ function initTests() {
.reply(200, newBestFriend);

testNock
.get("/person/2/contactInfos")
.reply(200, contactInfos);
.get("/person/2/contacts")
.reply(200, contacts);

testNock
.get("/project/5")
Expand Down
18 changes: 9 additions & 9 deletions src/test/rest-delete-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createClient, createResource, HalProperty, HalResource, resetCache } fr
import * as nock from "nock";
import { test } from "tape-async";

import { ContactInfos } from "./model/contact-infos";
import { Contacts } from "./model/contacts";

// mock list response
function initTests() {
Expand All @@ -21,11 +21,11 @@ function initTests() {
.reply(200, {success : "ok"});

testNock
.delete("/person/2/contactInfos")
.delete("/person/2/contacts")
.reply(200, {
_links : {
self : {
href : "http://test.fr/person/2/contactInfos",
href : "http://test.fr/person/2/contacts",
},
},
phone : "xxxxxxxxxx",
Expand Down Expand Up @@ -63,21 +63,21 @@ test("delete read halResource json response", async (t) => {
initTests();
const client = createClient();

const resource = createResource(client, HalResource, "http://test.fr/person/2/contactInfos");
const resource = createResource(client, HalResource, "http://test.fr/person/2/contacts");
const result = await client.delete(resource);

t.equals(result.prop("phone"), "xxxxxxxxxx");
t.equals(result.uri.uri, "http://test.fr/person/2/contactInfos");
t.equals(result.uri.uri, "http://test.fr/person/2/contacts");
});

test("delete read model class json response", async (t) => {
initTests();
const client = createClient();
const resource = createResource(client, ContactInfos, "http://test.fr/person/2/contactInfos");

const resource = createResource(client, Contacts, "http://test.fr/person/2/contacts");
const result = await resource.delete();

t.ok(result instanceof ContactInfos, "result is a ContactInfos");
t.ok(result instanceof Contacts, "result is a contacts");
t.equals(result.phone, "xxxxxxxxxx");
t.equals(result.uri.uri, "http://test.fr/person/2/contactInfos");
t.equals(result.uri.uri, "http://test.fr/person/2/contacts");
});
22 changes: 11 additions & 11 deletions src/test/rest-update-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createClient, createResource, HalProperty, HalResource, resetCache } fr
import * as nock from "nock";
import { test } from "tape-async";

import { ContactInfos } from "./model/contact-infos";
import { Contacts } from "./model/contacts";

let testNock;
const basePath = "http://test.fr/";
Expand Down Expand Up @@ -33,8 +33,8 @@ function initTests() {
},
},
_links : {
contactInfos : {
href : "http://test.fr/person/2/contactInfos",
contacts : {
href : "http://test.fr/person/2/contacts",
},
project: {
href: "http://test.fr/project/4",
Expand All @@ -55,10 +55,10 @@ function initTests() {
name : "Project 5",
};

const contactInfos = {
const contacts = {
_links : {
self : {
href : "http://test.fr/person/2/contactInfos",
href : "http://test.fr/person/2/contacts",
},
},
phone : "xxxxxxxxxx",
Expand All @@ -75,8 +75,8 @@ function initTests() {
.reply(200, newBestFriend);

testNock
.get("/person/2/contactInfos")
.reply(200, contactInfos);
.get("/person/2/contacts")
.reply(200, contacts);

testNock
.get("/project/5")
Expand Down Expand Up @@ -111,18 +111,18 @@ test("can update link using HalResource", async (t) => {

const resource = await client.fetchResource("http://test.fr/person/1");
resource.prop("name", "test");
await resource.prop("contactInfos").fetch();
resource.prop("contactInfos").prop("phone", "06XX1245XX");
await resource.prop("contacts").fetch();
resource.prop("contacts").prop("phone", "06XX1245XX");

const scope = nock(basePath)
.intercept("/person/1", "PATCH", {name: "test"})
.reply(200);
scope
.intercept("/person/2/contactInfos", "PATCH", {phone: "06XX1245XX"})
.intercept("/person/2/contacts", "PATCH", {phone: "06XX1245XX"})
.reply(200);

try {
const [result, result2] = await Promise.all([resource.update(), resource.prop("contactInfos").update()]);
const [result, result2] = await Promise.all([resource.update(), resource.prop("contacts").update()]);
t.equals(result.status, 200);
t.equals(result2.status, 200);
} catch (e) {
Expand Down
Loading

0 comments on commit c521570

Please sign in to comment.