Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exports in hal to build an HalState from scatch #472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/state/hal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class HalState<T = any> extends BaseState<T> {

}

private serializeLinks(): hal.HalResource['_links'] {
serializeLinks(): hal.HalResource['_links'] {

const links: hal.HalResource['_links'] = {
self: { href: this.uri },
Expand Down Expand Up @@ -113,7 +113,7 @@ export const factory:StateFactory = async (client, uri, response): Promise<HalSt
/**
* Parse the Hal _links object and populate the 'links' property.
*/
function parseHalLinks(context: string, body: hal.HalResource): Link[] {
export function parseHalLinks(context: string, body: hal.HalResource): Link[] {

if (body._links === undefined) {
return [];
Expand Down
74 changes: 72 additions & 2 deletions test/unit/state/hal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { factory } from '../../../src/state/hal';
import { Client } from '../../../src';
import { HalState, factory, parseHalLinks } from '../../../src/state/hal';
import { Client, Links } from '../../../src';
import { HalResource } from 'hal-types';

describe('HAL state factory', () => {

Expand Down Expand Up @@ -229,6 +230,75 @@ describe('HAL state factory', () => {
happy: 2020,
});

});
it('should correctly build HALState from scratch', async() => {

const hal = await callFactory({
_links: {
self: {
href: '/foo',
},
author: {
href: 'https://evertpot.com/',
},
foo: [
{
href: '/bar',
},
{
href: '/bar2',
},
{
href: '/bar3',
},
]
},
happy: 2020,
}) as HalState;

// More direct way to get a json
const json : HalResource = {
_links: hal.serializeLinks(),
...hal.data
};
// Building an HalState
const {
_links,
...newBody
} = json;
const context = json._links.self.href;
const links = new Links(context, parseHalLinks(context, json));
const hal2 = new HalState({
client: hal.client,
uri: context,
data: newBody,
headers: new Headers(),
links,
});
expect(JSON.parse(hal2.serializeBody())).to.eql({
_links: {
self: {
href: 'http://example/',
},
author: {
href: 'https://evertpot.com/',
},
foo: [
{
href: '/bar',
},
{
href: '/bar2',
},
{
href: '/bar3',
},
]
},
happy: 2020,
});


});
it('should handle JSON documents that are arrays', async () => {

Expand Down