@webiny/api-headless-cms-ddb-es
Version:
DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.
66 lines (63 loc) • 2.14 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createRichTextStorageTransformPlugin = void 0;
var _error = _interopRequireDefault(require("@webiny/error"));
var _apiHeadlessCms = require("@webiny/api-headless-cms");
var _api = require("@webiny/api");
const createRichTextStorageTransformPlugin = () => {
const plugin = new _apiHeadlessCms.StorageTransformPlugin({
name: "headless-cms.storage-transform.rich-text.default",
fieldType: "rich-text",
fromStorage: async ({
field,
value: storageValue,
plugins
}) => {
if (!storageValue) {
return storageValue;
} else if (typeof storageValue !== "object") {
throw new _error.default(`RichText value received in "fromStorage" function is not an object in field "${field.storageId}" - ${field.fieldId}.`);
}
let compressor;
try {
compressor = plugins.oneByType(_api.CompressorPlugin.type);
} catch (ex) {
return storageValue;
}
try {
const uncompressed = await compressor.getCompressor().decompress(storageValue);
// We must make sure the rich text value is an object.
// It is possible that it is a string if developers call the API with an already stringified value.
if (typeof uncompressed === "string") {
return JSON.parse(uncompressed);
}
return uncompressed;
} catch {
return storageValue;
}
},
toStorage: async ({
value,
plugins
}) => {
let compressor;
try {
compressor = plugins.oneByType(_api.CompressorPlugin.type);
} catch (ex) {
return value;
}
try {
return await compressor.getCompressor().compress(value);
} catch {
return value;
}
}
});
plugin.name = `headless-cms.dynamodb.storageTransform.rich-text`;
return plugin;
};
exports.createRichTextStorageTransformPlugin = createRichTextStorageTransformPlugin;
//# sourceMappingURL=richText.js.map