Skip to content

Commit ddfbe79

Browse files
committed
fix: improve logging
1 parent fc92a6c commit ddfbe79

File tree

4 files changed

+52
-61
lines changed

4 files changed

+52
-61
lines changed

dist/index.cjs.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ debug.adapter = (level, ...args) => {
7878
}
7979
};
8080

81-
const IGNORED_KEYS = ['Symbol(', '_', '$', '#'];
8281
class Parser {
8382
resolved = {};
8483
constructor(data, included = []) {
@@ -134,25 +133,23 @@ class Parser {
134133
return instance;
135134
}
136135
wrapWhenPartial(instance, loadedElement) {
137-
if (loadedElement.$_partial) {
138-
return new Proxy(instance, {
139-
get: function (target, prop) {
140-
if (prop === "$_partial") {
141-
return true;
142-
}
143-
if (prop in target) {
144-
return target[prop];
145-
}
146-
const propString = prop.toString();
147-
if (IGNORED_KEYS.some(k => propString.startsWith(k))) {
148-
return undefined;
149-
}
150-
debug('error', `Trying to call property "${prop.toString()}" to a model that is not included. Add "${loadedElement.type}" to included models.`);
151-
return undefined;
152-
}
153-
});
136+
if (!loadedElement.$_partial) {
137+
return instance;
154138
}
155-
return instance;
139+
return new Proxy(instance, {
140+
get: function (target, prop) {
141+
if (prop === "$_partial") {
142+
return true;
143+
}
144+
const propString = prop.toString();
145+
debug('error', `Trying to call property "${propString}" to a model that is not included. Add "${loadedElement.type}" to included models.`, {
146+
model: instance,
147+
property: propString,
148+
type: 'ACCESSING_NOT_INCLUDED_MODEL'
149+
});
150+
return target[prop];
151+
}
152+
});
156153
}
157154
parseRelationships(instance, loadedElement, relsData, included) {
158155
for (const key in loadedElement.relationships) {

dist/index.esm.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ debug.adapter = (level, ...args) => {
7474
}
7575
};
7676

77-
const IGNORED_KEYS = ['Symbol(', '_', '$', '#'];
7877
class Parser {
7978
resolved = {};
8079
constructor(data, included = []) {
@@ -130,25 +129,23 @@ class Parser {
130129
return instance;
131130
}
132131
wrapWhenPartial(instance, loadedElement) {
133-
if (loadedElement.$_partial) {
134-
return new Proxy(instance, {
135-
get: function (target, prop) {
136-
if (prop === "$_partial") {
137-
return true;
138-
}
139-
if (prop in target) {
140-
return target[prop];
141-
}
142-
const propString = prop.toString();
143-
if (IGNORED_KEYS.some(k => propString.startsWith(k))) {
144-
return undefined;
145-
}
146-
debug('error', `Trying to call property "${prop.toString()}" to a model that is not included. Add "${loadedElement.type}" to included models.`);
147-
return undefined;
148-
}
149-
});
132+
if (!loadedElement.$_partial) {
133+
return instance;
150134
}
151-
return instance;
135+
return new Proxy(instance, {
136+
get: function (target, prop) {
137+
if (prop === "$_partial") {
138+
return true;
139+
}
140+
const propString = prop.toString();
141+
debug('error', `Trying to call property "${propString}" to a model that is not included. Add "${loadedElement.type}" to included models.`, {
142+
model: instance,
143+
property: propString,
144+
type: 'ACCESSING_NOT_INCLUDED_MODEL'
145+
});
146+
return target[prop];
147+
}
148+
});
152149
}
153150
parseRelationships(instance, loadedElement, relsData, included) {
154151
for (const key in loadedElement.relationships) {

src/Parser.ts

+18-23
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { Model } from "./Model";
66
import { $registeredAttributes, $registeredModels, $registeredRelationships } from "./data";
77
import { debug } from "./utils";
88

9-
const IGNORED_KEYS = ['Symbol(', '_', '$', '#'];
10-
119
export class Parser {
1210
readonly resolved: Record<string, Model> = {};
1311

@@ -88,28 +86,25 @@ export class Parser {
8886
}
8987

9088
wrapWhenPartial(instance: Model, loadedElement: JSONModel & { $_partial?: boolean }) {
91-
if (loadedElement.$_partial) {
92-
return new Proxy(
93-
instance,
94-
{
95-
get: function<T extends object>(target: T, prop: keyof T) {
96-
if (prop === "$_partial") {
97-
return true;
98-
}
99-
if (prop in target) {
100-
return target[prop];
101-
}
102-
const propString = prop.toString();
103-
if (IGNORED_KEYS.some((k) => propString.startsWith(k))) {
104-
return undefined;
105-
}
106-
debug('error', `Trying to call property "${prop.toString()}" to a model that is not included. Add "${loadedElement.type}" to included models.`);
107-
return undefined;
108-
},
109-
})
89+
if (!loadedElement.$_partial) {
90+
return instance;
11091
}
111-
112-
return instance;
92+
return new Proxy(
93+
instance,
94+
{
95+
get: function<T extends object>(target: T, prop: keyof T) {
96+
if (prop === "$_partial") {
97+
return true;
98+
}
99+
const propString = prop.toString();
100+
debug('error', `Trying to call property "${propString}" to a model that is not included. Add "${loadedElement.type}" to included models.`, {
101+
model: instance,
102+
property: propString,
103+
type: 'ACCESSING_NOT_INCLUDED_MODEL'
104+
});
105+
return target[prop];
106+
},
107+
});
113108
}
114109

115110
private parseRelationships(instance: Model, loadedElement: JSONModel, relsData: RegisteredAttribute | undefined, included: JSONModel[]) {

test/basic.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ test("returns a Proxy when accessing to a not included model", () => {
9494
expect(model.relatedPost?.content).toBe(undefined);
9595
expect(logs.at(-1)).toBe("Trying to call property \"content\" to a model that is not included. Add \"posts\" to included models.");
9696
expect((model.relatedPost as any)?.$_partial).toBe(true);
97+
expect(model.relatedPost?.['name']).toBe(undefined);
98+
expect(logs.at(-1)).toBe("Trying to call property \"name\" to a model that is not included. Add \"posts\" to included models.");
9799
});
98100

99101
test("parses circular references", () => {

0 commit comments

Comments
 (0)