@@ -10,7 +10,7 @@ import {
1010 isReferenceObject ,
1111 isRelatedEntitySchema
1212} from '@utils/openapi/guards' ;
13- import { camelCase } from 'change-case' ;
13+ import { camelCase , pascalCase } from 'change-case' ;
1414import { OpenAPIV3 } from 'openapi-types' ;
1515import { loosePascalCase } from '@utils/case' ;
1616
@@ -19,6 +19,7 @@ export interface GeneratedEntity {
1919 properties : Map < string , PropertyMetaData > ;
2020 parentName ?: string ;
2121 source : string ;
22+ createProperties : InterfaceProperty [ ] ;
2223}
2324
2425export const generateEntities = (
@@ -78,12 +79,67 @@ export const generateEntities = (
7879 entities . set ( schemaName , {
7980 name : schemaName ,
8081 properties,
82+ createProperties : entityInterfaceProperties . filter ( ( prop ) => ! prop . readonly ) ,
8183 parentName : parentEntityInterfaceName ? camelCase ( parentEntityInterfaceName ) : undefined ,
8284 source : generateStatements (
83- generateInterface ( entityInterfaceName , entityInterfaceProperties , parentEntityInterfaceName )
85+ generateInterface (
86+ entityInterfaceName ,
87+ entityInterfaceProperties . map ( ( prop ) => ( {
88+ ...prop ,
89+ required : ! ! ( prop . required ?? prop . type ?. endsWith ( '[]' ) ) || prop . type !== 'string'
90+ } ) ) ,
91+ parentEntityInterfaceName
92+ )
8493 )
8594 } ) ;
8695 }
8796
8897 return entities ;
8998} ;
99+
100+ export const generateCreateEntityStatements = (
101+ entities : Map < string , GeneratedEntity > ,
102+ enums : Map < string , GeneratedEnum >
103+ ) : string [ ] => {
104+ const isPrimitiveOrEnumOrObject = ( propType ?: string ) => {
105+ return (
106+ ! propType ||
107+ enums . has ( propType ) ||
108+ propType === 'string' ||
109+ propType === 'number' ||
110+ propType === 'boolean' ||
111+ propType === '{}'
112+ ) ;
113+ } ;
114+
115+ const transformCreateEntityInterfaceProps = ( props : InterfaceProperty [ ] ) => {
116+ return props . map ( ( prop ) => {
117+ if ( isPrimitiveOrEnumOrObject ( prop . type ) ) {
118+ return prop ;
119+ }
120+
121+ if ( prop . type ?. endsWith ( '[]' ) ) {
122+ const typeWithoutBrackets = prop . type . replace ( / ^ \( ( .* ?) \) \[ \] $ / , '$1' ) ;
123+ return isPrimitiveOrEnumOrObject ( typeWithoutBrackets ) ? prop : { ...prop , type : `(${ typeWithoutBrackets } _Create)[]` } ;
124+ }
125+
126+ return { ...prop , type : `${ prop . type } _Create` } ;
127+ } ) ;
128+ } ;
129+
130+ const createEntityStatements : string [ ] = [ ] ;
131+
132+ const generateCreateEntityStatement = ( entity : GeneratedEntity , entityName : string ) : string => {
133+ const name = `${ pascalCase ( entityName ) } _Create` ;
134+ const parentName = entity . parentName ? `${ pascalCase ( entity . parentName ) } _Create` : undefined ;
135+ const interfaceProperties = Array . from ( transformCreateEntityInterfaceProps ( entity . createProperties ) ) ;
136+
137+ return generateStatements ( generateInterface ( name , interfaceProperties , parentName ) ) ;
138+ } ;
139+
140+ entities . forEach ( ( entity , name ) => {
141+ createEntityStatements . push ( generateCreateEntityStatement ( entity , name ) ) ;
142+ } ) ;
143+
144+ return createEntityStatements ;
145+ } ;
0 commit comments