Skip to content

Commit

Permalink
Revert "fix: relationship object serialization (#5)"
Browse files Browse the repository at this point in the history
This reverts commit 113ea51.
  • Loading branch information
ryanhaticus committed Dec 9, 2024
1 parent e1f1e20 commit 3a66c8c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 113 deletions.
13 changes: 13 additions & 0 deletions __tests__/serializers/serializeResourceLinkage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ describe('`serializeResourceLinkage`', () => {
chance = new Chance();
});

describe('when given a resource linkage', () => {
it('should return the resource linkage as is', () => {
const resourceLinkage = {
type: chance.string(),
id: chance.string(),
};

expect(serializeResourceLinkage(resourceLinkage)).toEqual(
resourceLinkage,
);
});
});

describe('when given an array of class instances', () => {
it('should throw an error if some element in the array is not an object', () => {
const classInstances = [1];
Expand Down
49 changes: 2 additions & 47 deletions __tests__/serializers/serializeResourceObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ describe('`serializeResourceObject`', () => {
});
});

describe('when the related class instance is not an object', () => {
it('should throw an error', () => {
describe('when the related class instance is a single object', () => {
it('should throw an error if the related class instance is not an object', () => {
const key = chance.string();

getMetadataBySymbolMocked.mockImplementation(
Expand All @@ -144,9 +144,7 @@ describe('`serializeResourceObject`', () => {
`Failed to serialize relationship object for ${key} because the value is not an object.`,
);
});
});

describe('when the related class instance is an object', () => {
it('should serialize the relationships object and return it', () => {
const key = chance.string();

Expand Down Expand Up @@ -203,49 +201,6 @@ describe('`serializeResourceObject`', () => {
});
});

describe('when the related class instance is already a relationship object', () => {
it('should return without serializing the relationship object', () => {
const key = chance.string();

getMetadataBySymbolMocked.mockImplementation(
(_: object, symbol: symbol) => {
if (symbol === resourceSymbol) {
return 'type';
}

if (symbol === relationshipsSymbol) {
return [[key, '']];
}

return undefined;
},
);

collectMocked.mockImplementation((_: object, symbol: symbol) => {
if (symbol === idSymbol) {
return 'id';
}

return undefined;
});

const relatedClassInstance = {
id: 'some id',
type: 'some type',
};

const classInstance = {
[key]: {
data: relatedClassInstance,
},
};

const result = serializeResourceObject(classInstance);

expect(result.relationships).toBeUndefined();
});
});

it('should return without the `relationships` field if no relationships are found', () => {
getMetadataBySymbolMocked.mockImplementation(
(_: object, symbol: symbol) => {
Expand Down
35 changes: 0 additions & 35 deletions __tests__/serializers/utils/isRelationshipObject.test.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/serializers/serializeResourceLinkage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { resourceSymbol } from '../decorators/resource';
import { collect } from './utils/collect';
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';
import { isObject } from './utils/isObject';
import { isResourceLinkage } from './utils/isResourceLinkage';

import type { JSONAPIResourceLinkage } from '../types/resourceLinkage';

export const serializeResourceLinkage = <I extends object>(
classInstanceCandidate_s: I | JSONAPIResourceLinkage,
classInstance_s: I | JSONAPIResourceLinkage,
): JSONAPIResourceLinkage => {
const classInstance_s = classInstanceCandidate_s as I;
if (isResourceLinkage(classInstance_s)) {
return classInstance_s;
}

if (Array.isArray(classInstance_s)) {
if (!classInstance_s.every(isObject)) {
Expand Down
5 changes: 0 additions & 5 deletions src/serializers/serializeResourceObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
JSONAPIResourceObject,
JSONObject,
} from '../types';
import { isRelationshipObject } from './utils/isRelationshipObject';

export const serializeResourceObject = <I extends object>(
classInstance: I,
Expand All @@ -44,10 +43,6 @@ export const serializeResourceObject = <I extends object>(
);
}

if (isRelationshipObject(relatedClassInstance_s)) {
return acc;
}

acc[key.toString()] = {
data: serializeResourceLinkage(relatedClassInstance_s),
};
Expand Down
1 change: 0 additions & 1 deletion src/serializers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ export * from './getMetadataBySymbol';
export * from './collect';
export * from './clean';
export * from './isObject';
export * from './isRelationshipObject';
export * from './isResourceIdentifierObject';
export * from './isResourceLinkage';
23 changes: 0 additions & 23 deletions src/serializers/utils/isRelationshipObject.ts

This file was deleted.

0 comments on commit 3a66c8c

Please sign in to comment.