Skip to content

Commit 1c84fdb

Browse files
committed
fix(debug): improve logging information and typing
1 parent 5953592 commit 1c84fdb

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/Parser.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { RegisteredAttribute } from "./interfaces/RegisteredAttribute";
44
import { RegisteredProperty } from "./interfaces/RegisteredProperty";
55
import { Model } from "./Model";
66
import { $registeredAttributes, $registeredModels, $registeredRelationships } from "./data";
7-
import { debug } from "./utils";
7+
import { debug, DEBUG } from "./utils";
88
import { RegisteredModel } from "./interfaces/RegisteredModel";
99

1010
export class Parser {
@@ -100,7 +100,7 @@ export class Parser {
100100
{
101101
get: function<T extends object>(target: T, prop: keyof T) {
102102
if (prop === "$_partial") {
103-
return target[prop];
103+
return true;
104104
}
105105
if (prop in target) {
106106
return target[prop];
@@ -109,7 +109,7 @@ export class Parser {
109109
debug('error', `Trying to call property "${propString}" to a model that is not included. Add "${loadedElement.type}" to included models.`, {
110110
model: instance,
111111
property: propString,
112-
type: 'ACCESSING_NOT_INCLUDED_MODEL'
112+
type: DEBUG.ACCESSING_NOT_INCLUDED_MODEL
113113
});
114114
return target[prop];
115115
},
@@ -126,7 +126,10 @@ export class Parser {
126126
);
127127
} else {
128128
(instance as any)[key] = this.parse(relation, included);
129-
debug('warn', `Undeclared relationship "${key}" in "${loadedElement.type}"`);
129+
debug('warn', `Undeclared relationship "${key}" in "${loadedElement.type}"`, {
130+
relationship: key,
131+
type: DEBUG.UNDECLARED_RELATIONSHOP
132+
});
130133
}
131134
}
132135

@@ -137,7 +140,10 @@ export class Parser {
137140
if ("default" in parser) {
138141
(instance as any)[parser.key] = parser.default;
139142
} else {
140-
debug('warn', `Missing relationships "${key}" in "${loadedElement.type}"`);
143+
debug('warn', `Missing relationships "${key}" in "${loadedElement.type}"`, {
144+
relationship: key,
145+
type: DEBUG.MISSING_RELATIONSHIP,
146+
});
141147
}
142148
}
143149
}
@@ -153,7 +159,10 @@ export class Parser {
153159
);
154160
} else {
155161
(instance as any)[key] = loadedElement.attributes[key];
156-
debug('warn', `Undeclared key "${key}" in "${loadedElement.type}"`);
162+
debug('warn', `Undeclared @Attr() "${key}" in model "${loadedElement.type}"`, {
163+
attribute: key,
164+
type: DEBUG.UNDECLARED_ATTRIBUTE
165+
});
157166
}
158167
}
159168

@@ -164,7 +173,10 @@ export class Parser {
164173
if ("default" in parser) {
165174
(instance as any)[parser.key] = parser.default;
166175
} else {
167-
debug('warn', `Missing attribute "${key}" in "${loadedElement.type}"`);
176+
debug('warn', `Missing attribute "${key}" in "${loadedElement.type}"`, {
177+
attribute: key,
178+
type: DEBUG.MISSING_ATTRIBUTE,
179+
});
168180
}
169181
}
170182
}
@@ -177,7 +189,11 @@ export class Parser {
177189
);
178190
if (!found) {
179191
debug(
180-
'info', `Relationship with type ${element.type} with id ${element.id} not present in included`
192+
'info', `Relationship with type ${element.type} with id ${element.id} not present in included. Skipping...`,
193+
{
194+
model: element,
195+
type: DEBUG.SKIPPED_INCLUDED_RELATIONSHIP
196+
}
181197
);
182198
}
183199

src/utils.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
function debug(level: 'info' | 'warn' | 'error', ...args: any[]) {
2-
debug.adapter(level, ...args);
1+
const DEBUG = {
2+
ACCESSING_NOT_INCLUDED_MODEL: 'ACCESSING_NOT_INCLUDED_MODEL' as const,
3+
UNDECLARED_RELATIONSHOP: 'UNDECLARED_RELATIONSHOP' as const,
4+
MISSING_RELATIONSHIP: 'MISSING_RELATIONSHIP' as const,
5+
UNDECLARED_ATTRIBUTE: 'UNDECLARED_ATTRIBUTE' as const,
6+
MISSING_ATTRIBUTE: 'MISSING_ATTRIBUTE' as const,
7+
SKIPPED_INCLUDED_RELATIONSHIP: 'SKIPPED_INCLUDED_RELATIONSHIP' as const,
8+
};
9+
10+
type DebugMetadata = {
11+
type: keyof typeof DEBUG;
12+
[key: string]: unknown;
13+
}
14+
15+
function debug(level: 'info' | 'warn' | 'error', message: string, meta?: DebugMetadata) {
16+
debug.adapter(level, message, meta);
317
}
418

5-
debug.adapter = (level: 'info' | 'warn' | 'error', ...args: any[]) => {
19+
debug.adapter = (level: 'info' | 'warn' | 'error', message: string, meta?: DebugMetadata) => {
620
switch (level) {
721
case 'warn':
8-
console.warn(...args);
22+
console.warn(message, meta);
923
break;
1024
case 'error':
11-
console.error(...args);
25+
console.error(message, meta);
1226
break;
1327
case 'info':
1428
default:
15-
console.log(...args);
29+
console.log(message, meta);
1630
break;
1731
}
1832
}
1933

20-
export { debug };
34+
35+
export { debug, DEBUG };

0 commit comments

Comments
 (0)