added getApiResultData helper

This commit is contained in:
2023-03-23 10:14:30 +10:00
parent eb1c475fd9
commit 55fffef5cb

View File

@@ -275,3 +275,23 @@ export const api = {
return await this.send(options);
},
};
/**
* Get an api result data as type.
*
* @param result The api result object.
* @param defaultValue The default data to return if no result exists.
* @returns Data object.
*/
export function getApiResultData<T>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result: any,
defaultValue: T | null = null
): T | null {
if (!result || !Object.prototype.hasOwnProperty.call(result, "data")) {
return defaultValue;
}
const data = result.data as T;
return data instanceof Object ? data : defaultValue;
}