2024-03-22 13:34:31 -04:00

97 lines
2.3 KiB
TypeScript

import type { BaseClient } from '../../../client/base';
import type { UsingClient } from '../../../commands';
import { fakePromise, type GatewayIntentBits } from '../../../common';
import type { Cache, ReturnCache } from '../../index';
export class BaseResource<T = any> {
client!: BaseClient;
namespace = 'base';
constructor(
protected cache: Cache,
client?: UsingClient,
) {
if (client) {
this.client = client;
}
}
/** @internal */
__setClient(client: UsingClient) {
this.client = client;
}
get rest() {
return this.client!.rest;
}
get adapter() {
return this.cache.adapter;
}
removeIfNI(intent: keyof typeof GatewayIntentBits, id: string) {
if (!this.cache.hasIntent(intent)) {
return this.remove(id);
}
return;
}
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, data: any) {
if (!this.cache.hasIntent(intent)) {
return fakePromise(this.set(id, data)).then(() => data);
}
}
get(id: string): ReturnCache<T | undefined> {
return this.adapter.get(this.hashId(id));
}
bulk(ids: string[]): ReturnCache<T[]> {
return fakePromise(this.adapter.get(ids.map(id => this.hashId(id)))).then(x => x.filter(y => y));
}
set(id: string, data: any) {
return fakePromise(this.addToRelationship(id)).then(() => this.adapter.set(this.hashId(id), data));
}
patch<T extends Record<any, any> = Record<any, any>>(id: string, data: T) {
return fakePromise(this.addToRelationship(id)).then(() => this.adapter.patch(false, this.hashId(id), data));
}
remove(id: string) {
return fakePromise(this.removeToRelationship(id)).then(() => this.adapter.remove(this.hashId(id)));
}
keys(): ReturnCache<string[]> {
return this.adapter.keys(this.namespace) as string[];
}
values(): ReturnCache<T[]> {
return this.adapter.values(this.namespace) as T[];
}
count() {
return this.adapter.count(this.namespace);
}
contains(id: string) {
return this.adapter.contains(this.namespace, id);
}
getToRelationship() {
return this.adapter.getToRelationship(this.namespace);
}
addToRelationship(id: string | string[]) {
return this.adapter.addToRelationship(this.namespace, id);
}
removeToRelationship(id: string | string[]) {
return this.adapter.removeToRelationship(this.namespace, id);
}
hashId(id: string) {
return `${this.namespace}.${id}`;
}
}