UNPKG

idempotency-redis

Version:
95 lines (94 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JSONSerializer = exports.DefaultErrorSerializer = void 0; const serialize_error_cjs_1 = require("serialize-error-cjs"); const serialization_errors_1 = require("./serialization.errors"); /** * A serializer for Error objects that utilizes serialize-error-cjs for * converting Error objects to and from a JSON serializable format. * This can be used as a base class for custom error serializers. */ class DefaultErrorSerializer { /** * Serializes an Error object into a string using serialize-error-cjs and JSON.stringify. * @param value The Error object to serialize. * @returns A string representation of the Error. */ serialize(value) { return JSON.stringify((0, serialize_error_cjs_1.serializeError)(value)); } /** * Deserializes a string back into an Error object using serialize-error-cjs and JSON.parse. * Throws if the format is not recognized as a serialized Error object. * @param value The string to deserialize. * @returns The deserialized Error object. * @throws SerializerError if the format is not recognized. */ deserialize(value) { let error; try { error = JSON.parse(value); } catch (err) { throw new serialization_errors_1.SerializerError('Invalid JSON', err); } if (isSerializedError(error)) { return (0, serialize_error_cjs_1.deserializeError)(error); } // Likely the value was serialized not by DefaultErrorSerializer, // possibly by a derived class. throw new serialization_errors_1.SerializerError('Invalid serialized error format'); } } exports.DefaultErrorSerializer = DefaultErrorSerializer; /** * A JSON serializer that assumes the value is already in a JSON serializable format. */ class JSONSerializer { /** * Serializes a value into a string using JSON.stringify. * @param value The value to serialize. * @returns A string representation of the value. * @throws SerializerError if the value is not JSON serializable. */ serialize(value) { try { // Assumes the value is already in a JSON serializable format. return JSON.stringify(value); } catch (err) { throw new serialization_errors_1.SerializerError('Not JSON serializable', err); } } /** * Deserializes a string back into a value using JSON.parse. * @param value The string to deserialize. * @returns The deserialized value. * @throws SerializerError if the value is not valid JSON. */ deserialize(value) { try { // Assumes the value is a serialized JSON string in the shape of T. return JSON.parse(value); } catch (err) { throw new serialization_errors_1.SerializerError('Invalid JSON', err); } } } exports.JSONSerializer = JSONSerializer; /** * Type guard function to check if a given object conforms to the SerializedError interface. * @param err The object to check. * @returns True if the object is a serialized error, false otherwise. */ function isSerializedError(err) { return (!!err && typeof err === 'object' && 'name' in err && typeof err['name'] === 'string' && 'message' in err && typeof err['message'] === 'string' && 'stack' in err && typeof err['stack'] === 'string'); }