v1.1.0
This commit is contained in:
+16
-23
@@ -1,9 +1,19 @@
|
||||
import uwuzuError from "./lib/error";
|
||||
import uwuzuFetch from "./lib/fetch";
|
||||
|
||||
export type parserType = (
|
||||
data: any,
|
||||
type: "request" | "response",
|
||||
endpoint: string,
|
||||
) => any;
|
||||
|
||||
interface sdkOptions {
|
||||
/** uwuzuサーバーのorigin */
|
||||
origin: string;
|
||||
/**
|
||||
* リクエスト/レスポンスのパーサー
|
||||
*/
|
||||
parser: parserType;
|
||||
/**
|
||||
* 通信に失敗した際の再試行回数です。
|
||||
* 全て失敗した場合はエラーを発生します。
|
||||
@@ -29,15 +39,18 @@ export default class uwuzu<
|
||||
M extends { [K in keyof M]: { body?: any; response: any } }
|
||||
> {
|
||||
readonly origin: string;
|
||||
readonly parser: parserType;
|
||||
readonly retry: number;
|
||||
readonly waiting: number;
|
||||
private _token: string | null = null;
|
||||
|
||||
constructor(options: sdkOptions) {
|
||||
this.origin = options.origin;
|
||||
this.parser = options.parser;
|
||||
this.retry = options.retry ?? 5;
|
||||
this.waiting = options.waiting ?? 500;
|
||||
|
||||
if (!this.parser) throw new uwuzuError("Invalid parser.");
|
||||
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)
|
||||
@@ -73,6 +86,8 @@ export default class uwuzu<
|
||||
let bodyParsed: any = args[0] ?? {};
|
||||
|
||||
if (typeof bodyParsed === "object") {
|
||||
bodyParsed = this.parser(bodyParsed, "request", endpoint);
|
||||
|
||||
bodyParsed = {
|
||||
...bodyParsed,
|
||||
token: this._token,
|
||||
@@ -93,29 +108,7 @@ export default class uwuzu<
|
||||
}
|
||||
);
|
||||
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
if (res.favorite_list !== undefined && res.success === true) {
|
||||
res.success = undefined;
|
||||
|
||||
const list = res.favorite_list.split(",");
|
||||
|
||||
return {
|
||||
success: true,
|
||||
favorite_list: list,
|
||||
};
|
||||
}
|
||||
const res = this.parser(await req.json(), "response", endpoint);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user