ketting
Version:
Opinionated HATEOAS / Rest client.
120 lines (119 loc) • 3.73 kB
TypeScript
import { State, HeadState } from './interface.js';
import { Links, LinkVariables } from '../link.js';
import Client from '../client.js';
import { Action, ActionInfo } from '../action.js';
import { Resource } from '../resource.js';
import { StateSerializedBody } from '#state-serialized-body';
type HeadStateInit = {
client: Client;
uri: string;
links: Links;
/**
* The full list of HTTP headers that were sent with the response.
*/
headers: Headers;
};
type StateInit<T> = {
client: Client;
uri: string;
data: T;
headers: Headers;
links: Links;
embedded?: State[];
actions?: ActionInfo[];
};
/**
* Implements a State object for HEAD responses
*/
export declare class BaseHeadState implements HeadState {
uri: string;
/**
* Timestamp of when the State was first generated
*/
timestamp: number;
/**
* The full list of HTTP headers that were sent with the response.
*/
headers: Headers;
/**
* All links associated with the resource.
*/
links: Links;
/**
* Reference to main client that created this state
*/
client: Client;
constructor(init: HeadStateInit);
/**
* Follows a relationship, based on its reltype. For example, this might be
* 'alternate', 'item', 'edit' or a custom url-based one.
*
* This function can also follow templated uris. You can specify uri
* variables in the optional variables argument.
*/
follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): Resource<TFollowedResource>;
/**
* Follows a relationship based on its reltype. This function returns a
* Promise that resolves to an array of Resource objects.
*
* If no resources were found, the array will be empty.
*/
followAll<TFollowedResource = any>(rel: string): Resource<TFollowedResource>[];
/**
* Content-headers are a subset of HTTP headers that related directly
* to the content. The obvious ones are Content-Type.
*
* This set of headers will be sent by the server along with a GET
* response, but will also be sent back to the server in a PUT
* request.
*/
contentHeaders(): Headers;
}
/**
* The Base State provides a convenient way to implement a new State type.
*/
export declare class BaseState<T> extends BaseHeadState implements State<T> {
data: T;
protected embedded: State[];
protected actionInfo: ActionInfo[];
constructor(init: StateInit<T>);
/**
* Return an action by name.
*
* If no name is given, the first action is returned. This is useful for
* formats that only supply 1 action, and no name.
*/
action<TFormData extends Record<string, any> = any>(name?: string): Action<TFormData>;
findAction<TFormData extends Record<string, any> = any>(name?: string): Action<TFormData> | undefined;
private doFindAction;
/**
* Returns all actions
*/
actions(): Action[];
/**
* Checks if the specified action exists.
*
* If no name is given, checks if _any_ action exists.
*/
hasAction(name?: string): boolean;
/**
* Returns a serialization of the state that can be used in a HTTP
* response.
*
* For example, a JSON object might simply serialize using
* JSON.serialize().
*/
serializeBody(): StateSerializedBody;
/**
* Certain formats can embed other resources, identified by their
* own URI.
*
* When a format has embedded resources, we will use these to warm
* the cache.
*
* This method returns every embedded resource.
*/
getEmbedded(): State[];
clone(): State<T>;
}
export {};