UNPKG

ketting

Version:

Opinionated HATEOAS / Rest client.

120 lines (119 loc) 2.9 kB
import { LinkHints } from 'hal-types'; export type Link = { /** * Target URI */ href: string; /** * Context URI. * * Used to resolve relative URIs */ context: string; /** * Relation type */ rel: string; /** * Link title */ title?: string; /** * Content type hint of the target resource */ type?: string; /** * Anchor. * * This describes where the link is linked from, from for example * a fragment in the current document */ anchor?: string; /** * Language of the target resource */ hreflang?: string; /** * HTML5 media attribute */ media?: string; /** * If templated is set to true, the href is a templated URI. */ templated?: boolean; /** * Link hints, as defined in draft-nottingham-link-hint */ hints?: LinkHints; /** * Link name * * This is sometimes used as a machine-readable secondary key for links. * * This is at least used in HAL, but there may be other formats: * * @see https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-06#section-5.5 */ name?: string; }; type NewLink = Omit<Link, 'context'>; /** * Links container, providing an easy way to manage a set of links. */ export declare class Links { defaultContext: string; private store; constructor(defaultContext: string, links?: Link[] | Links); /** * Adds a link to the list */ add(...links: (Link | NewLink)[]): void; add(rel: string, href: string): void; /** * Set a link * * If a link with the provided 'rel' already existed, it will be overwritten. */ set(link: Link | NewLink): void; set(rel: string, href: string): void; /** * Return a single link by its 'rel'. * * If the link does not exist, undefined is returned. */ get(rel: string): Link | undefined; /** * Delete all links with the given 'rel'. * * If the second argument is provided, only links that match the href will * be removed. */ delete(rel: string, href?: string): void; /** * Return all links that have a given rel. * * If no links with the rel were found, an empty array is returned. */ getMany(rel: string): Link[]; /** * Return all links. */ getAll(): Link[]; /** * Returns true if at least 1 link with the given rel exists. */ has(rel: string): boolean; } /** * The LinkNotFound error gets thrown whenever something tries to follow a * link by its rel, that doesn't exist */ export declare class LinkNotFound extends Error { } /** * A key->value map of variables to place in a templated link */ export type LinkVariables = { [key: string]: string | number | string[] | number[]; }; export {};