Skip to content

Commit

Permalink
fix(api): allow common resource attributes in create/update payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
reegodev committed Jul 20, 2021
1 parent e6f63e1 commit ea731c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
11 changes: 10 additions & 1 deletion __tests__/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ describe('serializer', () => {
metadata: {
asd: 'asdasd',
},
reference: 'test ref',
reference_origin: 'ref_origin',
}

const result = await serialize(
{
type: 'test_types',
attributes: ['name', 'description', 'sku_code'],
attributes: [
'name',
'description',
'sku_code',
'metadata',
'reference',
'reference_origin',
],
relationships: {},
},
payload,
Expand Down
16 changes: 12 additions & 4 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ export interface CommonResourceAttributes {
updated_at: string
type: string
}

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

export type AttributesPayload<T> = {
[K in keyof T]?: T[K]
} &
CommonPayloadAttributes

export type RelationshipsPayload<T> = {
[K in keyof T]?: T[K] | string
}
Expand Down Expand Up @@ -78,6 +80,12 @@ export const commonResourceFields: (keyof CommonResourceAttributes)[] = [
'updated_at',
]

export const commonPayloadAttributes: (keyof CommonPayloadAttributes)[] = [
'reference',
'reference_origin',
'metadata',
]

const requireId = <T, U>(id: string, config: ResourceConfig<T, U>): void => {
if (!id) {
throw new Error(`[${config.type}] Missing resource id`)
Expand Down
9 changes: 2 additions & 7 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ResourceConfig,
AttributesPayload,
RelationshipsPayload,
commonPayloadAttributes,
} from './resource'
import { getType, isObject } from './utils'

Expand Down Expand Up @@ -53,7 +54,7 @@ export const serialize = async <T, U>(

const serializer = new Serializer(config.type, {
keyForAttribute: 'snake_case',
attributes: config.attributes as string[],
attributes: (config.attributes as string[]).concat(commonPayloadAttributes),
})
const serialized = serializer.serialize(attributes)

Expand All @@ -63,12 +64,6 @@ 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 ea731c1

Please sign in to comment.