-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.main.ts
217 lines (184 loc) · 6.14 KB
/
example.main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import ts from "typescript";
import { Parser } from "xml2js";
import { readFileSync, writeFileSync } from "fs";
const sourceFile = ts.createSourceFile(
"soap-types.ts", // the output file name
"", // the text of the source code, not needed for our purposes
ts.ScriptTarget.Latest, // the target language version for the output file
false,
ts.ScriptKind.TS // output script kind. options include JS, TS, JSX, TSX and others
);
// Primitive datatypes defined by SOAP (there are more)
type SoapPrimitive =
| "xsd:boolean"
| "xsd:double"
| "xsd:float"
| "xsd:int"
| "xsd:short"
| "xsd:signedInt"
| "xsd:string"
| "xsd:unsignedInt"
| "xsd:unsignedShort"
| "xsd:dateTime";
// SOAP message type
interface IMessage {
meta: {
name: string;
};
part: {
meta: {
name: string;
type: SoapPrimitive;
minOccurs?: string; // default = 1, making the field required. 0 notes an optional field
};
}[];
}
interface IOperation {
meta: {
name: string;
};
input: {
meta: {
name: string;
};
};
output: {
meta: {
name: string;
};
};
}
// Top-level WSDL object structure
interface IServiceDefinition {
definitions: {
message: IMessage[];
portType: {
operation: IOperation[];
}[];
};
}
async function readWSDL(filePath: string): Promise<IServiceDefinition> {
const wsdlData = readFileSync(filePath, { encoding: "utf-8" });
const xmlParser = new Parser({
attrkey: "meta", // instructs the parser to pack XML node attributes into a sub-object titled "meta"
});
const serviceDefinition = await xmlParser.parseStringPromise(wsdlData);
console.log(JSON.stringify(serviceDefinition, null, 2));
// I would recommend a more explicit conversion, but for the sake of brevity, this example just casts the object to the interface type.
return serviceDefinition as IServiceDefinition;
}
const dataTypes: Record<SoapPrimitive, string> = {
"xsd:boolean": "Boolean",
"xsd:double": "Number",
"xsd:float": "Number",
"xsd:int": "Number",
"xsd:string": "String",
"xsd:short": "Number",
"xsd:signedInt": "Number",
"xsd:unsignedInt": "Number",
"xsd:dateTime": "Date",
"xsd:unsignedShort": "Number",
};
// Convert from SOAP primitive type to a Typescript type reference, defaulting to String
function typeFromSOAP(soapType: SoapPrimitive): ts.TypeReferenceNode {
const typeName = dataTypes[soapType] ?? "String";
return ts.factory.createTypeReferenceNode(typeName, []);
}
// used for marking attributes as optional
const optionalFieldMarker = ts.factory.createToken(ts.SyntaxKind.QuestionToken);
// used for adding `export` directive to generated type
const exportModifier = ts.factory.createModifiersFromModifierFlags(
ts.ModifierFlags.Export
);
function generateMessageInterface(message: IMessage): ts.InterfaceDeclaration {
// name of the interface
const interfaceSymbol = ts.factory.createIdentifier(message.meta.name);
const interfaceMembers = message.part.map((part) => {
const propertySymbol = ts.factory.createIdentifier(part.meta.name);
// WSDL treats fields with no minOccurs as required, and fields with minOccurs > 0 as required
const required =
!Boolean(part.meta.minOccurs) || Number(part.meta.minOccurs) > 0;
// representation of the interface property syntax
const member = ts.factory.createPropertySignature(
undefined, // modifiers (not allowed for interfaces)
propertySymbol, // name of the property
required ? undefined : optionalFieldMarker,
typeFromSOAP(part.meta.type) // data type
);
return member;
});
const cl = ts.factory.createInterfaceDeclaration(
undefined, // no decorators
exportModifier, // modifiers
interfaceSymbol, // interface name
undefined, // no generic type parameters
undefined, // no heritage clauses (extends, implements)
interfaceMembers // interface attributes
);
return cl;
}
// used for adding `async` and `export` modifier to generated function
const asyncExportModifier = ts.factory.createModifiersFromModifierFlags(
ts.ModifierFlags.Export | ts.ModifierFlags.Async
);
function generateOperationFunction(
methodName: string,
inputType: ts.InterfaceDeclaration,
outputType: ts.InterfaceDeclaration
): ts.FunctionDeclaration {
const functionIdentifier = ts.factory.createIdentifier(methodName);
const inputTypeNode = ts.factory.createTypeReferenceNode(inputType.name);
const outputTypeNode = ts.factory.createTypeReferenceNode(outputType.name);
const outputTypePromiseNode = ts.factory.createTypeReferenceNode("Promise", [
outputTypeNode,
]);
const inputParameter = ts.factory.createParameterDeclaration(
undefined, // decorators,
undefined, // modifiers,
undefined, // spread operator
"input", // name
undefined, // optional marker,
inputTypeNode, // type
undefined // initializer
);
const fn = ts.factory.createFunctionDeclaration(
undefined, // decorators
asyncExportModifier,
undefined, // asterisk token
functionIdentifier,
undefined, // generic type parameters
[inputParameter], // arguments
outputTypePromiseNode, // return type
undefined // function body
);
return fn;
}
async function main() {
// read service definition into object
const serviceDefinition = await readWSDL("quote_service.wsdl");
console.log(serviceDefinition.definitions.portType[0].operation);
// create interface definitions
const messageInterfaces = serviceDefinition.definitions.message.map(
generateMessageInterface
);
const operationFunctions =
serviceDefinition.definitions.portType[0].operation.map((op) =>
generateOperationFunction(
op.meta.name,
messageInterfaces[0],
messageInterfaces[1]
)
);
const nodes: ts.Node[] = [...messageInterfaces, ...operationFunctions];
const nodeArr = ts.factory.createNodeArray(nodes);
// printer for writing the AST to a file as code
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const result = printer.printList(
ts.ListFormat.MultiLine,
nodeArr,
sourceFile
);
// write the code to file
writeFileSync("soap-types.ts", result, { encoding: "utf-8" });
}
main();