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 if not isSuccess then
return { return {
status = "error", status = "error",
data = errorMessage message = message
} }
end end
return { return {
status = "success", status = "success",
data = data data = data,
message = message
} }
end end

View File

@ -21,11 +21,12 @@ export type AsyncThunkConfig = {
export type IResponseSuccess<T = unknown> = { export type IResponseSuccess<T = unknown> = {
status: 'success'; status: 'success';
data: T; data: T;
message?: string;
}; };
export type IResponseError = { export type IResponseError = {
status: 'error'; status: 'error';
data: string; message: string;
}; };
export type IResponse<T = unknown> = IResponseSuccess<T> | IResponseError; 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 // Will return whether the current environment is in a regular browser
import { toast } from 'react-toastify';
import { IResponse, IResponseSuccess } from '../types'; import { IResponse, IResponseSuccess } from '../types';
// and not CEF // and not CEF
@ -17,6 +18,9 @@ export const GuardResponse = (
response: IResponse response: IResponse
): response is IResponseSuccess => { ): response is IResponseSuccess => {
if (!response) return false; 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'; return response.status === 'success';
}; };