|
| 1 | +# HAL (Hypertext Application Language) |
| 2 | + |
| 3 | +TypedRest supports extracting links from response bodies using the [HAL specification](https://datatracker.ietf.org/doc/html/draft-kelly-json-hal). HAL provides a consistent format for embedding links and embedded resources within JSON responses. |
| 4 | + |
| 5 | +## HAL format |
| 6 | + |
| 7 | +HAL adds a `_links` object to JSON responses containing links organized by relation type: |
| 8 | + |
| 9 | +```json |
| 10 | +{ |
| 11 | + "id": 123, |
| 12 | + "name": "John Doe", |
| 13 | + "_links": { |
| 14 | + "self": { "href": "/users/123" }, |
| 15 | + "orders": { "href": "/users/123/orders" }, |
| 16 | + "search": { "href": "/users/{id}", "templated": true } |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## Content Type |
| 22 | + |
| 23 | +TODO: Check the content type is actually used |
| 24 | + |
| 25 | +TypedRest recognizes HAL responses by the `application/hal+json` content type: |
| 26 | + |
| 27 | +``` |
| 28 | +Content-Type: application/hal+json |
| 29 | +``` |
| 30 | + |
| 31 | +When this content type is present, TypedRest automatically parses the `_links` object and makes the links available through the standard link resolution methods. |
| 32 | + |
| 33 | +## Using HAL links |
| 34 | + |
| 35 | +After receiving a HAL response, you can resolve links just like with HTTP Link headers: |
| 36 | + |
| 37 | +TODO: Check code samples |
| 38 | + |
| 39 | +=== "C#" |
| 40 | + |
| 41 | + ```csharp |
| 42 | + await endpoint.ReadAsync(); |
| 43 | + |
| 44 | + // Resolve a single link |
| 45 | + Uri ordersUri = endpoint.Link("orders"); |
| 46 | + |
| 47 | + // Resolve a templated link |
| 48 | + Uri userUri = endpoint.LinkTemplate("search", new { id = "456" }); |
| 49 | + ``` |
| 50 | + |
| 51 | +=== "TypeScript" |
| 52 | + |
| 53 | + ```typescript |
| 54 | + await endpoint.read(); |
| 55 | + |
| 56 | + // Resolve a single link |
| 57 | + const ordersUri = endpoint.link("orders"); |
| 58 | + |
| 59 | + // Resolve a templated link |
| 60 | + const userUri = endpoint.linkTemplate("search", { id: "456" }); |
| 61 | + ``` |
| 62 | + |
| 63 | +## Multiple links |
| 64 | + |
| 65 | +HAL supports multiple links for the same relation type using an array: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "_links": { |
| 70 | + "item": [ |
| 71 | + { "href": "/items/1", "title": "First Item" }, |
| 72 | + { "href": "/items/2", "title": "Second Item" } |
| 73 | + ] |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Retrieve all links with `GetLinks`: |
| 79 | + |
| 80 | +TODO: Check code samples |
| 81 | + |
| 82 | +=== "C#" |
| 83 | + |
| 84 | + ```csharp |
| 85 | + var items = endpoint.GetLinks("item"); |
| 86 | + foreach (var (uri, title) in items) |
| 87 | + { |
| 88 | + Console.WriteLine($"{title}: {uri}"); |
| 89 | + } |
| 90 | + ``` |
| 91 | + |
| 92 | +=== "TypeScript" |
| 93 | + |
| 94 | + ```typescript |
| 95 | + const items = endpoint.getLinks("item"); |
| 96 | + for (const { uri, title } of items) { |
| 97 | + console.log(`${title}: ${uri}`); |
| 98 | + } |
| 99 | + ``` |
| 100 | + |
| 101 | +## Templated links |
| 102 | + |
| 103 | +HAL links can be marked as [templates](uri-templates.md) with the `templated` property: |
| 104 | + |
| 105 | +```json |
| 106 | +{ |
| 107 | + "_links": { |
| 108 | + "find": { |
| 109 | + "href": "/users{?name,email}", |
| 110 | + "templated": true |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +TODO: Check code samples |
| 117 | + |
| 118 | +=== "C#" |
| 119 | + |
| 120 | + ```csharp |
| 121 | + var findUri = endpoint.LinkTemplate("find", new { name = "John", email = "john@example.com" }); |
| 122 | + // Result: /users?name=John&email=john%40example.com |
| 123 | + ``` |
| 124 | + |
| 125 | +=== "TypeScript" |
| 126 | + |
| 127 | + ```typescript |
| 128 | + const findUri = endpoint.linkTemplate("find", { name: "John", email: "john@example.com" }); |
| 129 | + // Result: /users? name=John&email=john%40example.com |
| 130 | + ``` |
0 commit comments