import type { FileStore, StoredFile, UploadedFile } from './FileStore'; import { apiRequest } from './http'; /** The API's three file collections, one route each; textures, fonts and sounds differ in nothing else. */ export type FileRoute = 'textures' | 'fonts' | 'sounds'; export class HttpFileStore< U extends UploadedFile = UploadedFile, S extends StoredFile = StoredFile, > implements FileStore { constructor( private readonly endpoint: string, private readonly route: FileRoute, ) {} async upload(bytes: Blob | ArrayBuffer, type: string, name = ''): Promise { return apiRequest(this.endpoint, 'POST', this.route, { headers: { 'Content-Type': type }, body: bytes, query: { name }, }); } async list(): Promise { return apiRequest(this.endpoint, 'GET', this.route, {}); } async delete(file: string): Promise { await apiRequest(this.endpoint, 'DELETE', `${this.route}/${encodeURIComponent(file)}`, {}); } }