UNPKG

ketting

Version:

Opinionated HATEOAS / Rest client.

103 lines (102 loc) 2.54 kB
import { State } from './state/index.js'; import Client from './client.js'; import { Field } from './field.js'; import Resource from './resource.js'; export interface ActionInfo { /** * What url to post the form to. */ uri: string; /** * Action name. * * Some formats call this the 'rel' */ name: string | null; /** * Form title. * * Should be human-friendly. */ title?: string; /** * The HTTP method to use */ method: string; /** * The contentType to use for the form submission */ contentType: string; /** * Returns the list of fields associated to an action */ fields: Field[]; } /** * An action represents a hypermedia form submission or action. */ export interface Action<T extends Record<string, any> = Record<string, any>> extends ActionInfo { /** * Execute the action or submit the form. */ submit(formData: T): Promise<State>; /** * Return a field by name. */ field(name: string): Field | undefined; /** * Execute the action or submit the form, then return the next resource. * * If a server responds with a 201 Status code and a Location header, * it will automatically return the newly created resource. * * If the server responded with a 204 or 205, this function will return * `this`. */ submitFollow(formData: T): Promise<Resource>; } export declare class SimpleAction<TFormData extends Record<string, any>> implements Action { /** * What url to post the form to. */ uri: string; /** * Action name. * * Some formats call this the 'rel' */ name: string | null; /** * Form title. * * Should be human-friendly. */ title: string; /** * The HTTP method to use */ method: string; /** * The contentType to use for the form submission */ contentType: string; /** * Returns the list of fields associated to an action */ fields: Field[]; /** * Reference to client */ client: Client; constructor(client: Client, formInfo: ActionInfo); /** * Execute the action or submit the form. */ submit(formData: TFormData): Promise<State<any>>; submitFollow(formData: TFormData): Promise<Resource>; private validateForm; private fetchOrThrowWithBody; field(name: string): Field | undefined; } export declare class ActionNotFound extends Error { }