UNPKG

siren-types

Version:

A bunch of Siren types defined in TypeScript plus some useful functions

93 lines (92 loc) 3.41 kB
/** * These types are based on the JSON Schema found at: * * https://gist.github.com/kevinswiber/14477759858a768d2809326ca4300d26 */ export declare type Properties = { [key: string]: any; }; export declare type Siren<P extends Properties | undefined = undefined> = Entity<P>; export declare type EmbeddedRepr<P extends Properties | undefined = undefined> = P extends undefined ? EmbeddedRepresentationSubEntityWithoutProperties : EmbeddedRepresentationSubEntityWithProperties<P>; export declare type EmbeddedLink = EmbeddedLinkSubEntity; /** * This is the definition of a Siren entity. Note that everything is optional */ export interface Base { class?: string[]; title?: string; entities?: Array<SubEntity<Properties> | SubEntity<undefined>>; actions?: Action[]; links?: Link[]; } export declare type Entity<P extends Properties | undefined = undefined> = P extends Properties ? EntityWithProperties<P> : EntityWithoutProperties; export interface EntityWithProperties<P extends Properties> extends Base { properties: P; } export interface EntityWithoutProperties extends Base { properties?: undefined; } /** * Entities in Siren come in two flavors. Personally, I've never used the * embedded link sub-entity, but I include it here for completeness. */ export declare type SubEntity<P extends Properties | undefined = undefined> = EmbeddedLinkSubEntity | EmbeddedRepresentationSubEntity<P>; /** * The aforementioned (but never actually used by me) EmbeddedLinkSubEntity. */ export interface EmbeddedLinkSubEntity { class?: string[]; rel: string[]; href: string; type?: string; title?: string; } /** * An embedded sub-entity. It is just like any other Siren representation * except that it MUST include a relation as well. */ export interface EmbeddedRepresentationSubEntityWithProperties<P> extends EntityWithProperties<P> { rel: string[]; } export interface EmbeddedRepresentationSubEntityWithoutProperties extends EntityWithoutProperties { rel: string[]; } export declare type EmbeddedRepresentationSubEntity<P extends Properties | undefined = undefined> = P extends undefined ? EmbeddedRepresentationSubEntityWithoutProperties : EmbeddedRepresentationSubEntityWithProperties<P>; /** * Actions show available behaviors an entity exposes. */ export interface Action { name: string; class?: string[]; method?: "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS"; href: string; title?: string; type?: string; fields?: Field[]; } export interface Field { name: string; type?: "hidden" | "text" | "search" | "tel" | "url" | "email" | "password" | "datetime" | "date" | "month" | "week" | "time" | "datetime-local" | "number" | "range" | "color" | "checkbox" | "radio" | "file"; title?: string; value?: string | number | FieldValueObject[]; } /** * Value objects represent multiple selectable field values. Use * in conjunction with field `"type" = "radio"` and `"type" = "checkbox"` * to express that zero, one or many out of several possible values may be sent back to the server. */ export interface FieldValueObject { value: string | number; title?: string; selected?: boolean; } /** * Links represent navigational transitions. */ export interface Link { class?: string[]; title?: string; rel: string[]; href: string; type?: string; }