@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
245 lines • 7.19 kB
JavaScript
import * as Utils from '@datocms/rest-client-utils';
import BaseResource from '../../BaseResource';
export default class Field extends BaseResource {
/**
* Create a new field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/create
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
create(itemTypeId, body) {
return this.rawCreate(Utils.toId(itemTypeId), Utils.serializeRequestBody(body, {
type: 'field',
attributes: [
'label',
'field_type',
'api_key',
'localized',
'validators',
'appeareance',
'appearance',
'position',
'hint',
'default_value',
'deep_filtering_enabled',
],
relationships: ['fieldset'],
})).then((body) => Utils.deserializeResponseBody(body));
}
/**
* Create a new field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/create
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawCreate(itemTypeId, body) {
return this.client.request({
method: 'POST',
url: `/item-types/${itemTypeId}/fields`,
body,
});
}
/**
* Update a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/update
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
update(fieldId, body) {
return this.rawUpdate(Utils.toId(fieldId), Utils.serializeRequestBody(body, {
id: Utils.toId(fieldId),
type: 'field',
attributes: [
'default_value',
'label',
'api_key',
'localized',
'validators',
'appeareance',
'appearance',
'position',
'field_type',
'hint',
'deep_filtering_enabled',
],
relationships: ['fieldset'],
})).then((body) => Utils.deserializeResponseBody(body));
}
/**
* Update a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/update
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawUpdate(fieldId, body) {
return this.client.request({
method: 'PUT',
url: `/fields/${fieldId}`,
body,
});
}
/**
* List all fields of a model/block
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/instances
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
list(itemTypeId) {
return this.rawList(Utils.toId(itemTypeId)).then((body) => Utils.deserializeResponseBody(body));
}
/**
* List all fields of a model/block
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/instances
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawList(itemTypeId) {
return this.client.request({
method: 'GET',
url: `/item-types/${itemTypeId}/fields`,
});
}
/**
* List fields referencing a model/block
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/referencing
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
referencing(itemTypeId) {
return this.rawReferencing(Utils.toId(itemTypeId)).then((body) => Utils.deserializeResponseBody(body));
}
/**
* List fields referencing a model/block
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/referencing
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawReferencing(itemTypeId) {
return this.client.request({
method: 'GET',
url: `/item-types/${itemTypeId}/fields/referencing`,
});
}
/**
* List related fields
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/related
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
related(itemTypeId) {
return this.rawRelated(Utils.toId(itemTypeId)).then((body) => Utils.deserializeResponseBody(body));
}
/**
* List related fields
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/related
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
rawRelated(itemTypeId) {
return this.client.request({
method: 'GET',
url: `/item-types/${itemTypeId}/fields/related`,
});
}
/**
* Retrieve a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/self
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
find(fieldId) {
return this.rawFind(Utils.toId(fieldId)).then((body) => Utils.deserializeResponseBody(body));
}
/**
* Retrieve a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/self
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawFind(fieldId) {
return this.client.request({
method: 'GET',
url: `/fields/${fieldId}`,
});
}
/**
* Delete a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
destroy(fieldId) {
return this.rawDestroy(Utils.toId(fieldId)).then((body) => Utils.deserializeResponseBody(body));
}
/**
* Delete a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawDestroy(fieldId) {
return this.client.request({
method: 'DELETE',
url: `/fields/${fieldId}`,
});
}
/**
* Duplicate a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/duplicate
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
duplicate(fieldId) {
return this.rawDuplicate(Utils.toId(fieldId)).then((body) => Utils.deserializeResponseBody(body));
}
/**
* Duplicate a field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/field/duplicate
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawDuplicate(fieldId) {
return this.client.request({
method: 'POST',
url: `/fields/${fieldId}/duplicate`,
});
}
}
Field.TYPE = 'field';
//# sourceMappingURL=Field.js.map