import type { BookStackAccount } from './bookstack.classes.account.js'; import type { IBookStackShelf, IBookStackBook, IBookStackTag, IBookStackListParams, IBookStackListResponse, } from './bookstack.interfaces.js'; import { BookStackBook } from './bookstack.classes.book.js'; import { autoPaginate } from './bookstack.helpers.js'; export class BookStackShelf { public readonly id: number; public readonly name: string; public readonly slug: string; public readonly description: string; public readonly createdAt: string; public readonly updatedAt: string; public readonly createdBy: number; public readonly updatedBy: number; public readonly ownedBy: number; public readonly tags: IBookStackTag[]; /** @internal */ constructor( private accountRef: BookStackAccount, raw: IBookStackShelf, ) { this.id = raw.id; this.name = raw.name || ''; this.slug = raw.slug || ''; this.description = raw.description || ''; this.createdAt = raw.created_at || ''; this.updatedAt = raw.updated_at || ''; this.createdBy = raw.created_by; this.updatedBy = raw.updated_by; this.ownedBy = raw.owned_by; this.tags = raw.tags || []; } // --------------------------------------------------------------------------- // CRUD // --------------------------------------------------------------------------- async update(data: { name?: string; description?: string; description_html?: string; books?: number[]; tags?: IBookStackTag[]; }): Promise { const raw = await this.accountRef.request('PUT', `/shelves/${this.id}`, data); return new BookStackShelf(this.accountRef, raw); } async delete(): Promise { await this.accountRef.request('DELETE', `/shelves/${this.id}`); } // --------------------------------------------------------------------------- // Navigation — Books // --------------------------------------------------------------------------- async getBooks(opts?: IBookStackListParams): Promise { // The shelf detail endpoint includes books inline const detail = await this.accountRef.request('GET', `/shelves/${this.id}`); if (detail.books) { return detail.books.map((b) => new BookStackBook(this.accountRef, b)); } return []; } // --------------------------------------------------------------------------- // Serialization // --------------------------------------------------------------------------- toJSON(): IBookStackShelf { return { id: this.id, name: this.name, slug: this.slug, description: this.description, created_at: this.createdAt, updated_at: this.updatedAt, created_by: this.createdBy, updated_by: this.updatedBy, owned_by: this.ownedBy, tags: this.tags, }; } }