From d8ca17f6ea3e04979131ad70e55c9bdd02f4fc88 Mon Sep 17 00:00:00 2001 From: Julian Delerme Date: Fri, 13 Jan 2023 20:03:08 -0500 Subject: [PATCH] fix: changeCase for relation names (#21) When deserializing with the `changeCase` option, we should change the case of relation names so they are consistent with the other properties --- src/deserializer.ts | 11 +++++++--- src/utils.ts | 11 +++++----- tests/deserialize.spec.ts | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/deserializer.ts b/src/deserializer.ts index 5d526fa..dec27a1 100644 --- a/src/deserializer.ts +++ b/src/deserializer.ts @@ -1,5 +1,5 @@ import { AttributesObject, DocumentObject, ExistingResourceObject, Options, ResourceObject } from './types' -import { changeCase } from './utils' +import { caseTypes, changeCase } from './utils' type IncludedCache = Record> @@ -70,8 +70,13 @@ function parseJsonApiSimpleResourceData( continue } + let casedRelationName = relationName + if (options.changeCase) { + casedRelationName = caseTypes[options.changeCase](relationName) + } + if (Array.isArray(relationReference.data)) { - resource[relationName] = relationReference.data.map((relationData) => { + resource[casedRelationName] = relationReference.data.map((relationData) => { return findJsonApiIncluded(included, includedCache, relationData.type, relationData.id, options) }) } else if (relationReference && relationReference.data) { @@ -87,7 +92,7 @@ function parseJsonApiSimpleResourceData( relationResource.links = relationReference.links } - resource[relationName] = relationResource + resource[casedRelationName] = relationResource } } } diff --git a/src/utils.ts b/src/utils.ts index a089375..39d0380 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,11 @@ import { camelCase, snakeCase, paramCase } from 'change-case' import { AttributesObject, CaseType, JsonObject } from './types' type CaseFunction = (input: string) => string +export const caseTypes: Record = { + [CaseType.camelCase]: camelCase, + [CaseType.snakeCase]: snakeCase, + [CaseType.kebabCase]: paramCase, +} /** * Used to change the case (e.g. captalization) of the keys of a object @@ -11,12 +16,6 @@ type CaseFunction = (input: string) => string * @param deep */ export function changeCase(originalAttributes: AttributesObject, caseType: CaseType, deep = false): AttributesObject { - const caseTypes: Record = { - [CaseType.camelCase]: camelCase, - [CaseType.snakeCase]: snakeCase, - [CaseType.kebabCase]: paramCase, - } - const caseFunction = caseTypes[caseType] if (!caseFunction) { diff --git a/tests/deserialize.spec.ts b/tests/deserialize.spec.ts index 9ad9b9e..707010d 100644 --- a/tests/deserialize.spec.ts +++ b/tests/deserialize.spec.ts @@ -119,4 +119,48 @@ describe('deserialize', () => { }, ]) }) + + it('should change relationship name casing', () => { + const serialized: DocumentObject = { + data: [ + { + type: 'users', + id: '1', + attributes: { + 'first-name': 'Joe', + 'last-name': 'Doe', + }, + relationships: { + home_address: { + data: { + type: 'addr', + id: '1', + }, + }, + }, + }, + ], + included: [ + { + type: 'addr', + id: '1', + attributes: { + street: 'Street 1', + }, + }, + ], + } + + expect(deserialize(serialized, { changeCase: CaseType.camelCase })).toStrictEqual([ + { + id: '1', + firstName: 'Joe', + lastName: 'Doe', + homeAddress: { + id: '1', + street: 'Street 1', + }, + }, + ]) + }) })