Skip to content

Commit

Permalink
fix(serializer): include metadata in attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
reegodev committed Jul 16, 2021
1 parent 6b40c77 commit 113e04a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions __tests__/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('serializer', () => {
name: 'test name',
description: 'test description',
sku_code: '12345678',
metadata: {
asd: 'asdasd',
},
}

const result = await serialize(
Expand Down
5 changes: 5 additions & 0 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface CommonResourceAttributes {

export type AttributesPayload<T> = {
[K in keyof T]?: T[K]
} & {
reference?: string
reference_origin?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata?: Record<string, any>
}

export type RelationshipsPayload<T> = {
Expand Down
6 changes: 6 additions & 0 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export const serialize = async <T, U>(
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)
Expand Down

0 comments on commit 113e04a

Please sign in to comment.