|
| 1 | +import {Specification} from "../specification/src/api-specification"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import * as Domain from "elasticsearch-client-specification/src/domain"; |
| 5 | + |
| 6 | +export class FlightRecorderJsonGenerator { |
| 7 | + constructor(private specification: Specification) { } |
| 8 | + |
| 9 | + export(folder: string) { |
| 10 | + |
| 11 | + const f = path.join(__dirname, folder); |
| 12 | + this.specification.endpoints.forEach(api => { |
| 13 | + const pathPrefix = path.join(f, api.name); |
| 14 | + const request = this.toType(api.typeMapping.request); |
| 15 | + fs.writeFileSync(pathPrefix + "_request.json", JSON.stringify(request, null, 2)); |
| 16 | + const response = this.toType(api.typeMapping.response); |
| 17 | + fs.writeFileSync(pathPrefix + "_response.json", JSON.stringify(response, null, 2)); |
| 18 | + }); |
| 19 | + |
| 20 | + } |
| 21 | + private toType(typeName: string) { |
| 22 | + const type = this.specification.typeLookup[typeName]; |
| 23 | + return this.toSchema(type); |
| 24 | + } |
| 25 | + |
| 26 | + private dispatchInterface(i: Domain.Interface) { |
| 27 | + if (i.inheritsFromUnresolved.some(t => t === "String")) return "__" + i.name + "__"; |
| 28 | + |
| 29 | + switch (i.name) { |
| 30 | + case "Uri" : return "__uri__"; |
| 31 | + case "Date" : return "__date__"; |
| 32 | + case "TimeSpan" : return "__duration__"; |
| 33 | + case "SourceDocument" : return "__source__"; |
| 34 | + case "short" : |
| 35 | + case "byte" : |
| 36 | + case "integer" : |
| 37 | + case "long" : |
| 38 | + case "float" : |
| 39 | + case "double" : |
| 40 | + return i.name; |
| 41 | + } |
| 42 | + return i.properties.reduce((o, p) => ({...o, [p.name]: this.createInterfaceProperty(p)}), {}); |
| 43 | + } |
| 44 | + |
| 45 | + private createEnumSchema(enumType: Domain.Enum) { |
| 46 | + return { |
| 47 | + type: "string", |
| 48 | + description: enumType.flags ? "flags" : null, |
| 49 | + enum: enumType.members.map(e => e.name) |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + private createTypeSchema(type: Domain.Type) { |
| 54 | + if (type.name === "boolean") return "boolean"; |
| 55 | + if (type.name === "string") return "string"; |
| 56 | + |
| 57 | + const i = this.specification.typeLookup[type.name]; |
| 58 | + return {} |
| 59 | + // return this.toSchema(i); |
| 60 | + } |
| 61 | + |
| 62 | + private createArraySchema(arr: Domain.ArrayOf) { |
| 63 | + return [this.dispatchInstanceOf(arr.of)]; |
| 64 | + } |
| 65 | + |
| 66 | + private createDictionarySchema(dict: Domain.Dictionary) { |
| 67 | + // todo handle additionalProperties and find out how we can type the key. |
| 68 | + return { __name__ : this.dispatchInstanceOf(dict.value) }; |
| 69 | + } |
| 70 | + |
| 71 | + private createInterfaceProperty(property: Domain.InterfaceProperty) { |
| 72 | + return this.dispatchInstanceOf(property.type); |
| 73 | + } |
| 74 | + |
| 75 | + private createUnionOfSchema(union: Domain.UnionOf) { |
| 76 | + // union should be oneOf but open api does not support the full json-schema draft 4 |
| 77 | + return { __anyOf__ : [ |
| 78 | + union.items.map(i => this.dispatchInstanceOf(i)) |
| 79 | + ] }; |
| 80 | + } |
| 81 | + |
| 82 | + private dispatchInstanceOf(type: Domain.InstanceOf) { |
| 83 | + if (type instanceof Domain.Dictionary) return this.createDictionarySchema(type); |
| 84 | + if (type instanceof Domain.UnionOf) return this.createUnionOfSchema(type); |
| 85 | + if (type instanceof Domain.Type) return this.createTypeSchema(type); |
| 86 | + if (type instanceof Domain.ArrayOf) return this.createArraySchema(type); |
| 87 | + return { type: "object", description: "Unknown InstanceOf" }; |
| 88 | + } |
| 89 | + private toSchema(type: Domain.TypeDeclaration) { |
| 90 | + if (type instanceof Domain.Enum) return this.createEnumSchema(type); |
| 91 | + if (type instanceof Domain.Interface) return this.dispatchInterface(type); |
| 92 | + return { type: "object", description: "Unknown TypeDeclaration" }; |
| 93 | + } |
| 94 | +} |
0 commit comments