Skip to content

Commit 8702d1a

Browse files
committed
Add support for defining hypermedia affordances
Resolves apiaryio#18
1 parent cbb0808 commit 8702d1a

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

docs/element-definitions.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ Note: At the moment only the HTTP protocol is supported.
189189
The value of `relation` attribute SHOULD be interpreted as a link relation
190190
between transition's parent resource and the transition's target resource
191191
as specified in the `href` attribute.
192+
193+
If a custom relation is being used, it SHOULD be defined using an
194+
Affordance Element, and the value of the `relation` property SHOULD be
195+
the `id` of the defined affordance.
192196

193197
- `href` (Templated Href) - The URI template for this transition.
194198

@@ -211,6 +215,7 @@ Note: At the moment only the HTTP protocol is supported.
211215
Definition of any input message-body attribute for this transition.
212216

213217
- `contentTypes` (array[String]) - A collection of content types that MAY be used for the transition.
218+
214219
- `content` (array)
215220
- (Copy) - Transition description's copy text.
216221
- (HTTP Transaction) - An instance of transaction example.
@@ -234,6 +239,49 @@ Note: At the moment only the HTTP protocol is supported.
234239
}
235240
```
236241

242+
### Affordance (Base API Element)
243+
244+
An affordance element provides a way to provide affordance definitions for the
245+
API. These definitions can describe link relations used in a Transition Element,
246+
along with defining unsafe and idempotent transitions.
247+
248+
While data structures are used for defining semantics around data models,
249+
affrodances are used for defining semantics around hypermedia state transitions.
250+
251+
The control types used here are based on the types used in [ALPS][]. The meta
252+
property `id` SHOULD be used to define the machine-usable name of the
253+
affordance.
254+
255+
#### Properties
256+
257+
- `element`: affordance
258+
- `attributes`
259+
- `controlType` (enum)
260+
- safe - A hypermedia control that triggers a safe, idempotent state
261+
transition (e.g. HTTP.GET or HTTP.HEAD).
262+
- unsafe - A hypermedia control that triggers an unsafe, non-idempotent
263+
state transition (e.g. HTTP.POST).
264+
- idempotent - A hypermedia control that triggers an unsafe, idempotent
265+
state transition (e.g. HTTP.PUT or HTTP.DELETE).
266+
- `content` (array)
267+
- (Copy)
268+
269+
#### Example
270+
271+
```json
272+
{
273+
"element": "affordance",
274+
"meta": {
275+
"id": "self",
276+
"title": "Self"
277+
},
278+
"attributes": {
279+
"controlType": "safe"
280+
},
281+
"content": []
282+
}
283+
```
284+
237285
### Category (Base API Element)
238286

239287
Grouping element – a set of elements forming a logical unit of an API such as
@@ -260,6 +308,7 @@ transitions.
260308
- scenario - Category is set of steps.
261309
- transitions - Category is a group of transitions.
262310
- authSchemes - Category is a group of authentication and authorization scheme definitions
311+
- affordances - Category is a group of affordances
263312
- `attributes`
264313
- `meta` (array[Member Element]) - Arbitrary metadata
265314

@@ -531,6 +580,11 @@ HTTP response message.
531580
- `element`: httpResponse (string, fixed)
532581
- `attributes`
533582
- `statusCode` (number) - HTTP response status code.
583+
- `content` (array)
584+
- (Affordance) - Expected affordance for the given payload
585+
586+
The Ref Element MAY be used to reference existing affordances defined
587+
elsewhere.
534588

535589
## Data Structure Elements
536590

@@ -1756,6 +1810,7 @@ These elements and definitions are referenced as part of the base Refract specif
17561810
[MSON]: https://github.com/apiaryio/mson
17571811
[MSON Reference]: https://github.com/apiaryio/mson/blob/master/MSON%20Reference.md
17581812
[Refract]: https://github.com/refractproject/refract-spec/blob/master/refract-spec.md
1813+
[ALPS]: https://tools.ietf.org/html/draft-amundsen-richardson-foster-alps-01#section-2.2.12
17591814

17601815
[API Description Elements]: definitions/api-description-elements.md
17611816
[Data Structure Elements]: definitions/data-structure-elements.md

docs/overview.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# API Elements Overview
22

3-
**Version**: 1.0.0-rc1
3+
**Version**: 1.1.0
44

55
**Mime Type**: TBD
66

@@ -37,6 +37,7 @@ It is also helpful to know the relationship between elements. The list below sho
3737
- Data Structure
3838
- Category (Group of Resource Elements)
3939
- Category (Group of Authentication and Authorization Scheme Definitions)
40+
- Category (Group of Affordance Elements)
4041
- Resource
4142
- Copy
4243
- Data Structure
@@ -52,6 +53,7 @@ It is also helpful to know the relationship between elements. The list below sho
5253
- HTTP Response
5354
- Copy
5455
- Data Structure
56+
- Affordance
5557
- Asset
5658

5759
This main API Category element MAY also be wrapped in a Parse Result element for conveying parsing information, such as source maps, warnings, and errors.
@@ -104,6 +106,10 @@ const transaction = query(apielements, {element: 'httpTransaction'})[0];
104106

105107
Given that API Elements use [Refract][], the structure of the document is recursive by nature. When looking for specific elements, it is best then to walk the tree to look for a match. Querying the tree means that your code will be decoupled not only from specific API description documents, but it will also be decoupled from the structure of those documents.
106108

109+
## Managing Compatibility
110+
111+
When parsing an API Elements document, parsers should safely ignore any elements that were not in the API Elements specification at the time of implementation. This allows for the specification to evolve using semantic versioning and introduce non-breaking changes.
112+
107113
[Refract]: https://github.com/refractproject/refract-spec/blob/master/refract-spec.md
108114
[MSON]: https://github.com/apiaryio/mson
109115
[RFC 2119]: https://datatracker.ietf.org/doc/rfc2119/

0 commit comments

Comments
 (0)