2024-08-28 00:23:29 +02:00
|
|
|
import { Client, type Token } from "@versia/client";
|
2024-12-01 17:20:21 +01:00
|
|
|
import { toast } from "vue-sonner";
|
2024-04-22 09:38:51 +02:00
|
|
|
|
2024-06-08 01:09:15 +02:00
|
|
|
export const useClient = (
|
2025-01-29 04:39:33 +01:00
|
|
|
origin?: MaybeRef<URL>,
|
2024-06-10 05:24:55 +02:00
|
|
|
customToken: MaybeRef<Token | null> = null,
|
2024-08-28 00:23:29 +02:00
|
|
|
): Ref<Client> => {
|
2025-01-29 04:39:33 +01:00
|
|
|
const apiHost = window.location.origin;
|
|
|
|
|
const domain = identity.value?.instance.domain;
|
|
|
|
|
|
|
|
|
|
return ref(
|
|
|
|
|
new Client(
|
|
|
|
|
toValue(origin) ??
|
|
|
|
|
(domain ? new URL(`https://${domain}`) : new URL(apiHost)),
|
|
|
|
|
toValue(customToken)?.access_token ??
|
|
|
|
|
identity.value?.tokens.access_token ??
|
|
|
|
|
undefined,
|
|
|
|
|
(error) => {
|
|
|
|
|
toast.error(
|
|
|
|
|
error.response.data.error ?? "No error message provided",
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
) as Ref<Client>;
|
2024-04-22 09:38:51 +02:00
|
|
|
};
|
2024-07-21 15:33:32 +02:00
|
|
|
|
|
|
|
|
export const client = useClient();
|