Skip to content

Commit 996f55c

Browse files
committed
refactor: make decorators option parameter optional
1 parent 1a09d82 commit 996f55c

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

src/decorators/class.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ describe("@DocType() Decorator", function () {
2424
description: "test",
2525
});
2626
});
27+
28+
it("should collect class data correctly with default values", function () {
29+
DocType()(String);
30+
31+
const target = getTypeStorage();
32+
33+
expect(target.classes).toHaveLength(1);
34+
expect(target.classes[0].userData).toStrictEqual({
35+
name: "String",
36+
description: "This is a type 'String'.",
37+
});
38+
});
2739
});

src/decorators/class.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { ClassData } from "@utils/types";
33

44
type DocumentTypeOptions = ClassData["userData"];
55

6-
export function DocType(options: DocumentTypeOptions): ClassDecorator {
6+
export function DocType(options?: DocumentTypeOptions): ClassDecorator {
77
return target => {
88
getTypeStorage().collectClassData({
99
classType: target,
1010
userData: {
11-
...options,
11+
name: options?.name || target.name,
12+
description: options?.description || `This is a type '${target.name}'.`,
1213
},
1314
});
1415
};

src/decorators/field.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ describe("@DocField() decorator", function () {
3838
});
3939
});
4040

41+
it("should collect field data correctly with default values", function () {
42+
class MockedClass {
43+
@DocField()
44+
public test!: boolean;
45+
}
46+
47+
const target = getTypeStorage();
48+
const stringClass = target.classes.find(c => c.classType === MockedClass);
49+
if (!stringClass) {
50+
throw new Error("String class not found!");
51+
}
52+
53+
expect(stringClass.fieldMap).toHaveProperty("test");
54+
expect(Object.keys(stringClass.fieldMap)).toHaveLength(1);
55+
expect(stringClass.fieldMap["test"].userData).toStrictEqual({
56+
description: "field 'test' with type 'Boolean'.",
57+
nullable: false,
58+
});
59+
});
60+
4161
it("should collect field with an array type correctly", () => {
4262
class MockedClass {
4363
@DocField({

src/decorators/field.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface DocumentFieldOptions extends FieldUserData {
77
type?: TypeFn;
88
}
99

10-
export function DocField(fieldData: DocumentFieldOptions): DecoratorType {
10+
export function DocField(fieldData?: DocumentFieldOptions): DecoratorType {
1111
return <T>(target: object, propertyKey: string | symbol, descriptor?: TypedPropertyDescriptor<T>) => {
1212
if (typeof propertyKey === "symbol") {
1313
throw new Error("Symbol keys are not supported yet!");
@@ -20,7 +20,7 @@ export function DocField(fieldData: DocumentFieldOptions): DecoratorType {
2020
}
2121

2222
const type = Reflect.getMetadata("design:type", target, propertyKey);
23-
const desiredType = fieldData.type?.();
23+
const desiredType = fieldData?.type?.();
2424
const [targetType, isArray, isCustom] = checkType(type, desiredType, className, propertyKey);
2525

2626
getTypeStorage().collectFieldData({

src/utils/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ClassData {
3131

3232
// user defined
3333
userData: {
34-
name: string;
35-
description: string;
34+
name?: string;
35+
description?: string;
3636
};
3737
}

0 commit comments

Comments
 (0)