import { codeBlock , documentationBlock , unixEOL } from '@sap-cloud-sdk/util' ; import { OpenApiSchema , OpenApiObjectSchema , OpenApiObjectSchemaProperty } from '../openapi-types' ; import { getType } from '../parser' ; import { isReferenceObject , isArraySchema , isObjectSchema , isEnumSchema , isOneOfSchema , isAllOfSchema , isAnyOfSchema , isNotSchema , getSchemaPropertiesDocumentation } from '../schema-util' ; param schema returns internal export function serializeSchema ( schema ) { if ( isReferenceObject ( schema ) ) { return schema . schemaName ; } if ( isArraySchema ( schema ) ) { const type = serializeSchema ( schema . items ) ; return schema . uniqueItems ? `Set<${ type }>` : `${ type }[]` ; } if ( isObjectSchema ( schema ) ) { return serializeObjectSchema ( schema ) ; } if ( isEnumSchema ( schema ) ) { return schema . enum . join ( ' | ' ) ; } if ( isOneOfSchema ( schema ) ) { return schema . oneOf . map ( type => serializeSchema ( type ) ) . join ( ' | ' ) ; } if ( isAllOfSchema ( schema ) ) { return schema . allOf . map ( type => serializeSchema ( type ) ) . join ( ' & ' ) ; } if ( isAnyOfSchema ( schema ) ) { return schema . anyOf . map ( type => serializeSchema ( type ) ) . join ( ' | ' ) ; } if ( isNotSchema ( schema ) ) { return codeBlock `any` ; } return getType ( schema . type ) ; } function serializeObjectSchema ( schema ) { if ( ! schema . properties . length && typeof schema . additionalProperties !== 'object' ) { return 'Record<string, any>' ; } const types = [ ] ; if ( schema . properties . length ) { types . push ( serializeObjectSchemaForProperties ( schema . properties ) ) ; } if ( schema . additionalProperties ) { types . push ( `Record<string, ${ serializeSchema ( schema . additionalProperties ) }>` ) ; } return types . join ( ' | ' ) ; } function serializeObjectSchemaForProperties ( properties ) { return codeBlock `{
      ${ properties . map ( property => serializePropertyWithDocumentation ( property ) ) . join ( unixEOL ) }
    }` ; } function serializeProperty ( name , required , type ) { return `'${ name }'${ required ? '' : '?' }: ${ type };` ; } function serializePropertyWithDocumentation ( property ) { const documentation = schemaPropertyDocumentation ( property ) ; const serialized = serializeProperty ( property . name , property . required , serializeSchema ( property . schema ) ) ; if ( documentation ) { return [ documentation , serialized ] . join ( unixEOL ) ; } return serialized ; } internal export function schemaPropertyDocumentation ( schema ) { const signature = [ ] ; if ( schema . description ) { signature . push ( schema . description ) ; } signature . push ( ... getSchemaPropertiesDocumentation ( schema . schemaProperties ) ) ; return documentationBlock `${ signature . join ( unixEOL ) }` ; }