feat: add semisol.dev recommends api

This commit is contained in:
2023-05-18 11:42:30 +01:00
parent c52eb38833
commit c82a0faf28
6 changed files with 96 additions and 8 deletions

43
packages/app/src/External/SemisolDev.ts vendored Normal file
View File

@ -0,0 +1,43 @@
export interface RecommendedProfilesResponse {
quality: number;
recommendations: Array<[pubkey: string, score: number]>;
}
export class SemisolDevApiError extends Error {
body: string;
statusCode: number;
constructor(message: string, body: string, status: number) {
super(message);
this.body = body;
this.statusCode = status;
}
}
export default class SemisolDevApi {
readonly #url = "https://api.semisol.dev";
async sugguestedFollows(pubkey: string, follows: Array<string>) {
return await this.#json<RecommendedProfilesResponse>("POST", "/nosgraph/v1/recommend", {
pubkey,
exclude: [],
following: follows,
});
}
async #json<T>(method: string, path: string, body?: unknown) {
const url = `${this.#url}${path}`;
const res = await fetch(url, {
method: method ?? "GET",
body: body ? JSON.stringify(body) : undefined,
headers: {
...(body ? { "content-type": "application/json" } : {}),
},
});
if (res.ok) {
return (await res.json()) as T;
} else {
throw new SemisolDevApiError(`Failed to load content from ${url}`, await res.text(), res.status);
}
}
}