Skip to content

Commit

Permalink
feat: links deserialization
Browse files Browse the repository at this point in the history
Co-authored-by: jgriffin <jeremy.griffin@movehq.com>
Co-authored-by: Liz Johnson <lizjohnson@ip-192-168-86-32.us-gov-east-1.compute.internal>
  • Loading branch information
3 people authored Sep 23, 2022
1 parent 09be3b6 commit fc0b05d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"ts-jest": "^27.1.4",
"ts-node": "^10.7.0",
"type-fest": "^2.12.2",
"typedoc": "^0.22.13",
"typedoc": "^0.23.15",
"typedoc-plugin-markdown": "^3.11.14",
"typescript": "^4.6.3"
},
Expand Down
12 changes: 11 additions & 1 deletion src/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function parseJsonApiSimpleResourceData<TEntity, TExtraOptions>(
...attributes,
}

if (data.links) {
resource['links'] = data.links
}

if (id) {
includedCache[data.type][id] = resource
}
Expand All @@ -71,13 +75,19 @@ function parseJsonApiSimpleResourceData<TEntity, TExtraOptions>(
return findJsonApiIncluded(included, includedCache, relationData.type, relationData.id, options)
})
} else if (relationReference && relationReference.data) {
resource[relationName] = findJsonApiIncluded(
const relationResource = findJsonApiIncluded<Record<string, unknown>, TExtraOptions>(
included,
includedCache,
relationReference.data.type,
relationReference.data.id,
options,
)

if (relationReference.links) {
relationResource.links = relationReference.links
}

resource[relationName] = relationResource
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type AttributesObject = JsonObject

export type MetaObject = JsonObject

export type LinkObject = JsonObject

export type ResourceIdentifierObject = {
type: string
id: string
Expand All @@ -26,6 +28,7 @@ export type ResourceIdentifierObject = {
export type ExistingResourceObject = ResourceIdentifierObject & {
id: string
attributes: AttributesObject
links?: LinkObject
relationships?: Record<string, RelationshipObject>
}

Expand All @@ -35,6 +38,7 @@ export type ResourceObject = ExistingResourceObject | NewResourceObject

export type RelationshipObject = {
data: ResourceIdentifierObject | ResourceIdentifierObject[]
links?: LinkObject
}

export enum CaseType {
Expand Down
69 changes: 68 additions & 1 deletion tests/deserialize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('deserialize', () => {
],
}

expect(deserialize(serialized, { changeCase: CaseType.camelCase })).toEqual([
expect(deserialize(serialized, { changeCase: CaseType.camelCase })).toStrictEqual([
{
id: '1',
firstName: 'Joe',
Expand All @@ -52,4 +52,71 @@ describe('deserialize', () => {
},
])
})

it('deserialize includes links', () => {
const serialized: DocumentObject = {
data: [
{
type: 'users',
id: '1',
attributes: {
'first-name': 'Joe',
'last-name': 'Doe',
},
links: {
self: 'https://example.org/users/1',
action: 'https://example.org/action',
},
relationships: {
address: {
data: {
type: 'addr',
id: '1',
},
links: {
self: 'https://example.org/address/1/relationships/address',
related: 'https://example.org/address/1',
},
},
images: {
data: [
{ type: 'img', id: '1' },
{ type: 'img', id: '2' },
],
},
},
},
],
included: [
{
type: 'addr',
id: '1',
attributes: {
street: 'Street 1',
},
},
],
}

expect(deserialize(serialized, { changeCase: CaseType.camelCase })).toStrictEqual([
{
id: '1',
firstName: 'Joe',
lastName: 'Doe',
links: {
self: 'https://example.org/users/1',
action: 'https://example.org/action',
},
address: {
id: '1',
street: 'Street 1',
links: {
self: 'https://example.org/address/1/relationships/address',
related: 'https://example.org/address/1',
},
},
images: [{ id: '1' }, { id: '2' }],
},
])
})
})
2 changes: 1 addition & 1 deletion tests/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ describe('examples', () => {
const script = new vm.Script(scriptText)
script.runInContext(context)

expect(JSON.parse(result)).toEqual(JSON.parse(resultComment))
expect(JSON.parse(result)).toStrictEqual(JSON.parse(resultComment))
})
})
10 changes: 5 additions & 5 deletions tests/serialize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('serialize', () => {
it('should do simple transformation', () => {
const serialized = serialize(validEntity, 'users', validOptions)

expect(serialized).toEqual({
expect(serialized).toStrictEqual({
data: {
type: 'users',
attributes: {
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('serialize', () => {
it('should accept undefined options', () => {
const serialized = serialize(validEntity, 'users')

expect(serialized).toEqual(
expect(serialized).toStrictEqual(
expect.objectContaining({
data: expect.objectContaining({
attributes: expect.objectContaining({
Expand All @@ -70,7 +70,7 @@ describe('serialize', () => {
it('should accept empty entity', () => {
const serialized = serialize(undefined, 'users')

expect(serialized).toEqual({
expect(serialized).toStrictEqual({
// eslint-disable-next-line unicorn/no-null
data: null,
})
Expand All @@ -79,7 +79,7 @@ describe('serialize', () => {
it('should accept entity array', () => {
const serialized = serialize([validEntity], 'users')

expect(serialized).toEqual(
expect(serialized).toStrictEqual(
expect.objectContaining({
data: [
expect.objectContaining({
Expand All @@ -99,7 +99,7 @@ describe('serialize', () => {
},
})

expect(serialized).toEqual({
expect(serialized).toStrictEqual({
data: {
id: 5,
type: 'users',
Expand Down
2 changes: 1 addition & 1 deletion tests/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('transform', () => {
.withOptions({ idKey: '_id' })
.serialize()

expect(entitySerialized).toEqual({
expect(entitySerialized).toStrictEqual({
data: {
type: 'users',
id: 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('changeCase', () => {
})

it('should deep convert keys', () => {
expect(changeCase(input, CaseType.kebabCase, true)).toEqual({
expect(changeCase(input, CaseType.kebabCase, true)).toStrictEqual({
'first-name': 'Joe',
'last-name': 'Doe',
address: {
Expand Down

0 comments on commit fc0b05d

Please sign in to comment.