@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
49 lines • 1.66 kB
JavaScript
/**
* @module Serde
*/
import { SuperJSON } from "superjson";
import { DeserializationSerdeError, SerializationSerdeError, } from "../../../../serde/contracts/_module.js";
/**
* IMPORT_PATH: `"@daiso-tech/core/serde/super-json-serde-adapter"`
* @group Adapters
*/
export class SuperJsonSerdeAdapter {
superJson = new SuperJSON();
registerCustom(transformer) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const customTransformer = this.superJson.customTransformerRegistry.findByName(transformer.name);
const hasAlreadyCustomTransformer = customTransformer !== undefined;
if (hasAlreadyCustomTransformer) {
return;
}
this.superJson.registerCustom({
isApplicable(value) {
return transformer.isApplicable(value);
},
serialize(deserializedValue) {
return transformer.serialize(deserializedValue);
},
deserialize(serializedValue) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return transformer.deserialize(serializedValue);
},
}, transformer.name);
}
serialize(value) {
try {
return this.superJson.stringify(value);
}
catch (error) {
throw SerializationSerdeError.create(error);
}
}
deserialize(value) {
try {
return this.superJson.parse(value);
}
catch (error) {
throw DeserializationSerdeError.create(error);
}
}
}
//# sourceMappingURL=super-json-serde-adapter.js.map