import { Page, PublishedPage, CreatePagePayload, CreatePageFromCopyPayload, Version, MovePagePayload, RenamePagePayload, RecurringSchedule, VersionedPage, Schema, Folder, PageReferenceInfo, CreateTranslatedPagePayload } from '../types/api'; import { handleDelete, handleFetch, getServerPath, stringifyQuery } from './helpers'; import PageTree from '../tools/page-tree'; import { CrudSearchReponse, ITEMS_PER_PAGE } from './crud-api'; export default class PageApi { public static async getPages(clickedPageIds: string[]): Promise { const path = stringifyQuery('/pages/load', { clickedPageIds }); const response = await handleFetch(path); return await response.json(); } public static async getPublishedPages(): Promise { const response = await handleFetch('/pages/published'); return await response.json(); } public static async getFolders(): Promise { const response = await handleFetch('/pages/folders'); return await response.json(); } public static async getSchema(): Promise { const response = await handleFetch('/versions/schema'); return await response.json(); } public static async getPageSchema(): Promise { const response = await handleFetch('/pages/schema'); return await response.json(); } public static async getPageById(id: string): Promise { const response = await handleFetch(`/pages/${id}`); const page = await response.json(); page.versions = await PageApi.getPageVersionsById(id); return page; } public static async isLeafFolder(id: string): Promise { const response = await handleFetch(`/pages/${id}/is-leaf-folder`); return await response.json(); } public static async getPageVersionsById(id: string): Promise { const response = await handleFetch(`/pages/${id}/versions`); return await response.json(); } public static async create({ version, name, parentId, language }: CreatePagePayload): Promise { const path = stringifyQuery('/versions/create', { name, parentId, language }); const response = await handleFetch(path, { method: 'post', body: JSON.stringify(version) }); return await response.json(); } public static async createFromCopy(payload: CreatePageFromCopyPayload | CreateTranslatedPagePayload): Promise { const path = stringifyQuery('/versions/create-from-copy', payload); const response = await handleFetch(path, { method: 'post' }); return await response.json(); } public static async move({ page, newParentId, createRedirect }: MovePagePayload): Promise { const path = stringifyQuery(`/pages/${page.id}/move`, { newParentId, createRedirect }); const response = await handleFetch(path, { method: 'post' }); return await response.json(); } public static async rename({ page, name, createRedirect }: RenamePagePayload): Promise { const path = stringifyQuery(`/pages/${page.id}/rename`, { name, createRedirect }); const response = await handleFetch(path, { method: 'post' }); return await response.json(); } public static async delete(page: Page): Promise { return await handleDelete(`/pages/${page.id}`); } public static async updateVersion(version: Version): Promise { const response = await handleFetch(`/versions/update/${version.id}`, { method: 'put', body: JSON.stringify(version) }); return await response.json(); } public static async publishVersion(version: Version): Promise { const response = await handleFetch(`/versions/publish/${version.id}`, { method: 'post' }); return await response.json(); } public static async depublishVersion(version: Version): Promise { const response = await handleFetch(`/versions/depublish/${version.id}`, { method: 'post' }); return await response.json(); } public static async copyVersion(versionId: string): Promise { const response = await handleFetch(`/versions/copy/${versionId}`, { method: 'post' }); return await response.json(); } public static async deleteVersion(version: Version): Promise { return await handleDelete(`/versions/${version.id}`); } public static previewUrl(version: Version, versionCounter: number): string | null { return version.id ? `${getServerPath()}/versions/preview/${version.id}?counter=${versionCounter}` : null; } public static async scheduledVersionIds(pageId: string): Promise { const response = await handleFetch(`/pages/schedules/${pageId}`); return await response.json(); } public static async getRecurrentSchedulesForId(id: string): Promise { const response = await handleFetch(`/versions/schedules/${id}`); return await response.json(); } public static async scheduleDaily(schedule: RecurringSchedule): Promise { const response = await handleFetch(`/versions/schedule`, { method: 'post', body: JSON.stringify(schedule) }); return await response.json(); } public static deleteSchedule(id: string): Promise { return handleFetch(`/versions/schedules/delete/${id}`); } public static search(query: string, allowedTypes: string[]) { const path = stringifyQuery(`/pages/search`, { query, allowedTypes }); return handleFetch(path) .then((res) => res.json()) .then((pages) => new PageTree(pages as Page[]).items); } public static async searchCrud(query?: string, language?: string, page?: number, sort?: string): Promise { const queryParams = { query: query || '', page: page || 1, itemsPerPage: ITEMS_PER_PAGE, language: language || '', sort: sort || '' }; const path = stringifyQuery(`/pages/crud/search`, queryParams); const response = await handleFetch(path); return await response.json(); } public static async setLanguage(page: Page, language: string) { const path = stringifyQuery(`/pages/${page.id}/set-language`, { language }); const response = await handleFetch(path, { method: 'post' }); return await response.json(); } public static async getReferenceInfoForVersion(versionId: string): Promise { const response = await handleFetch(`/versions/reference-info/${versionId}`); return await response.json(); } }