@fern-api/typescript-dynamic-snippets 3.70.6 → 3.70.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -12,7 +12,7 @@ ${d}`);this.isAtStartOfLine()&&(u=d+u),i&&(u+=`
12
12
  `)}writeSemicolonIfLastCharacterIsNot(){this.lastCharacterIsSemicolon||this.writeInternal(";")}writeNewLineIfLastLineNot(){this.lastCharacterIsNewline||this.writeInternal(`
13
13
  `)}indent(){this.indentLevel++}dedent(){this.indentLevel--}delimit({nodes:t,delimiter:e,writeFunction:i}){if(t.length>0){let p=t[0];p!=null&&i(p);for(let d=1;d<t.length;d++){this.write(e);let u=t[d];u!=null&&i(u)}}}toString(){return this.buffer}writeInternal(t){if(t.length===0)return;let e=!this.lastCharacterIsNewline;this.hasWrittenAnything=!0,this.lastCharacterIsNewline=t.endsWith(`
14
14
  `),this.lastCharacterIsSemicolon=t.endsWith(";");let i=t.split(`
15
- `),p=X&&!this.shouldSkipTracking(i)&&this.lastCharacterIsNewline;e&&(this.lastLine=`${this.lastLine}${i.shift()||""}`),this.lastCharacterIsNewline&&i.pop(),this.lineBuffer.push(...i),p&&this.appendTrackingComment()}set lastLine(t){this.lineBuffer.length>0?this.lineBuffer[this.lineBuffer.length-1]=t:this.lineBuffer.push(t)}get lastLine(){return this.lineBuffer[this.lineBuffer.length-1]||""}shouldSkipTracking(t){return!1}formatStack(t,e=""){return t.map(i=>`${e?`(${e}) `:""} ${i.fn} - ${i.path} : ${i.position}`)}filterStack(t){return t.filter(e=>!e.fn.startsWith(`${this.constructor.name}.`))}appendTrackingComment(){let t=[...this.formatStack(this.filterStack(Ke({maxFrames:15,skip:3}))),...this.formatStack(this.filterStack(this.nodeStackFrames),"node")];switch(this.nodeStackFrames.length=0,ft){case"single":return this.singleLineTrackingComment(t);case"multiline":return this.multiLineTrackingComment(t);case"box":return this.boxTrackingComment(t)}}singleLineTrackingComment(t){this.lastLine=`${this.lastLine} // ${t.join(" ")}`}multiLineTrackingComment(t){this.lineBuffer.push(...t.map(e=>` // ${e}`))}boxTrackingComment(t){this.lineBuffer.push("/*",...t.map(e=>` ${e}`),"*/")}isAtStartOfLine(){return this.lastCharacterIsNewline||!this.hasWrittenAnything}getIndentString(){return" ".repeat(this.indentLevel*this.getTabSize())}getTabSize(){return $a}};l();f();c();l();f();c();l();f();c();var Oe=class{constructor(){v(this,"reporter");v(this,"snippet");v(this,"err");this.snippet=void 0,this.reporter=void 0,this.err=void 0}update({context:t,snippet:e}){this.shouldUpdate({snippet:e,reporter:t.errors})&&(this.reporter=t.errors.clone(),this.snippet=e)}getResponseOrThrow({endpoint:t}){if(this.snippet!=null&&this.reporter!=null)return{snippet:this.snippet,errors:this.reporter.toDynamicSnippetErrors()};throw this.err??new Error(`Failed to generate snippet for endpoint "${t.method} ${t.path}"`)}shouldUpdate({snippet:t,reporter:e}){return this.reporter==null||this.reporter.size()>0&&e.size()===0?!0:t.length>(this.snippet?.length??0)}};var Ue=class{constructor(t){v(this,"context");this.context=t}async generate(t,e={}){let i=this.resolveEndpoints({request:t,options:e});if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let p=new Oe;for(let d of i){let u=this.context.clone(),E=this.createSnippetGenerator(u);try{let I=await E.generateSnippet({endpoint:d,request:t,options:e});if(u.errors.empty())return{snippet:I,errors:void 0};p.update({context:u,snippet:I})}catch(I){p.err==null&&(p.err=I)}}return p.getResponseOrThrow({endpoint:t.endpoint})}async generateSnippetAst(t,e={}){let i=this.resolveEndpoints({request:t,options:e});if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let p;for(let d of i){let u=this.context.clone(),E=this.createSnippetGenerator(u);try{return await E.generateSnippetAst({endpoint:d,request:t,options:e})}catch(I){p=I}}throw p??new Error(`Failed to generate snippet AST for endpoint: ${t.endpoint.method} ${t.endpoint.path}`)}generateSync(t,e={}){let i=this.resolveEndpoints({request:t,options:e});if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let p=new Oe;for(let d of i){let u=this.context.clone(),E=this.createSnippetGenerator(u);try{let I=E.generateSnippetSync({endpoint:d,request:t,options:e});if(u.errors.empty())return{snippet:I,errors:void 0};p.update({context:u,snippet:I})}catch(I){p.err==null&&(p.err=I)}}return p.getResponseOrThrow({endpoint:t.endpoint})}resolveEndpoints({request:t,options:e}){if(e.endpointId!=null){let i=this.context.resolveEndpointById(e.endpointId);if(i==null)throw new Error(`No endpoint found with ID "${e.endpointId}"`);return[i]}return this.context.resolveEndpointLocationOrThrow(t.endpoint)}};l();f();c();l();f();c();l();f();c();var Me=class{constructor(){v(this,"REFERENCE_REGEX",/^(?:(\w+)::)?(GET|POST|PUT|DELETE|PATCH|HEAD)\s(\/\S*)$/)}validate(t){return this.REFERENCE_REGEX.test(t)?{type:"valid"}:{type:"invalid"}}tryParse(t){if(this.validate(t).type==="invalid")return;let i=t.match(this.REFERENCE_REGEX);if(!(i==null||i[2]==null||i[3]==null))return{namespace:i[1],method:i[2],path:i[3]}}};l();f();c();var B={Critical:"CRITICAL",Warning:"WARNING"},$e=class s{constructor(){v(this,"errors");v(this,"path");this.errors=[],this.path=[]}add(t){this.errors.push({...t,path:[...this.path]})}scope(t){this.path.push(t)}unscope(){this.path.pop()}getBySeverity(t){return this.errors.filter(e=>e.severity===t)}empty(){return this.errors.length===0}size(){return this.errors.length}truncate(t){this.errors.length=t}clone(){let t=new s;return t.errors=[...this.errors],t.path=[...this.path],t}reset(){this.errors=[],this.path=[]}toDynamicSnippetErrors(){return this.errors.map(t=>({severity:t.severity,path:t.path!=null?this.pathToStringArray(t.path):void 0,message:t.message}))}pathToStringArray(t){let e=[];for(let i of t){if(typeof i=="string"){e.push(i);continue}e[e.length-1]+=`[${i.index}]`}return e}};var je=class{constructor({ir:t,config:e,options:i={}}){v(this,"config");v(this,"options");v(this,"errors");v(this,"_ir");v(this,"httpEndpointReferenceParser");this._ir=t,this.config=e,this.options=i,this.errors=new $e,this.httpEndpointReferenceParser=new Me}associateQueryParametersByWireValue({parameters:t,values:e}){let i=[];for(let[p,d]of Object.entries(e))if(d!==void 0){this.errors.scope(p);try{let u=t.find(I=>I.name.wireValue===p);if(u==null)throw this.newParameterNotRecognizedError(p);let E=this.isListTypeReference(u.typeReference)&&!Array.isArray(d)?[d]:d;i.push({name:u.name,typeReference:u.typeReference,value:E})}finally{this.errors.unscope()}}return i}associateByWireValueOrDefault({parameters:t,values:e}){let i=[];for(let p of t){this.errors.scope(p.name.wireValue);try{let d=e[p.name.wireValue];if(d==null)if(p.typeReference.type==="primitive"&&p.typeReference.value==="STRING")d=`<${p.name.wireValue}>`;else{this.errors.add({severity:B.Critical,message:this.newParameterNotRecognizedError(p.name.wireValue).message});continue}i.push({name:p.name,typeReference:p.typeReference,value:d})}finally{this.errors.unscope()}}return i}associateByWireValue({parameters:t,values:e,ignoreMissingParameters:i}){let p=[];for(let[d,u]of Object.entries(e))if(u!==void 0){this.errors.scope(d);try{let E=t.find(I=>I.name.wireValue===d);if(E==null){if(i)continue;this.errors.add({severity:B.Critical,message:this.newParameterNotRecognizedError(d).message});continue}p.push({name:E.name,typeReference:E.typeReference,value:u})}finally{this.errors.unscope()}}return p}getExampleObjectProperties({parameters:t,snippetObject:e}){let i=typeof e=="object"&&e!==null&&!Array.isArray(e)?e:{},p=[];for(let d of t){let u=i[d.name.wireValue];u==null?d.typeReference.type==="nullable"&&p.push({name:d.name,typeReference:d.typeReference,value:null}):p.push({name:d.name,typeReference:d.typeReference,value:u})}return p}getSingleFileValue({property:t,record:e}){let i=e[t.wireValue];if(i!=null){if(typeof i!="string"){this.errors.add({severity:B.Critical,message:`Expected file value to be a string, got ${typeof i}`});return}return i}}getFileArrayValues({property:t,record:e}){let i=e[t.wireValue];if(i==null)return;if(!Array.isArray(i)){this.errors.add({severity:B.Critical,message:`Expected file array value to be an array of strings, got ${typeof i}`});return}let p=[];for(let d of i){if(typeof d!="string"){this.errors.add({severity:B.Critical,message:`Expected file array value to be an array of strings, got ${typeof d}`});return}p.push(d)}return p}getRecord(t){if(typeof t!="object"||Array.isArray(t)){this.errors.add({severity:B.Critical,message:`Expected object with key, value pairs but got: ${Array.isArray(t)?"array":typeof t}`});return}return t??{}}resolveNamedType({typeId:t}){let e=this._ir.types[t];if(e==null){this.errors.add({severity:B.Critical,message:`Type identified by "${t}" could not be found`});return}return e}resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e}){let i=this.getRecord(e);if(i==null)return;let p=t.discriminant.wireValue,d=i[p];if(d==null){this.errors.add({severity:B.Critical,message:this.newParameterNotRecognizedError(p).message});return}if(typeof d!="string"){this.errors.add({severity:B.Critical,message:`Expected discriminant value to be a string but got: ${typeof d}`});return}let u=t.types[d];if(u==null){this.errors.add({severity:B.Critical,message:`No type found for discriminant value "${d}"`});return}let{[p]:E,...I}=i;return{singleDiscriminatedUnionType:u,discriminantValue:u.discriminantValue,value:I}}resolveEndpointOrThrow(t){let e=this.httpEndpointReferenceParser.tryParse(t);if(e==null)throw new Error(`Failed to parse endpoint reference "${t}"`);return this.resolveEndpointLocationOrThrow(e)}resolveEndpointLocation(t){let e=[];for(let i of Object.values(this._ir.endpoints))this.parsedEndpointMatches({endpoint:i,parsedEndpoint:t})&&e.push(i);return e}resolveEndpointLocationOrThrow(t){let e=this.resolveEndpointLocation(t);if(e.length===0)throw new Error(`Failed to find endpoint identified by "${t.method} ${t.path}"`);return e}resolveEndpointById(t){return this._ir.endpoints[t]}resolveEndpointByIdOrThrow(t){let e=this.resolveEndpointById(t);if(e==null)throw new Error(`Failed to find endpoint with ID "${t}"`);return e}needsRequestParameter({request:t,inlinePathParameters:e,inlineFileProperties:i}){return this.includePathParametersInWrappedRequest({request:t,inlinePathParameters:e})||t.queryParameters!=null&&t.queryParameters.length>0||t.headers!=null&&t.headers.length>0?!0:t.body!=null?this.includeRequestBodyInWrappedRequest({body:t.body,inlineFileProperties:i}):!t.metadata?.onlyPathParameters}includePathParametersInWrappedRequest({request:t,inlinePathParameters:e}){return e&&(t.metadata?.includePathParameters??!1)}isFileUploadRequestBody(t){switch(t.type){case"fileUpload":return!0;case"properties":case"referenced":return!1;default:P(t)}}resolveEnvironmentName(t){if(this._ir.environments==null)return;let e=this._ir.environments.environments;switch(e.type){case"singleBaseUrl":{let i=e.environments.find(p=>p.id===t);return i?.name}case"multipleBaseUrls":{let i=e.environments.find(p=>p.id===t);return i?.name}default:P(e)}}isSingleEnvironmentID(t){return typeof t=="string"}isMultiEnvironmentValues(t){return typeof t=="object"}validateMultiEnvironmentUrlValues(t){if(this._ir.environments==null)return this.errors.add({severity:B.Critical,message:"Multiple environments are not supported for single base URL environments; use the baseUrl option instead"}),!1;let e=this._ir.environments.environments;switch(e.type){case"singleBaseUrl":return this.errors.add({severity:B.Critical,message:"Multiple environments are not supported for single base URL environments; use the baseUrl option instead"}),!1;case"multipleBaseUrls":{let i=e.environments[0];if(i==null)return this.errors.add({severity:B.Critical,message:"Multiple environments are not supported; use the baseUrl option instead"}),!1;let p=new Set(Le(i.urls));for(let d of Le(t))p.has(d)&&p.delete(d);return p.size>0?(this.errors.add({severity:B.Critical,message:`The provided environments are invalid; got: [${Object.keys(t).join(", ")}], expected: [${Le(i.urls).join(", ")}]`}),!1):!0}}}getValueAsNumber({value:t}){if(typeof t!="number"){this.errors.add({severity:B.Critical,message:this.newTypeMismatchError({expected:"number",value:t}).message});return}return t}getValueAsBoolean({value:t}){if(typeof t!="boolean"){this.errors.add({severity:B.Critical,message:this.newTypeMismatchError({expected:"boolean",value:t}).message});return}return t}getValueAsString({value:t}){if(typeof t!="string"){this.errors.add({severity:B.Critical,message:this.newTypeMismatchError({expected:"string",value:t}).message});return}return t}isOptional(t){switch(t.type){case"nullable":return this.isOptional(t.value);case"optional":return!0;case"named":{let e=this.resolveNamedType({typeId:t.value});return e==null?!1:e.type==="alias"?this.isNullable(e.typeReference):!1}}return!1}isNullable(t){switch(t.type){case"nullable":return!0;case"optional":return this.isNullable(t.value);case"named":{let e=this.resolveNamedType({typeId:t.value});return e==null?!1:e.type==="alias"?this.isNullable(e.typeReference):!1}}return!1}newAuthMismatchError({auth:t,values:e}){return new Error(`Expected auth type ${t.type}, got ${e.type}`)}newParameterNotRecognizedError(t){return new Error(`"${t}" is not a recognized parameter for this endpoint`)}newTypeMismatchError({expected:t,value:e}){return new Error(`Expected ${t}, got ${typeof e}`)}includeRequestBodyInWrappedRequest({body:t,inlineFileProperties:e}){switch(t.type){case"properties":case"referenced":return!0;case"fileUpload":return this.includeFileUploadBodyInWrappedRequest({fileUpload:t,inlineFileProperties:e});default:P(t)}}includeFileUploadBodyInWrappedRequest({fileUpload:t,inlineFileProperties:e}){return this.fileUploadHasBodyProperties({fileUpload:t})||e&&this.fileUploadHasFileProperties({fileUpload:t})}fileUploadHasBodyProperties({fileUpload:t}){return t.properties.some(e=>{switch(e.type){case"file":case"fileArray":return!1;case"bodyProperty":return!0;default:P(e)}})}fileUploadHasFileProperties({fileUpload:t}){return t.properties.some(e=>{switch(e.type){case"file":case"fileArray":return!0;case"bodyProperty":return!1;default:P(e)}})}isListTypeReference(t){return t.type==="optional"?this.isListTypeReference(t.value):t.type==="list"||t.type==="set"}parsedEndpointMatches({endpoint:t,parsedEndpoint:e}){return t.location.method===e.method&&t.location.path===e.path}};l();f();c();var J={PathParameters:"pathParameters",QueryParameters:"queryParameters",Headers:"headers",RequestBody:"requestBody"};l();f();c();l();f();c();l();f();c();l();f();c();l();f();c();l();f();c();var we=class extends Pe{constructor({customConfig:e,formatter:i}){super();v(this,"customConfig");v(this,"formatter");v(this,"imports",{});v(this,"defaultImports",{});v(this,"starImportAliases",{});v(this,"starImportAliasesInverse",{});this.customConfig=e,this.formatter=i??new ke}addImport(e){var i,p;if(e.importFrom!=null){switch(e.importFrom.type){case"default":{this.validateDefault(e);break}case"named":{this.validateNamed(e);break}case"star":{this.validateStar(e);break}}let d=(i=this.imports)[p=e.importFrom.moduleName]??(i[p]=[]);d.map(E=>E.name).includes(e.name)||d.push(e)}}validateDefault(e){var p,d;if(e.importFrom?.type!=="default")return;let i=(p=this.defaultImports)[d=e.importFrom.moduleName]??(p[d]=e);if(i.name!==e.name)throw new Error(`Cannot have multiple default imports for module ${e.importFrom.moduleName}: got ${e.name} but already had ${i.name}`)}validateNamed(e){if(e.importFrom?.type!=="named")return;let p=(this.imports[e.importFrom.moduleName]??[]).filter(u=>u.importFrom?.type==="star");if(p.length>0)throw new Error(`Cannot add named import ${e.name} because non-named imports ${p.map(u=>u.name)} already exist`);let d=[];for(let u of Object.values(this.imports))for(let E of u)E.importFrom?.type==="named"&&E.importFrom.moduleName!==e.importFrom.moduleName&&E.name===e.name&&d.push(E);if(d.length>0)throw new Error(`Cannot add named import from module ${e.importFrom.moduleName} because it is already imported from ${d[0]?.importFrom?.moduleName}`)}validateStar(e){var d,u,E,I;if(e.importFrom?.type!=="star")return;let i=(d=this.starImportAliases)[u=e.importFrom.moduleName]??(d[u]=e.importFrom.starImportAlias);if(i!==e.importFrom.starImportAlias)throw new Error(`Cannot have more than one alias for non-named imports from a module: got ${e.importFrom.starImportAlias} but already have ${i}.`);if(((E=this.starImportAliasesInverse)[I=e.importFrom.starImportAlias]??(E[I]=e.importFrom.moduleName))!==e.importFrom.moduleName)throw new Error(`Attempted to use alias ${e.importFrom.starImportAlias} for more than one module in the same file`)}};var _e=class extends we{constructor({customConfig:t,formatter:e}){super({customConfig:t,formatter:e})}async toStringAsync(){let t=this.getContent();if(this.formatter!=null)try{return this.formatter.format(t)}catch(e){throw new Error(`Failed to format TypeScript file: ${e}
15
+ `),p=X&&!this.shouldSkipTracking(i)&&this.lastCharacterIsNewline;e&&(this.lastLine=`${this.lastLine}${i.shift()||""}`),this.lastCharacterIsNewline&&i.pop(),this.lineBuffer.push(...i),p&&this.appendTrackingComment()}set lastLine(t){this.lineBuffer.length>0?this.lineBuffer[this.lineBuffer.length-1]=t:this.lineBuffer.push(t)}get lastLine(){return this.lineBuffer[this.lineBuffer.length-1]||""}shouldSkipTracking(t){return!1}formatStack(t,e=""){return t.map(i=>`${e?`(${e}) `:""} ${i.fn} - ${i.path} : ${i.position}`)}filterStack(t){return t.filter(e=>!e.fn.startsWith(`${this.constructor.name}.`))}appendTrackingComment(){let t=[...this.formatStack(this.filterStack(Ke({maxFrames:15,skip:3}))),...this.formatStack(this.filterStack(this.nodeStackFrames),"node")];switch(this.nodeStackFrames.length=0,ft){case"single":return this.singleLineTrackingComment(t);case"multiline":return this.multiLineTrackingComment(t);case"box":return this.boxTrackingComment(t)}}singleLineTrackingComment(t){this.lastLine=`${this.lastLine} // ${t.join(" ")}`}multiLineTrackingComment(t){this.lineBuffer.push(...t.map(e=>` // ${e}`))}boxTrackingComment(t){this.lineBuffer.push("/*",...t.map(e=>` ${e}`),"*/")}isAtStartOfLine(){return this.lastCharacterIsNewline||!this.hasWrittenAnything}getIndentString(){return" ".repeat(this.indentLevel*this.getTabSize())}getTabSize(){return $a}};l();f();c();l();f();c();l();f();c();var Oe=class{constructor(){v(this,"reporter");v(this,"snippet");v(this,"err");this.snippet=void 0,this.reporter=void 0,this.err=void 0}update({context:t,snippet:e}){this.shouldUpdate({snippet:e,reporter:t.errors})&&(this.reporter=t.errors.clone(),this.snippet=e)}getResponseOrThrow({endpoint:t}){if(this.snippet!=null&&this.reporter!=null)return{snippet:this.snippet,errors:this.reporter.toDynamicSnippetErrors()};throw this.err??new Error(`Failed to generate snippet for endpoint "${t.method} ${t.path}"`)}shouldUpdate({snippet:t,reporter:e}){return this.reporter==null||this.reporter.size()>0&&e.size()===0?!0:t.length>(this.snippet?.length??0)}};var Ue=class{constructor(t){v(this,"context");this.context=t}async generate(t,e={}){let i=this.resolveEndpoints({request:t,options:e});if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let p=new Oe;for(let d of i){let u=this.context.clone(),E=this.createSnippetGenerator(u);try{let I=await E.generateSnippet({endpoint:d,request:t,options:e});if(u.errors.empty())return{snippet:I,errors:void 0};p.update({context:u,snippet:I})}catch(I){p.err==null&&(p.err=I)}}return p.getResponseOrThrow({endpoint:t.endpoint})}async generateSnippetAst(t,e={}){let i=this.resolveEndpoints({request:t,options:e});if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let p;for(let d of i){let u=this.context.clone(),E=this.createSnippetGenerator(u);try{return await E.generateSnippetAst({endpoint:d,request:t,options:e})}catch(I){p=I}}throw p??new Error(`Failed to generate snippet AST for endpoint: ${t.endpoint.method} ${t.endpoint.path}`)}generateSync(t,e={}){let i=this.resolveEndpoints({request:t,options:e});if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let p=new Oe;for(let d of i){let u=this.context.clone(),E=this.createSnippetGenerator(u);try{let I=E.generateSnippetSync({endpoint:d,request:t,options:e});if(u.errors.empty())return{snippet:I,errors:void 0};p.update({context:u,snippet:I})}catch(I){p.err==null&&(p.err=I)}}return p.getResponseOrThrow({endpoint:t.endpoint})}resolveEndpoints({request:t,options:e}){if(e.endpointId!=null){let i=this.context.resolveEndpointById(e.endpointId);if(i==null)throw new Error(`No endpoint found with ID "${e.endpointId}"`);return[i]}return this.context.resolveEndpointLocationOrThrow(t.endpoint)}};l();f();c();l();f();c();l();f();c();var Me=class{constructor(){v(this,"REFERENCE_REGEX",/^(?:(\w+)::)?(GET|POST|PUT|DELETE|PATCH|HEAD)\s(\/\S*)$/)}validate(t){return this.REFERENCE_REGEX.test(t)?{type:"valid"}:{type:"invalid"}}tryParse(t){if(this.validate(t).type==="invalid")return;let i=t.match(this.REFERENCE_REGEX);if(!(i==null||i[2]==null||i[3]==null))return{namespace:i[1],method:i[2],path:i[3]}}};l();f();c();var B={Critical:"CRITICAL",Warning:"WARNING"},$e=class s{constructor(){v(this,"errors");v(this,"path");this.errors=[],this.path=[]}add(t){this.errors.push({...t,path:[...this.path]})}scope(t){this.path.push(t)}unscope(){this.path.pop()}getBySeverity(t){return this.errors.filter(e=>e.severity===t)}empty(){return this.errors.length===0}size(){return this.errors.length}truncate(t){this.errors.length=t}clone(){let t=new s;return t.errors=[...this.errors],t.path=[...this.path],t}reset(){this.errors=[],this.path=[]}toDynamicSnippetErrors(){return this.errors.map(t=>({severity:t.severity,path:t.path!=null?this.pathToStringArray(t.path):void 0,message:t.message}))}pathToStringArray(t){let e=[];for(let i of t){if(typeof i=="string"){e.push(i);continue}e[e.length-1]+=`[${i.index}]`}return e}};var je=class{constructor({ir:t,config:e,options:i={}}){v(this,"config");v(this,"options");v(this,"errors");v(this,"_ir");v(this,"httpEndpointReferenceParser");this._ir=t,this.config=e,this.options=i,this.errors=new $e,this.httpEndpointReferenceParser=new Me}associateQueryParametersByWireValue({parameters:t,values:e}){let i=[];for(let[p,d]of Object.entries(e))if(d!==void 0){this.errors.scope(p);try{let u=t.find(I=>I.name.wireValue===p);if(u==null)throw this.newParameterNotRecognizedError(p);let E=this.isListTypeReference(u.typeReference)&&!Array.isArray(d)?[d]:d;i.push({name:u.name,typeReference:u.typeReference,value:E})}finally{this.errors.unscope()}}return i}associateByWireValueOrDefault({parameters:t,values:e}){let i=[];for(let p of t){this.errors.scope(p.name.wireValue);try{let d=e[p.name.wireValue];if(d==null)if(p.typeReference.type==="primitive"&&p.typeReference.value==="STRING")d=`<${p.name.wireValue}>`;else{this.errors.add({severity:B.Critical,message:this.newParameterNotRecognizedError(p.name.wireValue).message});continue}i.push({name:p.name,typeReference:p.typeReference,value:d})}finally{this.errors.unscope()}}return i}associateByWireValue({parameters:t,values:e,ignoreMissingParameters:i}){let p=[],d=new Set;for(let u of t){let E=u.name.wireValue,I=e[E];if(I!==void 0){d.add(E),this.errors.scope(E);try{p.push({name:u.name,typeReference:u.typeReference,value:I})}finally{this.errors.unscope()}}}if(!i){for(let[u,E]of Object.entries(e))if(!(E===void 0||d.has(u))){this.errors.scope(u);try{this.errors.add({severity:B.Critical,message:this.newParameterNotRecognizedError(u).message})}finally{this.errors.unscope()}}}return p}getExampleObjectProperties({parameters:t,snippetObject:e}){let i=typeof e=="object"&&e!==null&&!Array.isArray(e)?e:{},p=[];for(let d of t){let u=i[d.name.wireValue];u==null?d.typeReference.type==="nullable"&&p.push({name:d.name,typeReference:d.typeReference,value:null}):p.push({name:d.name,typeReference:d.typeReference,value:u})}return p}getSingleFileValue({property:t,record:e}){let i=e[t.wireValue];if(i!=null){if(typeof i!="string"){this.errors.add({severity:B.Critical,message:`Expected file value to be a string, got ${typeof i}`});return}return i}}getFileArrayValues({property:t,record:e}){let i=e[t.wireValue];if(i==null)return;if(!Array.isArray(i)){this.errors.add({severity:B.Critical,message:`Expected file array value to be an array of strings, got ${typeof i}`});return}let p=[];for(let d of i){if(typeof d!="string"){this.errors.add({severity:B.Critical,message:`Expected file array value to be an array of strings, got ${typeof d}`});return}p.push(d)}return p}getRecord(t){if(typeof t!="object"||Array.isArray(t)){this.errors.add({severity:B.Critical,message:`Expected object with key, value pairs but got: ${Array.isArray(t)?"array":typeof t}`});return}return t??{}}resolveNamedType({typeId:t}){let e=this._ir.types[t];if(e==null){this.errors.add({severity:B.Critical,message:`Type identified by "${t}" could not be found`});return}return e}resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e}){let i=this.getRecord(e);if(i==null)return;let p=t.discriminant.wireValue,d=i[p];if(d==null){this.errors.add({severity:B.Critical,message:this.newParameterNotRecognizedError(p).message});return}if(typeof d!="string"){this.errors.add({severity:B.Critical,message:`Expected discriminant value to be a string but got: ${typeof d}`});return}let u=t.types[d];if(u==null){this.errors.add({severity:B.Critical,message:`No type found for discriminant value "${d}"`});return}let{[p]:E,...I}=i;return{singleDiscriminatedUnionType:u,discriminantValue:u.discriminantValue,value:I}}resolveEndpointOrThrow(t){let e=this.httpEndpointReferenceParser.tryParse(t);if(e==null)throw new Error(`Failed to parse endpoint reference "${t}"`);return this.resolveEndpointLocationOrThrow(e)}resolveEndpointLocation(t){let e=[];for(let i of Object.values(this._ir.endpoints))this.parsedEndpointMatches({endpoint:i,parsedEndpoint:t})&&e.push(i);return e}resolveEndpointLocationOrThrow(t){let e=this.resolveEndpointLocation(t);if(e.length===0)throw new Error(`Failed to find endpoint identified by "${t.method} ${t.path}"`);return e}resolveEndpointById(t){return this._ir.endpoints[t]}resolveEndpointByIdOrThrow(t){let e=this.resolveEndpointById(t);if(e==null)throw new Error(`Failed to find endpoint with ID "${t}"`);return e}needsRequestParameter({request:t,inlinePathParameters:e,inlineFileProperties:i}){return this.includePathParametersInWrappedRequest({request:t,inlinePathParameters:e})||t.queryParameters!=null&&t.queryParameters.length>0||t.headers!=null&&t.headers.length>0?!0:t.body!=null?this.includeRequestBodyInWrappedRequest({body:t.body,inlineFileProperties:i}):!t.metadata?.onlyPathParameters}includePathParametersInWrappedRequest({request:t,inlinePathParameters:e}){return e&&(t.metadata?.includePathParameters??!1)}isFileUploadRequestBody(t){switch(t.type){case"fileUpload":return!0;case"properties":case"referenced":return!1;default:P(t)}}resolveEnvironmentName(t){if(this._ir.environments==null)return;let e=this._ir.environments.environments;switch(e.type){case"singleBaseUrl":{let i=e.environments.find(p=>p.id===t);return i?.name}case"multipleBaseUrls":{let i=e.environments.find(p=>p.id===t);return i?.name}default:P(e)}}isSingleEnvironmentID(t){return typeof t=="string"}isMultiEnvironmentValues(t){return typeof t=="object"}validateMultiEnvironmentUrlValues(t){if(this._ir.environments==null)return this.errors.add({severity:B.Critical,message:"Multiple environments are not supported for single base URL environments; use the baseUrl option instead"}),!1;let e=this._ir.environments.environments;switch(e.type){case"singleBaseUrl":return this.errors.add({severity:B.Critical,message:"Multiple environments are not supported for single base URL environments; use the baseUrl option instead"}),!1;case"multipleBaseUrls":{let i=e.environments[0];if(i==null)return this.errors.add({severity:B.Critical,message:"Multiple environments are not supported; use the baseUrl option instead"}),!1;let p=new Set(Le(i.urls));for(let d of Le(t))p.has(d)&&p.delete(d);return p.size>0?(this.errors.add({severity:B.Critical,message:`The provided environments are invalid; got: [${Object.keys(t).join(", ")}], expected: [${Le(i.urls).join(", ")}]`}),!1):!0}}}getValueAsNumber({value:t}){if(typeof t!="number"){this.errors.add({severity:B.Critical,message:this.newTypeMismatchError({expected:"number",value:t}).message});return}return t}getValueAsBoolean({value:t}){if(typeof t!="boolean"){this.errors.add({severity:B.Critical,message:this.newTypeMismatchError({expected:"boolean",value:t}).message});return}return t}getValueAsString({value:t}){if(typeof t!="string"){this.errors.add({severity:B.Critical,message:this.newTypeMismatchError({expected:"string",value:t}).message});return}return t}isOptional(t){switch(t.type){case"nullable":return this.isOptional(t.value);case"optional":return!0;case"named":{let e=this.resolveNamedType({typeId:t.value});return e==null?!1:e.type==="alias"?this.isNullable(e.typeReference):!1}}return!1}isNullable(t){switch(t.type){case"nullable":return!0;case"optional":return this.isNullable(t.value);case"named":{let e=this.resolveNamedType({typeId:t.value});return e==null?!1:e.type==="alias"?this.isNullable(e.typeReference):!1}}return!1}newAuthMismatchError({auth:t,values:e}){return new Error(`Expected auth type ${t.type}, got ${e.type}`)}newParameterNotRecognizedError(t){return new Error(`"${t}" is not a recognized parameter for this endpoint`)}newTypeMismatchError({expected:t,value:e}){return new Error(`Expected ${t}, got ${typeof e}`)}includeRequestBodyInWrappedRequest({body:t,inlineFileProperties:e}){switch(t.type){case"properties":case"referenced":return!0;case"fileUpload":return this.includeFileUploadBodyInWrappedRequest({fileUpload:t,inlineFileProperties:e});default:P(t)}}includeFileUploadBodyInWrappedRequest({fileUpload:t,inlineFileProperties:e}){return this.fileUploadHasBodyProperties({fileUpload:t})||e&&this.fileUploadHasFileProperties({fileUpload:t})}fileUploadHasBodyProperties({fileUpload:t}){return t.properties.some(e=>{switch(e.type){case"file":case"fileArray":return!1;case"bodyProperty":return!0;default:P(e)}})}fileUploadHasFileProperties({fileUpload:t}){return t.properties.some(e=>{switch(e.type){case"file":case"fileArray":return!0;case"bodyProperty":return!1;default:P(e)}})}isListTypeReference(t){return t.type==="optional"?this.isListTypeReference(t.value):t.type==="list"||t.type==="set"}parsedEndpointMatches({endpoint:t,parsedEndpoint:e}){return t.location.method===e.method&&t.location.path===e.path}};l();f();c();var J={PathParameters:"pathParameters",QueryParameters:"queryParameters",Headers:"headers",RequestBody:"requestBody"};l();f();c();l();f();c();l();f();c();l();f();c();l();f();c();l();f();c();var we=class extends Pe{constructor({customConfig:e,formatter:i}){super();v(this,"customConfig");v(this,"formatter");v(this,"imports",{});v(this,"defaultImports",{});v(this,"starImportAliases",{});v(this,"starImportAliasesInverse",{});this.customConfig=e,this.formatter=i??new ke}addImport(e){var i,p;if(e.importFrom!=null){switch(e.importFrom.type){case"default":{this.validateDefault(e);break}case"named":{this.validateNamed(e);break}case"star":{this.validateStar(e);break}}let d=(i=this.imports)[p=e.importFrom.moduleName]??(i[p]=[]);d.map(E=>E.name).includes(e.name)||d.push(e)}}validateDefault(e){var p,d;if(e.importFrom?.type!=="default")return;let i=(p=this.defaultImports)[d=e.importFrom.moduleName]??(p[d]=e);if(i.name!==e.name)throw new Error(`Cannot have multiple default imports for module ${e.importFrom.moduleName}: got ${e.name} but already had ${i.name}`)}validateNamed(e){if(e.importFrom?.type!=="named")return;let p=(this.imports[e.importFrom.moduleName]??[]).filter(u=>u.importFrom?.type==="star");if(p.length>0)throw new Error(`Cannot add named import ${e.name} because non-named imports ${p.map(u=>u.name)} already exist`);let d=[];for(let u of Object.values(this.imports))for(let E of u)E.importFrom?.type==="named"&&E.importFrom.moduleName!==e.importFrom.moduleName&&E.name===e.name&&d.push(E);if(d.length>0)throw new Error(`Cannot add named import from module ${e.importFrom.moduleName} because it is already imported from ${d[0]?.importFrom?.moduleName}`)}validateStar(e){var d,u,E,I;if(e.importFrom?.type!=="star")return;let i=(d=this.starImportAliases)[u=e.importFrom.moduleName]??(d[u]=e.importFrom.starImportAlias);if(i!==e.importFrom.starImportAlias)throw new Error(`Cannot have more than one alias for non-named imports from a module: got ${e.importFrom.starImportAlias} but already have ${i}.`);if(((E=this.starImportAliasesInverse)[I=e.importFrom.starImportAlias]??(E[I]=e.importFrom.moduleName))!==e.importFrom.moduleName)throw new Error(`Attempted to use alias ${e.importFrom.starImportAlias} for more than one module in the same file`)}};var _e=class extends we{constructor({customConfig:t,formatter:e}){super({customConfig:t,formatter:e})}async toStringAsync(){let t=this.getContent();if(this.formatter!=null)try{return this.formatter.format(t)}catch(e){throw new Error(`Failed to format TypeScript file: ${e}
16
16
  ${t}`)}return t}toString(){let t=this.getContent();if(this.formatter!=null)try{return this.formatter.formatSync(t)}catch(e){throw new Error(`Failed to format TypeScript file: ${e}
17
17
  ${t}`)}return t}getContent(){let t=this.stringifyImports();return t.length>0?t+`
18
18
  `+this.buffer:this.buffer}stringifyImports(){let t="";for(let[e,i]of Object.entries(this.imports)){let p=this.defaultImports[e],d="",u=i.filter(I=>I.importFrom?.type==="named"),E=this.starImportAliases[e];if(u.length>0||p!=null||E!=null){if(t+="import",p!=null&&(t+=` ${p.name}`),u.length>0){for(let F of u.slice(0,-1))d+=`${F.name}, `;let I=u[u.length-1];I!=null&&(d+=`${I.name}`),p!=null&&(t+=","),t+=` { ${d} }`}E!=null&&((p!=null||u.length>0)&&(t+=", "),t+=` * as ${E}`),t+=` from "${e}";