|
1 | 1 | // Inspired by: https://github.com/omar-dulaimi/prisma-trpc-generator |
2 | 2 |
|
3 | 3 | import { PluginError, PluginOptions, analyzePolicies, requireOption, resolvePath } from '@zenstackhq/sdk'; |
4 | | -import { DataModel, Model, TypeDef, TypeDefField, TypeDefFieldType, isDataModel, isEnum, isTypeDef } from '@zenstackhq/sdk/ast'; |
| 4 | +import { |
| 5 | + DataModel, |
| 6 | + Model, |
| 7 | + TypeDef, |
| 8 | + TypeDefField, |
| 9 | + TypeDefFieldType, |
| 10 | + isDataModel, |
| 11 | + isEnum, |
| 12 | + isTypeDef, |
| 13 | +} from '@zenstackhq/sdk/ast'; |
5 | 14 | import { |
6 | 15 | AggregateOperationSupport, |
7 | 16 | addMissingInputObjectTypesForAggregate, |
@@ -48,7 +57,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase { |
48 | 57 | output = resolvePath(output, this.options); |
49 | 58 |
|
50 | 59 | // input types |
51 | | - this.inputObjectTypes.push(...this.dmmf.schema.inputObjectTypes.prisma); |
| 60 | + this.inputObjectTypes.push(...(this.dmmf.schema.inputObjectTypes.prisma ?? [])); |
52 | 61 | this.outputObjectTypes.push(...this.dmmf.schema.outputObjectTypes.prisma); |
53 | 62 |
|
54 | 63 | // add input object types that are missing from Prisma dmmf |
@@ -649,13 +658,13 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase { |
649 | 658 | for (const _enum of [...(this.dmmf.schema.enumTypes.model ?? []), ...this.dmmf.schema.enumTypes.prisma]) { |
650 | 659 | schemas[upperCaseFirst(_enum.name)] = this.generateEnumComponent(_enum); |
651 | 660 | } |
652 | | - |
| 661 | + |
653 | 662 | // Also add enums from AST that might not be in DMMF (e.g., only used in TypeDefs) |
654 | 663 | for (const enumDecl of this.model.declarations.filter(isEnum)) { |
655 | 664 | if (!schemas[upperCaseFirst(enumDecl.name)]) { |
656 | 665 | schemas[upperCaseFirst(enumDecl.name)] = { |
657 | 666 | type: 'string', |
658 | | - enum: enumDecl.fields.map(f => f.name) |
| 667 | + enum: enumDecl.fields.map((f) => f.name), |
659 | 668 | }; |
660 | 669 | } |
661 | 670 | } |
@@ -765,12 +774,15 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase { |
765 | 774 | return result; |
766 | 775 | } |
767 | 776 |
|
768 | | - private generateField(def: { kind: DMMF.FieldKind; type: string; isList: boolean; isRequired: boolean; name?: string }, modelName?: string) { |
| 777 | + private generateField( |
| 778 | + def: { kind: DMMF.FieldKind; type: string; isList: boolean; isRequired: boolean; name?: string }, |
| 779 | + modelName?: string |
| 780 | + ) { |
769 | 781 | // For Json fields, check if there's a corresponding TypeDef in the original model |
770 | 782 | if (def.kind === 'scalar' && def.type === 'Json' && modelName && def.name) { |
771 | | - const dataModel = this.model.declarations.find(d => isDataModel(d) && d.name === modelName) as DataModel; |
| 783 | + const dataModel = this.model.declarations.find((d) => isDataModel(d) && d.name === modelName) as DataModel; |
772 | 784 | if (dataModel) { |
773 | | - const field = dataModel.fields.find(f => f.name === def.name); |
| 785 | + const field = dataModel.fields.find((f) => f.name === def.name); |
774 | 786 | if (field?.type.reference?.ref && isTypeDef(field.type.reference.ref)) { |
775 | 787 | // This Json field references a TypeDef |
776 | 788 | return this.wrapArray( |
@@ -876,7 +888,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase { |
876 | 888 | if (type.reference?.ref) { |
877 | 889 | return this.ref(type.reference.ref.name, true); |
878 | 890 | } |
879 | | - |
| 891 | + |
880 | 892 | // For scalar types, reuse the existing mapping logic |
881 | 893 | // Note: Json type is handled as empty schema for consistency |
882 | 894 | return match(type.type) |
@@ -913,7 +925,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase { |
913 | 925 | .with(P.union('JSON', 'Json'), () => { |
914 | 926 | // For Json fields, check if there's a specific TypeDef reference |
915 | 927 | // Otherwise, return empty schema for arbitrary JSON |
916 | | - const isTypeDefType = this.model.declarations.some(d => isTypeDef(d) && d.name === type); |
| 928 | + const isTypeDefType = this.model.declarations.some((d) => isTypeDef(d) && d.name === type); |
917 | 929 | return isTypeDefType ? this.ref(type, false) : {}; |
918 | 930 | }) |
919 | 931 | .otherwise((type) => this.ref(type.toString(), false)); |
|
0 commit comments