mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-10-23 01:54:10 -05:00
Add CoralApiInterface
This commit is contained in:
@@ -27,11 +27,17 @@ export async function getToken(
|
||||
data: SavedToken;
|
||||
}>
|
||||
export async function getToken(
|
||||
storage: persist.LocalStorage, token: string, proxy_url?: string, ratelimit?: boolean
|
||||
storage: persist.LocalStorage, token: string, proxy_url?: undefined, ratelimit?: boolean
|
||||
): Promise<{
|
||||
nso: CoralApi;
|
||||
data: SavedToken;
|
||||
}>
|
||||
export async function getToken(
|
||||
storage: persist.LocalStorage, token: string, proxy_url?: string, ratelimit?: boolean
|
||||
): Promise<{
|
||||
nso: CoralApi | ZncProxyApi;
|
||||
data: SavedToken;
|
||||
}>
|
||||
export async function getToken(
|
||||
storage: persist.LocalStorage, token: string, proxy_url?: string, ratelimit = SHOULD_LIMIT_USE
|
||||
) {
|
||||
@@ -73,7 +79,9 @@ export async function getToken(
|
||||
expires_at: Date.now() + (data.credential.expiresIn * 1000),
|
||||
};
|
||||
|
||||
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, {existingToken});
|
||||
if (nso instanceof CoralApi) {
|
||||
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, {existingToken});
|
||||
}
|
||||
|
||||
await storage.setItem('NsoToken.' + token, existingToken);
|
||||
await storage.setItem('NintendoAccountToken.' + data.user.id, token);
|
||||
@@ -90,7 +98,9 @@ export async function getToken(
|
||||
new ZncProxyApi(proxy_url, token) :
|
||||
CoralApi.createWithSavedToken(existingToken);
|
||||
|
||||
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, {existingToken});
|
||||
if (nso instanceof CoralApi) {
|
||||
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, {existingToken});
|
||||
}
|
||||
|
||||
return {nso, data: existingToken};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import persist from 'node-persist';
|
||||
import CoralApi from '../api/coral.js';
|
||||
import { CoralApiInterface } from '../api/coral.js';
|
||||
import { ActiveEvent, Announcements, CurrentUser, Friend, Game, Presence, PresenceState, WebServices, CoralErrorResponse, GetActiveEventResult } from '../api/coral-types.js';
|
||||
import ZncProxyApi from '../api/znc-proxy.js';
|
||||
import { ErrorResponse } from '../api/util.js';
|
||||
@@ -26,9 +26,9 @@ export class ZncNotifications extends Loop {
|
||||
constructor(
|
||||
public storage: persist.LocalStorage,
|
||||
public token: string,
|
||||
public nso: CoralApi,
|
||||
public nso: CoralApiInterface,
|
||||
public data: Omit<SavedToken, 'expires_at'>,
|
||||
public user?: CoralUser,
|
||||
public user?: CoralUser<CoralApiInterface>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as crypto from 'node:crypto';
|
||||
import * as persist from 'node-persist';
|
||||
import createDebug from '../util/debug.js';
|
||||
import CoralApi, { Result } from '../api/coral.js';
|
||||
import CoralApi, { CoralApiInterface, Result } from '../api/coral.js';
|
||||
import ZncProxyApi from '../api/znc-proxy.js';
|
||||
import { Announcements, Friends, Friend, GetActiveEventResult, CoralSuccessResponse, WebService, WebServices } from '../api/coral-types.js';
|
||||
import { getToken, SavedToken } from './auth/coral.js';
|
||||
@@ -52,7 +52,8 @@ export default class Users<T extends UserData> {
|
||||
}
|
||||
|
||||
static coral(store: Store | persist.LocalStorage, znc_proxy_url: string, ratelimit?: boolean): Users<CoralUser<ZncProxyApi>>
|
||||
static coral(store: Store | persist.LocalStorage, znc_proxy_url?: string, ratelimit?: boolean): Users<CoralUser>
|
||||
static coral(store: Store | persist.LocalStorage, znc_proxy_url?: undefined, ratelimit?: boolean): Users<CoralUser<CoralApi>>
|
||||
static coral(store: Store | persist.LocalStorage, znc_proxy_url?: string, ratelimit?: boolean): Users<CoralUser<CoralApiInterface>>
|
||||
static coral(_store: Store | persist.LocalStorage, znc_proxy_url?: string, ratelimit?: boolean) {
|
||||
const store = 'storage' in _store ? _store : null;
|
||||
const storage = 'storage' in _store ? _store.storage : _store;
|
||||
@@ -83,7 +84,7 @@ export default class Users<T extends UserData> {
|
||||
}
|
||||
}
|
||||
|
||||
export interface CoralUserData<T extends CoralApi = CoralApi> extends UserData {
|
||||
export interface CoralUserData<T extends CoralApiInterface = CoralApi> extends UserData {
|
||||
nso: T;
|
||||
data: SavedToken;
|
||||
announcements: CoralSuccessResponse<Announcements>;
|
||||
@@ -92,7 +93,7 @@ export interface CoralUserData<T extends CoralApi = CoralApi> extends UserData {
|
||||
active_event: CoralSuccessResponse<GetActiveEventResult>;
|
||||
}
|
||||
|
||||
export class CoralUser<T extends CoralApi = CoralApi> implements CoralUserData<T> {
|
||||
export class CoralUser<T extends CoralApiInterface = CoralApi> implements CoralUserData<T> {
|
||||
created_at = Date.now();
|
||||
expires_at = Infinity;
|
||||
|
||||
@@ -171,6 +172,10 @@ export class CoralUser<T extends CoralApi = CoralApi> implements CoralUserData<T
|
||||
}
|
||||
|
||||
async addFriend(nsa_id: string) {
|
||||
if (!(this.nso instanceof CoralApi)) {
|
||||
throw new Error('Cannot send friend requests using Coral API proxy');
|
||||
}
|
||||
|
||||
if (nsa_id === this.data.nsoAccount.user.nsaId) {
|
||||
throw new Error('Cannot add self as a friend');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user