From 113e04ae44ed7578fc6f240f58bb2cfa1f58ca94 Mon Sep 17 00:00:00 2001 From: Matteo Rigon Date: Fri, 16 Jul 2021 11:41:38 +0200 Subject: [PATCH] fix(serializer): include metadata in attributes --- __tests__/serializer.spec.ts | 3 +++ src/resource.ts | 5 +++++ src/serializer.ts | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/__tests__/serializer.spec.ts b/__tests__/serializer.spec.ts index 792b637..3a87d56 100644 --- a/__tests__/serializer.spec.ts +++ b/__tests__/serializer.spec.ts @@ -33,6 +33,9 @@ describe('serializer', () => { name: 'test name', description: 'test description', sku_code: '12345678', + metadata: { + asd: 'asdasd', + }, } const result = await serialize( diff --git a/src/resource.ts b/src/resource.ts index 9dde513..7b51fbf 100644 --- a/src/resource.ts +++ b/src/resource.ts @@ -28,6 +28,11 @@ export interface CommonResourceAttributes { export type AttributesPayload = { [K in keyof T]?: T[K] +} & { + reference?: string + reference_origin?: string + // eslint-disable-next-line @typescript-eslint/no-explicit-any + metadata?: Record } export type RelationshipsPayload = { diff --git a/src/serializer.ts b/src/serializer.ts index bbf26d4..b203ea3 100644 --- a/src/serializer.ts +++ b/src/serializer.ts @@ -59,6 +59,12 @@ export const serialize = async ( serialized.data.attributes = {} } + // Add metadata if original attributes contain it. + // For some reason jsonapi-serializer strips the key + if (attributes.metadata && !serialized.data.attributes.metadata) { + serialized.data.attributes.metadata = attributes.metadata + } + // Manually add relationships to the serialized payload. // Serializing relationships seems broken in jsonapi-serializer const relationshipKeys = Object.keys(relationships)