mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import type { Middleware } from '@reduxjs/toolkit';
|
|
import { isRejectedWithValue } from '@reduxjs/toolkit';
|
|
import { toast } from 'features/toast/toast';
|
|
import { t } from 'i18next';
|
|
import { z } from 'zod';
|
|
|
|
const zRejectedForbiddenAction = z.object({
|
|
payload: z.object({
|
|
status: z.literal(403),
|
|
data: z.object({
|
|
detail: z.string(),
|
|
}),
|
|
}),
|
|
meta: z
|
|
.object({
|
|
arg: z
|
|
.object({
|
|
endpointName: z.string().optional(),
|
|
})
|
|
.optional(),
|
|
})
|
|
.optional(),
|
|
});
|
|
|
|
export const authToastMiddleware: Middleware = () => (next) => (action) => {
|
|
if (isRejectedWithValue(action)) {
|
|
try {
|
|
const parsed = zRejectedForbiddenAction.parse(action);
|
|
const endpointName = parsed.meta?.arg?.endpointName;
|
|
if (endpointName === 'getImageDTO') {
|
|
// do not show toast if problem is image access
|
|
return next(action);
|
|
}
|
|
|
|
const customMessage = parsed.payload.data.detail !== 'Forbidden' ? parsed.payload.data.detail : undefined;
|
|
toast({
|
|
id: `auth-error-toast-${endpointName}`,
|
|
title: t('toast.somethingWentWrong'),
|
|
status: 'error',
|
|
description: customMessage,
|
|
});
|
|
} catch (error) {
|
|
// no-op
|
|
}
|
|
}
|
|
|
|
return next(action);
|
|
};
|