UNPKG

anl

Version:
3 lines 5.26 kB
const e=require(`../shared/format.cjs`),t=require(`../shared/naming.cjs`),n=require(`../shared/schema-utils.cjs`),r=require(`../shared/http.cjs`),i={schemas:`#/components/schemas/`,parameters:`#/components/parameters/`,definitions:`#/definitions/`};var a=class{config;schemas;parameters;referenceCache=new Map;resolvedToOriginalName=new Map;handleError;constructor(e,t,n,r){this.config=e,this.schemas=t??{},this.parameters=n??{},this.handleError=r}stringifySchemaResult(t){if(Array.isArray(t)){let n=e.getLineEnding(this.config),r=e.getIndentation(this.config);return`{${n}${t.join(n)}${n}${r}}`}return t}handleComplexType(e){try{return e.oneOf?e.oneOf.map(e=>this.stringifySchemaResult(this.main(e))).join(` | `):e.allOf?e.allOf.map(e=>this.stringifySchemaResult(this.main(e))).join(` & `):e.anyOf?e.anyOf.map(e=>this.stringifySchemaResult(this.main(e))).join(` | `):e.enum?e.type===`number`||e.type===`integer`?e.enum.join(` | `):e.enum.map(e=>`'${e}'`).join(` | `):`unknown`}catch(e){return this.handleError({type:`SCHEMA`,message:`Failed to handle complex type`,details:e}),`unknown`}}referenceObjectParse(e){try{let n=e.$ref,r=this.referenceCache.get(n);if(r)return r;let a=n;n.startsWith(i.schemas)&&(a=n.replace(i.schemas,``)),n.startsWith(i.parameters)&&(a=n.replace(i.parameters,``)),n.startsWith(i.definitions)&&(a=n.replace(i.definitions,``));let o=t.resolveSchemaName(a),s=t.typeNameToFileName(o),c=this.schemas?.[a];t.containsChinese(a)&&this.resolvedToOriginalName.set(o,a);let l=!1;if(c&&!(`$ref`in c)){let e=`properties`in c||c.type===`object`,t=c.type===`array`||`items`in c;`enum`in c&&Array.isArray(c.enum)&&!e&&!t&&(l=!0)}else l=/Enum$/i.test(a);let u=l?t.getEnumTypeName(this.config,o):o,d=t.getServerSegment(this.config),f=t.appendEnumSegment(t.adjustImportPathForSegment(this.config.importEnumPath??``,d),t.getEnumSegment(this.config)),p=d?`../../models/${d}`:`../models`,m=l?`import('${f}/${s}').${u}`:`import('${p}/${s}').${o}`;return this.referenceCache.set(n,m),m}catch(e){return this.handleError({type:`REFERENCE`,message:`Failed to parse reference object`,details:e}),`unknown`}}nonArraySchemaObjectParse(e){if(!e)return`unknown`;if(e.format===`binary`||e.type===`string`&&e.format===`binary`)return`File`;switch(e.type){case`boolean`:return`boolean`;case`integer`:case`number`:return`number`;case`object`:return this.propertiesParse(e.properties);case`string`:return e.format===`binary`?`File`:`string`;default:return`unknown`}}arraySchemaObjectParse(e){if(e.type!==`array`)return``;let{items:t}=e,n=`$ref`in t?t:null,r=t;if(n)return`Array<${this.referenceObjectParse(n)}>`;if(r){let e=this.main(t);return Array.isArray(e)?`Array<{${e.join(` `)}}>`:`Array<${e}>`}return``}propertiesParse(e){return n.formatObjectProperties(e,this.config,e=>this.main(e))}transformResponseModel(t,n){if(!n)return t;if(n.modelPattern&&typeof t==`string`){let e=/^import\('[^']+'\)\.(\w+)$/.exec(t),r=e?e[1]:t;if(!new RegExp(n.modelPattern).test(r))return t}try{switch(n.type){case`unwrap`:{let e=n.dataField??`data`;if(Array.isArray(t))return this.handleError({type:`RESPONSE`,message:`Response model unwrap is not supported for inline object response types`}),t;let r=/^import\('([^']+)'\)\.(\w+)$/.exec(t);if(r){let[,t,n]=r,i=this.resolvedToOriginalName.get(n)??n,a=this.schemas?.[i];if(a&&!(`$ref`in a)){let t=a;if(t.properties?.[e]){let n=t.properties[e];return this.main(n)}else this.handleError({type:`RESPONSE`,message:`Field "${e}" not found in response type "${n}"`})}}return t}case`wrap`:{if(!n.wrapperFields)return this.handleError({type:`RESPONSE`,message:`wrapperFields is required when using wrap transform`}),t;let r=e.getIndentation(this.config),i=r+r,a=[],o=n.dataField??`data`;for(let[e,r]of Object.entries(n.wrapperFields))if(e===o)if(Array.isArray(t)){let n=t.join(` `);a.push(`${i}${e}?: {${n}\n${i}};`)}else a.push(`${i}${e}?: ${t};`);else a.push(`${i}${e}?: ${r};`);return a}case`replace`:return n.wrapperType?n.wrapperType:(this.handleError({type:`RESPONSE`,message:`wrapperType is required when using replace transform`}),t);default:return t}}catch(e){return this.handleError({type:`RESPONSE`,message:`Failed to transform response model`,details:e}),t}}responseObjectParse(e){try{let t=e.content;if(!t)return``;let n;for(let e of r.SUPPORTED_REQUEST_TYPES_ALL)if(t[e]?.schema){n=t[e].schema;break}return n?this.main(n):``}catch(e){return this.handleError({type:`RESPONSE`,message:`Failed to parse response object`,details:e}),``}}main(e){try{if(!e)return`unknown`;if(`oneOf`in e||`allOf`in e||`anyOf`in e||`enum`in e)return this.handleComplexType(e);if(`$ref`in e)return this.referenceObjectParse(e);let t=e,r=t.type,i=n.nullableSuffix(t.nullable),a=n.applyTypeMapping(this.config,t);if(a)return a;if(r===`array`&&t.items)return n.stringifyArrayType(this.main(t.items),this.config);if(r===`object`||typeof t==`object`){if(t.properties){let e=this.propertiesParse(t.properties);return e.length?e:[`unknown`]}if(t.additionalProperties===!0)return`Record<string, unknown>`+i;if(typeof t.additionalProperties==`object`)return`Record<string, ${this.main(t.additionalProperties)}>`+i}return`unknown`}catch(e){return this.handleError({type:`SCHEMA`,message:`Failed to parse schema`,details:e}),`unknown`}}};exports.SchemaResolver=a;