First Commit

This commit is contained in:
2026-01-19 19:51:25 +09:00
commit 5a16b0dbbb
28 changed files with 1321 additions and 0 deletions
+114
View File
@@ -0,0 +1,114 @@
import uwuzuError from "@/lib/error";
import uwuzuFetch from "@/lib/fetch";
interface sdkOptions {
/** uwuzuサーバーのorigin */
origin: string;
/**
* 通信に失敗した際の再試行回数です。
* 全て失敗した場合はエラーを発生します。
* 指定しない場合は5回が設定されます。
*/
retry?: number;
/**
* 通信に失敗した際に動作する再試行間の待機時間(ミリ秒)です。
* 再試行毎に2倍にされます。
* 指定しない場合は500msが設定されます。
*/
waiting?: number;
}
/* 必須かどうかの推論 */
type BodyArgs<M, E extends keyof M> =
M[E] extends { body: never } ? [] :
M[E] extends { body: infer B } ? [body: B] :
M[E] extends { body?: infer B } ? [body?: B] :
[];
export default class uwuzu<
M extends { [K in keyof M]: { body?: any; response: any } }
> {
readonly origin: string;
readonly retry: number;
readonly waiting: number;
private _token: string | null = null;
constructor(options: sdkOptions) {
this.origin = options.origin;
this.retry = options.retry ?? 5;
this.waiting = options.waiting ?? 500;
if (this.retry < 1) throw new uwuzuError("Invalid retry count.");
if (this.waiting < 1) throw new uwuzuError("Invalid base waiting time.");
if (options.origin !== new URL(options.origin).origin)
throw new uwuzuError("Invalid origin.");
}
/** APIトークン */
get token(): string | null {
return this._token;
}
set token(token: string) {
if (token.length !== 64) throw new uwuzuError("Invalid token.");
this._token = token;
}
/** APIリクエスト */
// 型あり
public async request<E extends keyof M>(
endpoint: E,
...args: BodyArgs<M, E>
): Promise<M[E]["response"]>;
// 型なし
public async request(
endpoint: string,
body?: any
): Promise<any>;
public async request(
endpoint: string,
...args: any[]
): Promise<any> {
let bodyParsed: any = args[0] ?? {};
if (typeof bodyParsed === "object") {
bodyParsed = {
...bodyParsed,
token: this._token,
};
bodyParsed = JSON.stringify(bodyParsed);
}
const req = await uwuzuFetch(
this.origin,
this.retry,
this.waiting,
"endpoint",
endpoint as string,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
cache: "no-store",
body: bodyParsed,
}
);
let res = await req.json();
if (res["0"] !== undefined && res.success === true) {
res.success = undefined;
const data = Object.values(res).filter(Boolean);
return {
success: true,
data,
};
}
return res;
}
}