scidapi-client
Version:
Supercell ID API client
70 lines (69 loc) • 2.69 kB
JavaScript
import { generateRfp, makeUuid } from "./utils.js";
/**
* Client for interacting with the Supercell ID (SCID) API.
*/
class SCIDClient {
constructor(scidToken, rfpKeyHex) {
this.scidToken = scidToken;
this.rfpKeyHex = rfpKeyHex;
this.DEFAULT_USER_AGENT = "scid/ltb.client:1.0";
this.DEFAULT_HEADERS = {
"User-Agent": this.DEFAULT_USER_AGENT,
"Accept-Language": "en",
"Accept-Encoding": "gzip",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
};
}
async scidRequest(endpoint, body) {
const path = `/api/social/v3/${endpoint}`;
const uuid = makeUuid();
const headers = {
Authorization: `Bearer ${this.scidToken}`,
"User-Agent": this.DEFAULT_USER_AGENT,
"X-Supercell-Device-Id": uuid,
};
const rfp = generateRfp(path, "POST", body.toString(), this.rfpKeyHex, headers);
const res = await fetch(`https://id.supercell.com${path}`, {
method: "POST",
headers: {
...this.DEFAULT_HEADERS,
...headers,
"X-Supercell-Request-Forgery-Protection": rfp,
"Content-Length": body.toString().length.toString(),
},
body,
});
if (!res.ok) {
throw new Error(`SCID request failed: ${res.status} ${res.statusText}`);
}
return res.json();
}
async getProfile(input, type) {
const body = new URLSearchParams(input ? { [type]: input } : { fetchPresence: "true" });
return this.scidRequest("profile.get", body);
}
async getProfileList(list, type) {
const body = new URLSearchParams({
[type]: `[${list.map((id) => `"${id}"`).join(",")}]`,
format: "BASIC",
});
return this.scidRequest("profiles.list", body);
}
async getFriends(scid) {
const body = new URLSearchParams(scid ? { scid } : { fetchPresence: "true" });
return this.scidRequest("friends.list", body);
}
async sendFriendRequest(list) {
const body = new URLSearchParams({ scids: `[${list.map((id) => `"${id}"`).join(",")}]` });
return this.scidRequest("friends.createRequest", body);
}
async acceptFriendRequest(list) {
const body = new URLSearchParams({ scids: `[${list.map((id) => `"${id}"`).join(",")}]` });
return this.scidRequest("friends.acceptRequest", body);
}
async removeFriends(scid) {
const body = new URLSearchParams({ scid });
return this.scidRequest("friends.remove", body);
}
}
export default SCIDClient;