@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
120 lines (118 loc) • 4.2 kB
JavaScript
/*
* 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';
export function fetchBlueprints() {
return get('/studio/api/2/sites/available_blueprints').pipe(pluck('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) => {
var _a;
return {
id: site.siteId,
uuid: site.uuid,
name: (_a = site.name) !== null && _a !== void 0 ? _a : site.siteId,
description: site.desc,
imageUrl: `/.crafter/screenshots/default.png?crafterSite=${site.siteId}`
};
}),
{
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(
pluck('response'),
map(() => {
var _a;
return {
id: site.siteId,
name: site.siteName,
description: (_a = site.description) !== null && _a !== void 0 ? _a : '',
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(
pluck('response'),
map(() => true)
);
}
export function update(site) {
return postJSON(`/studio/api/2/sites/${site.id}`, {
name: site.name,
description: site.description
}).pipe(pluck('response'));
}
export function exists(siteId) {
return get(`/studio/api/1/services/api/1/site/exists.json?site=${siteId}`).pipe(pluck('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(pluck('response'));
}
export function hasInitialPublish(siteId) {
return get(`/studio/api/2/publish/available_targets?siteId=${siteId}`).pipe(pluck('response', 'published'));
}