-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend hypermedia interface to support collections
This addresses use case 3 (retrieving events). * Initial commit. * Changes after PR feerback. * Added two first use cases in form of a full web stack integration test. * Regenerated docs. * Changes after code review/ * Minor changes after latest review. * Minor changes after latest review. * Few more minor changes before merging. * Fixed test setup for travis * Changes for resource processing that doesn't remove hypermedia * Added use-case 3 integration test and required implementation changes. * Code review feedback changes * Quick code review change * Minor feedback fix
- Loading branch information
Showing
13 changed files
with
235 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"@context": "/api/context.jsonld", | ||
"@id": "/api/events", | ||
"@type": "hydra:Collection", | ||
"member": [ | ||
{ | ||
"@id": "/api/events/1", | ||
"@type": "schema:Event" | ||
}, | ||
{ | ||
"@id": "/api/events/2", | ||
"@type": "schema:Event" | ||
}, | ||
{ | ||
"@id": "/api/events/3", | ||
"@type": "schema:Event" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {IHypermedia} from "./IHypermedia"; | ||
import {IResource} from "./IResource"; | ||
/** | ||
* @interface Provides an abstraction layer over hypermedia container. | ||
*/ | ||
export interface IHypermediaContainer extends Array<IHypermedia> | ||
{ | ||
/** | ||
* @readonly Gets a collection members. This may be null if the resource owning this container is not a hydra:Collection. | ||
*/ | ||
readonly members: Array<IResource>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {IWebResource} from "./DataModel/IWebResource"; | ||
import {IHypermedia} from "./DataModel/IHypermedia"; | ||
import {IHydraResource} from "./DataModel/IHydraResource"; | ||
import {hydra} from "./namespaces"; | ||
import {IResource} from "./DataModel/IResource"; | ||
|
||
/** | ||
* @class @name ResourceEnrichmentProvider | ||
* Provides IWebResource enrichment routines. | ||
*/ | ||
export default class ResourceEnrichmentProvider | ||
{ | ||
private static properties = | ||
{ | ||
members: { type: hydra.Collection, propertyName: "members" } | ||
}; | ||
|
||
/** | ||
* Enriches a given resource with IHypermediaContainer specific properties. | ||
* @param resource Resource to be enriched. | ||
* @returns {IWebResource} | ||
*/ | ||
public enrichHypermedia(resource: IWebResource): IWebResource | ||
{ | ||
if (!resource) | ||
{ | ||
return resource; | ||
} | ||
|
||
if (!resource.hypermedia) | ||
{ | ||
Object.defineProperty(resource, "hypermedia", { value: new Array<IHypermedia>(), enumerable: false }); | ||
} | ||
|
||
for (let propertyName of Object.keys(ResourceEnrichmentProvider.properties)) | ||
{ | ||
let propertyDefinition = ResourceEnrichmentProvider.properties[propertyName]; | ||
let value = null; | ||
let collections = resource.hypermedia | ||
.filter(item => | ||
(<IHydraResource>item).isA && | ||
(<IHydraResource>item).isA.find(type => type === propertyDefinition.type)); | ||
if (collections.length > 0) | ||
{ | ||
value = Array.prototype.concat.apply( | ||
new Array<IResource>(), | ||
collections.map(collection => (<any>collection)[propertyDefinition.propertyName])); | ||
} | ||
|
||
Object.defineProperty(resource.hypermedia, propertyName, { value: value, enumerable: false }); | ||
} | ||
|
||
return resource; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
export let hydra: any = new String("http://www.w3.org/ns/hydra/core#"); | ||
hydra.namespace = hydra.toString(); | ||
hydra.apiDocumentation = hydra + "apiDocumentation"; | ||
hydra.member = hydra + "member"; | ||
hydra.ApiDocumentation = hydra + "ApiDocumentation"; | ||
hydra.EntryPoint = hydra + "EntryPoint"; | ||
hydra.EntryPoint = hydra + "EntryPoint"; | ||
hydra.Collection = hydra + "Collection"; | ||
hydra.Resource = hydra + "Resource"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.