Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

hal.ts

46 lines1.4 KB

1/**
2 * HAL link objects (PLAN.md task 11.1). Standard HAL gives agents `href`
3 * (RFC 6570 templates where applicable); we extend each link with `method`,
4 * `contentType`, and an inline `example` because agents — unlike humans
5 * reading docs — need those to act without a second lookup. Documented in
6 * llms.txt.
7 */
8​
9export interface HalLink {
10 href: string
11 method: 'GET' | 'POST' | 'DELETE'
12 contentType: string
13 templated?: true
14 /** A concrete, copy-pasteable instantiation of href (URL for GETs, a
15 * request body for POSTs). */
16 example?: string | Record<string, unknown>
18​
19interface LinkOptions {
20 example?: string | Record<string, unknown>
21 contentType?: string
23​
24function isTemplated(href: string): boolean {
25 return href.includes('{')
27​
28export function halGet(href: string, options: LinkOptions = {}): HalLink {
29 return {
30 href,
31 method: 'GET',
32 contentType: options.contentType ?? 'application/json',
33 ...(isTemplated(href) ? { templated: true as const } : {}),
34 ...(options.example !== undefined ? { example: options.example } : {}),
35 }
37​
38export function halPost(href: string, options: LinkOptions = {}): HalLink {
39 return {
40 href,
41 method: 'POST',
42 contentType: options.contentType ?? 'application/json',
43 ...(isTemplated(href) ? { templated: true as const } : {}),
44 ...(options.example !== undefined ? { example: options.example } : {}),
45 }