feishin/src/renderer/api/utils.ts

52 lines
1.6 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/api/types';
2024-03-05 14:05:01 -08:00
import { ServerFeature } from '/@/renderer/api/features-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);
}
};
2024-03-03 22:15:49 -08:00
export const hasFeature = (server: ServerListItem | null, feature: ServerFeature): boolean => {
if (!server || !server.features) {
return false;
}
return server.features[feature] ?? false;
};
2024-04-14 21:58:25 -07:00
export const SEPARATOR_STRING = ' · ';