@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
785 lines • 28.7 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var Utils = __importStar(require("@datocms/rest-client-utils"));
var BaseResource_1 = __importDefault(require("../../BaseResource"));
var Item = /** @class */ (function (_super) {
__extends(Item, _super);
function Item() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* List all records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/instances
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.list = function (queryParams) {
return this.rawList(queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* List all records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/instances
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawList = function (queryParams) {
return this.client.request({
method: 'GET',
url: '/items',
queryParams: queryParams,
});
};
/**
* Async iterator to auto-paginate over elements returned by list()
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/instances
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.listPagedIterator = function (queryParams, iteratorOptions) {
return __asyncGenerator(this, arguments, function listPagedIterator_1() {
var _a, _b, _c, element, e_1_1;
var _d, e_1, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
_g.trys.push([0, 9, 10, 15]);
_a = true, _b = __asyncValues(this.rawListPagedIterator(queryParams, iteratorOptions));
_g.label = 1;
case 1: return [4 /*yield*/, __await(_b.next())];
case 2:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 8];
_f = _c.value;
_a = false;
_g.label = 3;
case 3:
_g.trys.push([3, , 6, 7]);
element = _f;
return [4 /*yield*/, __await(Utils.deserializeJsonEntity(element))];
case 4: return [4 /*yield*/, _g.sent()];
case 5:
_g.sent();
return [3 /*break*/, 7];
case 6:
_a = true;
return [7 /*endfinally*/];
case 7: return [3 /*break*/, 1];
case 8: return [3 /*break*/, 15];
case 9:
e_1_1 = _g.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 15];
case 10:
_g.trys.push([10, , 13, 14]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 12];
return [4 /*yield*/, __await(_e.call(_b))];
case 11:
_g.sent();
_g.label = 12;
case 12: return [3 /*break*/, 14];
case 13:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 14: return [7 /*endfinally*/];
case 15: return [2 /*return*/];
}
});
});
};
/**
* Async iterator to auto-paginate over elements returned by rawList()
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/instances
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawListPagedIterator = function (queryParams, iteratorOptions) {
var _this = this;
Utils.warnOnPageQueryParam(queryParams);
return Utils.rawPageIterator({
defaultLimit: 30,
maxLimit: 500,
}, function (page) {
return _this.rawList(__assign(__assign({}, queryParams), { page: page }));
}, iteratorOptions);
};
/**
* Validates an existing record field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/validate_existing
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.validateExisting = function (itemId, body) {
return this.rawValidateExisting(Utils.toId(itemId), Utils.serializeRequestBody(body, {
id: Utils.toId(itemId),
type: 'item',
attributes: '*',
relationships: ['item_type', 'creator'],
}));
};
/**
* Validates an existing record field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/validate_existing
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.rawValidateExisting = function (itemId, body) {
return this.client.request({
method: 'POST',
url: "/items/".concat(itemId, "/validate"),
body: body,
});
};
/**
* Validates a record field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/validate_new
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.validateNew = function (body) {
return this.rawValidateNew(Utils.serializeRequestBody(body, {
type: 'item',
attributes: '*',
relationships: ['item_type', 'creator'],
}));
};
/**
* Validates a record field
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/validate_new
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.rawValidateNew = function (body) {
return this.client.request({
method: 'POST',
url: '/items/validate',
body: body,
});
};
/**
* Create a new record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/create
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.create = function (body) {
return this.rawCreate(Utils.serializeRequestBody(body, {
type: 'item',
attributes: '*',
relationships: ['item_type', 'creator'],
})).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Create a new record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/create
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawCreate = function (body) {
return this.client.request({
method: 'POST',
url: '/items',
body: body,
});
};
/**
* Duplicate a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/duplicate
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.duplicate = function (itemId) {
return this.rawDuplicate(Utils.toId(itemId)).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Duplicate a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/duplicate
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawDuplicate = function (itemId) {
return this.client.request({
method: 'POST',
url: "/items/".concat(itemId, "/duplicate"),
});
};
/**
* Update a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/update
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.update = function (itemId, body) {
return this.rawUpdate(Utils.toId(itemId), Utils.serializeRequestBody(body, {
id: Utils.toId(itemId),
type: 'item',
attributes: '*',
relationships: ['item_type', 'creator'],
})).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Update a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/update
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawUpdate = function (itemId, body) {
return this.client.request({
method: 'PUT',
url: "/items/".concat(itemId),
body: body,
});
};
/**
* Referenced records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/references
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.references = function (itemId, queryParams) {
return this.rawReferences(Utils.toId(itemId), queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Referenced records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/references
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawReferences = function (itemId, queryParams) {
return this.client.request({
method: 'GET',
url: "/items/".concat(itemId, "/references"),
queryParams: queryParams,
});
};
/**
* Retrieve a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/self
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.find = function (itemId, queryParams) {
return this.rawFind(Utils.toId(itemId), queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Retrieve a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/self
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawFind = function (itemId, queryParams) {
return this.client.request({
method: 'GET',
url: "/items/".concat(itemId),
queryParams: queryParams,
});
};
/**
* Retrieve information regarding changes between current and published versions of the record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/current_vs_published_state
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.currentVsPublishedState = function (itemId) {
return this.rawCurrentVsPublishedState(Utils.toId(itemId)).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Retrieve information regarding changes between current and published versions of the record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/current_vs_published_state
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.rawCurrentVsPublishedState = function (itemId) {
return this.client.request({
method: 'GET',
url: "/items/".concat(itemId, "/current-vs-published-state"),
});
};
/**
* Delete a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.destroy = function (itemId) {
return this.rawDestroy(Utils.toId(itemId)).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Delete a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawDestroy = function (itemId) {
return this.client.request({
method: 'DELETE',
url: "/items/".concat(itemId),
});
};
/**
* Delete multiple records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/batch_destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.batchDestroy = function (queryParams) {
return this.rawBatchDestroy(queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Delete multiple records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/batch_destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.rawBatchDestroy = function (queryParams) {
return this.client.request({
method: 'DELETE',
url: '/items',
queryParams: queryParams,
});
};
/**
* Publish multiple records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/batch_publish
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.batchPublish = function (queryParams) {
return this.rawBatchPublish(queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Publish multiple records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/batch_publish
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.rawBatchPublish = function (queryParams) {
return this.client.request({
method: 'PUT',
url: '/items/publish',
queryParams: queryParams,
});
};
/**
* Unpublish multiple records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/batch_unpublish
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.batchUnpublish = function (queryParams) {
return this.rawBatchUnpublish(queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Unpublish multiple records
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/batch_unpublish
*
* @throws {ApiError}
* @throws {TimeoutError}
*
* @deprecated This API call is to be considered private and might change without notice
*/
Item.prototype.rawBatchUnpublish = function (queryParams) {
return this.client.request({
method: 'PUT',
url: '/items/unpublish',
queryParams: queryParams,
});
};
/**
* Publish a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/publish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.publish = function (itemId, body, queryParams) {
return this.rawPublish(Utils.toId(itemId), body
? Utils.serializeRequestBody(body, {
type: 'selective_publish_operation',
attributes: ['content_in_locales', 'non_localized_content'],
relationships: [],
})
: null, queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Publish a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/publish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawPublish = function (itemId, body, queryParams) {
return this.client.request({
method: 'PUT',
url: "/items/".concat(itemId, "/publish"),
body: body,
queryParams: queryParams,
});
};
/**
* Unpublish a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/unpublish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.unpublish = function (itemId, body, queryParams) {
return this.rawUnpublish(Utils.toId(itemId), body
? Utils.serializeRequestBody(body, {
type: 'selective_unpublish_operation',
attributes: ['content_in_locales'],
relationships: [],
})
: null, queryParams).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Unpublish a record
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/unpublish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawUnpublish = function (itemId, body, queryParams) {
return this.client.request({
method: 'PUT',
url: "/items/".concat(itemId, "/unpublish"),
body: body,
queryParams: queryParams,
});
};
/**
* Publish items in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_publish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.bulkPublish = function (body) {
return this.rawBulkPublish(Utils.serializeRequestBody(body, {
type: 'item_bulk_publish_operation',
attributes: [],
relationships: ['items'],
})).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Publish items in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_publish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawBulkPublish = function (body) {
return this.client.request({
method: 'POST',
url: '/items/bulk/publish',
body: body,
});
};
/**
* Unpublish items in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_unpublish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.bulkUnpublish = function (body) {
return this.rawBulkUnpublish(Utils.serializeRequestBody(body, {
type: 'item_bulk_unpublish_operation',
attributes: [],
relationships: ['items'],
})).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Unpublish items in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_unpublish
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawBulkUnpublish = function (body) {
return this.client.request({
method: 'POST',
url: '/items/bulk/unpublish',
body: body,
});
};
/**
* Destroy items in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.bulkDestroy = function (body) {
return this.rawBulkDestroy(Utils.serializeRequestBody(body, {
type: 'item_bulk_destroy_operation',
attributes: [],
relationships: ['items'],
})).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Destroy items in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_destroy
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawBulkDestroy = function (body) {
return this.client.request({
method: 'POST',
url: '/items/bulk/destroy',
body: body,
});
};
/**
* Move items to stage in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_move_to_stage
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.bulkMoveToStage = function (body) {
return this.rawBulkMoveToStage(Utils.serializeRequestBody(body, {
type: 'item_bulk_move_to_stage_operation',
attributes: ['stage'],
relationships: ['items'],
})).then(function (body) {
return Utils.deserializeResponseBody(body);
});
};
/**
* Move items to stage in bulk
*
* Read more: https://www.datocms.com/docs/content-management-api/resources/item/bulk_move_to_stage
*
* @throws {ApiError}
* @throws {TimeoutError}
*/
Item.prototype.rawBulkMoveToStage = function (body) {
return this.client.request({
method: 'POST',
url: '/items/bulk/move-to-stage',
body: body,
});
};
Item.TYPE = 'item';
return Item;
}(BaseResource_1.default));
exports.default = Item;
//# sourceMappingURL=Item.js.map