@odata2ts/odata2ts 0.33.1 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/lib/FactoryFunctionModel.d.ts +3 -3
- package/lib/FactoryFunctionModel.js.map +1 -1
- package/lib/OptionModel.d.ts +9 -0
- package/lib/OptionModel.js.map +1 -1
- package/lib/app.js +18 -8
- package/lib/app.js.map +1 -1
- package/lib/data-model/DataModel.d.ts +12 -6
- package/lib/data-model/DataModel.js +10 -0
- package/lib/data-model/DataModel.js.map +1 -1
- package/lib/data-model/DataModelDigestion.js +37 -20
- package/lib/data-model/DataModelDigestion.js.map +1 -1
- package/lib/data-model/DataModelDigestionV2.js +1 -1
- package/lib/data-model/DataModelDigestionV2.js.map +1 -1
- package/lib/data-model/DataModelDigestionV4.js +1 -10
- package/lib/data-model/DataModelDigestionV4.js.map +1 -1
- package/lib/data-model/DataTypeModel.d.ts +17 -7
- package/lib/data-model/DataTypeModel.js.map +1 -1
- package/lib/data-model/NamingHelper.d.ts +1 -0
- package/lib/data-model/NamingHelper.js +4 -1
- package/lib/data-model/NamingHelper.js.map +1 -1
- package/lib/data-model/ServiceConfigHelper.js +2 -2
- package/lib/data-model/ServiceConfigHelper.js.map +1 -1
- package/lib/data-model/validation/NameClashValidator.d.ts +23 -0
- package/lib/data-model/validation/NameClashValidator.js +89 -0
- package/lib/data-model/validation/NameClashValidator.js.map +1 -0
- package/lib/data-model/validation/NameValidator.d.ts +6 -17
- package/lib/data-model/validation/NameValidator.js +0 -77
- package/lib/data-model/validation/NameValidator.js.map +1 -1
- package/lib/data-model/validation/NoopValidator.d.ts +12 -0
- package/lib/data-model/validation/NoopValidator.js +34 -0
- package/lib/data-model/validation/NoopValidator.js.map +1 -0
- package/lib/defaultConfig.js +1 -0
- package/lib/defaultConfig.js.map +1 -1
- package/lib/generator/ImportContainer.d.ts +44 -11
- package/lib/generator/ImportContainer.js +150 -47
- package/lib/generator/ImportContainer.js.map +1 -1
- package/lib/generator/ImportedNameValidator.d.ts +5 -0
- package/lib/generator/ImportedNameValidator.js +26 -0
- package/lib/generator/ImportedNameValidator.js.map +1 -0
- package/lib/generator/ModelGenerator.js +113 -72
- package/lib/generator/ModelGenerator.js.map +1 -1
- package/lib/generator/QueryObjectGenerator.js +134 -107
- package/lib/generator/QueryObjectGenerator.js.map +1 -1
- package/lib/generator/ServiceGenerator.d.ts +1 -1
- package/lib/generator/ServiceGenerator.js +160 -161
- package/lib/generator/ServiceGenerator.js.map +1 -1
- package/lib/generator/import/ImportObjects.d.ts +66 -0
- package/lib/generator/import/ImportObjects.js +83 -0
- package/lib/generator/import/ImportObjects.js.map +1 -0
- package/lib/project/FileHandler.d.ts +28 -0
- package/lib/project/FileHandler.js +66 -0
- package/lib/project/FileHandler.js.map +1 -0
- package/lib/project/ProjectManager.d.ts +44 -18
- package/lib/project/ProjectManager.js +156 -111
- package/lib/project/ProjectManager.js.map +1 -1
- package/lib/project/TsMorphHelper.d.ts +11 -0
- package/lib/project/TsMorphHelper.js +66 -0
- package/lib/project/TsMorphHelper.js.map +1 -0
- package/package.json +10 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ModelGenerator.js","sourceRoot":"","sources":["../../src/generator/ModelGenerator.ts"],"names":[],"mappings":";;;AACA,uCAAmF;AAMnF,uDAAoD;AAE7C,MAAM,cAAc,GAAiC,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,EAAE;IACpH,MAAM,SAAS,GAAG,IAAI,cAAc,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;IAC5F,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC;AAC9B,CAAC,CAAC;AAHW,QAAA,cAAc,kBAGzB;AAEF,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAE3C,MAAM,cAAc;IAGlB,YACU,SA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CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;gBACzC,OAAO;oBACL,IAAI,EAAE,CAAC,CAAC,IAAI;oBACZ,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;oBACzB,gBAAgB,EAAE,CAAC,CAAC,CAAC,QAAQ;iBAC9B,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { ODataVersions } from \"@odata2ts/odata-core\";\nimport { JSDocStructure, OptionalKind, SourceFile, StructureKind } from \"ts-morph\";\n\nimport { DataModel } from \"../data-model/DataModel\";\nimport { ComplexType, DataTypes, ModelType, OperationType, PropertyModel } from \"../data-model/DataTypeModel\";\nimport { NamingHelper } from \"../data-model/NamingHelper\";\nimport { EntityBasedGeneratorFunction, GeneratorFunctionOptions } from \"../FactoryFunctionModel\";\nimport { ImportContainer } from \"./ImportContainer\";\n\nexport const generateModels: EntityBasedGeneratorFunction = (dataModel, sourceFile, version, options, namingHelper) => {\n const generator = new ModelGenerator(dataModel, sourceFile, version, options, namingHelper);\n return generator.generate();\n};\n\nconst DEFERRED_CONTENT = \"DeferredContent\";\n\nclass ModelGenerator {\n private importContainer!: ImportContainer;\n\n constructor(\n private dataModel: DataModel,\n private sourceFile: SourceFile,\n private version: ODataVersions,\n private options: GeneratorFunctionOptions,\n private namingHelper: NamingHelper\n ) {}\n\n public generate(): void {\n this.importContainer = new ImportContainer(this.namingHelper.getFileNames());\n\n this.generateEnums();\n this.generateModels();\n if (!this.options.skipOperations) {\n this.generateUnboundOperationParams();\n }\n }\n\n private generateEnums() {\n this.dataModel.getEnums().forEach((et) => {\n this.sourceFile.addEnum({\n name: et.name,\n isExported: true,\n members: et.members.map((mem) => ({ name: mem, initializer: `\"${mem}\"` })),\n });\n });\n }\n\n private generateModels() {\n this.dataModel.getEntityTypes().forEach((model) => {\n this.generateModel(model);\n if (!this.options.skipIdModels && model.generateId) {\n this.generateIdModel(model);\n }\n if (!this.options.skipEditableModels && !model.abstract) {\n this.generateEditableModel(model);\n }\n if (!this.options.skipOperations) {\n this.generateBoundOperationParams(model.fqName);\n }\n });\n this.dataModel.getComplexTypes().forEach((model) => {\n this.generateModel(model);\n if (!this.options.skipEditableModels && !model.abstract) {\n this.generateEditableModel(model);\n }\n });\n\n this.sourceFile.addImportDeclarations(this.importContainer.getImportDeclarations());\n }\n\n private generateModel(model: ComplexType | ModelType) {\n this.sourceFile.addInterface({\n name: model.name,\n isExported: true,\n properties: model.props.map((p) => {\n const isEntity = p.dataType == DataTypes.ModelType;\n return {\n name: p.name,\n type: this.getPropType(p),\n // props for entities or entity collections are not added in V4 if not explicitly expanded\n hasQuestionToken: this.dataModel.isV4() && isEntity,\n docs: this.options.skipComments ? undefined : [this.generatePropDoc(p, model)],\n };\n }),\n extends: model.finalBaseClass ? [this.namingHelper.getModelName(model.finalBaseClass)] : undefined,\n });\n }\n\n private generatePropDoc(prop: PropertyModel, model: ComplexType | ModelType): OptionalKind<JSDocStructure> {\n const isKeyProp = (model as ModelType).keyNames?.includes(prop.odataName);\n const baseAttribs: Array<string> = [];\n if (isKeyProp) {\n baseAttribs.push(\"**Key Property**: This is a key property used to identify the entity.\");\n }\n if (prop.managed) {\n baseAttribs.push(\"**Managed**: This property is managed on the server side and cannot be edited.\");\n }\n if (prop.converters?.length) {\n baseAttribs.push(`**Applied Converters**: ${prop.converters.map((c) => c.converterId).join(\",\")}.`);\n }\n\n const attributeTable: Array<[string, string]> = [\n [\"Name\", prop.odataName],\n [\"Type\", prop.odataType],\n ];\n if (prop.required) {\n attributeTable.push([\"Nullable\", \"false\"]);\n }\n\n const description =\n (baseAttribs ? baseAttribs.join(\"<br/>\") + \"\\n\\n\" : \"\") +\n \"OData Attributes:\\n\" +\n \"|Attribute Name | Attribute Value |\\n| --- | ---|\\n\" +\n attributeTable.map((row) => `| ${row[0]} | \\`${row[1]}\\` |`).join(\"\\n\");\n\n return { kind: StructureKind.JSDoc, description };\n }\n\n private getPropType(prop: PropertyModel): string {\n const isEntity = prop.dataType == DataTypes.ModelType;\n\n // V2 entity special: deferred content\n let suffix = \"\";\n if (isEntity && this.dataModel.isV2()) {\n this.importContainer.addFromCore(DEFERRED_CONTENT);\n suffix = ` | ${DEFERRED_CONTENT}`;\n }\n // custom types which require type imports => possible via converters\n else if (prop.typeModule) {\n this.importContainer.addCustomType(prop.typeModule, prop.type);\n }\n\n // Collections\n if (prop.isCollection) {\n const type = `Array<${prop.type}>`;\n if (this.dataModel.isV2() && this.options.v2ModelsWithExtraResultsWrapping) {\n return `{ results: ${type} }` + suffix;\n } else {\n return type + suffix;\n }\n }\n\n // primitive, enum & complex types\n return prop.type + (prop.required ? \"\" : \" | null\") + suffix;\n }\n\n private generateIdModel(model: ModelType) {\n const singleType = model.keys.length === 1 ? `${model.keys[0].type} | ` : \"\";\n const keyTypes = model.keys.map((keyProp) => `${keyProp.name}: ${this.getPropType(keyProp)}`).join(\",\");\n const type = `${singleType}{${keyTypes}}`;\n\n this.sourceFile.addTypeAlias({\n name: model.idModelName,\n isExported: true,\n type,\n });\n }\n\n private generateEditableModel(model: ComplexType) {\n const entityTypes = [DataTypes.ModelType, DataTypes.ComplexType];\n const allProps = [...model.baseProps, ...model.props].filter((p) => !p.managed);\n\n const requiredProps = allProps\n .filter((p) => p.required && !entityTypes.includes(p.dataType))\n .map((p) => `\"${p.name}\"`)\n .join(\" | \");\n const optionalProps = allProps\n .filter((p) => !p.required && !entityTypes.includes(p.dataType))\n .map((p) => `\"${p.name}\"`)\n .join(\" | \");\n const complexProps = allProps.filter((p) => p.dataType === DataTypes.ComplexType);\n\n const extendsClause = [\n requiredProps ? `Pick<${model.name}, ${requiredProps}>` : null,\n optionalProps ? `Partial<Pick<${model.name}, ${optionalProps}>>` : null,\n ].filter((e): e is string => !!e);\n\n this.sourceFile.addInterface({\n name: model.editableName,\n isExported: true,\n extends: extendsClause,\n properties: !complexProps\n ? undefined\n : complexProps.map((p) => {\n return {\n name: p.name,\n type: this.getEditablePropType(p),\n // optional props don't need to be specified in editable model\n // also, entities would require deep insert func => we make it optional for now\n hasQuestionToken: !p.required || p.dataType === DataTypes.ModelType,\n };\n }),\n });\n }\n\n private getEditablePropType(prop: PropertyModel): string {\n const type =\n prop.dataType === DataTypes.ModelType\n ? this.dataModel.getEntityType(prop.fqType)!.editableName\n : prop.dataType === DataTypes.ComplexType\n ? this.dataModel.getComplexType(prop.fqType)!.editableName\n : prop.type;\n\n // Collections\n if (prop.isCollection) {\n return `Array<${type}>`;\n }\n\n // primitive, enum & complex types\n return type + (prop.required ? \"\" : \" | null\");\n }\n\n private generateUnboundOperationParams() {\n this.dataModel.getUnboundOperationTypes().forEach((operation) => {\n this.generateOperationParams(operation);\n });\n }\n\n private generateBoundOperationParams(entityName: string) {\n [\n ...this.dataModel.getEntityTypeOperations(entityName),\n ...this.dataModel.getEntitySetOperations(entityName),\n ].forEach((operation) => {\n this.generateOperationParams(operation);\n });\n }\n\n private generateOperationParams(operation: OperationType) {\n if (!operation.parameters.length) {\n return;\n }\n this.sourceFile.addInterface({\n name: operation.paramsModelName,\n isExported: true,\n properties: operation.parameters.map((p) => {\n return {\n name: p.name,\n type: this.getPropType(p),\n hasQuestionToken: !p.required,\n };\n }),\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ModelGenerator.js","sourceRoot":"","sources":["../../src/generator/ModelGenerator.ts"],"names":[],"mappings":";;;;AACA,uCAAuE;AAQvE,0DAAqD;AAG9C,MAAM,cAAc,GAAiC,CAC1D,OAAuB,EACvB,SAAS,EACT,OAAO,EACP,OAAO,EACP,YAAY,EACZ,EAAE;IACF,MAAM,SAAS,GAAG,IAAI,cAAc,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;IACzF,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC;AAC9B,CAAC,CAAC;AATW,QAAA,cAAc,kBASzB;AAEF,MAAM,cAAc;IAClB,YACU,OAAuB,EACvB,SA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,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC;YACjE,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,wBAAwB,CAAC,aAAa,CAAC,CAAC;YAElE,UAAU,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;gBAC/B,IAAI,CAAC,uBAAuB,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;YAChD,CAAC,CAAC,CAAC;QACL,CAAC;KAAA;IAEO,uBAAuB,CAAC,IAAiB,EAAE,SAAwB;QACzE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,EAAE;YAChC,OAAO;SACR;QACD,IAAI,CAAC,OAAO,EAAE,CAAC,YAAY,CAAC;YAC1B,IAAI,EAAE,SAAS,CAAC,eAAe;YAC/B,UAAU,EAAE,IAAI;YAChB,UAAU,EAAE,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;gBACzC,OAAO;oBACL,IAAI,EAAE,CAAC,CAAC,IAAI;oBACZ,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;oBAC5C,gBAAgB,EAAE,CAAC,CAAC,CAAC,QAAQ;iBAC9B,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { ODataVersions } from \"@odata2ts/odata-core\";\r\nimport { JSDocStructure, OptionalKind, StructureKind } from \"ts-morph\";\r\n\r\nimport { DataModel } from \"../data-model/DataModel\";\r\nimport { ComplexType, DataTypes, EntityType, OperationType, PropertyModel } from \"../data-model/DataTypeModel\";\r\nimport { NamingHelper } from \"../data-model/NamingHelper\";\r\nimport { EntityBasedGeneratorFunction, GeneratorFunctionOptions } from \"../FactoryFunctionModel\";\r\nimport { FileHandler } from \"../project/FileHandler\";\r\nimport { ProjectManager } from \"../project/ProjectManager\";\r\nimport { CoreImports } from \"./import/ImportObjects\";\r\nimport { ImportContainer } from \"./ImportContainer\";\r\n\r\nexport const generateModels: EntityBasedGeneratorFunction = (\r\n project: ProjectManager,\r\n dataModel,\r\n version,\r\n options,\r\n namingHelper\r\n) => {\r\n const generator = new ModelGenerator(project, dataModel, version, options, namingHelper);\r\n return generator.generate();\r\n};\r\n\r\nclass ModelGenerator {\r\n constructor(\r\n private project: ProjectManager,\r\n private dataModel: DataModel,\r\n private version: ODataVersions,\r\n private options: GeneratorFunctionOptions,\r\n private namingHelper: NamingHelper\r\n ) {}\r\n\r\n public async generate(): Promise<void> {\r\n this.project.initModels();\r\n\r\n const promises: Array<Promise<void>> = [\r\n ...this.generateEnums(),\r\n ...this.generateEntityTypeModels(),\r\n ...this.generateComplexTypeModels(),\r\n ];\r\n\r\n if (!this.options.skipOperations) {\r\n promises.push(this.generateUnboundOperationParams());\r\n }\r\n\r\n await Promise.all(promises);\r\n\r\n return this.project.finalizeModels();\r\n }\r\n\r\n private generateEnums() {\r\n return this.dataModel.getEnums().map((et) => {\r\n const file = this.project.createOrGetModelFile(et.folderPath, et.modelName);\r\n\r\n file.getFile().addEnum({\r\n name: et.modelName,\r\n isExported: true,\r\n members: et.members.map((mem) => ({ name: mem, initializer: `\"${mem}\"` })),\r\n });\r\n\r\n return this.project.finalizeFile(file);\r\n });\r\n }\r\n\r\n private generateEntityTypeModels() {\r\n return this.dataModel.getEntityTypes().map((model) => {\r\n const file = this.project.createOrGetModelFile(model.folderPath, model.modelName, [\r\n model.modelName,\r\n model.id.modelName,\r\n model.editableName,\r\n ]);\r\n\r\n // query model\r\n this.generateModel(file, model);\r\n\r\n // key model\r\n if (!this.options.skipIdModels && model.generateId) {\r\n this.generateIdModel(file, model);\r\n }\r\n\r\n // editable model\r\n if (!this.options.skipEditableModels) {\r\n this.generateEditableModel(file, model);\r\n }\r\n\r\n // param models for bound operations\r\n if (!this.options.skipOperations) {\r\n [\r\n ...this.dataModel.getEntityTypeOperations(model.fqName),\r\n ...this.dataModel.getEntitySetOperations(model.fqName),\r\n ].forEach((operation) => {\r\n this.generateOperationParams(file, operation);\r\n });\r\n }\r\n\r\n return this.project.finalizeFile(file);\r\n });\r\n }\r\n\r\n private generateComplexTypeModels() {\r\n return this.dataModel.getComplexTypes().map((model) => {\r\n const file = this.project.createOrGetModelFile(model.folderPath, model.modelName, [\r\n model.modelName,\r\n model.editableName,\r\n ]);\r\n\r\n // query model\r\n this.generateModel(file, model);\r\n\r\n // editable model\r\n if (!this.options.skipEditableModels) {\r\n this.generateEditableModel(file, model);\r\n }\r\n\r\n return this.project.finalizeFile(file);\r\n });\r\n }\r\n\r\n private generateModel(file: FileHandler, model: ComplexType | EntityType) {\r\n const imports = file.getImports();\r\n let extendsClause = undefined;\r\n if (model.finalBaseClass) {\r\n const modelName = imports.addGeneratedModel(\r\n model.baseClasses[0],\r\n this.namingHelper.getModelName(model.finalBaseClass)\r\n );\r\n extendsClause = [modelName];\r\n }\r\n\r\n file.getFile().addInterface({\r\n name: model.modelName,\r\n isExported: true,\r\n properties: model.props.map((p) => {\r\n const isEntity = p.dataType == DataTypes.ModelType;\r\n return {\r\n name: p.name,\r\n type: this.getPropType(file.getImports(), p),\r\n // props for entities or entity collections are not added in V4 if not explicitly expanded\r\n hasQuestionToken: this.dataModel.isV4() && isEntity,\r\n docs: this.options.skipComments ? undefined : [this.generatePropDoc(p, model)],\r\n };\r\n }),\r\n extends: extendsClause,\r\n });\r\n }\r\n\r\n private generatePropDoc(prop: PropertyModel, model: ComplexType | EntityType): OptionalKind<JSDocStructure> {\r\n const isKeyProp = (model as EntityType).keyNames?.includes(prop.odataName);\r\n const baseAttribs: Array<string> = [];\r\n if (isKeyProp) {\r\n baseAttribs.push(\"**Key Property**: This is a key property used to identify the entity.\");\r\n }\r\n if (prop.managed) {\r\n baseAttribs.push(\"**Managed**: This property is managed on the server side and cannot be edited.\");\r\n }\r\n if (prop.converters?.length) {\r\n baseAttribs.push(`**Applied Converters**: ${prop.converters.map((c) => c.converterId).join(\",\")}.`);\r\n }\r\n\r\n const attributeTable: Array<[string, string]> = [\r\n [\"Name\", prop.odataName],\r\n [\"Type\", prop.odataType],\r\n ];\r\n if (prop.required) {\r\n attributeTable.push([\"Nullable\", \"false\"]);\r\n }\r\n\r\n const description =\r\n (baseAttribs ? baseAttribs.join(\"<br/>\") + \"\\n\\n\" : \"\") +\r\n \"OData Attributes:\\n\" +\r\n \"|Attribute Name | Attribute Value |\\n| --- | ---|\\n\" +\r\n attributeTable.map((row) => `| ${row[0]} | \\`${row[1]}\\` |`).join(\"\\n\");\r\n\r\n return { kind: StructureKind.JSDoc, description };\r\n }\r\n\r\n private getPropType(imports: ImportContainer, prop: PropertyModel): string {\r\n // V2 entity special: deferred content\r\n let suffix = \"\";\r\n if (this.dataModel.isV2() && prop.dataType == DataTypes.ModelType) {\r\n const [defContent] = imports.addCoreLib(this.version, CoreImports.DeferredContent);\r\n suffix = ` | ${defContent}`;\r\n }\r\n\r\n let typeName: string;\r\n if (prop.dataType === DataTypes.PrimitiveType) {\r\n // custom types which require type imports => possible via converters\r\n typeName = prop.typeModule ? imports.addCustomType(prop.typeModule, prop.type) : prop.type;\r\n } else {\r\n typeName = imports.addGeneratedModel(prop.fqType, prop.type);\r\n }\r\n\r\n // Collections\r\n if (prop.isCollection) {\r\n const type = `Array<${typeName}>`;\r\n if (this.dataModel.isV2() && this.options.v2ModelsWithExtraResultsWrapping) {\r\n return `{ results: ${type} }` + suffix;\r\n } else {\r\n return type + suffix;\r\n }\r\n }\r\n\r\n // primitive, enum & complex types\r\n return typeName + (prop.required ? \"\" : \" | null\") + suffix;\r\n }\r\n\r\n private generateIdModel(file: FileHandler, model: EntityType) {\r\n const singleType = model.keys.length === 1 ? `${model.keys[0].type} | ` : \"\";\r\n const keyTypes = model.keys\r\n .map((keyProp) => `${keyProp.name}: ${this.getPropType(file.getImports(), keyProp)}`)\r\n .join(\",\");\r\n const type = `${singleType}{${keyTypes}}`;\r\n\r\n file.getFile().addTypeAlias({\r\n name: model.id.modelName,\r\n isExported: true,\r\n type,\r\n });\r\n }\r\n\r\n private generateEditableModel(file: FileHandler, model: ComplexType) {\r\n const entityTypes = [DataTypes.ModelType, DataTypes.ComplexType];\r\n const allProps = [...model.baseProps, ...model.props].filter((p) => !p.managed);\r\n\r\n const requiredProps = allProps\r\n .filter((p) => p.required && !entityTypes.includes(p.dataType))\r\n .map((p) => `\"${p.name}\"`)\r\n .join(\" | \");\r\n const optionalProps = allProps\r\n .filter((p) => !p.required && !entityTypes.includes(p.dataType))\r\n .map((p) => `\"${p.name}\"`)\r\n .join(\" | \");\r\n const complexProps = allProps.filter((p) => p.dataType === DataTypes.ComplexType);\r\n\r\n const extendsClause = [\r\n requiredProps ? `Pick<${model.modelName}, ${requiredProps}>` : null,\r\n optionalProps ? `Partial<Pick<${model.modelName}, ${optionalProps}>>` : null,\r\n ].filter((e): e is string => !!e);\r\n\r\n file.getFile().addInterface({\r\n name: model.editableName,\r\n isExported: true,\r\n extends: extendsClause,\r\n properties: !complexProps\r\n ? undefined\r\n : complexProps.map((p) => {\r\n return {\r\n name: p.name,\r\n type: this.getEditablePropType(file.getImports(), p),\r\n // optional props don't need to be specified in editable model\r\n // also, entities would require deep insert func => we make it optional for now\r\n hasQuestionToken: !p.required || p.dataType === DataTypes.ModelType,\r\n };\r\n }),\r\n });\r\n }\r\n\r\n private getEditablePropType(imports: ImportContainer, prop: PropertyModel): string {\r\n const isModelType = [DataTypes.ModelType, DataTypes.ComplexType].includes(prop.dataType);\r\n\r\n let editableType = prop.type;\r\n if (isModelType) {\r\n const editName = this.dataModel.getComplexType(prop.fqType)!.editableName;\r\n editableType = imports.addGeneratedModel(prop.fqType, editName);\r\n }\r\n\r\n // Collections\r\n if (prop.isCollection) {\r\n return `Array<${editableType}>`;\r\n }\r\n\r\n // primitive, enum & complex types\r\n return editableType + (prop.required ? \"\" : \" | null\");\r\n }\r\n\r\n private async generateUnboundOperationParams() {\r\n const unboundOps = this.dataModel.getUnboundOperationTypes();\r\n const reservedNames = unboundOps.map((op) => op.paramsModelName);\r\n const file = this.project.createOrGetMainModelFile(reservedNames);\r\n\r\n unboundOps.forEach((operation) => {\r\n this.generateOperationParams(file, operation);\r\n });\r\n }\r\n\r\n private generateOperationParams(file: FileHandler, operation: OperationType) {\r\n if (!operation.parameters.length) {\r\n return;\r\n }\r\n file.getFile().addInterface({\r\n name: operation.paramsModelName,\r\n isExported: true,\r\n properties: operation.parameters.map((p) => {\r\n return {\r\n name: p.name,\r\n type: this.getPropType(file.getImports(), p),\r\n hasQuestionToken: !p.required,\r\n };\r\n }),\r\n });\r\n }\r\n}\r\n"]}
|
|
@@ -1,79 +1,105 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateQueryObjects = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const odata_core_1 = require("@odata2ts/odata-core");
|
|
5
6
|
const ts_morph_1 = require("ts-morph");
|
|
6
7
|
const processors_1 = require("xml2js/lib/processors");
|
|
7
|
-
const
|
|
8
|
-
const generateQueryObjects = (
|
|
9
|
-
const generator = new QueryObjectGenerator(
|
|
8
|
+
const ImportObjects_1 = require("./import/ImportObjects");
|
|
9
|
+
const generateQueryObjects = (project, dataModel, version, options, namingHelper) => {
|
|
10
|
+
const generator = new QueryObjectGenerator(project, dataModel, version, options, namingHelper);
|
|
10
11
|
return generator.generate();
|
|
11
12
|
};
|
|
12
13
|
exports.generateQueryObjects = generateQueryObjects;
|
|
13
14
|
class QueryObjectGenerator {
|
|
14
|
-
constructor(
|
|
15
|
+
constructor(project, dataModel, version, options, namingHelper) {
|
|
16
|
+
this.project = project;
|
|
15
17
|
this.dataModel = dataModel;
|
|
16
|
-
this.sourceFile = sourceFile;
|
|
17
18
|
this.version = version;
|
|
18
19
|
this.options = options;
|
|
19
20
|
this.namingHelper = namingHelper;
|
|
20
21
|
}
|
|
21
22
|
generate() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
this.project.initQObjects();
|
|
25
|
+
// process EntityType & ComplexType
|
|
26
|
+
const promises = [...this.generateEntityTypes(), ...this.generateComplexTypes()];
|
|
27
|
+
if (!this.options.skipOperations) {
|
|
28
|
+
// process unbound operations
|
|
29
|
+
promises.push(this.generateUnboundOperations());
|
|
30
|
+
}
|
|
31
|
+
yield Promise.all(promises);
|
|
32
|
+
return this.project.finalizeQObjects();
|
|
33
|
+
});
|
|
28
34
|
}
|
|
29
|
-
|
|
30
|
-
this.dataModel.getEntityTypes().
|
|
31
|
-
this.
|
|
35
|
+
generateEntityTypes() {
|
|
36
|
+
return this.dataModel.getEntityTypes().map((model) => {
|
|
37
|
+
const file = this.project.createOrGetQObjectFile(model.folderPath, model.qName, [
|
|
38
|
+
model.qName,
|
|
39
|
+
(0, processors_1.firstCharLowerCase)(model.qName),
|
|
40
|
+
model.id.qName,
|
|
41
|
+
]);
|
|
42
|
+
// q object
|
|
43
|
+
this.generateModel(file, model);
|
|
44
|
+
// qId function
|
|
32
45
|
if (!this.options.skipIdModels && model.generateId) {
|
|
33
|
-
this.generateIdFunction(
|
|
46
|
+
this.generateIdFunction(file, model);
|
|
34
47
|
}
|
|
48
|
+
// bound q operations
|
|
35
49
|
if (!this.options.skipOperations) {
|
|
36
|
-
|
|
50
|
+
[
|
|
51
|
+
...this.dataModel.getEntityTypeOperations(model.fqName),
|
|
52
|
+
...this.dataModel.getEntitySetOperations(model.fqName),
|
|
53
|
+
].forEach((operation) => {
|
|
54
|
+
this.generateOperation(file, operation, model.fqName);
|
|
55
|
+
});
|
|
37
56
|
}
|
|
57
|
+
return this.project.finalizeFile(file);
|
|
38
58
|
});
|
|
39
|
-
|
|
40
|
-
|
|
59
|
+
}
|
|
60
|
+
generateComplexTypes() {
|
|
61
|
+
return this.dataModel.getComplexTypes().map((model) => {
|
|
62
|
+
const file = this.project.createOrGetQObjectFile(model.folderPath, model.qName, [
|
|
63
|
+
model.qName,
|
|
64
|
+
(0, processors_1.firstCharLowerCase)(model.qName),
|
|
65
|
+
]);
|
|
66
|
+
this.generateModel(file, model);
|
|
67
|
+
return this.project.finalizeFile(file);
|
|
41
68
|
});
|
|
42
|
-
if (this.dataModel.getEntityTypes().length || this.dataModel.getComplexTypes().length) {
|
|
43
|
-
importContainer.addFromQObject("QueryObject");
|
|
44
|
-
}
|
|
45
69
|
}
|
|
46
|
-
generateModel(
|
|
47
|
-
|
|
70
|
+
generateModel(file, model) {
|
|
71
|
+
const imports = file.getImports();
|
|
72
|
+
let extendsClause;
|
|
48
73
|
if (model.baseClasses.length) {
|
|
49
74
|
const baseClass = model.baseClasses[0];
|
|
50
|
-
const baseModel = this.dataModel.
|
|
75
|
+
const baseModel = this.dataModel.getModel(baseClass);
|
|
51
76
|
if (!baseModel) {
|
|
52
77
|
throw new Error(`Entity or complex type "${baseClass}" from baseClass attribute not found!`);
|
|
53
78
|
}
|
|
54
|
-
extendsClause = baseModel.qName;
|
|
79
|
+
extendsClause = imports.addGeneratedQObject(baseClass, baseModel.qName);
|
|
55
80
|
}
|
|
56
|
-
|
|
81
|
+
else {
|
|
82
|
+
[extendsClause] = imports.addQObject(ImportObjects_1.QueryObjectImports.QueryObject);
|
|
83
|
+
}
|
|
84
|
+
file.getFile().addClass({
|
|
57
85
|
name: model.qName,
|
|
58
86
|
isExported: true,
|
|
59
87
|
extends: extendsClause,
|
|
60
|
-
isAbstract: model.abstract,
|
|
61
|
-
properties: this.generateQueryObjectProps(model.props
|
|
88
|
+
// isAbstract: model.abstract,
|
|
89
|
+
properties: this.generateQueryObjectProps(file.getImports(), model.props),
|
|
90
|
+
});
|
|
91
|
+
file.getFile().addVariableStatement({
|
|
92
|
+
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
|
|
93
|
+
isExported: true,
|
|
94
|
+
declarations: [
|
|
95
|
+
{
|
|
96
|
+
name: (0, processors_1.firstCharLowerCase)(model.qName),
|
|
97
|
+
initializer: `new ${model.qName}()`,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
62
100
|
});
|
|
63
|
-
if (!model.abstract) {
|
|
64
|
-
this.sourceFile.addVariableStatement({
|
|
65
|
-
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
|
|
66
|
-
isExported: true,
|
|
67
|
-
declarations: [
|
|
68
|
-
{
|
|
69
|
-
name: (0, processors_1.firstCharLowerCase)(model.qName),
|
|
70
|
-
initializer: `new ${model.qName}()`,
|
|
71
|
-
},
|
|
72
|
-
],
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
101
|
}
|
|
76
|
-
generateQueryObjectProps(
|
|
102
|
+
generateQueryObjectProps(importContainer, props) {
|
|
77
103
|
return props.map((prop) => {
|
|
78
104
|
const { odataName } = prop;
|
|
79
105
|
const name = this.namingHelper.getQPropName(prop.name);
|
|
@@ -81,27 +107,24 @@ class QueryObjectGenerator {
|
|
|
81
107
|
let qPathInit;
|
|
82
108
|
// factor in collections
|
|
83
109
|
if (prop.isCollection) {
|
|
84
|
-
const cType =
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
qPathInit = `new ${
|
|
90
|
-
importContainer.addFromQObject(cType);
|
|
91
|
-
if (!isModelType) {
|
|
92
|
-
importContainer.addFromQObject(qObject);
|
|
93
|
-
}
|
|
110
|
+
const cType = isModelType ? ImportObjects_1.QueryObjectImports.QEntityCollectionPath : ImportObjects_1.QueryObjectImports.QCollectionPath;
|
|
111
|
+
const collectionType = importContainer.addQObject(cType);
|
|
112
|
+
const qObject = isModelType
|
|
113
|
+
? importContainer.addGeneratedQObject(prop.fqType, prop.qObject)
|
|
114
|
+
: importContainer.addFromQObject(prop.qObject)[0];
|
|
115
|
+
qPathInit = `new ${collectionType}(this.withPrefix("${odataName}"), () => ${qObject})`;
|
|
94
116
|
}
|
|
95
117
|
else {
|
|
118
|
+
// add import for data type
|
|
119
|
+
const qPath = importContainer.addFromQObject(prop.qPath);
|
|
96
120
|
if (isModelType) {
|
|
97
|
-
|
|
121
|
+
const qObject = importContainer.addGeneratedQObject(prop.fqType, prop.qObject);
|
|
122
|
+
qPathInit = `new ${qPath}(this.withPrefix("${odataName}"), () => ${qObject})`;
|
|
98
123
|
}
|
|
99
124
|
else {
|
|
100
|
-
let converterStmt = this.generateConverterStmt(prop.converters
|
|
101
|
-
qPathInit = `new ${
|
|
125
|
+
let converterStmt = this.generateConverterStmt(importContainer, prop.converters);
|
|
126
|
+
qPathInit = `new ${qPath}(this.withPrefix("${odataName}")${converterStmt ? `, ${converterStmt}` : ""})`;
|
|
102
127
|
}
|
|
103
|
-
// add import for data type
|
|
104
|
-
importContainer.addFromQObject(prop.qPath);
|
|
105
128
|
}
|
|
106
129
|
return {
|
|
107
130
|
name,
|
|
@@ -111,36 +134,36 @@ class QueryObjectGenerator {
|
|
|
111
134
|
};
|
|
112
135
|
});
|
|
113
136
|
}
|
|
114
|
-
generateConverterStmt(
|
|
137
|
+
generateConverterStmt(importContainer, converters) {
|
|
115
138
|
if (!(converters === null || converters === void 0 ? void 0 : converters.length)) {
|
|
116
|
-
return;
|
|
139
|
+
return undefined;
|
|
117
140
|
}
|
|
118
|
-
converters.
|
|
119
|
-
importContainer.addCustomType(converter.package, converter.converterId);
|
|
141
|
+
const converterIds = converters.map((converter) => {
|
|
142
|
+
return importContainer.addCustomType(converter.package, converter.converterId);
|
|
120
143
|
});
|
|
121
|
-
if (
|
|
122
|
-
return
|
|
144
|
+
if (converterIds.length === 1) {
|
|
145
|
+
return converterIds[0];
|
|
123
146
|
}
|
|
124
147
|
else {
|
|
125
|
-
importContainer.addCustomType("@odata2ts/converter-runtime", "createChain");
|
|
126
|
-
const [first, second, ...moreConverters] =
|
|
127
|
-
return moreConverters.reduce((stmt,
|
|
148
|
+
const createChain = importContainer.addCustomType("@odata2ts/converter-runtime", "createChain");
|
|
149
|
+
const [first, second, ...moreConverters] = converterIds;
|
|
150
|
+
return moreConverters.reduce((stmt, convId) => `${stmt}.chain(${convId})`, `${createChain}(${first}, ${second})`);
|
|
128
151
|
}
|
|
129
152
|
}
|
|
130
|
-
generateIdFunction(
|
|
131
|
-
const
|
|
132
|
-
importContainer.
|
|
133
|
-
importContainer.addGeneratedModel(model.
|
|
134
|
-
|
|
135
|
-
name: model.
|
|
153
|
+
generateIdFunction(file, model) {
|
|
154
|
+
const importContainer = file.getImports();
|
|
155
|
+
const qFunc = importContainer.addQObject(ImportObjects_1.QueryObjectImports.QId);
|
|
156
|
+
const idModelName = importContainer.addGeneratedModel(model.fqName, model.id.modelName);
|
|
157
|
+
file.getFile().addClass({
|
|
158
|
+
name: model.id.qName,
|
|
136
159
|
isExported: true,
|
|
137
|
-
extends: `${qFunc}<${
|
|
160
|
+
extends: `${qFunc}<${idModelName}>`,
|
|
138
161
|
properties: [
|
|
139
162
|
{
|
|
140
163
|
name: "params",
|
|
141
164
|
scope: ts_morph_1.Scope.Private,
|
|
142
165
|
isReadonly: true,
|
|
143
|
-
initializer: this.getParamInitString(model.keys
|
|
166
|
+
initializer: this.getParamInitString(importContainer, model.keys),
|
|
144
167
|
},
|
|
145
168
|
],
|
|
146
169
|
methods: [
|
|
@@ -151,76 +174,80 @@ class QueryObjectGenerator {
|
|
|
151
174
|
],
|
|
152
175
|
});
|
|
153
176
|
}
|
|
154
|
-
getParamInitString(
|
|
177
|
+
getParamInitString(importContainer, props) {
|
|
155
178
|
return `[${props
|
|
156
179
|
.map((prop) => {
|
|
157
180
|
var _a;
|
|
158
|
-
|
|
159
|
-
if (prop.
|
|
160
|
-
importContainer.
|
|
181
|
+
let complexQParam = "";
|
|
182
|
+
if (prop.dataType === "ModelType" /* DataTypes.ModelType */ || prop.dataType === "ComplexType" /* DataTypes.ComplexType */) {
|
|
183
|
+
const importedQObject = importContainer.addGeneratedQObject(prop.fqType, prop.qObject);
|
|
184
|
+
complexQParam = `, new ${importedQObject}()`;
|
|
161
185
|
}
|
|
186
|
+
const qParam = importContainer.addFromQObject(prop.qParam);
|
|
162
187
|
const isMappedNameNecessary = prop.odataName !== prop.name;
|
|
163
188
|
const mappedName = isMappedNameNecessary ? `"${prop.name}"` : ((_a = prop.converters) === null || _a === void 0 ? void 0 : _a.length) ? "undefined" : undefined;
|
|
164
|
-
const converterStmt = this.generateConverterStmt(prop.converters
|
|
189
|
+
const converterStmt = this.generateConverterStmt(importContainer, prop.converters);
|
|
165
190
|
const mappedNameParam = mappedName ? `, ${mappedName}` : "";
|
|
166
|
-
const complexQParam = isComplexParam ? `, new ${prop.qObject}()` : "";
|
|
167
191
|
const converterParam = converterStmt ? `, ${converterStmt}` : "";
|
|
168
|
-
return `new ${
|
|
192
|
+
return `new ${qParam}("${prop.odataName}"${complexQParam}${mappedNameParam}${converterParam})`;
|
|
169
193
|
})
|
|
170
194
|
.join(",")}]`;
|
|
171
195
|
}
|
|
172
|
-
generateUnboundOperations(
|
|
173
|
-
|
|
174
|
-
this.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
...this.dataModel.getEntitySetOperations(fqEntityName),
|
|
181
|
-
].forEach((operation) => {
|
|
182
|
-
this.generateOperation(operation, importContainer);
|
|
196
|
+
generateUnboundOperations() {
|
|
197
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
198
|
+
const unboundOps = this.dataModel.getUnboundOperationTypes();
|
|
199
|
+
const reservedNames = unboundOps.map((op) => op.qName);
|
|
200
|
+
const file = this.project.createOrGetMainQObjectFile(reservedNames);
|
|
201
|
+
unboundOps.forEach((operation) => {
|
|
202
|
+
this.generateOperation(file, operation, "");
|
|
203
|
+
});
|
|
183
204
|
});
|
|
184
205
|
}
|
|
185
|
-
generateOperation(operation,
|
|
206
|
+
generateOperation(file, operation, baseFqName) {
|
|
186
207
|
var _a;
|
|
208
|
+
const imports = file.getImports();
|
|
187
209
|
const isV2 = this.version === odata_core_1.ODataVersions.V2;
|
|
188
|
-
const qOperation = operation.type === "Action" /* OperationTypes.Action */ ? "QAction" : "QFunction";
|
|
189
210
|
const returnType = operation.returnType;
|
|
190
|
-
let returnTypeOpStmt = "";
|
|
191
211
|
const hasParams = operation.parameters.length > 0;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
212
|
+
// imports
|
|
213
|
+
const qOp = operation.type === "Action" /* OperationTypes.Action */ ? ImportObjects_1.QueryObjectImports.QAction : ImportObjects_1.QueryObjectImports.QFunction;
|
|
214
|
+
const qOperation = imports.addQObject(qOp);
|
|
215
|
+
const paramModelName = operation.parameters.length
|
|
216
|
+
? imports.addGeneratedModel(baseFqName, operation.paramsModelName)
|
|
217
|
+
: undefined;
|
|
218
|
+
let returnTypeOpStmt = "";
|
|
196
219
|
if (returnType) {
|
|
220
|
+
const collectionSuffix = returnType.isCollection ? "_COLLECTION" : "";
|
|
197
221
|
if (returnType.dataType === "ComplexType" /* DataTypes.ComplexType */ || returnType.dataType === "ModelType" /* DataTypes.ModelType */) {
|
|
198
222
|
if (returnType.qObject) {
|
|
199
|
-
|
|
200
|
-
|
|
223
|
+
const [opRt, rts, qComplexParam] = imports.addQObject(ImportObjects_1.QueryObjectImports.OperationReturnType, ImportObjects_1.QueryObjectImports.ReturnTypes, ImportObjects_1.QueryObjectImports.QComplexParam);
|
|
224
|
+
const returnQName = imports.addGeneratedQObject(returnType.fqType, returnType.qObject);
|
|
225
|
+
returnTypeOpStmt = `new ${opRt}(${rts}.COMPLEX${collectionSuffix}, new ${qComplexParam}("NONE", new ${returnQName}))`;
|
|
201
226
|
}
|
|
202
227
|
}
|
|
203
228
|
// currently, it only makes sense to add the OperationReturnType if a converter is present
|
|
204
229
|
else if (returnType.converters && returnType.qParam) {
|
|
205
|
-
|
|
206
|
-
const
|
|
230
|
+
const [rtClass, rtTypes] = imports.addQObject(ImportObjects_1.QueryObjectImports.OperationReturnType, ImportObjects_1.QueryObjectImports.ReturnTypes);
|
|
231
|
+
const [qParam] = imports.addFromQObject(returnType.qParam);
|
|
232
|
+
// TODO: some constants with string concat
|
|
233
|
+
const rtKind = `${rtTypes}.VALUE${collectionSuffix}`;
|
|
207
234
|
const converterParam = returnType.converters
|
|
208
|
-
? ", " + this.generateConverterStmt(returnType.converters
|
|
235
|
+
? ", " + this.generateConverterStmt(imports, returnType.converters)
|
|
209
236
|
: "";
|
|
210
|
-
returnTypeOpStmt = `new
|
|
237
|
+
returnTypeOpStmt = `new ${rtClass}(${rtKind}, new ${qParam}("NONE", undefined${converterParam}))`;
|
|
211
238
|
}
|
|
212
239
|
}
|
|
213
|
-
|
|
240
|
+
file.getFile().addClass({
|
|
214
241
|
name: operation.qName,
|
|
215
242
|
isExported: true,
|
|
216
|
-
extends: qOperation + (hasParams ? `<${
|
|
243
|
+
extends: qOperation + (hasParams ? `<${paramModelName}>` : ""),
|
|
217
244
|
properties: [
|
|
218
245
|
{
|
|
219
246
|
name: "params",
|
|
220
247
|
scope: ts_morph_1.Scope.Private,
|
|
221
248
|
isReadonly: true,
|
|
222
249
|
type: ((_a = operation.parameters) === null || _a === void 0 ? void 0 : _a.length) ? undefined : "[]",
|
|
223
|
-
initializer: this.getParamInitString(operation.parameters
|
|
250
|
+
initializer: this.getParamInitString(imports, operation.parameters),
|
|
224
251
|
},
|
|
225
252
|
],
|
|
226
253
|
ctors: [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryObjectGenerator.js","sourceRoot":"","sources":["../../src/generator/QueryObjectGenerator.ts"],"names":[],"mappings":";;;AACA,qDAAqD;AACrD,uCAAkH;AAClH,sDAA2D;AAa3D,uDAAoD;AAE7C,MAAM,oBAAoB,GAAiC,CAChE,SAAS,EACT,UAAU,EACV,OAAO,EACP,OAAO,EACP,YAAY,EACZ,EAAE;IACF,MAAM,SAAS,GAAG,IAAI,oBAAoB,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;IAClG,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC;AAC9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{ ODataVersions } from \"@odata2ts/odata-core\";\nimport { OptionalKind, PropertyDeclarationStructure, Scope, SourceFile, VariableDeclarationKind } from \"ts-morph\";\nimport { firstCharLowerCase } from \"xml2js/lib/processors\";\n\nimport { DataModel } from \"../data-model/DataModel\";\nimport {\n ComplexType,\n DataTypes,\n ModelType,\n OperationType,\n OperationTypes,\n PropertyModel,\n} from \"../data-model/DataTypeModel\";\nimport { NamingHelper } from \"../data-model/NamingHelper\";\nimport { EntityBasedGeneratorFunction, GeneratorFunctionOptions } from \"../FactoryFunctionModel\";\nimport { ImportContainer } from \"./ImportContainer\";\n\nexport const generateQueryObjects: EntityBasedGeneratorFunction = (\n dataModel,\n sourceFile,\n version,\n options,\n namingHelper\n) => {\n const generator = new QueryObjectGenerator(dataModel, sourceFile, version, options, namingHelper);\n return generator.generate();\n};\n\nclass QueryObjectGenerator {\n constructor(\n private dataModel: DataModel,\n private sourceFile: SourceFile,\n private version: ODataVersions,\n private options: GeneratorFunctionOptions,\n private namingHelper: NamingHelper\n ) {}\n\n public generate(): void {\n const importContainer = new ImportContainer(this.namingHelper.getFileNames());\n\n this.generateModels(importContainer);\n if (!this.options.skipOperations) {\n this.generateUnboundOperations(importContainer);\n }\n\n this.sourceFile.addImportDeclarations(importContainer.getImportDeclarations(false));\n }\n\n private generateModels(importContainer: ImportContainer) {\n this.dataModel.getEntityTypes().forEach((model) => {\n this.generateModel(model, importContainer);\n if (!this.options.skipIdModels && model.generateId) {\n this.generateIdFunction(model, importContainer);\n }\n if (!this.options.skipOperations) {\n this.generateBoundOperations(model.fqName, importContainer);\n }\n });\n this.dataModel.getComplexTypes().forEach((model) => {\n this.generateModel(model, importContainer);\n });\n\n if (this.dataModel.getEntityTypes().length || this.dataModel.getComplexTypes().length) {\n importContainer.addFromQObject(\"QueryObject\");\n }\n }\n\n private generateModel(model: ComplexType, importContainer: ImportContainer) {\n let extendsClause = \"QueryObject\";\n if (model.baseClasses.length) {\n const baseClass = model.baseClasses[0];\n const baseModel = this.dataModel.getEntityType(baseClass) || this.dataModel.getComplexType(baseClass);\n if (!baseModel) {\n throw new Error(`Entity or complex type \"${baseClass}\" from baseClass attribute not found!`);\n }\n\n extendsClause = baseModel.qName;\n }\n\n this.sourceFile.addClass({\n name: model.qName,\n isExported: true,\n extends: extendsClause,\n isAbstract: model.abstract,\n properties: this.generateQueryObjectProps(model.props, importContainer),\n });\n\n if (!model.abstract) {\n this.sourceFile.addVariableStatement({\n declarationKind: VariableDeclarationKind.Const,\n isExported: true,\n declarations: [\n {\n name: firstCharLowerCase(model.qName),\n initializer: `new ${model.qName}()`,\n },\n ],\n });\n }\n }\n\n private generateQueryObjectProps(\n props: Array<PropertyModel>,\n importContainer: ImportContainer\n ): Array<OptionalKind<PropertyDeclarationStructure>> {\n return props.map((prop) => {\n const { odataName } = prop;\n const name = this.namingHelper.getQPropName(prop.name);\n const isModelType = prop.dataType === DataTypes.ModelType || prop.dataType === DataTypes.ComplexType;\n let qPathInit: string;\n\n // factor in collections\n if (prop.isCollection) {\n const cType = `Q${isModelType ? \"Entity\" : \"\"}CollectionPath`;\n const qObject = prop.qObject;\n\n if (!qObject) {\n throw new Error(\"QObject for collection is missing!\");\n }\n\n qPathInit = `new ${cType}(this.withPrefix(\"${odataName}\"), () => ${qObject})`;\n\n importContainer.addFromQObject(cType);\n if (!isModelType) {\n importContainer.addFromQObject(qObject);\n }\n } else {\n if (isModelType) {\n qPathInit = `new ${prop.qPath}(this.withPrefix(\"${odataName}\"), () => ${prop.qObject!})`;\n } else {\n let converterStmt = this.generateConverterStmt(prop.converters, importContainer);\n qPathInit = `new ${prop.qPath}(this.withPrefix(\"${odataName}\")${converterStmt ? `, ${converterStmt}` : \"\"})`;\n }\n // add import for data type\n importContainer.addFromQObject(prop.qPath);\n }\n\n return {\n name,\n scope: Scope.Public,\n isReadonly: true,\n initializer: qPathInit,\n } as OptionalKind<PropertyDeclarationStructure>;\n });\n }\n\n private generateConverterStmt(converters: Array<ValueConverterImport> | undefined, importContainer: ImportContainer) {\n if (!converters?.length) {\n return;\n }\n converters.forEach((converter) => {\n importContainer.addCustomType(converter.package, converter.converterId);\n });\n\n if (converters.length === 1) {\n return converters[0].converterId;\n } else {\n importContainer.addCustomType(\"@odata2ts/converter-runtime\", \"createChain\");\n\n const [first, second, ...moreConverters] = converters;\n return moreConverters.reduce(\n (stmt, conv) => `${stmt}.chain(${conv.converterId})`,\n `createChain(${first.converterId}, ${second.converterId})`\n );\n }\n }\n\n private generateIdFunction(model: ModelType, importContainer: ImportContainer) {\n const qFunc = \"QId\";\n importContainer.addFromQObject(qFunc);\n importContainer.addGeneratedModel(model.idModelName);\n\n this.sourceFile.addClass({\n name: model.qIdFunctionName,\n isExported: true,\n extends: `${qFunc}<${model.idModelName}>`,\n properties: [\n {\n name: \"params\",\n scope: Scope.Private,\n isReadonly: true,\n initializer: this.getParamInitString(model.keys, importContainer),\n },\n ],\n methods: [\n {\n name: \"getParams\",\n statements: [\"return this.params\"],\n },\n ],\n });\n }\n\n private getParamInitString(props: Array<PropertyModel>, importContainer: ImportContainer) {\n return `[${props\n .map((prop) => {\n const isComplexParam = prop.dataType === DataTypes.ModelType || prop.dataType === DataTypes.ComplexType;\n if (prop.qParam) {\n importContainer.addFromQObject(prop.qParam);\n }\n const isMappedNameNecessary = prop.odataName !== prop.name;\n const mappedName = isMappedNameNecessary ? `\"${prop.name}\"` : prop.converters?.length ? \"undefined\" : undefined;\n const converterStmt = this.generateConverterStmt(prop.converters, importContainer);\n const mappedNameParam = mappedName ? `, ${mappedName}` : \"\";\n const complexQParam = isComplexParam ? `, new ${prop.qObject}()` : \"\";\n const converterParam = converterStmt ? `, ${converterStmt}` : \"\";\n return `new ${prop.qParam}(\"${prop.odataName}\"${complexQParam}${mappedNameParam}${converterParam})`;\n })\n .join(\",\")}]`;\n }\n\n private generateUnboundOperations(importContainer: ImportContainer) {\n this.dataModel.getUnboundOperationTypes().forEach((operation) => {\n this.generateOperation(operation, importContainer);\n });\n }\n\n private generateBoundOperations(fqEntityName: string, importContainer: ImportContainer) {\n [\n ...this.dataModel.getEntityTypeOperations(fqEntityName),\n ...this.dataModel.getEntitySetOperations(fqEntityName),\n ].forEach((operation) => {\n this.generateOperation(operation, importContainer);\n });\n }\n\n private generateOperation(operation: OperationType, importContainer: ImportContainer) {\n const isV2 = this.version === ODataVersions.V2;\n const qOperation = operation.type === OperationTypes.Action ? \"QAction\" : \"QFunction\";\n const returnType = operation.returnType;\n let returnTypeOpStmt: string = \"\";\n const hasParams = operation.parameters.length > 0;\n importContainer.addFromQObject(qOperation);\n if (hasParams) {\n importContainer.addGeneratedModel(operation.paramsModelName);\n }\n if (returnType) {\n if (returnType.dataType === DataTypes.ComplexType || returnType.dataType === DataTypes.ModelType) {\n if (returnType.qObject) {\n importContainer.addFromQObject(\"OperationReturnType\", \"ReturnTypes\", \"QComplexParam\");\n returnTypeOpStmt = `new OperationReturnType(ReturnTypes.COMPLEX${\n returnType.isCollection ? \"_COLLECTION\" : \"\"\n }, new QComplexParam(\"NONE\", new ${returnType.qObject}))`;\n }\n }\n // currently, it only makes sense to add the OperationReturnType if a converter is present\n else if (returnType.converters && returnType.qParam) {\n importContainer.addFromQObject(\"OperationReturnType\", \"ReturnTypes\", returnType.qParam);\n const rtKind = \"ReturnTypes.VALUE\" + (returnType.isCollection ? \"_COLLECTION\" : \"\");\n const converterParam = returnType.converters\n ? \", \" + this.generateConverterStmt(returnType.converters, importContainer)\n : \"\";\n returnTypeOpStmt = `new OperationReturnType(${rtKind}, new ${returnType.qParam}(\"NONE\", undefined${converterParam}))`;\n }\n }\n\n this.sourceFile.addClass({\n name: operation.qName,\n isExported: true,\n extends: qOperation + (hasParams ? `<${operation.paramsModelName}>` : \"\"),\n properties: [\n {\n name: \"params\",\n scope: Scope.Private,\n isReadonly: true,\n type: operation.parameters?.length ? undefined : \"[]\",\n initializer: this.getParamInitString(operation.parameters, importContainer),\n },\n ],\n ctors: [\n {\n statements: [\n `super(\"${operation.odataName}\"${returnTypeOpStmt ? \", \" + returnTypeOpStmt : isV2 ? \", undefined\" : \"\"}${\n isV2 ? \", { v2Mode: true }\" : \"\"\n })`,\n ],\n },\n ],\n methods: [\n {\n name: \"getParams\",\n statements: [\"return this.params\"],\n },\n // functions without params: add an overriding buildUrl() to not force users to have to pass undefined as param\n ...(operation.type === OperationTypes.Function && !hasParams\n ? [\n {\n name: \"buildUrl\",\n statements: [\"return super.buildUrl(undefined)\"],\n },\n ]\n : []),\n ],\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"QueryObjectGenerator.js","sourceRoot":"","sources":["../../src/generator/QueryObjectGenerator.ts"],"names":[],"mappings":";;;;AACA,qDAAqD;AACrD,uCAAsG;AACtG,sDAA2D;AAe3D,0DAA4D;AAGrD,MAAM,oBAAoB,GAAiC,CAChE,OAAO,EACP,SAAS,EACT,OAAO,EACP,OAAO,EACP,YAAY,EACZ,EAAE;IACF,MAAM,SAAS,GAAG,IAAI,oBAAoB,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;IAC/F,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC;AAC9B,CAAC,CAAC;AATW,QAAA,oBAAoB,wBAS/B;AAEF,MAAM,oBAAoB;IACxB,YACU,OAAuB,EACvB,SAAoB,EACpB,OAAsB,EACtB,OAAiC,EACjC,YAA0B;QAJ1B,YAAO,GAAP,OAAO,CAAgB;QACvB,cAAS,GAAT,SAAS,CAAW;QACpB,YAAO,GAAP,OAAO,CAAe;QACtB,YAAO,GAAP,OAAO,CAA0B;QACjC,iBAAY,GAAZ,YAAY,CAAc;IACjC,CAAC;IAES,QAAQ;;YACnB,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC;YAE5B,mCAAmC;YACnC,MAAM,QAAQ,GAAyB,CAAC,GAAG,IAAI,CAAC,mBAAmB,EAAE,EAAE,GAAG,IAAI,CAAC,oBAAoB,EAAE,CAAC,CAAC;YACvG,IAAI,CAAC,IAA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KAAK;YACrB,UAAU,EAAE,IAAI;YAChB,OAAO,EAAE,UAAU,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,cAAc,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;YAC9D,UAAU,EAAE;gBACV;oBACE,IAAI,EAAE,QAAQ;oBACd,KAAK,EAAE,gBAAK,CAAC,OAAO;oBACpB,UAAU,EAAE,IAAI;oBAChB,IAAI,EAAE,CAAA,MAAA,SAAS,CAAC,UAAU,0CAAE,MAAM,EAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI;oBACrD,WAAW,EAAE,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,SAAS,CAAC,UAAU,CAAC;iBACpE;aACF;YACD,KAAK,EAAE;gBACL;oBACE,UAAU,EAAE;wBACV,UAAU,SAAS,CAAC,SAAS,IAAI,gBAAgB,CAAC,CAAC,CAAC,IAAI,GAAG,gBAAgB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,GACrG,IAAI,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,EAChC,GAAG;qBACJ;iBACF;aACF;YACD,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,WAAW;oBACjB,UAAU,EAAE,CAAC,oBAAoB,CAAC;iBACnC;gBACD,+GAA+G;gBAC/G,GAAG,CAAC,SAAS,CAAC,IAAI,6CAA4B,IAAI,CAAC,SAAS;oBAC1D,CAAC,CAAC;wBACE;4BACE,IAAI,EAAE,UAAU;4BAChB,UAAU,EAAE,CAAC,kCAAkC,CAAC;yBACjD;qBACF;oBACH,CAAC,CAAC,EAAE,CAAC;aACR;SACF,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { ValueConverterImport } from \"@odata2ts/converter-runtime\";\r\nimport { ODataVersions } from \"@odata2ts/odata-core\";\r\nimport { OptionalKind, PropertyDeclarationStructure, Scope, VariableDeclarationKind } from \"ts-morph\";\r\nimport { firstCharLowerCase } from \"xml2js/lib/processors\";\r\n\r\nimport { DataModel } from \"../data-model/DataModel\";\r\nimport {\r\n ComplexType,\r\n DataTypes,\r\n EntityType,\r\n OperationType,\r\n OperationTypes,\r\n PropertyModel,\r\n} from \"../data-model/DataTypeModel\";\r\nimport { NamingHelper } from \"../data-model/NamingHelper\";\r\nimport { EntityBasedGeneratorFunction, GeneratorFunctionOptions } from \"../FactoryFunctionModel\";\r\nimport { FileHandler } from \"../project/FileHandler\";\r\nimport { ProjectManager } from \"../project/ProjectManager\";\r\nimport { QueryObjectImports } from \"./import/ImportObjects\";\r\nimport { ImportContainer } from \"./ImportContainer\";\r\n\r\nexport const generateQueryObjects: EntityBasedGeneratorFunction = (\r\n project,\r\n dataModel,\r\n version,\r\n options,\r\n namingHelper\r\n) => {\r\n const generator = new QueryObjectGenerator(project, dataModel, version, options, namingHelper);\r\n return generator.generate();\r\n};\r\n\r\nclass QueryObjectGenerator {\r\n constructor(\r\n private project: ProjectManager,\r\n private dataModel: DataModel,\r\n private version: ODataVersions,\r\n private options: GeneratorFunctionOptions,\r\n private namingHelper: NamingHelper\r\n ) {}\r\n\r\n public async generate(): Promise<void> {\r\n this.project.initQObjects();\r\n\r\n // process EntityType & ComplexType\r\n const promises: Array<Promise<void>> = [...this.generateEntityTypes(), ...this.generateComplexTypes()];\r\n if (!this.options.skipOperations) {\r\n // process unbound operations\r\n promises.push(this.generateUnboundOperations());\r\n }\r\n\r\n await Promise.all(promises);\r\n\r\n return this.project.finalizeQObjects();\r\n }\r\n\r\n private generateEntityTypes() {\r\n return this.dataModel.getEntityTypes().map((model) => {\r\n const file = this.project.createOrGetQObjectFile(model.folderPath, model.qName, [\r\n model.qName,\r\n firstCharLowerCase(model.qName),\r\n model.id.qName,\r\n ]);\r\n\r\n // q object\r\n this.generateModel(file, model);\r\n\r\n // qId function\r\n if (!this.options.skipIdModels && model.generateId) {\r\n this.generateIdFunction(file, model);\r\n }\r\n\r\n // bound q operations\r\n if (!this.options.skipOperations) {\r\n [\r\n ...this.dataModel.getEntityTypeOperations(model.fqName),\r\n ...this.dataModel.getEntitySetOperations(model.fqName),\r\n ].forEach((operation) => {\r\n this.generateOperation(file, operation, model.fqName);\r\n });\r\n }\r\n\r\n return this.project.finalizeFile(file);\r\n });\r\n }\r\n\r\n private generateComplexTypes() {\r\n return this.dataModel.getComplexTypes().map((model) => {\r\n const file = this.project.createOrGetQObjectFile(model.folderPath, model.qName, [\r\n model.qName,\r\n firstCharLowerCase(model.qName),\r\n ]);\r\n\r\n this.generateModel(file, model);\r\n\r\n return this.project.finalizeFile(file);\r\n });\r\n }\r\n\r\n private generateModel(file: FileHandler, model: ComplexType) {\r\n const imports = file.getImports();\r\n\r\n let extendsClause: string;\r\n if (model.baseClasses.length) {\r\n const baseClass = model.baseClasses[0];\r\n const baseModel = this.dataModel.getModel(baseClass) as ComplexType;\r\n if (!baseModel) {\r\n throw new Error(`Entity or complex type \"${baseClass}\" from baseClass attribute not found!`);\r\n }\r\n extendsClause = imports.addGeneratedQObject(baseClass, baseModel.qName);\r\n } else {\r\n [extendsClause] = imports.addQObject(QueryObjectImports.QueryObject);\r\n }\r\n\r\n file.getFile().addClass({\r\n name: model.qName,\r\n isExported: true,\r\n extends: extendsClause,\r\n // isAbstract: model.abstract,\r\n properties: this.generateQueryObjectProps(file.getImports(), model.props),\r\n });\r\n\r\n file.getFile().addVariableStatement({\r\n declarationKind: VariableDeclarationKind.Const,\r\n isExported: true,\r\n declarations: [\r\n {\r\n name: firstCharLowerCase(model.qName),\r\n initializer: `new ${model.qName}()`,\r\n },\r\n ],\r\n });\r\n }\r\n\r\n private generateQueryObjectProps(\r\n importContainer: ImportContainer,\r\n props: Array<PropertyModel>\r\n ): Array<OptionalKind<PropertyDeclarationStructure>> {\r\n return props.map((prop) => {\r\n const { odataName } = prop;\r\n const name = this.namingHelper.getQPropName(prop.name);\r\n const isModelType = prop.dataType === DataTypes.ModelType || prop.dataType === DataTypes.ComplexType;\r\n let qPathInit: string;\r\n\r\n // factor in collections\r\n if (prop.isCollection) {\r\n const cType = isModelType ? QueryObjectImports.QEntityCollectionPath : QueryObjectImports.QCollectionPath;\r\n const collectionType = importContainer.addQObject(cType);\r\n const qObject = isModelType\r\n ? importContainer.addGeneratedQObject(prop.fqType, prop.qObject!)\r\n : importContainer.addFromQObject(prop.qObject!)[0];\r\n\r\n qPathInit = `new ${collectionType}(this.withPrefix(\"${odataName}\"), () => ${qObject})`;\r\n } else {\r\n // add import for data type\r\n const qPath = importContainer.addFromQObject(prop.qPath);\r\n if (isModelType) {\r\n const qObject = importContainer.addGeneratedQObject(prop.fqType, prop.qObject!);\r\n qPathInit = `new ${qPath}(this.withPrefix(\"${odataName}\"), () => ${qObject})`;\r\n } else {\r\n let converterStmt = this.generateConverterStmt(importContainer, prop.converters);\r\n qPathInit = `new ${qPath}(this.withPrefix(\"${odataName}\")${converterStmt ? `, ${converterStmt}` : \"\"})`;\r\n }\r\n }\r\n\r\n return {\r\n name,\r\n scope: Scope.Public,\r\n isReadonly: true,\r\n initializer: qPathInit,\r\n } as OptionalKind<PropertyDeclarationStructure>;\r\n });\r\n }\r\n\r\n private generateConverterStmt(importContainer: ImportContainer, converters: Array<ValueConverterImport> | undefined) {\r\n if (!converters?.length) {\r\n return undefined;\r\n }\r\n const converterIds = converters.map((converter) => {\r\n return importContainer.addCustomType(converter.package, converter.converterId);\r\n });\r\n\r\n if (converterIds.length === 1) {\r\n return converterIds[0];\r\n } else {\r\n const createChain = importContainer.addCustomType(\"@odata2ts/converter-runtime\", \"createChain\");\r\n\r\n const [first, second, ...moreConverters] = converterIds;\r\n return moreConverters.reduce((stmt, convId) => `${stmt}.chain(${convId})`, `${createChain}(${first}, ${second})`);\r\n }\r\n }\r\n\r\n private generateIdFunction(file: FileHandler, model: EntityType) {\r\n const importContainer = file.getImports();\r\n const qFunc = importContainer.addQObject(QueryObjectImports.QId);\r\n const idModelName = importContainer.addGeneratedModel(model.fqName, model.id.modelName);\r\n\r\n file.getFile().addClass({\r\n name: model.id.qName,\r\n isExported: true,\r\n extends: `${qFunc}<${idModelName}>`,\r\n properties: [\r\n {\r\n name: \"params\",\r\n scope: Scope.Private,\r\n isReadonly: true,\r\n initializer: this.getParamInitString(importContainer, model.keys),\r\n },\r\n ],\r\n methods: [\r\n {\r\n name: \"getParams\",\r\n statements: [\"return this.params\"],\r\n },\r\n ],\r\n });\r\n }\r\n\r\n private getParamInitString(importContainer: ImportContainer, props: Array<PropertyModel>) {\r\n return `[${props\r\n .map((prop) => {\r\n let complexQParam = \"\";\r\n if (prop.dataType === DataTypes.ModelType || prop.dataType === DataTypes.ComplexType) {\r\n const importedQObject = importContainer.addGeneratedQObject(prop.fqType, prop.qObject!);\r\n complexQParam = `, new ${importedQObject}()`;\r\n }\r\n\r\n const qParam = importContainer.addFromQObject(prop.qParam!);\r\n const isMappedNameNecessary = prop.odataName !== prop.name;\r\n const mappedName = isMappedNameNecessary ? `\"${prop.name}\"` : prop.converters?.length ? \"undefined\" : undefined;\r\n const converterStmt = this.generateConverterStmt(importContainer, prop.converters);\r\n const mappedNameParam = mappedName ? `, ${mappedName}` : \"\";\r\n const converterParam = converterStmt ? `, ${converterStmt}` : \"\";\r\n return `new ${qParam}(\"${prop.odataName}\"${complexQParam}${mappedNameParam}${converterParam})`;\r\n })\r\n .join(\",\")}]`;\r\n }\r\n\r\n private async generateUnboundOperations() {\r\n const unboundOps = this.dataModel.getUnboundOperationTypes();\r\n const reservedNames = unboundOps.map((op) => op.qName);\r\n const file = this.project.createOrGetMainQObjectFile(reservedNames);\r\n\r\n unboundOps.forEach((operation) => {\r\n this.generateOperation(file, operation, \"\");\r\n });\r\n }\r\n\r\n private generateOperation(file: FileHandler, operation: OperationType, baseFqName: string) {\r\n const imports = file.getImports();\r\n const isV2 = this.version === ODataVersions.V2;\r\n const returnType = operation.returnType;\r\n const hasParams = operation.parameters.length > 0;\r\n\r\n // imports\r\n const qOp = operation.type === OperationTypes.Action ? QueryObjectImports.QAction : QueryObjectImports.QFunction;\r\n const qOperation = imports.addQObject(qOp);\r\n const paramModelName = operation.parameters.length\r\n ? imports.addGeneratedModel(baseFqName, operation.paramsModelName)\r\n : undefined;\r\n\r\n let returnTypeOpStmt: string = \"\";\r\n if (returnType) {\r\n const collectionSuffix = returnType.isCollection ? \"_COLLECTION\" : \"\";\r\n if (returnType.dataType === DataTypes.ComplexType || returnType.dataType === DataTypes.ModelType) {\r\n if (returnType.qObject) {\r\n const [opRt, rts, qComplexParam] = imports.addQObject(\r\n QueryObjectImports.OperationReturnType,\r\n QueryObjectImports.ReturnTypes,\r\n QueryObjectImports.QComplexParam\r\n );\r\n const returnQName = imports.addGeneratedQObject(returnType.fqType, returnType.qObject);\r\n\r\n returnTypeOpStmt = `new ${opRt}(${rts}.COMPLEX${collectionSuffix}, new ${qComplexParam}(\"NONE\", new ${returnQName}))`;\r\n }\r\n }\r\n // currently, it only makes sense to add the OperationReturnType if a converter is present\r\n else if (returnType.converters && returnType.qParam) {\r\n const [rtClass, rtTypes] = imports.addQObject(\r\n QueryObjectImports.OperationReturnType,\r\n QueryObjectImports.ReturnTypes\r\n );\r\n const [qParam] = imports.addFromQObject(returnType.qParam);\r\n\r\n // TODO: some constants with string concat\r\n const rtKind = `${rtTypes}.VALUE${collectionSuffix}`;\r\n\r\n const converterParam = returnType.converters\r\n ? \", \" + this.generateConverterStmt(imports, returnType.converters)\r\n : \"\";\r\n returnTypeOpStmt = `new ${rtClass}(${rtKind}, new ${qParam}(\"NONE\", undefined${converterParam}))`;\r\n }\r\n }\r\n\r\n file.getFile().addClass({\r\n name: operation.qName,\r\n isExported: true,\r\n extends: qOperation + (hasParams ? `<${paramModelName}>` : \"\"),\r\n properties: [\r\n {\r\n name: \"params\",\r\n scope: Scope.Private,\r\n isReadonly: true,\r\n type: operation.parameters?.length ? undefined : \"[]\",\r\n initializer: this.getParamInitString(imports, operation.parameters),\r\n },\r\n ],\r\n ctors: [\r\n {\r\n statements: [\r\n `super(\"${operation.odataName}\"${returnTypeOpStmt ? \", \" + returnTypeOpStmt : isV2 ? \", undefined\" : \"\"}${\r\n isV2 ? \", { v2Mode: true }\" : \"\"\r\n })`,\r\n ],\r\n },\r\n ],\r\n methods: [\r\n {\r\n name: \"getParams\",\r\n statements: [\"return this.params\"],\r\n },\r\n // functions without params: add an overriding buildUrl() to not force users to have to pass undefined as param\r\n ...(operation.type === OperationTypes.Function && !hasParams\r\n ? [\r\n {\r\n name: \"buildUrl\",\r\n statements: [\"return super.buildUrl(undefined)\"],\r\n },\r\n ]\r\n : []),\r\n ],\r\n });\r\n }\r\n}\r\n"]}
|
|
@@ -8,4 +8,4 @@ export interface PropsAndOps extends Required<Pick<ClassDeclarationStructure, "p
|
|
|
8
8
|
}
|
|
9
9
|
export interface ServiceGeneratorOptions extends Pick<ConfigFileOptions, "enablePrimitivePropertyServices" | "v4BigNumberAsString"> {
|
|
10
10
|
}
|
|
11
|
-
export declare function generateServices(
|
|
11
|
+
export declare function generateServices(project: ProjectManager, dataModel: DataModel, version: ODataVersions, namingHelper: NamingHelper, options?: ServiceGeneratorOptions): Promise<void>;
|