they changed the character info endpoint for some reason

master
Drake 1 year ago
parent 5bb927af05
commit da0a1e99bb

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

Loading…
Cancel
Save