58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
|
"use server";
|
||
|
|
||
|
import { revalidateTag } from "next/cache";
|
||
|
import { cookies } from "next/headers";
|
||
|
import { Token } from "../types";
|
||
|
|
||
|
export async function addToken(_: Token, item: FormData): Promise<Token> {
|
||
|
const token = cookies().get("token");
|
||
|
const result = await fetch(
|
||
|
"https://tachy.daoyoucloud.com/api/tokens:create",
|
||
|
{
|
||
|
method: "POST",
|
||
|
headers: {
|
||
|
"X-App": "danmu-sim",
|
||
|
"X-Authenticator": "basic",
|
||
|
authorization: "Bearer " + token?.value,
|
||
|
"content-type": "application/json",
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
uid: item.get("uid") as string,
|
||
|
token: item.get("token") as string,
|
||
|
}),
|
||
|
}
|
||
|
);
|
||
|
const latest = await result.json();
|
||
|
revalidateTag("tokens");
|
||
|
return latest.data
|
||
|
}
|
||
|
|
||
|
export async function getToken(): Promise<Token> {
|
||
|
const token = cookies().get("token");
|
||
|
const params = new URLSearchParams();
|
||
|
params.append("pageSize", "20");
|
||
|
params.append("limit", "1");
|
||
|
params.append("sort", "-createdAt");
|
||
|
params.append(
|
||
|
"filter",
|
||
|
JSON.stringify({ $and: [{ createdBy: { id: { $eq: "{{$user.id}}" } } }] })
|
||
|
);
|
||
|
const liveTokens = await fetch(
|
||
|
"https://tachy.daoyoucloud.com/api/tokens:list?" + params.toString(),
|
||
|
{
|
||
|
method: "GET",
|
||
|
headers: {
|
||
|
"X-App": "danmu-sim",
|
||
|
"X-Authenticator": "basic",
|
||
|
authorization: "Bearer " + token?.value,
|
||
|
"content-type": "application/json",
|
||
|
},
|
||
|
next: {
|
||
|
tags: ["tokens"],
|
||
|
},
|
||
|
}
|
||
|
);
|
||
|
const result = await liveTokens.json();
|
||
|
return result?.data?.[0] ?? { uid: "", token: "" };
|
||
|
}
|