|
| 1 | +# Relative URIs |
| 2 | + |
| 3 | +The most straightforward way to navigate between endpoints is using hard-coded relative URIs. When creating child endpoints, you pass a relative URI that is resolved against the parent endpoint's URI. |
| 4 | + |
| 5 | +## Basic usage |
| 6 | + |
| 7 | +=== "C#" |
| 8 | + |
| 9 | + ```csharp |
| 10 | + var client = new EntryEndpoint(new Uri("http://example.com/api/")); |
| 11 | + var contacts = new CollectionEndpoint<Contact>(client, "contacts"); |
| 12 | + // Results in: http://example.com/api/contacts |
| 13 | + ``` |
| 14 | + |
| 15 | +=== "TypeScript" |
| 16 | + |
| 17 | + ```typescript |
| 18 | + const client = new EntryEndpoint(new URL("http://example.com/api/")); |
| 19 | + const contacts = new CollectionEndpoint<Contact>(client, "contacts"); |
| 20 | + // Results in: http://example.com/api/contacts |
| 21 | + ``` |
| 22 | + |
| 23 | +## Trailing slash pattern |
| 24 | + |
| 25 | +Standard URI resolution can be tricky when the parent URI doesn't have a trailing slash. Consider: |
| 26 | + |
| 27 | +- Base URI: `http://example.com/endpoint` |
| 28 | +- Relative URI: `subresource` |
| 29 | +- Standard resolution: `http://example.com/subresource` (replaces `endpoint`) |
| 30 | + |
| 31 | +This is often not the desired behavior. TypedRest provides a non-standard `./` prefix pattern to handle this: |
| 32 | + |
| 33 | +=== "C#" |
| 34 | + |
| 35 | + ```csharp |
| 36 | + var parent = new ElementEndpoint<MyEntity>(client, "endpoint"); |
| 37 | + // parent.Uri = http://example.com/endpoint |
| 38 | + |
| 39 | + // Without ./ prefix - replaces last segment |
| 40 | + var child1 = new ActionEndpoint(parent, "subresource"); |
| 41 | + // child1.Uri = http://example.com/subresource |
| 42 | + |
| 43 | + // With ./ prefix - appends to path |
| 44 | + var child2 = new ActionEndpoint(parent, "./subresource"); |
| 45 | + // child2.Uri = http://example.com/endpoint/subresource |
| 46 | + ``` |
| 47 | + |
| 48 | +=== "TypeScript" |
| 49 | + |
| 50 | + ```typescript |
| 51 | + const parent = new ElementEndpoint<MyEntity>(client, "endpoint"); |
| 52 | + // parent.uri = http://example.com/endpoint |
| 53 | + |
| 54 | + // Without ./ prefix - replaces last segment |
| 55 | + const child1 = new ActionEndpoint(parent, "subresource"); |
| 56 | + // child1.uri = http://example.com/subresource |
| 57 | + |
| 58 | + // With ./ prefix - appends to path |
| 59 | + const child2 = new ActionEndpoint(parent, "./subresource"); |
| 60 | + // child2.uri = http://example.com/endpoint/subresource |
| 61 | + ``` |
| 62 | + |
| 63 | +The `./` prefix tells TypedRest to ensure a trailing slash on the parent URI before resolving the relative URI. This makes the relative URI resolution behave as if the parent URI was `http://example.com/endpoint/`. |
| 64 | + |
| 65 | +## Default links |
| 66 | + |
| 67 | +You can also register default links that will be used when the server doesn't provide links for a specific relation type: |
| 68 | + |
| 69 | +=== "C#" |
| 70 | + |
| 71 | + ```csharp |
| 72 | + class MyEndpoint : EndpointBase |
| 73 | + { |
| 74 | + public MyEndpoint(IEndpoint referrer, string relativeUri) |
| 75 | + : base(referrer, relativeUri) |
| 76 | + { |
| 77 | + SetDefaultLink("related", "./related-resource"); |
| 78 | + } |
| 79 | + } |
| 80 | + ``` |
| 81 | + |
| 82 | +=== "TypeScript" |
| 83 | + |
| 84 | + ```typescript |
| 85 | + class MyEndpoint extends Endpoint { |
| 86 | + constructor(referrer: Endpoint, relativeUri: string) { |
| 87 | + super(referrer, relativeUri); |
| 88 | + this.setDefaultLink("related", "./related-resource"); |
| 89 | + } |
| 90 | + } |
| 91 | + ``` |
0 commit comments