SingleItemMiddleware.js 806 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const SerializerMiddleware = require("./SerializerMiddleware");
  6. /**
  7. * @typedef {any} DeserializedType
  8. * @typedef {any[]} SerializedType
  9. * @extends {SerializerMiddleware<any, any[]>}
  10. */
  11. class SingleItemMiddleware extends SerializerMiddleware {
  12. /**
  13. * @param {DeserializedType} data data
  14. * @param {Object} context context object
  15. * @returns {SerializedType|Promise<SerializedType>} serialized data
  16. */
  17. serialize(data, context) {
  18. return [data];
  19. }
  20. /**
  21. * @param {SerializedType} data data
  22. * @param {Object} context context object
  23. * @returns {DeserializedType|Promise<DeserializedType>} deserialized data
  24. */
  25. deserialize(data, context) {
  26. return data[0];
  27. }
  28. }
  29. module.exports = SingleItemMiddleware;