-
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.
Add support for IriTemplate and IriTemplateMapping
- Loading branch information
Showing
19 changed files
with
401 additions
and
161 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
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,35 +1,62 @@ | ||
{ | ||
"@context": "/api/context.jsonld", | ||
"@id": "/api/events", | ||
"@type": ["hydra:Collection", "hydra:Resource"], | ||
"member": [ | ||
"@graph": [ | ||
{ | ||
"@id": "/api/events/1", | ||
"@type": "schema:Event" | ||
"@id": "/api/events", | ||
"@type": ["hydra:Collection", "hydra:Resource"], | ||
"member": [ | ||
{ | ||
"@id": "/api/events/1", | ||
"@type": "schema:Event" | ||
}, | ||
{ | ||
"@id": "/api/events/2", | ||
"@type": "schema:Event" | ||
}, | ||
{ | ||
"@id": "/api/events/3", | ||
"@type": "schema:Event" | ||
} | ||
], | ||
"search": { | ||
"@type": "hydra:IriTemplate", | ||
"template": "/api/events{?searchText}", | ||
"mappings": { | ||
"@type": "hydra:IriTemplateMapping", | ||
"variable": "searchText", | ||
"property": "hydra:freetextQuery", | ||
"variableRepresentation": "hydra:BasicRepresentation" | ||
} | ||
}, | ||
"examplevocab:filter": { | ||
"@type": "hydra:IriTemplate", | ||
"template": "/api/events{?eventName,eventDescription}", | ||
"mappings": [ | ||
{ | ||
"@type": "hydra:IriTemplateMapping", | ||
"variable": "eventName", | ||
"property": "schema:name", | ||
"variableRepresentation": "hydra:BasicRepresentation" | ||
}, | ||
{ | ||
"@type": "hydra:IriTemplateMapping", | ||
"variable": "eventDescription", | ||
"property": "schema:description", | ||
"variableRepresentation": "hydra:BasicRepresentation" | ||
} | ||
] | ||
}, | ||
"operation": [ | ||
{ | ||
"@type": ["hydra:Operation", "schema:CreateAction", "schema:AddAction"], | ||
"method": "POST", | ||
"expects": "schema:Event" | ||
} | ||
] | ||
}, | ||
{ | ||
"@id": "/api/events/2", | ||
"@type": "schema:Event" | ||
}, | ||
{ | ||
"@id": "/api/events/3", | ||
"@type": "schema:Event" | ||
} | ||
], | ||
"search": { | ||
"@type": "hydra:IriTemplate", | ||
"template": "/api/events{?searchText}", | ||
"mappings": { | ||
"variable": "searchText", | ||
"property": "hydra:freetextQuery", | ||
"variableRepresentation": "hydra:BasicRepresentation" | ||
} | ||
}, | ||
"operation": [ | ||
{ | ||
"@type": ["hydra:Operation", "schema:CreateAction", "schema:AddAction"], | ||
"method": "POST", | ||
"expects": "schema:Event" | ||
"@id": "examplevocab:filter", | ||
"@type": "hydra:TemplatedLink" | ||
} | ||
] | ||
} |
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,22 @@ | ||
import { IHydraResource } from "./IHydraResource"; | ||
import { IResource } from "./IResource"; | ||
|
||
/** | ||
* Provides an abstract description of a resource that points to another one. | ||
* @interface | ||
*/ | ||
export interface IPointingResource extends IHydraResource { | ||
/** | ||
* Gets a base URL that can be used to resolve target in case it is relative. | ||
* @readonly | ||
* @returns {string} | ||
*/ | ||
readonly baseUrl: string; | ||
|
||
/** | ||
* Gets a target URL to be called. | ||
* @readonly | ||
* @returns {IResource} | ||
*/ | ||
readonly target: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
import { ILink } from "./ILink"; | ||
import { IResource } from "./IResource"; | ||
import { ITemplatedResource } from "./ITemplatedResource"; | ||
|
||
/** | ||
* Provides a link that can has an URI template. | ||
* @interface | ||
*/ | ||
|
||
export interface ITemplatedLink extends ILink { | ||
/** | ||
* Expands an URI template with given variables. | ||
* @param templateVariables {{ [name: string]: string }} Template variables with values. | ||
* @returns {IResource} | ||
*/ | ||
expandTarget(templateVariables: { [name: string]: string }): IResource; | ||
} | ||
export interface ITemplatedLink extends ILink, ITemplatedResource<ILink> {} |
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,14 +1,8 @@ | ||
import { IOperation } from "./IOperation"; | ||
import { ITemplatedResource } from "./ITemplatedResource"; | ||
|
||
/** | ||
* Describes an {@link IOperation} that uses an URI template to point to the target of the request. | ||
* @interface | ||
*/ | ||
export interface ITemplatedOperation extends IOperation { | ||
/** | ||
* Expands an URI template with given variables. | ||
* @param templateVariables {{ [name: string]: string }} Template variables with values. | ||
* @returns {IOperation} | ||
*/ | ||
expandTarget(templateVariables: { [name: string]: string }): IOperation; | ||
} | ||
export interface ITemplatedOperation extends IOperation, ITemplatedResource<IOperation> {} |
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,20 @@ | ||
import { IPointingResource } from "./IPointingResource"; | ||
|
||
export interface IDictionary { | ||
[name: string]: string; | ||
} | ||
export type MappingBuilder = (MappingsBuilder) => void; | ||
|
||
/** | ||
* Provides an abstract description of a resource with expandable template. | ||
* @interface | ||
*/ | ||
export interface ITemplatedResource<T extends IPointingResource> extends IPointingResource { | ||
/** | ||
* Expands an URI template with given variables. | ||
* @param {{ [name: string]: string } | (MappingsBuilder) => void} mappedVariables Template variables with values or | ||
* {@link MappingsBuilder}. | ||
* @returns {T} | ||
*/ | ||
expandTarget(mappedVariables: IDictionary | MappingBuilder): T; | ||
} |
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,34 @@ | ||
import MappingsCollection from "./Collections/MappingsCollection"; | ||
import PropertyMapping from "./PropertyMapping"; | ||
|
||
export default class MappingsBuilder { | ||
private readonly mappings: MappingsCollection; | ||
private readonly result: { [name: string]: string }; | ||
|
||
public constructor(mappings: MappingsCollection) { | ||
this.mappings = mappings; | ||
this.result = {}; | ||
} | ||
|
||
public withProperty(property: string): PropertyMapping { | ||
const mapping = this.mappings.ofProperty(property).first(); | ||
if (!mapping) { | ||
throw new Error(`Invalid mapping of property '${property}.`); | ||
} | ||
|
||
return new PropertyMapping(this, this.result, mapping); | ||
} | ||
|
||
public withVariable(variableName: string): PropertyMapping { | ||
const mapping = this.mappings.ofVariableName(variableName).first(); | ||
if (!mapping) { | ||
throw new Error(`Invalid mapping of variable name mapping '${variableName}.`); | ||
} | ||
|
||
return new PropertyMapping(this, this.result, mapping); | ||
} | ||
|
||
public complete(): { [name: string]: string } { | ||
return this.result; | ||
} | ||
} |
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 @@ | ||
import { IIriTemplateMapping } from "./IIriTemplateMapping"; | ||
import MappingsBuilder from "./MappingsBuilder"; | ||
|
||
export default class PropertyMapping { | ||
private readonly builder: MappingsBuilder; | ||
private readonly result: { [name: string]: string }; | ||
private readonly mapping: IIriTemplateMapping; | ||
|
||
public constructor(builder: MappingsBuilder, result: { [name: string]: string }, mapping: IIriTemplateMapping) { | ||
this.builder = builder; | ||
this.result = result; | ||
this.mapping = mapping; | ||
} | ||
|
||
public havingValueOf(value: string): MappingsBuilder { | ||
this.result[this.mapping.variable] = value; | ||
return this.builder; | ||
} | ||
} |
Oops, something went wrong.