UNPKG

@webiny/api-headless-cms-ddb

Version:

DynamoDB storage operations plugin for Headless CMS API.

68 lines (64 loc) 2.07 kB
"use strict"; 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"); /** * File is @internal */ const createRichTextStorageTransformPlugin = () => { return 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; } } }); }; exports.createRichTextStorageTransformPlugin = createRichTextStorageTransformPlugin; //# sourceMappingURL=richText.js.map