Skip to main content

How to deal with the API response

Response Format​

interface APIResponseCore {
meta?: Record<string, any>;
links?: ResponseLinksObject;
}

interface SuccessResponse<T> extends APIResponseCore {
data: T
}

interface ErrorResponse extends APIResponseCore {
errors: ResponseError[]
}

type APIResponse<T> = SuccessResponse<T> | ErrorResponse;

Example Code​

const {
data: contacts,
errors: list_contacts_errors
} = await fetch(
list_contacts_url,
options
).then(r => r.json());

if (list_contacts_errors){
throw list_contacts_errors;
}
for (let contact of contacts){
// ...
}