feat: Extend renderer to use serializers
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import serializers from '../serializers/index.js';
|
||||||
|
|
||||||
const isPaginated = (object) =>
|
const isPaginated = (object) =>
|
||||||
object?.pageInfo &&
|
object?.pageInfo &&
|
||||||
object?.totalCount !== undefined &&
|
object?.totalCount !== undefined &&
|
||||||
@@ -10,14 +12,23 @@ const totalCount = (object) =>
|
|||||||
isPaginated(object) ? object.totalCount : isArray(object) ? object.length : 1;
|
isPaginated(object) ? object.totalCount : isArray(object) ? object.length : 1;
|
||||||
|
|
||||||
const renderObject = (response, object) => {
|
const renderObject = (response, object) => {
|
||||||
const data = isPaginated(object) ? object.records : object;
|
let data = isPaginated(object) ? object.records : object;
|
||||||
|
const type = isPaginated(object)
|
||||||
|
? object.records[0].constructor.name
|
||||||
|
: object.constructor.name;
|
||||||
|
|
||||||
|
const serializer = serializers[type];
|
||||||
|
|
||||||
|
if (serializer) {
|
||||||
|
data = Array.isArray(data)
|
||||||
|
? data.map((item) => serializer(item))
|
||||||
|
: serializer(data);
|
||||||
|
}
|
||||||
|
|
||||||
const computedPayload = {
|
const computedPayload = {
|
||||||
data,
|
data,
|
||||||
meta: {
|
meta: {
|
||||||
type: isPaginated(object)
|
type,
|
||||||
? object.records[0].constructor.name
|
|
||||||
: object.constructor.name,
|
|
||||||
count: totalCount(object),
|
count: totalCount(object),
|
||||||
isArray: isArray(object),
|
isArray: isArray(object),
|
||||||
currentPage: isPaginated(object) ? object.pageInfo.currentPage : null,
|
currentPage: isPaginated(object) ? object.pageInfo.currentPage : null,
|
||||||
|
Reference in New Issue
Block a user