Skip to content

Commit

Permalink
refactor: make decorators option parameter optional
Browse files Browse the repository at this point in the history
  • Loading branch information
async3619 committed Dec 9, 2022
1 parent 1a09d82 commit 996f55c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/decorators/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ describe("@DocType() Decorator", function () {
description: "test",
});
});

it("should collect class data correctly with default values", function () {
DocType()(String);

const target = getTypeStorage();

expect(target.classes).toHaveLength(1);
expect(target.classes[0].userData).toStrictEqual({
name: "String",
description: "This is a type 'String'.",
});
});
});
5 changes: 3 additions & 2 deletions src/decorators/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ClassData } from "@utils/types";

type DocumentTypeOptions = ClassData["userData"];

export function DocType(options: DocumentTypeOptions): ClassDecorator {
export function DocType(options?: DocumentTypeOptions): ClassDecorator {
return target => {
getTypeStorage().collectClassData({
classType: target,
userData: {
...options,
name: options?.name || target.name,
description: options?.description || `This is a type '${target.name}'.`,
},
});
};
Expand Down
20 changes: 20 additions & 0 deletions src/decorators/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ describe("@DocField() decorator", function () {
});
});

it("should collect field data correctly with default values", function () {
class MockedClass {
@DocField()
public test!: boolean;
}

const target = getTypeStorage();
const stringClass = target.classes.find(c => c.classType === MockedClass);
if (!stringClass) {
throw new Error("String class not found!");
}

expect(stringClass.fieldMap).toHaveProperty("test");
expect(Object.keys(stringClass.fieldMap)).toHaveLength(1);
expect(stringClass.fieldMap["test"].userData).toStrictEqual({
description: "field 'test' with type 'Boolean'.",
nullable: false,
});
});

it("should collect field with an array type correctly", () => {
class MockedClass {
@DocField({
Expand Down
4 changes: 2 additions & 2 deletions src/decorators/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface DocumentFieldOptions extends FieldUserData {
type?: TypeFn;
}

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

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

getTypeStorage().collectFieldData({
Expand Down
4 changes: 2 additions & 2 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ClassData {

// user defined
userData: {
name: string;
description: string;
name?: string;
description?: string;
};
}

0 comments on commit 996f55c

Please sign in to comment.