Skip to content

Commit b320e5e

Browse files
committed
Added Endpoint.getLinkTemplate()
1 parent 7894e72 commit b320e5e

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

endpoints/Endpoint.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class Endpoint {
161161
}
162162

163163
/**
164-
* Resolves all links with a specific relation type cached from the last request.
164+
* Resolves all links with a specific relation type. Uses cached data from last response.
165165
* @param rel The relation type of the links to look for.
166166
*/
167167
getLinks(rel: string): { uri: URL; title?: string; }[] {
@@ -180,7 +180,7 @@ export class Endpoint {
180180
}
181181

182182
/**
183-
* Resolves a single link with a specific relation type.
183+
* Resolves a single link with a specific relation type. Uses cached data from last response.
184184
* @param rel The relation type of the link to look for.
185185
* @throws {@link NotFoundError}: No link with the specified `rel` could be found.
186186
*/
@@ -194,19 +194,28 @@ export class Endpoint {
194194
}
195195

196196
/**
197-
* Resolves a link template with a specific relation type.
197+
* Resolves a link template with a specific relation type. Uses cached data from last response.
198198
* @param rel The relation type of the link template to look for.
199199
* @param variables Variables for resolving the template.
200200
* @throws {@link NotFoundError}: No link template with the specified `rel` could be found.
201201
*/
202202
linkTemplate(rel: string, variables: { [key: string]: any; }): URL {
203-
const href = this.links.find(x => x.templated && x.rel === rel)?.href
203+
return this.join(URI.expand!(this.getLinkTemplate(rel), variables).toString());
204+
}
205+
206+
/**
207+
* Retrieves a link template with a specific relation type. Uses cached data from last response. Prefer {@link linkTemplate} when possible.
208+
* @param rel The relation type of the link template to look for.
209+
* @throws {@link NotFoundError}: No link template with the specified `rel` could be found.
210+
*/
211+
getLinkTemplate(rel: string) {
212+
const template = this.links.find(x => x.templated && x.rel === rel)?.href
204213
?? this.defaultLinkTemplates.get(rel);
205214

206-
if (!href)
215+
if (!template)
207216
throw new NotFoundError(`No link template with rel=${rel} provided by endpoint ${this.uri}.`, 0);
208217

209-
return this.join(URI.expand!(href, variables).toString());
218+
return template;
210219
}
211220

212221
// NOTE: Always replace entire array rather than modifying it to avoid async issues.

0 commit comments

Comments
 (0)