Skip to content

Commit

Permalink
refactor: light rework
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaticus committed Nov 24, 2024
1 parent 69e315f commit 49cd938
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 53 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion __tests__/serializers/serializeResourceObject.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Chance } from 'chance';
import { idSymbol } from '../../src/decorators/id';
import { linksSymbol } from '../../src/decorators/links';
import { linksSymbol } from '../../src/decorators/link';
import { metaSymbol } from '../../src/decorators/meta';
import { relationshipsSymbol } from '../../src/decorators/relationship';
import { resourceSymbol } from '../../src/decorators/resource';
Expand Down
42 changes: 21 additions & 21 deletions __tests__/serializers/utils/serializeResourceLinkage.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Chance } from "chance";
import { resourceSymbol } from "../../../src/decorators/resource";
import { serializeResourceLinkage } from "../../../src/serializers/serializeResourceLinkage";
import { collect } from "../../../src/serializers/utils/collect";
import { getMetadataBySymbol } from "../../../src/serializers/utils/getMetadataBySymbol";
import { idSymbol } from "../../../src";
import { Chance } from 'chance';
import { idSymbol } from '../../../src';
import { resourceSymbol } from '../../../src/decorators/resource';
import { serializeResourceLinkage } from '../../../src/serializers/serializeResourceLinkage';
import { collect } from '../../../src/serializers/utils/collect';
import { getMetadataBySymbol } from '../../../src/serializers/utils/getMetadataBySymbol';

jest.mock('../../../src/serializers/utils/getMetadataBySymbol');
const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol);
Expand Down Expand Up @@ -35,10 +35,10 @@ describe('`serializeResourceLinkage`', () => {
];

getMetadataBySymbolMocked.mockImplementation((_, symbol) => {
if(symbol === resourceSymbol) {
if (symbol === resourceSymbol) {
return undefined;
}
})
});

expect(() => serializeResourceLinkage(classInstances)).toThrow(
'Failed to serialize relationship object because the provided class instance is not a resource.',
Expand All @@ -53,10 +53,10 @@ describe('`serializeResourceLinkage`', () => {
];

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

collectMocked.mockImplementation(() => undefined);

Expand All @@ -74,18 +74,18 @@ describe('`serializeResourceLinkage`', () => {
{
someIdField: chance.string(),
someTypeField: chance.string(),
}
},
];

getMetadataBySymbolMocked.mockImplementation((object, symbol) => {
if(symbol === resourceSymbol) {
if (symbol === resourceSymbol) {
// @ts-expect-error
return object.someTypeField;
}
});

collectMocked.mockImplementation((object, symbol) => {
if(symbol === idSymbol) {
if (symbol === idSymbol) {
// @ts-expect-error
return object.someIdField;
}
Expand All @@ -97,7 +97,7 @@ describe('`serializeResourceLinkage`', () => {
classInstances.map(({ someIdField, someTypeField }) => ({
type: someTypeField,
id: someIdField,
}))
})),
);
});
});
Expand All @@ -109,10 +109,10 @@ describe('`serializeResourceLinkage`', () => {
};

getMetadataBySymbolMocked.mockImplementation((_, symbol) => {
if(symbol === resourceSymbol) {
if (symbol === resourceSymbol) {
return undefined;
}
})
});

expect(() => serializeResourceLinkage(classInstance)).toThrow(
'Failed to serialize relationship object because the provided class instance is not a resource.',
Expand All @@ -125,10 +125,10 @@ describe('`serializeResourceLinkage`', () => {
};

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

collectMocked.mockImplementation(() => undefined);

Expand All @@ -144,14 +144,14 @@ describe('`serializeResourceLinkage`', () => {
};

getMetadataBySymbolMocked.mockImplementation((object, symbol) => {
if(symbol === resourceSymbol) {
if (symbol === resourceSymbol) {
// @ts-expect-error
return object.someTypeField;
}
});

collectMocked.mockImplementation((object, symbol) => {
if(symbol === idSymbol) {
if (symbol === idSymbol) {
// @ts-expect-error
return object.someIdField;
}
Expand All @@ -165,4 +165,4 @@ describe('`serializeResourceLinkage`', () => {
});
});
});
})
});
2 changes: 1 addition & 1 deletion src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './attribute';
export * from './id';
export * from './links';
export * from './link';
export * from './meta';
export * from './relationship';
export * from './resource';
File renamed without changes.
6 changes: 3 additions & 3 deletions src/decorators/relationship.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { JSONAPIResourceLinkage } from '../types/resourceLinkage';
import type { NonArray } from '../types/utils/nonArray';

import { buildSymbol } from './utils/buildSymbol';
import { isValidFieldKey } from './utils/isValidFieldKey';

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

export const relationshipsSymbol = buildSymbol('relationships');

type ForeignKey<T> = Extract<T, JSONAPIResourceLinkage> extends never
Expand Down
20 changes: 9 additions & 11 deletions src/serializers/deserializeResourceObject.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
attributesSymbol,
idSymbol,
linksSymbol,
metaSymbol,
relationshipsSymbol,
resourceSymbol,
} from '../decorators';
import { getMetadataBySymbol } from './utils';

import type { JSONAPIResourceObject } from '../types';
import { attributesSymbol } from '../decorators/attribute';
import { idSymbol } from '../decorators/id';
import { linksSymbol } from '../decorators/link';
import { metaSymbol } from '../decorators/meta';
import { relationshipsSymbol } from '../decorators/relationship';
import { resourceSymbol } from '../decorators/resource';
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';

import type { JSONAPIResourceObject } from '../types/resourceObject';

export const deserializeResourceObject = <
C,
Expand Down
5 changes: 2 additions & 3 deletions src/serializers/serializeIncludedResourceObjects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';

import { relationshipsSymbol } from '../decorators';
import { relationshipsSymbol } from '../decorators/relationship';
import { serializeResourceObject } from './serializeResourceObject';
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';
import { isObject } from './utils/isObject';

import type { JSONAPIResourceObject } from '../types';
Expand Down
7 changes: 4 additions & 3 deletions src/serializers/serializeResourceLinkage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { idSymbol, resourceSymbol } from '../decorators';
import { idSymbol } from '../decorators/id';
import { resourceSymbol } from '../decorators/resource';
import { collect } from './utils/collect';
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';
import { isObject } from './utils/isObject';

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

export const serializeResourceLinkage = <I extends object>(
classInstance_s: I,
Expand Down Expand Up @@ -59,4 +60,4 @@ export const serializeResourceLinkage = <I extends object>(
type,
id,
};
};
};
18 changes: 8 additions & 10 deletions src/serializers/serializeResourceObject.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
attributesSymbol,
idSymbol,
linksSymbol,
metaSymbol,
relationshipsSymbol,
resourceSymbol,
} from '../decorators';
import { attributesSymbol } from '../decorators/attribute';
import { idSymbol } from '../decorators/id';
import { linksSymbol } from '../decorators/link';
import { metaSymbol } from '../decorators/meta';
import { relationshipsSymbol } from '../decorators/relationship';
import { resourceSymbol } from '../decorators/resource';
import { serializeResourceLinkage } from './serializeResourceLinkage';
import { clean } from './utils/clean';
import { collect } from './utils/collect';
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';
import { isObject } from './utils/isObject';
Expand All @@ -17,8 +17,6 @@ import type {
JSONAPIResourceObject,
JSONObject,
} from '../types';
import { serializeResourceLinkage } from './serializeResourceLinkage';
import { clean } from './utils/clean';

export const serializeResourceObject = <I extends object>(
classInstance: I,
Expand Down

0 comments on commit 49cd938

Please sign in to comment.