danmu-sim/app/songs/actions.tsx

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-04-06 18:08:51 +08:00
"use server";
import {
InternetProviderLyricSearchResponse,
getSearchResults,
} from "@/lib/actions/lyrics";
2024-04-06 18:08:51 +08:00
import { LyricSource } from "@/lib/types";
import { cookies } from "next/headers";
export async function searchSongs(
_currentState: InternetProviderLyricSearchResponse[],
formData: FormData
): Promise<InternetProviderLyricSearchResponse[]> {
const list = await getSearchResults({
name: formData.get("name") as string,
});
if (list) {
return list;
}
return [];
}
2024-04-06 18:08:51 +08:00
export async function addSong(
_: unknown,
item: InternetProviderLyricSearchResponse
): Promise<unknown> {
const token = cookies().get("token");
await fetch("https://tachy.daoyoucloud.com/api/songs:create", {
method: "POST",
headers: {
"X-App": "danmu-sim",
"X-Authenticator": "basic",
authorization: "Bearer " + token?.value,
"content-type": "application/json",
},
body: JSON.stringify({
artist: item.artist,
name: item.name,
source: LyricSource.NETEASE,
source_id: item.id,
}),
});
const params = new URLSearchParams();
params.append('pageSize', '99999');
params.append('filter', JSON.stringify({"$and":[{"createdBy":{"id":{"$eq":"{{$user.id}}"}}}]}))
const songs = await fetch("https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), {
method: "GET",
headers: {
"X-App": "danmu-sim",
"X-Authenticator": "basic",
authorization: "Bearer " + token?.value,
"content-type": "application/json",
},
});
const result = await songs.json();
console.log(result.data);
return result?.data;
}
export async function listSongs(): Promise<any[]> {
const token = cookies().get("token");
const params = new URLSearchParams();
params.append('pageSize', '99999');
params.append('filter', JSON.stringify({"$and":[{"createdBy":{"id":{"$eq":"{{$user.id}}"}}}]}))
const songs = await fetch("https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), {
method: "GET",
headers: {
"X-App": "danmu-sim",
"X-Authenticator": "basic",
authorization: "Bearer " + token?.value,
"content-type": "application/json",
},
});
const result = await songs.json();
return result?.data ?? [];
}