| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const { register } = require("./serialization");
|
|---|
| 8 |
|
|---|
| 9 | /** @typedef {import("../serialization/ObjectMiddleware").Constructor} Constructor */
|
|---|
| 10 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 11 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {{ serialize: (context: ObjectSerializerContext) => void, deserialize: (context: ObjectDeserializerContext) => void }} SerializableClass */
|
|---|
| 14 | /**
|
|---|
| 15 | * Defines the serializable class constructor type used by this module.
|
|---|
| 16 | * @template {SerializableClass} T
|
|---|
| 17 | * @typedef {(new (...params: EXPECTED_ANY[]) => T) & { deserialize?: (context: ObjectDeserializerContext) => T }} SerializableClassConstructor
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Represents ClassSerializer.
|
|---|
| 22 | * @template {SerializableClass} T
|
|---|
| 23 | */
|
|---|
| 24 | class ClassSerializer {
|
|---|
| 25 | /**
|
|---|
| 26 | * Creates an instance of ClassSerializer.
|
|---|
| 27 | * @param {SerializableClassConstructor<T>} Constructor constructor
|
|---|
| 28 | */
|
|---|
| 29 | constructor(Constructor) {
|
|---|
| 30 | this.Constructor = Constructor;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Serializes this instance into the provided serializer context.
|
|---|
| 35 | * @param {T} obj obj
|
|---|
| 36 | * @param {ObjectSerializerContext} context context
|
|---|
| 37 | */
|
|---|
| 38 | serialize(obj, context) {
|
|---|
| 39 | obj.serialize(context);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Restores this instance from the provided deserializer context.
|
|---|
| 44 | * @param {ObjectDeserializerContext} context context
|
|---|
| 45 | * @returns {T} obj
|
|---|
| 46 | */
|
|---|
| 47 | deserialize(context) {
|
|---|
| 48 | if (typeof this.Constructor.deserialize === "function") {
|
|---|
| 49 | return this.Constructor.deserialize(context);
|
|---|
| 50 | }
|
|---|
| 51 | const obj = new this.Constructor();
|
|---|
| 52 | obj.deserialize(context);
|
|---|
| 53 | return obj;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Processes the provided constructor.
|
|---|
| 59 | * @template {Constructor} T
|
|---|
| 60 | * @param {T} Constructor the constructor
|
|---|
| 61 | * @param {string} request the request which will be required when deserializing
|
|---|
| 62 | * @param {string | null=} name the name to make multiple serializer unique when sharing a request
|
|---|
| 63 | */
|
|---|
| 64 | module.exports = (Constructor, request, name = null) => {
|
|---|
| 65 | register(Constructor, request, name, new ClassSerializer(Constructor));
|
|---|
| 66 | };
|
|---|