|
|
|
@ -72,13 +72,14 @@ export default class Client {
|
|
|
|
|
|
|
|
|
|
async fetch(
|
|
|
|
|
input: string | URL | Request,
|
|
|
|
|
options?: FetchOptions | undefined
|
|
|
|
|
options?: FetchOptions | undefined,
|
|
|
|
|
) {
|
|
|
|
|
if (this.#client)
|
|
|
|
|
if (this.#client) {
|
|
|
|
|
options = {
|
|
|
|
|
client: this.#client,
|
|
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return await fetch(input, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -98,13 +99,14 @@ export default class Client {
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const json = await res.json();
|
|
|
|
|
|
|
|
|
|
this.token = json.key;
|
|
|
|
|
if (!this.token)
|
|
|
|
|
if (!this.token) {
|
|
|
|
|
throw new Error("Authentication failed, your token might be invalid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -113,12 +115,13 @@ export default class Client {
|
|
|
|
|
*/
|
|
|
|
|
public async fetchCategories(): Promise<Category[]> {
|
|
|
|
|
const res = await this.fetch(
|
|
|
|
|
"https://beta.character.ai/chat/character/categories/"
|
|
|
|
|
"https://beta.character.ai/chat/character/categories/",
|
|
|
|
|
);
|
|
|
|
|
const { categories } = await res.json();
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(categories))
|
|
|
|
|
if (!Array.isArray(categories)) {
|
|
|
|
|
throw new Error("API provided invalid category data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return categories;
|
|
|
|
|
}
|
|
|
|
@ -136,12 +139,13 @@ export default class Client {
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const { featured_characters } = await res.json();
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(featured_characters))
|
|
|
|
|
if (!Array.isArray(featured_characters)) {
|
|
|
|
|
throw new Error("API provided invalid featured characters data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return featured_characters;
|
|
|
|
|
}
|
|
|
|
@ -153,7 +157,7 @@ export default class Client {
|
|
|
|
|
*/
|
|
|
|
|
public async fetchCharacterInfo(id: string): Promise<CharacterInfo> {
|
|
|
|
|
const res = await this.fetch(
|
|
|
|
|
"https://beta.character.ai/chat/character/info/",
|
|
|
|
|
"https://beta.character.ai/chat/character/",
|
|
|
|
|
{
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
external_id: id,
|
|
|
|
@ -164,7 +168,7 @@ export default class Client {
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const { character } = await res.json();
|
|
|
|
|
|
|
|
|
@ -178,7 +182,7 @@ export default class Client {
|
|
|
|
|
*/
|
|
|
|
|
public async search(query: string): Promise<SearchResult[]> {
|
|
|
|
|
const url = new URL(
|
|
|
|
|
"https://beta.character.ai/chat/characters/search/"
|
|
|
|
|
"https://beta.character.ai/chat/characters/search/",
|
|
|
|
|
);
|
|
|
|
|
url.searchParams.set("query", query);
|
|
|
|
|
|
|
|
|
@ -191,8 +195,9 @@ export default class Client {
|
|
|
|
|
});
|
|
|
|
|
const { characters } = await res.json();
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(characters))
|
|
|
|
|
if (!Array.isArray(characters)) {
|
|
|
|
|
throw new Error("API provided invalid search response");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return characters;
|
|
|
|
|
}
|
|
|
|
@ -216,7 +221,7 @@ export default class Client {
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const history = await res.json();
|
|
|
|
|
|
|
|
|
@ -242,13 +247,13 @@ export default class Client {
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const history = await res.json();
|
|
|
|
|
|
|
|
|
|
if (history.status === "No Such History") return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return history;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|