Skip to content

New types for HAL resource #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> & {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @marcialfps , We believe it would be better to move everything from line 27 to 109 to a separate Halstack -client types library.

Also will investigate how to contribute to the public npm or @types and push all the types we are exporting from Halstack libraries, including hasltack-react-hal and even the halstack-react-cdk.

We will start and spike to learn more about @types and agree on the best approach moving forward. I would keep this PR on Hold for the time being if that's ok with you.

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
];
19 changes: 13 additions & 6 deletions lib/src/hooks/useHalResource.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useState, useEffect } from "react";
import { HalApiCaller } from "@dxc-technology/halstack-client";
import { ErrorResponse, RequestStatus, UseHalResource, UseHalResourceResponse } from "./types";
import {
ErrorResponse,
HalResourceType,
Interactions,
RequestStatus,
UseHalResource,
UseHalResourceResponse,
} from "./types";

const buildErrorResponse = (error) => {
const errorResponse: ErrorResponse = {
Expand All @@ -21,8 +28,8 @@ const useHalResource = ({
}: UseHalResource): UseHalResourceResponse => {
const [status, setStatus] = useState<RequestStatus>("idle");
const [error, setError] = useState<ErrorResponse>();
const [resource, setResource] = useState<object>(null);
const [interactions, setInteractions] = useState<object>(null);
const [resource, setResource] = useState<HalResourceType>();
const [interactions, setInteractions] = useState<Interactions>();

useEffect(() => {
const getInteractionHandler = (method, resourceSelf, methodHref) => {
Expand Down Expand Up @@ -65,7 +72,7 @@ const useHalResource = ({
};
};

const getInteractions = (halResource) => {
const getInteractions = (halResource: HalResourceType) => {
return halResource
.getInteractions()
.map((interaction) => ({
Expand Down Expand Up @@ -98,15 +105,15 @@ const useHalResource = ({
setInteractions(getInteractions(response.halResource));
setStatus("resolved");
} else {
setResource(null);
setResource(undefined);
const errorResponse = buildErrorResponse({
message: "Response does not contain a valid HAL resource",
});
setError(errorResponse);
setStatus("rejected");
}
} catch (err) {
setResource(null);
setResource(undefined);
setError(buildErrorResponse(err));
setStatus("rejected");
}
Expand Down