|
| 1 | +# HAL (Hypertext Application Language) |
| 2 | + |
| 3 | +TypedRest supports links encoded in resources using the HAL format. HAL provides a standard way to embed links and related resources directly in JSON responses. |
| 4 | + |
| 5 | +For details on the HAL specification, see the [HAL draft specification](https://datatracker.ietf.org/doc/html/draft-kelly-json-hal). TypedRest focuses on making HAL links easy to consume within your endpoint hierarchy. |
| 6 | + |
| 7 | +## HAL Content Type |
| 8 | + |
| 9 | +HAL uses the content type `application/hal+json` to indicate HAL-formatted responses. TypedRest automatically detects this content type and extracts links accordingly. |
| 10 | + |
| 11 | +A typical HAL response looks like: |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "_links": { |
| 16 | + "self": { |
| 17 | + "href": "/contacts/1337" |
| 18 | + }, |
| 19 | + "edit": { |
| 20 | + "href": "/contacts/1337/edit" |
| 21 | + }, |
| 22 | + "next": { |
| 23 | + "href": "/contacts/1338" |
| 24 | + } |
| 25 | + }, |
| 26 | + "name": "John Smith", |
| 27 | + "email": "john@example.com" |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Using HAL Links |
| 32 | + |
| 33 | +HAL links are accessed using the same `Link()` and `GetLinks()` methods as HTTP Link headers: |
| 34 | + |
| 35 | +=== "C#" |
| 36 | + |
| 37 | + ```csharp |
| 38 | + var contact = new ElementEndpoint<Contact>(client, relativeUri: "./contacts/1337"); |
| 39 | + var contactData = await contact.ReadAsync(); |
| 40 | + |
| 41 | + // Retrieve a link from the HAL response |
| 42 | + var editUri = contact.Link("edit"); |
| 43 | + if (editUri != null) |
| 44 | + { |
| 45 | + var editEndpoint = new Endpoint(contact, editUri); |
| 46 | + } |
| 47 | + ``` |
| 48 | + |
| 49 | +=== "TypeScript" |
| 50 | + |
| 51 | + ```typescript |
| 52 | + const contact = new ElementEndpoint<Contact>(client, "./contacts/1337"); |
| 53 | + const contactData = await contact.read(); |
| 54 | + |
| 55 | + // Retrieve a link from the HAL response |
| 56 | + const editUri = contact.link("edit"); |
| 57 | + if (editUri) { |
| 58 | + const editEndpoint = new Endpoint(contact, editUri); |
| 59 | + } |
| 60 | + ``` |
| 61 | + |
| 62 | +## Multiple Links |
| 63 | + |
| 64 | +HAL supports multiple links with the same relation type. Use `GetLinks()` to retrieve all of them: |
| 65 | + |
| 66 | +=== "C#" |
| 67 | + |
| 68 | + ```csharp |
| 69 | + var resource = new ElementEndpoint<Resource>(client, relativeUri: "./resource"); |
| 70 | + await resource.ReadAsync(); |
| 71 | + |
| 72 | + // Get all "alternate" links |
| 73 | + var alternates = resource.GetLinks("alternate"); |
| 74 | + foreach (var alternate in alternates) |
| 75 | + { |
| 76 | + Console.WriteLine($"Alternate: {alternate}"); |
| 77 | + } |
| 78 | + ``` |
| 79 | + |
| 80 | +=== "TypeScript" |
| 81 | + |
| 82 | + ```typescript |
| 83 | + const resource = new ElementEndpoint<Resource>(client, "./resource"); |
| 84 | + await resource.read(); |
| 85 | + |
| 86 | + // Get all "alternate" links |
| 87 | + const alternates = resource.getLinks("alternate"); |
| 88 | + for (const alternate of alternates) { |
| 89 | + console.log(`Alternate: ${alternate}`); |
| 90 | + } |
| 91 | + ``` |
| 92 | + |
| 93 | +In HAL, multiple links are represented as an array: |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "_links": { |
| 98 | + "alternate": [ |
| 99 | + {"href": "/resource.xml"}, |
| 100 | + {"href": "/resource.pdf"} |
| 101 | + ] |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Templated Links |
| 107 | + |
| 108 | +HAL supports templated links using URI Templates ([RFC 6570](https://tools.ietf.org/html/rfc6570)): |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "_links": { |
| 113 | + "search": { |
| 114 | + "href": "/contacts{?name,email}", |
| 115 | + "templated": true |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Use `LinkTemplate()` to resolve templated links: |
| 122 | + |
| 123 | +=== "C#" |
| 124 | + |
| 125 | + ```csharp |
| 126 | + var contacts = new CollectionEndpoint<Contact>(client, relativeUri: "./contacts"); |
| 127 | + await contacts.ReadAllAsync(); |
| 128 | + |
| 129 | + // Resolve the templated link |
| 130 | + var searchUri = contacts.LinkTemplate("search", new {name = "Smith"}); |
| 131 | + ``` |
| 132 | + |
| 133 | +=== "TypeScript" |
| 134 | + |
| 135 | + ```typescript |
| 136 | + const contacts = new CollectionEndpoint<Contact>(client, "./contacts"); |
| 137 | + await contacts.readAll(); |
| 138 | + |
| 139 | + // Resolve the templated link |
| 140 | + const searchUri = contacts.linkTemplate("search", {name: "Smith"}); |
| 141 | + ``` |
| 142 | + |
| 143 | +## Combining with Link Headers |
| 144 | + |
| 145 | +TypedRest can extract links from both HTTP Link headers and HAL `_links` objects simultaneously. By default, both sources are checked, with header links taking precedence over HAL links when both provide a link with the same relation type. |
| 146 | + |
| 147 | +If you need more control over link extraction priority or want to use only specific sources, you can configure the link extraction behavior: |
| 148 | + |
| 149 | +=== "C#" |
| 150 | + |
| 151 | + ```csharp |
| 152 | + var endpoint = new Endpoint(new Uri("http://example.com/resource")); |
| 153 | + |
| 154 | + // Use AggregateLinkExtractor to combine multiple extractors |
| 155 | + endpoint.LinkExtractor = new AggregateLinkExtractor( |
| 156 | + new HeaderLinkExtractor(), |
| 157 | + new HalLinkExtractor() |
| 158 | + ); |
| 159 | + ``` |
| 160 | + |
| 161 | +=== "TypeScript" |
| 162 | + |
| 163 | + ```typescript |
| 164 | + const endpoint = new Endpoint(new URL("http://example.com/resource")); |
| 165 | + |
| 166 | + // Use AggregateLinkExtractor to combine multiple extractors |
| 167 | + endpoint.linkExtractor = new AggregateLinkExtractor( |
| 168 | + new HeaderLinkExtractor(), |
| 169 | + new HalLinkExtractor() |
| 170 | + ); |
| 171 | + ``` |
| 172 | + |
| 173 | +The `AggregateLinkExtractor` queries each extractor in order and returns the first match found, allowing you to control precedence by ordering the extractors. |
0 commit comments