initial
This commit is contained in:
135
ts/bookstack.classes.page.ts
Normal file
135
ts/bookstack.classes.page.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import type { BookStackAccount } from './bookstack.classes.account.js';
|
||||
import type {
|
||||
IBookStackPage,
|
||||
IBookStackTag,
|
||||
IBookStackComment,
|
||||
TBookStackExportFormat,
|
||||
} from './bookstack.interfaces.js';
|
||||
|
||||
export class BookStackPage {
|
||||
public readonly id: number;
|
||||
public readonly bookId: number;
|
||||
public readonly chapterId: number;
|
||||
public readonly name: string;
|
||||
public readonly slug: string;
|
||||
public readonly html: string;
|
||||
public readonly markdown: string;
|
||||
public readonly priority: number;
|
||||
public readonly draft: boolean;
|
||||
public readonly template: boolean;
|
||||
public readonly revisionCount: number;
|
||||
public readonly editor: 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: IBookStackPage,
|
||||
) {
|
||||
this.id = raw.id;
|
||||
this.bookId = raw.book_id;
|
||||
this.chapterId = raw.chapter_id;
|
||||
this.name = raw.name || '';
|
||||
this.slug = raw.slug || '';
|
||||
this.html = raw.html || '';
|
||||
this.markdown = raw.markdown || '';
|
||||
this.priority = raw.priority || 0;
|
||||
this.draft = raw.draft || false;
|
||||
this.template = raw.template || false;
|
||||
this.revisionCount = raw.revision_count || 0;
|
||||
this.editor = raw.editor || '';
|
||||
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;
|
||||
html?: string;
|
||||
markdown?: string;
|
||||
book_id?: number;
|
||||
chapter_id?: number;
|
||||
tags?: IBookStackTag[];
|
||||
priority?: number;
|
||||
}): Promise<BookStackPage> {
|
||||
const raw = await this.accountRef.request<IBookStackPage>('PUT', `/pages/${this.id}`, data);
|
||||
return new BookStackPage(this.accountRef, raw);
|
||||
}
|
||||
|
||||
async delete(): Promise<void> {
|
||||
await this.accountRef.request('DELETE', `/pages/${this.id}`);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Export
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async export(format: TBookStackExportFormat): Promise<string | Uint8Array> {
|
||||
if (format === 'pdf') {
|
||||
return this.accountRef.requestBinary(`/pages/${this.id}/export/${format}`);
|
||||
}
|
||||
return this.accountRef.requestText('GET', `/pages/${this.id}/export/${format}`);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Comments
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async getComments(): Promise<IBookStackComment[]> {
|
||||
const result = await this.accountRef.request<{ data: IBookStackComment[] }>(
|
||||
'GET',
|
||||
`/comments?filter[page_id]=${this.id}`,
|
||||
);
|
||||
return result.data;
|
||||
}
|
||||
|
||||
async addComment(data: {
|
||||
html: string;
|
||||
reply_to?: number;
|
||||
content_ref?: string;
|
||||
}): Promise<IBookStackComment> {
|
||||
return this.accountRef.request<IBookStackComment>('POST', '/comments', {
|
||||
page_id: this.id,
|
||||
...data,
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Serialization
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
toJSON(): IBookStackPage {
|
||||
return {
|
||||
id: this.id,
|
||||
book_id: this.bookId,
|
||||
chapter_id: this.chapterId,
|
||||
name: this.name,
|
||||
slug: this.slug,
|
||||
html: this.html,
|
||||
markdown: this.markdown,
|
||||
priority: this.priority,
|
||||
draft: this.draft,
|
||||
template: this.template,
|
||||
revision_count: this.revisionCount,
|
||||
editor: this.editor,
|
||||
created_at: this.createdAt,
|
||||
updated_at: this.updatedAt,
|
||||
created_by: this.createdBy,
|
||||
updated_by: this.updatedBy,
|
||||
owned_by: this.ownedBy,
|
||||
tags: this.tags,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user