import { Page, PublishedPage, CreatePagePayload, CreatePageFromCopyPayload, Version, MovePagePayload, RenamePagePayload, RecurringSchedule, VersionedPage, Schema, Folder, } from '../types/api'; import { handleDelete, handleFetch, serverPath, stringifyQuery } from './helpers'; import BaseApi from './base-api'; class PageApi extends BaseApi { public async getAllPages(): Promise { const response = await handleFetch(`/pages/all/${this.language}`); return await response.json(); } public async getPublishedPages(): Promise { const response = await handleFetch(`/pages/published/${this.language}`); return await response.json(); } public async getFolders(): Promise { const response = await handleFetch('/pages/folders'); return await response.json(); } public async getSchema(): Promise { const response = await handleFetch('/versions/schema'); return await response.json(); } public async getPageById(id: string): Promise { const response = await handleFetch(`/pages/${id}`); const page = await response.json(); page.versions = await this.getPageVersionsById(id); return page; } public async getPageVersionsById(id: string): Promise { const response = await handleFetch(`/pages/${id}/versions`); return await response.json(); } public async create({ version, name, parentId }: CreatePagePayload): Promise { const path = stringifyQuery('/versions/create', { name, parentId }); const response = await handleFetch(path, { method: 'post', body: JSON.stringify(version), }); return await response.json(); } public async createFromCopy(payload: CreatePageFromCopyPayload): Promise { const path = stringifyQuery('/versions/create-from-copy', payload); const response = await handleFetch(path, { method: 'post', }); return await response.json(); } public async move({ page, newParentId }: MovePagePayload): Promise { const path = stringifyQuery(`/pages/${page.id}/move`, { newParentId }); const response = await handleFetch(path, { method: 'post', }); return await response.json(); } public async rename({ page, name }: RenamePagePayload): Promise { const path = stringifyQuery(`/pages/${page.id}/rename`, { name }); const response = await handleFetch(path, { method: 'post', }); return await response.json(); } public async delete(page: Page): Promise { return await handleDelete(`/pages/${page.id}`); } public async updateVersion(version: Version): Promise { const response = await handleFetch(`/versions/update/${version.id}`, { method: 'put', body: JSON.stringify(version), }); return await response.json(); } public async publishVersion(version: Version): Promise { const response = await handleFetch(`/versions/publish/${version.id}`, { method: 'post', }); return await response.json(); } public async depublishVersion(version: Version): Promise { const response = await handleFetch(`/versions/depublish/${version.id}`, { method: 'post', }); return await response.json(); } public async copyVersion(version: Version): Promise { const response = await handleFetch(`/versions/copy/${version.id}`, { method: 'post', }); return await response.json(); } public async deleteVersion(version: Version): Promise { return await handleDelete(`/versions/${version.id}`); } public previewUrl(version: Version, versionCounter: number): string | null { return version.id ? `${serverPath}/versions/preview/${version.id}?counter=${versionCounter}` : null; } public async scheduledVersionIds(pageId: string): Promise { const response = await handleFetch(`/pages/schedules/${pageId}`); return await response.json(); } public async getRecurrentSchedulesForId(id: string): Promise { const response = await handleFetch(`/versions/schedules/${id}`); return await response.json(); } public async scheduleDaily(schedule: RecurringSchedule): Promise { const response = await handleFetch(`/versions/schedule`, { method: 'post', body: JSON.stringify(schedule), }); return await response.json(); } public deleteSchedule(id: string): Promise { return handleFetch(`/versions/schedules/delete/${id}`); } } export default new PageApi();