Skip to content

Commit bfe7a4d

Browse files
authored
refactor(data-structure): use @himenon/path-oriented-data-structure (#11)
* refactor: use @himenon/path-oriented-data-structure * refactor: remove unused code * refactor: remove unused state
1 parent 47e4ca0 commit bfe7a4d

27 files changed

+270
-748
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"node-fetch": "2.6.1"
7373
},
7474
"dependencies": {
75+
"@himenon/path-oriented-data-structure": "0.1.0",
7576
"@types/json-schema": "7.0.6",
7677
"ajv": "7.0.3",
7778
"dot-prop": "6.0.1",

src/Converter/v3/Context.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const create = (entryPoint: string, store: Store.Type, factory: TypeScrip
8888
});
8989
if (ts.isTypeLiteralNode(typeNode)) {
9090
store.addStatement(reference.path, {
91-
type: "interface",
91+
kind: "interface",
9292
name: reference.name,
9393
value: factory.InterfaceDeclaration.create({
9494
export: true,
@@ -107,7 +107,7 @@ export const create = (entryPoint: string, store: Store.Type, factory: TypeScrip
107107
});
108108
store.addStatement(reference.path, {
109109
name: reference.name,
110-
type: "typeAlias",
110+
kind: "typeAlias",
111111
value,
112112
});
113113
}
@@ -123,7 +123,7 @@ export const create = (entryPoint: string, store: Store.Type, factory: TypeScrip
123123
});
124124
store.addStatement(reference.path, {
125125
name: reference.name,
126-
type: "typeAlias",
126+
kind: "typeAlias",
127127
value,
128128
});
129129
}

src/Converter/v3/components/Headers.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,9 @@ export const generateNamespace = (
1818
context: ToTypeNode.Context,
1919
): void => {
2020
store.addComponent("headers", {
21-
type: "namespace",
21+
kind: "namespace",
2222
name: Name.Components.Headers,
23-
value: factory.Namespace.create({
24-
export: true,
25-
name: Name.Components.Headers,
26-
statements: [],
27-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
28-
}),
29-
statements: {},
23+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
3024
});
3125
Object.entries(headers).forEach(([name, header]) => {
3226
if (Guard.isReference(header)) {
@@ -40,7 +34,7 @@ export const generateNamespace = (
4034
}
4135
} else {
4236
store.addStatement(`components/headers/${name}`, {
43-
type: "typeAlias",
37+
kind: "typeAlias",
4438
name: name,
4539
value: Header.generateTypeNode(entryPoint, currentPoint, factory, name, header, context),
4640
});

src/Converter/v3/components/Operation.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,16 @@ export const generateNamespace = (
4747
throw new Error("not setting operationId\n" + JSON.stringify(operation));
4848
}
4949
store.addStatement(basePath, {
50-
type: "namespace",
50+
kind: "namespace",
5151
name,
52-
value: factory.Namespace.create({
53-
export: true,
54-
name,
55-
comment: ExternalDocumentation.addComment(Servers.addComment([generateComment(operation)], operation.servers), operation.externalDocs),
56-
deprecated: operation.deprecated,
57-
statements: [],
58-
}),
59-
statements: {},
52+
comment: ExternalDocumentation.addComment(Servers.addComment([generateComment(operation)], operation.servers), operation.externalDocs),
53+
deprecated: operation.deprecated,
6054
});
6155

6256
if (operation.parameters) {
6357
const parameterName = "Parameter";
6458
store.addStatement(`${basePath}/Parameter`, {
65-
type: "interface",
59+
kind: "interface",
6660
name: parameterName,
6761
value: Parameter.generateInterface(entryPoint, currentPoint, factory, parameterName, operation.parameters, context),
6862
});
@@ -79,13 +73,13 @@ export const generateNamespace = (
7973
const contentPath = path.join(reference.path, "Content"); // requestBodyはNamespaceを形成するため
8074
const name = "Content";
8175
store.addStatement(contentPath, {
82-
type: "interface",
76+
kind: "interface",
8377
name: name,
8478
value: RequestBody.generateInterface(entryPoint, reference.referencePoint, factory, name, reference.data, context),
8579
});
8680
const typeAliasName = context.resolveReferencePath(currentPoint, contentPath).name;
8781
store.addStatement(`${basePath}/RequestBody`, {
88-
type: "typeAlias",
82+
kind: "typeAlias",
8983
name: typeAliasName,
9084
value: factory.TypeAliasDeclaration.create({
9185
export: true,
@@ -143,7 +137,7 @@ export const generateStatements = (
143137
const contentPath = path.join(reference.path, "Content"); // requestBodyはNamespaceを形成するため
144138
const name = "Content";
145139
store.addStatement(contentPath, {
146-
type: "interface",
140+
kind: "interface",
147141
name: name,
148142
value: RequestBody.generateInterface(entryPoint, reference.referencePoint, factory, name, reference.data, context),
149143
});

src/Converter/v3/components/Parameters.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ export const generateNamespace = (
1919
): void => {
2020
const basePath = "components/parameters";
2121
store.addComponent("parameters", {
22-
type: "namespace",
22+
kind: "namespace",
2323
name: Name.Components.Parameters,
24-
value: factory.Namespace.create({
25-
export: true,
26-
name: Name.Components.Parameters,
27-
statements: [],
28-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
29-
}),
30-
statements: {},
24+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
3125
});
3226

3327
Object.entries(parameters).forEach(([name, parameter]) => {
@@ -41,7 +35,7 @@ export const generateNamespace = (
4135
}
4236
Schema.addSchema(entryPoint, currentPoint, store, factory, reference.path, reference.name, reference.data.schema, context);
4337
store.addStatement(`${basePath}/${name}`, {
44-
type: "typeAlias",
38+
kind: "typeAlias",
4539
name: name,
4640
value: factory.TypeAliasDeclaration.create({
4741
export: true,
@@ -55,7 +49,7 @@ export const generateNamespace = (
5549
} else {
5650
const path = `${basePath}/${name}`;
5751
store.addStatement(path, {
58-
type: "typeAlias",
52+
kind: "typeAlias",
5953
name: name,
6054
value: Paramter.generateTypeAlias(entryPoint, currentPoint, factory, name, parameter, context),
6155
});
@@ -72,15 +66,9 @@ export const generateNamespaceWithList = (
7266
context: ToTypeNode.Context,
7367
): void => {
7468
store.addComponent("parameters", {
75-
type: "namespace",
69+
kind: "namespace",
7670
name: Name.Components.Parameters,
77-
value: factory.Namespace.create({
78-
export: true,
79-
name: Name.Components.Parameters,
80-
statements: [],
81-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
82-
}),
83-
statements: {},
71+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
8472
});
8573

8674
parameters.forEach(parameter => {
@@ -91,14 +79,14 @@ export const generateNamespaceWithList = (
9179
}
9280
const path = `components/parameters/${reference.name}`;
9381
return store.addStatement(path, {
94-
type: "typeAlias",
82+
kind: "typeAlias",
9583
name: reference.name,
9684
value: Paramter.generateTypeAlias(entryPoint, reference.referencePoint, factory, reference.name, reference.data, context),
9785
});
9886
}
9987
const path = `components/parameters/${parameter.name}`;
10088
return store.addStatement(path, {
101-
type: "typeAlias",
89+
kind: "typeAlias",
10290
name: parameter.name,
10391
value: Paramter.generateTypeAlias(entryPoint, currentPoint, factory, parameter.name, parameter, context),
10492
});

src/Converter/v3/components/PathItem.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@ export const generateNamespace = (
2222
const basePath = `${parentPath}/${name}`;
2323
const topComment = options && options.topComment && options.topComment;
2424
store.addStatement(basePath, {
25-
type: "namespace",
25+
kind: "namespace",
2626
name,
27-
value: factory.Namespace.create({
28-
export: true,
29-
name,
30-
statements: [],
31-
comment: Servers.addComment([topComment, pathItem.description], pathItem.servers),
32-
}),
33-
statements: {},
27+
comment: Servers.addComment([topComment, pathItem.description], pathItem.servers),
3428
});
3529
if (pathItem.get) {
3630
Operation.generateNamespace(entryPoint, currentPoint, store, factory, basePath, "GET", pathItem.get, context);

src/Converter/v3/components/PathItems.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ export const generateNamespace = (
2020
const basePath = "components/pathItems";
2121

2222
store.addComponent("pathItems", {
23-
type: "namespace",
23+
kind: "namespace",
2424
name: Name.Components.PathItems,
25-
value: factory.Namespace.create({
26-
export: true,
27-
name: Name.Components.PathItems,
28-
statements: [],
29-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
30-
}),
31-
statements: {},
25+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
3226
});
3327

3428
Object.entries(pathItems).forEach(([key, pathItem]) => {

src/Converter/v3/components/RequestBodies.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ export const generateNamespace = (
1919
): void => {
2020
const basePath = "components/requestBodies";
2121
store.addComponent("requestBodies", {
22-
type: "namespace",
22+
kind: "namespace",
2323
name: Name.Components.RequestBodies,
24-
value: factory.Namespace.create({
25-
export: true,
26-
name: Name.Components.RequestBodies,
27-
statements: [],
28-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
29-
}),
30-
statements: {},
24+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
3125
});
3226

3327
Object.entries(requestBodies).forEach(([name, requestBody]) => {

src/Converter/v3/components/RequestBody.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ export const generateNamespace = (
3535
): void => {
3636
const basePath = `${parentName}/${name}`;
3737
store.addStatement(basePath, {
38-
type: "namespace",
38+
kind: "namespace",
3939
name,
40-
value: factory.Namespace.create({
41-
export: true,
42-
name,
43-
comment: requestBody.description,
44-
statements: [generateInterface(entryPoint, currentPoint, factory, "Content", requestBody, context)],
45-
}),
46-
statements: {},
40+
comment: requestBody.description,
41+
});
42+
store.addStatement(`${basePath}/Content`, {
43+
kind: "interface",
44+
name: "Content",
45+
value: generateInterface(entryPoint, currentPoint, factory, "Content", requestBody, context),
4746
});
4847
};

src/Converter/v3/components/Response.ts

+14-35
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from "path";
22

33
import { Factory } from "../../../CodeGenerator";
44
import * as Name from "../Name";
5-
import { Def, State, Store } from "../store";
5+
import { Store } from "../store";
66
import * as ToTypeNode from "../toTypeNode";
77
import { OpenApi } from "../types";
88
import * as Header from "./Header";
@@ -20,28 +20,22 @@ export const generateNamespace = (
2020
): void => {
2121
const basePath = `${parentPath}/${name}`;
2222
store.addStatement(basePath, {
23-
type: "namespace",
23+
kind: "namespace",
2424
name,
25-
value: factory.Namespace.create({
26-
export: true,
27-
name: name,
28-
comment: response.description,
29-
statements: [],
30-
}),
31-
statements: {},
25+
comment: response.description,
3226
});
3327

3428
if (response.headers) {
3529
store.addStatement(`${basePath}/Header`, {
36-
type: "interface",
30+
kind: "interface",
3731
name: Name.ComponentChild.Header,
3832
value: Header.generateInterface(entryPoint, currentPoint, factory, Name.ComponentChild.Header, response.headers, context),
3933
});
4034
}
4135

4236
if (response.content) {
4337
store.addStatement(`${basePath}/Content`, {
44-
type: "interface",
38+
kind: "interface",
4539
name: Name.ComponentChild.Content,
4640
value: MediaType.generateInterface(entryPoint, currentPoint, factory, Name.ComponentChild.Content, response.content, context),
4741
});
@@ -61,42 +55,27 @@ export const generateReferenceNamespace = (
6155
const basePath = `${parentPath}/${nameWithStatusCode}`;
6256
const referenceNamespaceName = context.resolveReferencePath(currentPoint, responseReference.path).name;
6357
store.addStatement(basePath, {
64-
type: "namespace",
58+
kind: "namespace",
6559
name: nameWithStatusCode,
66-
value: factory.Namespace.create({
67-
export: true,
68-
name: nameWithStatusCode,
69-
statements: [],
70-
}),
71-
statements: {},
7260
});
73-
// TODO Type Guard
74-
const headerNamespace = store.getStatement(path.join(responseReference.path, "Header"), "namespace") as
75-
| Def.NamespaceStatement<State.A, State.B, State.C>
76-
| undefined;
61+
const headerNamespace = store.getStatement(path.join(responseReference.path, "Header"), "namespace");
7762
if (headerNamespace) {
7863
store.addStatement(`${basePath}/Header`, {
79-
type: "namespace",
64+
kind: "namespace",
8065
name: Name.ComponentChild.Header,
81-
value: factory.Namespace.create({
82-
export: true,
83-
name: Name.ComponentChild.Header,
84-
statements: [],
85-
}),
86-
statements: {},
8766
});
88-
Object.values(headerNamespace.statements).forEach(statement => {
67+
Object.values(headerNamespace.getChildren()).forEach(statement => {
8968
if (!statement) {
9069
return;
9170
}
92-
if (statement.type === "interface" || statement.type === "typeAlias") {
71+
if (statement.kind === "interface" || statement.kind === "typeAlias") {
9372
const aliasName = [referenceNamespaceName, Name.ComponentChild.Header, statement.name].join(".");
94-
store.addStatement(`${basePath}/Header/${statement.value.name.text}`, {
95-
type: "typeAlias",
73+
store.addStatement(`${basePath}/Header/${statement.name}`, {
74+
kind: "typeAlias",
9675
name: aliasName,
9776
value: factory.TypeAliasDeclaration.create({
9877
export: true,
99-
name: statement.value.name.text,
78+
name: statement.name,
10079
type: factory.TypeReferenceNode.create({
10180
name: aliasName,
10281
}),
@@ -106,7 +85,7 @@ export const generateReferenceNamespace = (
10685
});
10786
}
10887
store.addStatement(`${basePath}/Content`, {
109-
type: "typeAlias",
88+
kind: "typeAlias",
11089
name: Name.ComponentChild.Content,
11190
value: factory.TypeAliasDeclaration.create({
11291
export: true,

src/Converter/v3/components/Responses.ts

+4-16
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@ export const generateNamespace = (
2323
): void => {
2424
const basePath = "components/responses";
2525
store.addComponent("responses", {
26-
type: "namespace",
26+
kind: "namespace",
2727
name: Name.Components.Responses,
28-
value: factory.Namespace.create({
29-
export: true,
30-
name: Name.Components.Responses,
31-
statements: [],
32-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
33-
}),
34-
statements: {},
28+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
3529
});
3630
Object.entries(responses).forEach(([name, response]) => {
3731
if (Guard.isReference(response)) {
@@ -69,15 +63,9 @@ export const generateNamespaceWithStatusCode = (
6963
): void => {
7064
const basePath = `${parentPath}/responses`;
7165
store.addStatement(basePath, {
72-
type: "namespace",
66+
kind: "namespace",
7367
name: Name.ComponentChild.Response,
74-
value: factory.Namespace.create({
75-
export: true,
76-
name: Name.ComponentChild.Response,
77-
statements: [],
78-
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
79-
}),
80-
statements: {},
68+
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
8169
});
8270

8371
Object.entries(responses).forEach(([statusCode, response]) => {

0 commit comments

Comments
 (0)