import { ResponsesType } from "./response" ; import { Schemas } from "./schema" ; import { normalizePath } from "./util" ; import { REF , parseObject , parseArray } from "./parse" ; import { isObject , isArray , DataType } from "./dataType" ; import { getRefName } from "../utils" ; type MockData = { [ path ] : any ; } ; export const composeMockData = ( responses , schemas ) : MockData => { const ret = { } ; Object . keys ( responses ) . forEach ( ( path ) => { const res = responses [ path ] ; const pathKey = normalizePath ( path ) ; let response = "" ; if ( ! res ) { return ; } Object . entries ( res . response ) . forEach ( ( [ status , content ] ) => { const val = content ?. [ "application/json" ] || content ?. [ "application/octet-stream" ] || content ?. [ "multipart/form-data" ] ; if ( ! val ) { return ; } if ( val . example ) { response = val . example ; } else if ( val . examples ) { const examplesKey = Object . keys ( val . examples ) ; if ( examplesKey . length <= 1 ) { response = val . examples ; } else { } } else if ( "schema" in val ) { const { schema } = val ; const ref = schema [ REF ] ; if ( ref ) { const schemaName = getRefName ( ref ) ; if ( schemaName ) { response = schemas [ schemaName ] ; } } else { if ( isObject ( schema ) ) { response = parseObject ( schema , schemas ) ; } else if ( isArray ( schema ) ) { response = parseArray ( schema , schemas ) ; } else if ( schema . properties ) { response = schema . properties ; } else if ( schema . type ) { response = DataType . defaultValue ( schema ) ; } } } ret [ pathKey ] = { method : res . method , path : res . path , response : { [ status ] : response } , } ; } ) ; } ) ; return ret ; } ;