feishin/src/renderer/api/utils.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-04-23 02:09:25 -07:00
import { AxiosHeaders } from 'axios';
import { z } from 'zod';
import { toast } from '/@/renderer/components';
import { useAuthStore } from '/@/renderer/store';
import { ServerListItem } from '/@/renderer/types';
2023-04-23 02:09:25 -07:00
// Since ts-rest client returns a strict response type, we need to add the headers to the body object
export const resultWithHeaders = <ItemType extends z.ZodTypeAny>(itemSchema: ItemType) => {
2023-07-01 19:10:05 -07:00
return z.object({
data: itemSchema,
headers: z.instanceof(AxiosHeaders),
});
2023-04-23 02:09:25 -07:00
};
2023-04-24 01:21:29 -07:00
export const resultSubsonicBaseResponse = <ItemType extends z.ZodRawShape>(
2023-07-01 19:10:05 -07:00
itemSchema: ItemType,
2023-04-24 01:21:29 -07:00
) => {
2023-07-01 19:10:05 -07:00
return z.object({
'subsonic-response': z
.object({
status: z.string(),
version: z.string(),
})
.extend(itemSchema),
});
2023-04-24 01:21:29 -07:00
};
export const authenticationFailure = (currentServer: ServerListItem | null) => {
2023-07-01 19:10:05 -07:00
toast.error({
message: 'Your session has expired.',
});
2023-07-01 19:10:05 -07:00
if (currentServer) {
const serverId = currentServer.id;
const token = currentServer.ndCredential;
console.log(`token is expired: ${token}`);
useAuthStore.getState().actions.updateServer(serverId, { ndCredential: undefined });
useAuthStore.getState().actions.setCurrentServer(null);
}
};