import { flat , titleFormat } from '@sap-cloud-sdk/util' ; import { OpenAPIV3 } from 'openapi-types' ; import { OpenApiAllOfSchema , OpenApiAnyOfSchema , OpenApiArraySchema , OpenApiEnumSchema , OpenApiNotSchema , OpenApiObjectSchema , OpenApiOneOfSchema , OpenApiReferenceSchema , OpenApiSchema , OpenApiSchemaProperties , SchemaNaming } from './openapi-types' ; import { SchemaRefMapping } from './parser' ; param schemas returns internal export function collectRefs ( ... schemas ) { return getUniqueRefs ( flat ( schemas . map ( schema => collectAllRefs ( schema ) ) ) ) ; } param schema returns function collectAllRefs ( schema ) { if ( ! schema ) { return [ ] ; } if ( isReferenceObject ( schema ) ) { return [ schema ] ; } return Object . values ( schema ) . filter ( value => typeof value === 'object' ) . reduce ( ( refs , value ) => [ ... refs , ... collectAllRefs ( value ) ] , [ ] ) ; } param refs returns internal export function getUniqueRefs ( refs ) { return refs . reduce ( ( uniqueRefs , collectedRef ) => { if ( ! uniqueRefs . some ( ref => ref . $ref === collectedRef . $ref ) ) { uniqueRefs . push ( collectedRef ) ; } return uniqueRefs ; } , [ ] ) ; } param obj returns internal export function isReferenceObject ( obj ) { return ! ! obj ?. $ref ; } param obj returns internal export function isArraySchema ( obj ) { return obj ?. items ; } param obj returns internal export function isObjectSchema ( obj ) { return obj ?. properties ; } param obj returns internal export function isEnumSchema ( obj ) { return obj ?. enum ; } param obj returns internal export function isOneOfSchema ( obj ) { return obj ?. oneOf ; } param obj returns internal export function isAllOfSchema ( obj ) { return obj ?. allOf ; } param obj returns internal export function isAnyOfSchema ( obj ) { return obj ?. anyOf ; } param obj returns internal export function isNotSchema ( obj ) { return obj ?. not ; } param obj param schemaRefMapping returns internal export function getSchemaNamingFromRef ( obj , schemaRefMapping ) { const ref = isReferenceObject ( obj ) ? obj . $ref : obj ; const schemaNaming = schemaRefMapping [ ref ] ; if ( ! schemaNaming ) { throw new Error ( `Could not find schema naming for reference path '${ ref }'. Schema does not exist.` ) ; } return schemaNaming ; } internal export function getSchemaPropertiesDocumentation ( schemaProperties ) { return Object . entries ( schemaProperties || [ ] ) . map ( ( [ propertyName , value ] ) => { if ( propertyName === 'deprecated' ) { return '@deprecated' ; } if ( propertyName === 'example' ) { return `@example ${ JSON . stringify ( schemaProperties ?. example , null , 2 ) }` ; } return `${ titleFormat ( propertyName ) }: ${ JSON . stringify ( value , null , 2 ) }.` ; } ) ; }