Skip to content

Commit

Permalink
Fixes issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
jun-sheaf committed Mar 1, 2021
1 parent ed2b641 commit e43fa08
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.5.0

### Minor Changes

- Fixes https://github.com/mu-io/ts-japi/issues/23

## 1.4.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-japi",
"version": "1.4.0",
"version": "1.5.0",
"description": "A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification",
"main": "lib/index.js",
"scripts": {
Expand Down
14 changes: 8 additions & 6 deletions src/utils/serializer.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ export async function recurseRelators(
for (let i = 0, len = relators.length; i < len; i++) {
const relator = relators[i];
const relatedData = await Promise.all(data.map(relator.getRelatedData));
const newData: any[] = [];
const newRelators = relator.getRelatedRelators();
const newData: any[] = [];
await Promise.all(
relatedData.flat().map(async (datum) => {
if (datum !== null) {
relatedData
.flat()
.filter((d) => d !== null)
.map(async (datum) => {
const resource = await relator.getRelatedResource(datum);
const key = resource.getKey();
if (!keys.includes(key)) {
included.push(resource);
keys.push(key);
included.push(resource);
newData.push(datum);
}
}
})
})
);
if (newData.length > 0 && newRelators) {
queue.push([newData, Object.values(newRelators)]);
Expand Down
43 changes: 43 additions & 0 deletions test/issue-23.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Serializer, Relator } from "../lib";
import { inspect } from "util";

describe("Serializer", () => {
it("Should traverse a depth > 1", async () => {
type A = { id: string; prop: string };
type B = { id: string; prop: string };
type C = { id: string; prop: string };

const counters = { AtoB: 0, BtoC: 0 };

const SerializerC = new Serializer<C>("c");

const BtoCRelator = new Relator<B, C>(async (data) => {
counters.BtoC++;
return { id: "1", prop: "c" };
}, SerializerC);

const SerializerB = new Serializer<B>("b", {
relators: { c: BtoCRelator },
});

const AtoBRelator = new Relator<A, B>(async (data) => {
counters.AtoB++;
return { id: "1", prop: "b" };
}, SerializerB);

const SerializerA = new Serializer<A>("a", {
relators: { b: AtoBRelator },
});

const serialized = await SerializerA.serialize({ id: "1", prop: "a" }, { depth: 2 });

console.log(
`A to B: ${counters.AtoB}\nB to C: ${counters.BtoC}\nSerialized Data:\n${inspect(
serialized,
false,
20
)}`
);
counters.AtoB = counters.BtoC = 0;
});
});

0 comments on commit e43fa08

Please sign in to comment.