contentful-management
Version:
Client for Contentful's Content Management API
62 lines (59 loc) • 2.17 kB
JavaScript
import copy from 'fast-copy';
import { post, del as del$1, get as get$1, put } from './raw.js';
import { normalizeSelect } from './utils.js';
const getSpaceEnvBaseUrl = (params) => `/spaces/${params.spaceId}/environments/${params.environmentId}`;
function getParentPlural(parentEntityType) {
switch (parentEntityType) {
case 'Entry':
return 'entries';
case 'Experience':
return 'experiences';
case 'Fragment':
return 'fragments';
case 'Template':
return 'templates';
case 'ComponentType':
return 'component_types';
}
}
const normalizeTaskParentParams = (paramsOrg) => 'entryId' in paramsOrg
? {
spaceId: paramsOrg.spaceId,
environmentId: paramsOrg.environmentId,
parentEntityType: 'Entry',
parentEntityId: paramsOrg.entryId,
}
: paramsOrg;
const getBaseUrl = (paramsOrg) => {
const params = normalizeTaskParentParams(paramsOrg);
const parentPlural = getParentPlural(params.parentEntityType);
return `${getSpaceEnvBaseUrl(params)}/${parentPlural}/${params.parentEntityId}/tasks`;
};
const getTaskUrl = (params) => `${getBaseUrl(params)}/${params.taskId}`;
const get = (http, params) => get$1(http, getTaskUrl(params));
const getMany = (http, params) => get$1(http, getBaseUrl(params), {
params: normalizeSelect(params.query),
});
/**
* @deprecated use `getMany` instead. `getAll` may never be removed for app compatibility reasons.
*/
const getAll = getMany;
const create = (http, params, rawData) => {
const data = copy(rawData);
return post(http, getBaseUrl(params), data);
};
const update = (http, params, rawData, headers) => {
const data = copy(rawData);
delete data.sys;
return put(http, getTaskUrl(params), data, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
...headers,
},
});
};
const del = (http, { version, ...params }) => {
return del$1(http, getTaskUrl(params), { headers: { 'X-Contentful-Version': version } });
};
export { create, del, get, getAll, getMany, update };
//# sourceMappingURL=task.js.map