|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
import Chat from "./Chat.ts";
|
|
|
|
|
|
|
|
|
|
import { wrapFetch } from "https://deno.land/x/another_cookiejar@v5.0.2/mod.ts";
|
|
|
|
|
|
|
|
|
|
interface FetchOptions extends RequestInit {
|
|
|
|
|
client?: Deno.HttpClient;
|
|
|
|
|
}
|
|
|
|
@ -57,6 +59,7 @@ interface SearchResult {
|
|
|
|
|
export default class Client {
|
|
|
|
|
public token?: string;
|
|
|
|
|
#client?: Deno.HttpClient;
|
|
|
|
|
#cookieFetch = wrapFetch();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initalizes a HTTP(S) proxy for each request (Requires --unstable)
|
|
|
|
@ -73,6 +76,7 @@ export default class Client {
|
|
|
|
|
async fetch(
|
|
|
|
|
input: string | URL | Request,
|
|
|
|
|
options?: FetchOptions | undefined,
|
|
|
|
|
retryUntilSuccess = true,
|
|
|
|
|
) {
|
|
|
|
|
if (this.#client) {
|
|
|
|
|
options = {
|
|
|
|
@ -80,7 +84,23 @@ export default class Client {
|
|
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return await fetch(input, options);
|
|
|
|
|
let res = await this.#cookieFetch(input, options);
|
|
|
|
|
let resText = await res.clone().text();
|
|
|
|
|
|
|
|
|
|
while (
|
|
|
|
|
!resText.startsWith("{") && !resText.startsWith("[") && retryUntilSuccess
|
|
|
|
|
) {
|
|
|
|
|
console.log("Failed request! Retrying...");
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
|
res = await this.#cookieFetch(input, options);
|
|
|
|
|
resText = await res.clone().text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
body: res.body,
|
|
|
|
|
text: () => res,
|
|
|
|
|
json: () => JSON.parse(resText),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -170,7 +190,7 @@ export default class Client {
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const { character } = await res.json();
|
|
|
|
|
const { character } = res.json();
|
|
|
|
|
|
|
|
|
|
return character;
|
|
|
|
|
}
|
|
|
|
@ -193,6 +213,7 @@ export default class Client {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const resText = await res.text();
|
|
|
|
|
const { characters } = await res.json();
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(characters)) {
|
|
|
|
|