Skip to content

Commit bb896e2

Browse files
test: add test for generating Reply interface
1 parent f7241ce commit bb896e2

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/index.spec.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { generateReplyInterfaces } from ".";
2+
3+
describe("#generateReplyInterfaces", () => {
4+
it("should generate the reply interface with an empty schema", async () => {
5+
expect(await generateReplyInterfaces("")).toEqual("type Reply = {}");
6+
expect(await generateReplyInterfaces("XXX")).toEqual("type XXXReply = {}");
7+
});
8+
9+
it("should generate the reply interface with a single schema", async () => {
10+
const schema = {
11+
"2xx": {
12+
additionalProperties: false,
13+
properties: {
14+
guid: {
15+
type: "string",
16+
},
17+
},
18+
type: "object",
19+
},
20+
};
21+
const genertatedInterface = `\
22+
export interface Reply2XX {
23+
guid?: string;
24+
}
25+
26+
type Reply = Reply2XX\
27+
`;
28+
29+
expect(await generateReplyInterfaces("", schema)).toEqual(
30+
genertatedInterface
31+
);
32+
});
33+
34+
it("should generate the reply interface with a multiple schema", async () => {
35+
const schema = {
36+
"2xx": {
37+
additionalProperties: false,
38+
properties: {
39+
guid: {
40+
type: "string",
41+
},
42+
},
43+
type: "object",
44+
},
45+
"4xx": {
46+
additionalProperties: false,
47+
properties: {
48+
status: {
49+
type: "number",
50+
},
51+
},
52+
type: "object",
53+
},
54+
};
55+
const genertatedInterface = `\
56+
export interface Reply2XX {
57+
guid?: string;
58+
}
59+
60+
export interface Reply4XX {
61+
status?: number;
62+
}
63+
64+
type Reply = Reply2XX | Reply4XX\
65+
`;
66+
67+
expect(await generateReplyInterfaces("", schema)).toEqual(
68+
genertatedInterface
69+
);
70+
});
71+
});

src/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { compile } from "json-schema-to-typescript";
66
const opts = { bannerComment: "" };
77
const defaultSchema = { type: "object", additionalProperties: false };
88

9-
async function generateReplyInterfaces(prefix: string, replies = {}) {
9+
export async function generateReplyInterfaces(
10+
prefix: string,
11+
replies: Record<any, any> = {}
12+
) {
1013
const generatedInterfaces = [];
1114
const generatedReplyNames = [];
12-
for (const [replyCode, replySchema] of Object.entries<any>(replies)) {
15+
for (const [replyCode, replySchema] of Object.entries(replies)) {
1316
generatedReplyNames.push(prefix + "Reply" + replyCode.toUpperCase());
1417
generatedInterfaces.push(
1518
await compile(
@@ -22,9 +25,8 @@ async function generateReplyInterfaces(prefix: string, replies = {}) {
2225

2326
return `
2427
${generatedInterfaces.join("\n")}
25-
26-
type ${prefix}Reply = ${generatedReplyNames.join(" | ")}
27-
`;
28+
type ${prefix}Reply = ${generatedReplyNames.join(" | ") || "{}"}
29+
`.trim();
2830
}
2931

3032
async function writeFile(

0 commit comments

Comments
 (0)