UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

128 lines (126 loc) 4.46 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import { get, postJSON } from '../utils/ajax'; import { map, pluck } from 'rxjs/operators'; import { underscore } from '../utils/string'; import { reversePluckProps } from '../utils/object'; export function fetchBlueprints() { return get('/studio/api/2/sites/available_blueprints').pipe(map((response) => response?.response?.blueprints)); } export function fetchAll(paginationOptions) { const options = Object.assign( { limit: 100, offset: 0 }, paginationOptions || {} ); return get(`/studio/api/2/users/me/sites?limit=${options.limit}&offset=${options.offset}`).pipe( map(({ response }) => Object.assign( response.sites.map((site) => ({ id: site.siteId, uuid: site.uuid, name: site.name ?? site.siteId, description: site.desc, state: site.state })), { limit: response.limit, offset: response.offset, total: response.total } ) ) ); } export function create(site) { let api1Params = {}; Object.entries(site).forEach(([key, value]) => { if (key === 'siteName') { api1Params.name = value; } else { api1Params[underscore(key)] = value; } }); return postJSON('/studio/api/1/services/api/1/site/create.json', api1Params).pipe( map(() => ({ id: site.siteId, name: site.siteName, description: site.description ?? '', uuid: null, imageUrl: `/.crafter/screenshots/default.png?crafterSite=${site.siteId}` })) ); } export function duplicate(site) { return postJSON(`/studio/api/2/sites/${site.sourceSiteId}/duplicate`, reversePluckProps(site, 'sourceSiteId')).pipe( map(() => ({ id: site.siteId, name: site.siteName, description: site.description ?? '', uuid: null, imageUrl: `/.crafter/screenshots/default.png?crafterSite=${site.siteId}` })) ); } export function trash(id) { return postJSON('/studio/api/1/services/api/1/site/delete-site.json', { siteId: id }).pipe(map(() => true)); } export function update(site) { return postJSON(`/studio/api/2/sites/${site.id}`, { name: site.name, description: site.description }).pipe(map((response) => response?.response)); } export function exists(siteId) { return get(`/studio/api/1/services/api/1/site/exists.json?site=${siteId}`).pipe( map((response) => response?.response?.exists) ); } export function validateActionPolicy(site, action) { const multi = Array.isArray(action); const actions = multi ? action : [action]; const toPluck = ['response', 'results', !multi && '0'].filter(Boolean); return postJSON(`/studio/api/2/sites/${site}/policy/validate`, { actions }).pipe(pluck(...toPluck)); } export function fetchLegacySite(siteId) { return get(`/studio/api/1/services/api/1/site/get.json?site_id=${siteId}`).pipe( map((response) => response?.response) ); } export function hasInitialPublish(siteId) { return get(`/studio/api/2/publish/available_targets?siteId=${siteId}`).pipe( map((response) => response?.response?.published) ); }