update response interface

This commit is contained in:
Lorraxs 2024-02-28 12:58:09 +07:00
parent 751c18cb32
commit 4e93523115
3 changed files with 11 additions and 5 deletions

View File

@ -1,12 +1,13 @@
function Response(isSuccess, errorMessage, data)
function Response(isSuccess, message, data)
if not isSuccess then
return {
status = "error",
data = errorMessage
message = message
}
end
return {
status = "success",
data = data
data = data,
message = message
}
end

View File

@ -21,11 +21,12 @@ export type AsyncThunkConfig = {
export type IResponseSuccess<T = unknown> = {
status: 'success';
data: T;
message?: string;
};
export type IResponseError = {
status: 'error';
data: string;
message: string;
};
export type IResponse<T = unknown> = IResponseSuccess<T> | IResponseError;

View File

@ -1,5 +1,6 @@
// Will return whether the current environment is in a regular browser
import { toast } from 'react-toastify';
import { IResponse, IResponseSuccess } from '../types';
// and not CEF
@ -17,6 +18,9 @@ export const GuardResponse = (
response: IResponse
): response is IResponseSuccess => {
if (!response) return false;
if (response.status === 'error') throw new Error(response.data);
if (response.status === 'error') throw new Error(response.message);
if (response.message) {
toast.success(response.message);
}
return response.status === 'success';
};