Skip to content

Commit f9c6840

Browse files
committed
feat: optional対応
1 parent 75c4280 commit f9c6840

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
lines changed

Diff for: src/__tests__/generateInterface-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe("Interface Generator", () => {
44
it("Change Interface Name", () => {
55
const name = "MyTestInterface";
66
const expectResult = `interface ${name} {\n}\n`;
7-
const result = generateInterface({ name, schemas: {} });
7+
const result = generateInterface({ name, schema: {} });
88
expect(result).toBe(expectResult);
99
});
1010
});

Diff for: src/__tests__/interfaceMembers-test.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@ const schemas: JSONSchema4 = {
88
type: "string",
99
required: false,
1010
},
11-
age: {
12-
type: "number",
13-
},
14-
body: {
15-
type: "object",
16-
properties: {
17-
child: {
18-
type: "string",
19-
},
20-
},
21-
},
2211
},
2312
};
2413

14+
const interfaceName = "MyTestInterface";
15+
const expectResult = `interface ${interfaceName} {
16+
name?: string;
17+
}
18+
`;
19+
2520
describe("Interface Generator", () => {
2621
it("Change Interface Name", () => {
27-
const name = "MyTestInterface";
28-
const expectResult = `interface ${name} {\n}\n`;
29-
const result = generateInterface({ name, schemas });
30-
console.log(JSON.stringify(result));
22+
const result = generateInterface({ name: interfaceName, schema: schemas });
3123
expect(result).toBe(expectResult);
3224
});
3325
});

Diff for: src/generateInterface.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@ import { JSONSchema4 } from "json-schema";
44

55
interface Params {
66
name: string;
7-
schemas: JSONSchema4;
7+
schema?: JSONSchema4;
88
}
99

10-
const code = `
11-
interface DummyInterface {
12-
13-
}
14-
`;
10+
const code = `interface DummyInterface {}`;
1511

1612
export const generateInterface = (params: Params): string => {
1713
const result = transform(code, [
1814
Traverse.InterfaceName.traverse({ name: params.name }),
1915
Traverse.InterfaceAppendMembers.traverse({
20-
schemas: params.schemas,
16+
schemas: params.schema || {},
2117
}),
2218
]);
23-
console.log({ result });
2419
return result;
2520
};

Diff for: src/traverse/interfaceAppendMembers.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ const createObjectMemberType = ({ factory }: ts.TransformationContext, members:
3333
return factory.createTypeLiteralNode(members);
3434
};
3535

36+
const createOptional = ({ factory }: ts.TransformationContext) => {
37+
return factory.createToken(ts.SyntaxKind.QuestionToken);
38+
};
39+
40+
const createPropertySignatures = (context: ts.TransformationContext, properties: { [key: string]: JSONSchema4 }): ts.PropertySignature[] => {
41+
return Object.entries(properties).map(([key, childSchema]) => {
42+
childSchema.required;
43+
return context.factory.createPropertySignature(
44+
undefined,
45+
context.factory.createIdentifier(key),
46+
childSchema.required === false ? createOptional(context) : undefined,
47+
createTypeNode(context, childSchema),
48+
);
49+
});
50+
};
51+
3652
/**
3753
* Don't use root schema root
3854
*/
@@ -41,14 +57,7 @@ const createTypeNode = (context: ts.TransformationContext, schema: JSONSchema4):
4157
if (!schema.properties) {
4258
return createObjectMemberType(context, []);
4359
}
44-
const members = Object.entries(schema.properties).map(([key, childSchema]) => {
45-
return context.factory.createPropertySignature(
46-
undefined,
47-
context.factory.createIdentifier(key),
48-
undefined,
49-
createTypeNode(context, childSchema),
50-
);
51-
});
60+
const members = createPropertySignatures(context, schema.properties);
5261
return createObjectMemberType(context, members);
5362
} else if (schema.type === "string") {
5463
return createStringType(context);
@@ -78,14 +87,7 @@ const createInterfaceFromRootObjectSchema = (context: ts.TransformationContext,
7887
if (schema.type !== "object" || !schema.properties) {
7988
return [];
8089
}
81-
return Object.entries(schema.properties).map(([key, childSchema]) => {
82-
return context.factory.createPropertySignature(
83-
undefined,
84-
context.factory.createIdentifier(key),
85-
undefined,
86-
createTypeNode(context, childSchema),
87-
);
88-
});
90+
return createPropertySignatures(context, schema.properties);
8991
};
9092

9193
/**

0 commit comments

Comments
 (0)