2023-04-23 02:09:25 -07:00
|
|
|
import { AxiosHeaders } from 'axios';
|
|
|
|
|
import { z } from 'zod';
|
2023-06-13 17:52:51 +00:00
|
|
|
import { toast } from '/@/renderer/components';
|
|
|
|
|
import { useAuthStore } from '/@/renderer/store';
|
2024-02-17 00:57:10 -08:00
|
|
|
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
|
|
|
};
|
2023-06-13 17:52:51 +00:00
|
|
|
|
|
|
|
|
export const authenticationFailure = (currentServer: ServerListItem | null) => {
|
2023-07-01 19:10:05 -07:00
|
|
|
toast.error({
|
|
|
|
|
message: 'Your session has expired.',
|
|
|
|
|
});
|
2023-06-13 17:52:51 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2023-06-13 17:52:51 +00:00
|
|
|
};
|
2024-02-03 22:47:57 -08:00
|
|
|
|
2024-03-03 22:15:49 -08:00
|
|
|
export const hasFeature = (server: ServerListItem | null, feature: ServerFeature): boolean => {
|
2024-02-03 22:47:57 -08:00
|
|
|
if (!server || !server.features) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 08:31:51 +00:00
|
|
|
return server.features[feature] ?? false;
|
2024-02-03 22:47:57 -08:00
|
|
|
};
|