refactor: ♻️ Replace megalodon with @lysand-org/client

This commit is contained in:
Jesse Wierzbinski 2024-06-07 13:09:15 -10:00
parent 5c528e8d03
commit 0bd3237965
No known key found for this signature in database
41 changed files with 147 additions and 170 deletions

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Account } from "~/types/mastodon/account";
export const useAccount = (
client: MaybeRef<Mastodon | null>,
client: MaybeRef<LysandClient | null>,
accountId: MaybeRef<string | null>,
) => {
if (!client) {

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Account } from "~/types/mastodon/account";
export const useAccountSearch = (
client: MaybeRef<Mastodon | null>,
client: MaybeRef<LysandClient | null>,
q: string,
): Ref<Account[] | null> => {
const output = ref(null as Account[] | null);

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Status } from "~/types/mastodon/status";
export const useAccountTimeline = (
client: Mastodon | null,
client: LysandClient | null,
id: MaybeRef<string | null>,
options: MaybeRef<{
limit?: number | undefined;

View file

@ -1,8 +1,12 @@
import type { LysandClient } from "@lysand-org/client";
import { StorageSerializers } from "@vueuse/core";
import type { OAuth } from "megalodon";
export type ApplicationData = Awaited<
ReturnType<LysandClient["createApp"]>
>["data"];
export const useAppData = () => {
return useLocalStorage<OAuth.AppData | null>("lysand:app_data", null, {
return useLocalStorage<ApplicationData | null>("lysand:app_data", null, {
serializer: StorageSerializers.object,
});
};

View file

@ -1,7 +1,6 @@
import type { Mastodon } from "megalodon";
import type { InstanceWithExtra } from "./Instance";
import type { LysandClient } from "@lysand-org/client";
export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
export const useCacheRefresh = (client: MaybeRef<LysandClient | null>) => {
if (process.server) return;
const tokenData = useTokenData();
@ -43,7 +42,7 @@ export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
toValue(client)
?.getInstance()
.then((res) => {
instance.value = res.data as InstanceWithExtra;
instance.value = res.data;
});
});
};

View file

@ -1,11 +1,13 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
type ExtendedDescription = {
updated_at: string;
content: string;
};
export const useExtendedDescription = (client: MaybeRef<Mastodon | null>) => {
export const useExtendedDescription = (
client: MaybeRef<LysandClient | null>,
) => {
if (!ref(client).value) {
return ref(null as ExtendedDescription | null);
}
@ -13,7 +15,7 @@ export const useExtendedDescription = (client: MaybeRef<Mastodon | null>) => {
const output = ref(null as ExtendedDescription | null);
ref(client)
.value?.client.get("/api/v1/instance/extended_description")
.value?.getInstanceExtendedDescription()
.then((res) => {
output.value = res.data;
});

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Status } from "~/types/mastodon/status";
export const useHomeTimeline = (
client: Mastodon | null,
client: LysandClient | null,
options: MaybeRef<{
local?: boolean;
limit?: number;

View file

@ -1,26 +1,15 @@
import type { LysandClient } from "@lysand-org/client";
import { StorageSerializers } from "@vueuse/core";
import type { Mastodon } from "megalodon";
import type { Instance } from "~/types/mastodon/instance";
export type InstanceWithExtra = Instance & {
banner: string | null;
lysand_version: string;
sso: {
forced: boolean;
providers: {
id: string;
name: string;
icon?: string;
}[];
};
};
// Return type of LysandClient.getInstance
export type Instance = Awaited<ReturnType<LysandClient["getInstance"]>>["data"];
export const useInstance = () => {
if (process.server) {
return ref(null);
}
return useLocalStorage<InstanceWithExtra | null>("lysand:instance", null, {
return useLocalStorage<Instance | null>("lysand:instance", null, {
serializer: StorageSerializers.object,
});
};

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Status } from "~/types/mastodon/status";
export const useLocalTimeline = (
client: Mastodon | null,
client: LysandClient | null,
options: MaybeRef<
Partial<{
only_media: boolean;

View file

@ -1,15 +1,18 @@
import { Mastodon, type OAuth } from "megalodon";
import { LysandClient, type Token } from "@lysand-org/client";
export const useMegalodon = (
tokenData?: MaybeRef<OAuth.TokenData | null>,
export const useClient = (
tokenData?: MaybeRef<Token | null>,
disableOnServer = false,
): Ref<Mastodon | null> => {
): Ref<LysandClient | null> => {
if (disableOnServer && process.server) {
return ref(null);
}
return computed(
() =>
new Mastodon(useBaseUrl().value, toValue(tokenData)?.access_token),
new LysandClient(
new URL(useBaseUrl().value),
toValue(tokenData)?.access_token,
),
);
};

View file

@ -1,7 +1,10 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Status } from "~/types/mastodon/status";
export const useNote = (client: MaybeRef<Mastodon | null>, noteId: string) => {
export const useNote = (
client: MaybeRef<LysandClient | null>,
noteId: string,
) => {
if (!ref(client).value) {
return ref(null as Status | null);
}

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Context } from "~/types/mastodon/context";
export const useNoteContext = (
client: MaybeRef<Mastodon | null>,
client: MaybeRef<LysandClient | null>,
noteId: MaybeRef<string | null>,
) => {
if (!ref(client).value) {

View file

@ -1,9 +1,9 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Status } from "~/types/mastodon/status";
export const useNoteData = (
noteProp: MaybeRef<Status | undefined>,
client: Ref<Mastodon | null>,
client: Ref<LysandClient | null>,
) => {
const isReply = computed(() => !!toValue(noteProp)?.in_reply_to_id);
const isQuote = computed(() => !!toValue(noteProp)?.quote);

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Notification } from "~/types/mastodon/notification";
export const useNotificationTimeline = (
client: Mastodon | null,
client: LysandClient | null,
options: MaybeRef<{
limit?: number | undefined;
max_id?: string | undefined;

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Status } from "~/types/mastodon/status";
export const usePublicTimeline = (
client: Mastodon | null,
client: LysandClient | null,
options: MaybeRef<{
only_media?: boolean;
limit?: number;

View file

@ -1,8 +1,8 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Relationship } from "~/types/mastodon/relationship";
export const useRelationship = (
client: MaybeRef<Mastodon | null>,
client: MaybeRef<LysandClient | null>,
accountId: MaybeRef<string | null>,
) => {
const relationship = ref(null as Relationship | null);

View file

@ -1,10 +1,10 @@
import type { Mastodon } from "megalodon";
import type { LysandClient } from "@lysand-org/client";
import type { Account } from "~/types/mastodon/account";
import type { Mention } from "~/types/mastodon/mention";
export const useResolveMentions = (
mentions: Ref<Mention[]>,
client: Mastodon | null,
client: LysandClient | null,
): Ref<Account[]> => {
if (!client) {
return ref([]);

View file

@ -1,6 +1,6 @@
import type { InstanceWithExtra } from "./Instance";
import type { Instance } from "./Instance";
export const useSSOConfig = (): Ref<InstanceWithExtra["sso"] | null> => {
export const useSSOConfig = (): Ref<Instance["sso"] | null> => {
const instance = useInstance();
return computed(() => instance.value?.sso || null);

View file

@ -1,4 +1,4 @@
import type { Mastodon, Response } from "megalodon";
import type { LysandClient, Output } from "@lysand-org/client";
interface BaseOptions {
max_id?: string;
@ -6,9 +6,9 @@ interface BaseOptions {
}
type FetchTimelineFunction<Element, Options> = (
client: Mastodon,
client: LysandClient,
options: Options & BaseOptions,
) => Promise<Response<Element[]>>;
) => Promise<Output<Element[]>>;
export const useTimeline = <
Element extends {
@ -16,7 +16,7 @@ export const useTimeline = <
},
Options,
>(
client: Mastodon | null,
client: LysandClient | null,
fetchTimeline: FetchTimelineFunction<Element, Options> | null | undefined,
options: MaybeRef<Options & BaseOptions>,
): {
@ -98,7 +98,7 @@ export const useIdTimeline = <
},
Options,
>(
client: Mastodon | null,
client: LysandClient | null,
id: MaybeRef<string | null>,
fetchTimeline: FetchTimelineFunction<Element, Options> | null | undefined,
options: MaybeRef<Options & BaseOptions>,

View file

@ -1,8 +1,8 @@
import type { Token } from "@lysand-org/client";
import { StorageSerializers } from "@vueuse/core";
import type { OAuth } from "megalodon";
export const useTokenData = () => {
return useLocalStorage<OAuth.TokenData | null>("lysand:token_data", null, {
return useLocalStorage<Token | null>("lysand:token_data", null, {
serializer: StorageSerializers.object,
});
};