Client
The ChessNowClient Class
ChessNowClient
You should probably create only one instance. All HTTP methods throw ChessNowError on failure.
You can set a default token or specify a token for each request.
This is useful if you build a client that needs to make requests on behalf of multiple users, while keeping it simple for clients that only need to make requests on behalf of a single user.
Even if you only need to make requests on behalf of a single user at a time, being able to set a token per request could prove useful. For example, if you setup an account switcher.
Usage
import { ChessNowClient } from "@chess-now/api";
const api = new ChessNowClient();
api.setDefaultToken("main-account-token");
const user = await api.getAccountInfo(); // To use the previously set default token
const match = await api.getMatch(42, "another-account-token"); // To use a specific token
// WebSocket
api.connect();
api.subscribe(); // subscribes you to all events you are authorized to receive with the default token
// or
api.subscribe(["challenge", "match"]); // subscribes you to specific events with the default token
// or
api.subscribe("another-account-token"); // subscribes you to all events you are authorized to receive with "another-account-token"
// or
api.subscribe("another-account-token", ["challenge", "match"]); // subscribes you to specific events with "another-account-token"
api.on("challenge:request", ({ payload }) => {
console.log("New challenge from", payload.challenge.from);
});HTTP Methods
Each return type links to its full definition on the Types page.
| Method | Returns |
|---|---|
getAccountInfo(token?) | User |
getMyChallenges(token?) | Challenge[] |
getMyMatches(token?) | Match[] |
getChallenge(id, token?) | Challenge |
requestChallenge(username, options?, token?) | Challenge |
acceptChallenge(id, token?) | { challenge: Challenge, match: Match } |
denyChallenge(id, token?) | { challenge: Challenge } |
getMatch(id, token?) | Match |
makeMove(id, san, token?) | { move: Move, match: Match } |
updateAccountInfo(data, token?) | User |
getUserInfo(username, token?) | User |
getUserMatches(username, token?) | Match[] |
getUserFriends(username, token?) | Friendship[] |
getMyBots(token?) | User[] |
deleteBot(botId, token?) | void |
registerBot(botInfo, token?) | { bot: User, apiKey: ApiKey } |
resetBotToken(botId, token?) | ApiKey |
initDeviceAuth(scopes) | DeviceAuthInitResponse |
getDeviceToken(deviceCode) | DeviceTokenResponse |
getFriends(token?) | Friendship[] |
getFriendRequests(token?) | FriendRequest[] |
sendFriendRequest(username, token?) | FriendRequest |
acceptFriendRequest(id, token?) | Friendship |
denyFriendRequest(id, token?) | FriendRequest |
removeFriend(friendshipId, token?) | Friendship |
WebSocket Methods
| Method | Returns |
|---|---|
connect() | void |
disconnect() | void |
subscribe(tokenOrEvents?, events?) | void |
matchSubscribe(matchId, token?) | void |
watchDeviceAuth(userCode, deviceCode) | void |
on(event, handler) | void |
off(event, handler) | void |
WebSocket Events
Each payload links to its full definition on the WebSocket Events page.
Events starting with response: are sent so you can verify the success or failure of a websocket request, as they don't have the usual flow of a HTTP request.
| Event | Payload |
|---|---|
challenge:request | ChallengeRequestPayload |
challenge:accepted | ChallengeAcceptedPayload |
challenge:denied | ChallengeDeniedPayload |
match:board_move | MatchMovePayload |
match:game_over | MatchGameOverPayload |
device_auth | DeviceAuthPayload |
friend:request | FriendRequestPayload |
friend:accepted | FriendAcceptedPayload |
friend:denied | FriendDeniedPayload |
friend:removed | FriendRemovedPayload |
response:subscribe | SubscribeResponsePayload |
response:watch_device_auth | WatchDeviceAuthResponsePayload |
response:match_subscribe | MatchSubscribeResponsePayload |
Full API
Prop
Type