@redocly/asyncapi-docs
Version:
Async API docs for Redocly Realm
2 lines (1 loc) • 1.78 kB
JavaScript
function o(n){switch(n){case"null":return{type:"null"};case"boolean":return{type:"boolean"};case"int":case"long":return{type:"integer"};case"float":case"double":return{type:"number"};case"bytes":return{type:"string",contentEncoding:"base64"};case"string":return{type:"string"};default:throw new Error(`Unknown primitive type: ${n}`)}}function l(n){return{type:"string",enum:n.symbols,title:n.name,description:n.doc}}function a(n){var t;const e={},u=[];return(t=n.fields)===null||t===void 0||t.forEach(r=>{e[r.name]=i(r.type),r.doc&&(e[r.name].description=r.doc),r.default!==void 0&&(e[r.name].default=r.default),r.default===void 0&&!f(r.type)&&u.push(r.name)}),{type:"object",title:n.name,description:n.doc,properties:e,required:u.length>0?u:void 0,additionalProperties:!1}}function f(n){return Array.isArray(n)?n.some(t=>t==="null"||typeof t=="object"&&t.type==="null"):!1}function c(n){return{type:"array",items:i(n.items),description:n.doc}}function s(n){return{type:"object",additionalProperties:i(n.values),description:n.doc}}function d(n){const t=n.findIndex(e=>e==="null"||typeof e=="object"&&e.type==="null");if(t!==-1){const e=[...n];return e.splice(t,1),e.length===1?Object.assign(Object.assign({},i(e[0])),{nullable:!0}):{oneOf:e.map(i),nullable:!0}}return{oneOf:n.map(i)}}function i(n){if(typeof n=="string")return o(n);if(Array.isArray(n))return d(n);if(typeof n=="object"&&typeof n.type=="string")switch(n.type){case"record":return a(n);case"enum":return l(n);case"array":return c(n);case"map":return s(n);default:return o(n.type)}if(typeof n=="object"&&n.items)return c(n);if(typeof n=="object"&&n.values)return s(n);throw new Error(`Invalid Avro schema type: ${JSON.stringify(n)}`)}function p(n){const t=i(n);return Object.assign({},t)}export{p as avroToJsonSchema};