Skip to content

Commit

Permalink
feat(types): 🏷️ Add new types for Hal resource
Browse files Browse the repository at this point in the history
New needed types are added to improve integration with typescript
  • Loading branch information
marcialfps committed Jan 22, 2024
1 parent b5815ce commit 183f0d7
Showing 1 changed file with 91 additions and 2 deletions.
93 changes: 91 additions & 2 deletions lib/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,101 @@ export type UseHalResource = {

export type RequestStatus = "idle" | "fetching" | "resolved" | "rejected" | "interaction";

type InteractionType = Record<string, unknown> & {
rel: string;
href: string;
};

type OptionsProperty = {
key: string;
schema: Record<string, unknown>;
};

type PropertyType = {
key: string;
value: unknown;
};

type HalInteractionType = InteractionType & {
getSchemaProperties: () => OptionsProperty[];
getSchemaProperty: (key: string) => OptionsProperty | null;
hasProperty: (property: string) => boolean;
getRequiredProperties: () => string[];
isPropertyRequired: (key: string) => boolean;
addSchemaProperties: (properties: OptionsProperty[]) => void;
addSchemaProperty: (property: OptionsProperty, schema: Record<string, unknown>) => void;
};

type HalItemType = {
href: string;
summary?: unknown;
title: string;
name: string;
};

type LinkType = Record<string, unknown> & {
rel: string;
};

type HalPropertyType = PropertyType & {
isRequired: () => boolean;
getSchema: () => OptionsProperty | null;
existsInInteraction: (rel: string) => boolean;
};

type ItemType = Record<string, unknown> & {
href: string;
};

type OptionsType = {
links: InteractionType[];
properties?: OptionsProperty[];
title: string;
required?: string[];
};

export type HalResourceType = {
resourceRepresentation?: unknown;
getTitle: () => string;
getInteractions: () => HalInteractionType[];
getInteraction: (rel: string) => HalInteractionType;
getItems: () => HalItemType[];
getItem: (index: number) => HalItemType | null;
getLinks: () => LinkType[];
getLink: (rel: string) => LinkType | null;
getProperties: () => HalPropertyType[];
getProperty: (key: string) => HalPropertyType | null;
getSchemaProperties: () => OptionsProperty[];
getSchemaProperty: (key: string) => OptionsProperty | null;
getRequiredProperties: () => string[];

addLink: (link: LinkType) => void;
addLinks: (links: LinkType[]) => void;
addItem: (item: ItemType) => void;
addItems: (items: ItemType[]) => void;
addProperty: (property: PropertyType) => void;
addProperties: (properties: PropertyType[]) => void;

addOptionsProperties: (properties: OptionsProperty[]) => void;
addOptionsProperty: (property: OptionsProperty) => void;
addTitle: (title: string) => void;
addInteraction: (interaction: InteractionType) => void;
addOptions: (options: OptionsType[]) => void;
};

export type Interactions = Record<string, unknown>;

/**
* Array with the following stateful variables.
* - resource: HalResource
* - requestStatus: 'idle' | 'fetching' | 'resolved' | 'rejected' | 'interaction'
* - requestError: ErrorResponse
* - resourceInteractions: object. This is an object containing as many entries as interactions (_options.links) are available in the HAL resource.
* - resourceInteractions: object. This is an object containing as many entries as interactions (_options.links) are available in the HAL resource.
* Each entry has the rel value of the interaction as a key, and a function that you can execute passing a payload as a parameter.
*/
export type UseHalResourceResponse = [any, RequestStatus, ErrorResponse, any];
export type UseHalResourceResponse = [
HalResourceType | undefined,
RequestStatus,
ErrorResponse | undefined,
Interactions | undefined
];

0 comments on commit 183f0d7

Please sign in to comment.