cordis/client/webapp/fetchable-ensured.ts
2021-11-20 19:06:50 -06:00

39 lines
1.6 KiB
TypeScript

import { GuildMetadata, Member, Channel, Message, Resource, Token } from "./data-types";
import { Fetchable, GuaranteedFetchable } from "./guild-types";
export default class EnsuredFetchable implements GuaranteedFetchable {
constructor(
private fetchable: Fetchable
) {}
private static guarantee<T>(value: T | null): T {
if (!value) throw new Error('got null value');
return value;
}
async fetchMetadata(): Promise<GuildMetadata> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchMetadata());
}
async fetchMembers(): Promise<Member[]> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchMembers());
}
async fetchChannels(): Promise<Channel[]> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchChannels());
}
async fetchMessagesRecent(channelId: string, number: number): Promise<Message[]> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchMessagesRecent(channelId, number));
}
async fetchMessagesBefore(channelId: string, messageId: string, number: number): Promise<Message[]> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchMessagesBefore(channelId, messageId, number));
}
async fetchMessagesAfter(channelId: string, messageId: string, number: number): Promise<Message[]> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchMessagesAfter(channelId, messageId, number));
}
async fetchResource(resourceId: string): Promise<Resource> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchResource(resourceId));
}
async fetchTokens(): Promise<Token[]> {
return EnsuredFetchable.guarantee(await this.fetchable.fetchTokens());
}
}