@fern-api/go-dynamic-snippets 1.22.2 → 1.22.5
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 +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ ${p}`);this.isAtStartOfLine()&&(s=p+s),i&&(s+=`
|
|
|
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 a=t[0];a!=null&&i(a);for(let p=1;p<t.length;p++){this.write(e);let s=t[p];s!=null&&i(s)}}}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
|
-
`),a=ne&&!this.shouldSkipTracking(i)&&this.lastCharacterIsNewline;e&&(this.lastLine=`${this.lastLine}${i.shift()||""}`),this.lastCharacterIsNewline&&i.pop(),this.lineBuffer.push(...i),a&&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(ct({maxFrames:15,skip:3}))),...this.formatStack(this.filterStack(this.nodeStackFrames),"node")];switch(this.nodeStackFrames.length=0,At){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 ai}};y();w();g();y();w();g();y();w();g();var Ge=class{constructor(){m(this,"reporter");m(this,"snippet");m(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 je=class{constructor(t){m(this,"context");this.context=t}async generate(t,e={}){let i=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let a=new Ge;for(let p of i){let s=this.context.clone(),f=this.createSnippetGenerator(s);try{let I=await f.generateSnippet({endpoint:p,request:t,options:e});if(s.errors.empty())return{snippet:I,errors:void 0};a.update({context:s,snippet:I})}catch(I){a.err==null&&(a.err=I)}}return a.getResponseOrThrow({endpoint:t.endpoint})}async generateSnippetAst(t,e={}){let i=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let a;for(let p of i){let s=this.context.clone(),f=this.createSnippetGenerator(s);try{return await f.generateSnippetAst({endpoint:p,request:t,options:e})}catch(I){a=I}}throw a??new Error(`Failed to generate snippet AST for endpoint: ${t.endpoint.method} ${t.endpoint.path}`)}generateSync(t,e={}){let i=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let a=new Ge;for(let p of i){let s=this.context.clone(),f=this.createSnippetGenerator(s);try{let I=f.generateSnippetSync({endpoint:p,request:t,options:e});if(s.errors.empty())return{snippet:I,errors:void 0};a.update({context:s,snippet:I})}catch(I){a.err==null&&(a.err=I)}}return a.getResponseOrThrow({endpoint:t.endpoint})}};y();w();g();y();w();g();y();w();g();var He=class{constructor(){m(this,"REFERENCE_REGEX",/^(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[1]==null||i[2]==null))return{method:i[1],path:i[2]}}};y();w();g();var L={Critical:"CRITICAL",Warning:"WARNING"},ze=class u{constructor(){m(this,"errors");m(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}clone(){let t=new u;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 Ye=class{constructor({ir:t,config:e,options:i={}}){m(this,"config");m(this,"options");m(this,"errors");m(this,"_ir");m(this,"httpEndpointReferenceParser");this._ir=t,this.config=e,this.options=i,this.errors=new ze,this.httpEndpointReferenceParser=new He}associateQueryParametersByWireValue({parameters:t,values:e}){let i=[];for(let[a,p]of Object.entries(e)){this.errors.scope(a);try{let s=t.find(I=>I.name.wireValue===a);if(s==null)throw this.newParameterNotRecognizedError(a);let f=this.isListTypeReference(s.typeReference)&&!Array.isArray(p)?[p]:p;i.push({name:s.name,typeReference:s.typeReference,value:f})}finally{this.errors.unscope()}}return i}associateByWireValueOrDefault({parameters:t,values:e}){let i=[];for(let a of t){this.errors.scope(a.name.wireValue);try{let p=e[a.name.wireValue];if(p==null)if(a.typeReference.type==="primitive"&&a.typeReference.value==="STRING")p=`<${a.name.wireValue}>`;else{this.errors.add({severity:L.Critical,message:this.newParameterNotRecognizedError(a.name.wireValue).message});continue}i.push({name:a.name,typeReference:a.typeReference,value:p})}finally{this.errors.unscope()}}return i}associateByWireValue({parameters:t,values:e,ignoreMissingParameters:i}){let a=[];for(let[p,s]of Object.entries(e)){this.errors.scope(p);try{let f=t.find(I=>I.name.wireValue===p);if(f==null){if(i)continue;this.errors.add({severity:L.Critical,message:this.newParameterNotRecognizedError(p).message});continue}a.push({name:f.name,typeReference:f.typeReference,value:s})}finally{this.errors.unscope()}}return a}getExampleObjectProperties({parameters:t,snippetObject:e}){let i=typeof e=="object"&&e!==null&&!Array.isArray(e)?e:{},a=[];for(let p of t){let s=i[p.name.wireValue];s==null?p.typeReference.type==="nullable"&&a.push({name:p.name,typeReference:p.typeReference,value:null}):a.push({name:p.name,typeReference:p.typeReference,value:s})}return a}getSingleFileValue({property:t,record:e}){let i=e[t.wireValue];if(i!=null){if(typeof i!="string"){this.errors.add({severity:L.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:L.Critical,message:`Expected file array value to be an array of strings, got ${typeof i}`});return}let a=[];for(let p of i){if(typeof p!="string"){this.errors.add({severity:L.Critical,message:`Expected file array value to be an array of strings, got ${typeof p}`});return}a.push(p)}return a}getRecord(t){if(typeof t!="object"||Array.isArray(t)){this.errors.add({severity:L.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:L.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 a=t.discriminant.wireValue,p=i[a];if(p==null){this.errors.add({severity:L.Critical,message:this.newParameterNotRecognizedError(a).message});return}if(typeof p!="string"){this.errors.add({severity:L.Critical,message:`Expected discriminant value to be a string but got: ${typeof p}`});return}let s=t.types[p];if(s==null){this.errors.add({severity:L.Critical,message:`No type found for discriminant value "${p}"`});return}let{[a]:f,...I}=i;return{singleDiscriminatedUnionType:s,discriminantValue:s.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}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:U(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(a=>a.id===t);return i?.name}case"multipleBaseUrls":{let i=e.environments.find(a=>a.id===t);return i?.name}default:U(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:L.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:L.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:L.Critical,message:"Multiple environments are not supported; use the baseUrl option instead"}),!1;let a=new Set(Oe(i.urls));for(let p of Oe(t))a.has(p)&&a.delete(p);return a.size>0?(this.errors.add({severity:L.Critical,message:`The provided environments are invalid; got: [${Object.keys(t).join(", ")}], expected: [${Oe(i.urls).join(", ")}]`}),!1):!0}}}getValueAsNumber({value:t}){if(typeof t!="number"){this.errors.add({severity:L.Critical,message:this.newTypeMismatchError({expected:"number",value:t}).message});return}return t}getValueAsBoolean({value:t}){if(typeof t!="boolean"){this.errors.add({severity:L.Critical,message:this.newTypeMismatchError({expected:"boolean",value:t}).message});return}return t}getValueAsString({value:t}){if(typeof t!="string"){this.errors.add({severity:L.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:U(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:U(e)}})}fileUploadHasFileProperties({fileUpload:t}){return t.properties.some(e=>{switch(e.type){case"file":case"fileArray":return!0;case"bodyProperty":return!1;default:U(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}};y();w();g();var re={PathParameters:"pathParameters",QueryParameters:"queryParameters",Headers:"headers",RequestBody:"requestBody"};y();w();g();y();w();g();function Ke(u,t){let e=u.split("/").pop()??u;return t?.stripExtension?e.split(".")[0]??e:e}y();w();g();y();w();g();y();w();g();y();w();g();var ci=/[^0-9a-zA-Z_]/g,ui=["break","case","chan","const","continue","default","defer","else","fallthrough","for","func","go","goto","if","import","interface","map","package","range","return","select","struct","switch","type","var"],Ie=class extends qe{constructor({packageName:e,rootImportPath:i,importPath:a,customConfig:p,formatter:s}){super();m(this,"packageName");m(this,"rootImportPath");m(this,"importPath");m(this,"customConfig");m(this,"formatter");m(this,"imports",{});this.packageName=e,this.rootImportPath=i,this.importPath=a,this.customConfig=p,this.formatter=s??new We}addImport(e){let i=this.imports[e];if(i!=null)return i;if(e===this.rootImportPath&&this.customConfig.packageName!=null)return this.imports[e]=this.customConfig.packageName,this.customConfig.packageName;let a=new Set(Object.values(this.imports)),p=e.split("/");for(let f=1;f<=p.length;f++){let I=p.slice(-f),x=this.getValidAlias(I.join(""));if(!a.has(x))return this.imports[e]=x,x}let s=this.getValidAlias(Ke(e));for(;a.has(s);)s="_"+s;return this.imports[e]=s,s}isValidAlias(e){return!ui.includes(e)}getValidAlias(e){let i=e.split("-");if(i[0]==null)return e;let a=i.map(p=>p.replace(ci,"")).join("");return this.isValidAlias(a)?a:"_"+a}};var Je=class extends Ie{constructor({packageName:t,rootImportPath:e,importPath:i,customConfig:a,formatter:p}){super({packageName:t,rootImportPath:e,importPath:i,customConfig:a,formatter:p})}async toStringAsync(){let t=this.getContent();if(this.formatter!=null)try{return this.formatter.format(t)}catch(e){throw new Error(`Failed to format Go file: ${e}
|
|
15
|
+
`),a=ne&&!this.shouldSkipTracking(i)&&this.lastCharacterIsNewline;e&&(this.lastLine=`${this.lastLine}${i.shift()||""}`),this.lastCharacterIsNewline&&i.pop(),this.lineBuffer.push(...i),a&&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(ct({maxFrames:15,skip:3}))),...this.formatStack(this.filterStack(this.nodeStackFrames),"node")];switch(this.nodeStackFrames.length=0,At){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 ai}};y();w();g();y();w();g();y();w();g();var Ge=class{constructor(){m(this,"reporter");m(this,"snippet");m(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 je=class{constructor(t){m(this,"context");this.context=t}async generate(t,e={}){let i=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let a=new Ge;for(let p of i){let s=this.context.clone(),f=this.createSnippetGenerator(s);try{let I=await f.generateSnippet({endpoint:p,request:t,options:e});if(s.errors.empty())return{snippet:I,errors:void 0};a.update({context:s,snippet:I})}catch(I){a.err==null&&(a.err=I)}}return a.getResponseOrThrow({endpoint:t.endpoint})}async generateSnippetAst(t,e={}){let i=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let a;for(let p of i){let s=this.context.clone(),f=this.createSnippetGenerator(s);try{return await f.generateSnippetAst({endpoint:p,request:t,options:e})}catch(I){a=I}}throw a??new Error(`Failed to generate snippet AST for endpoint: ${t.endpoint.method} ${t.endpoint.path}`)}generateSync(t,e={}){let i=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(i.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let a=new Ge;for(let p of i){let s=this.context.clone(),f=this.createSnippetGenerator(s);try{let I=f.generateSnippetSync({endpoint:p,request:t,options:e});if(s.errors.empty())return{snippet:I,errors:void 0};a.update({context:s,snippet:I})}catch(I){a.err==null&&(a.err=I)}}return a.getResponseOrThrow({endpoint:t.endpoint})}};y();w();g();y();w();g();y();w();g();var He=class{constructor(){m(this,"REFERENCE_REGEX",/^(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[1]==null||i[2]==null))return{method:i[1],path:i[2]}}};y();w();g();var L={Critical:"CRITICAL",Warning:"WARNING"},ze=class u{constructor(){m(this,"errors");m(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}clone(){let t=new u;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 Ye=class{constructor({ir:t,config:e,options:i={}}){m(this,"config");m(this,"options");m(this,"errors");m(this,"_ir");m(this,"httpEndpointReferenceParser");this._ir=t,this.config=e,this.options=i,this.errors=new ze,this.httpEndpointReferenceParser=new He}associateQueryParametersByWireValue({parameters:t,values:e}){let i=[];for(let[a,p]of Object.entries(e))if(p!==void 0){this.errors.scope(a);try{let s=t.find(I=>I.name.wireValue===a);if(s==null)throw this.newParameterNotRecognizedError(a);let f=this.isListTypeReference(s.typeReference)&&!Array.isArray(p)?[p]:p;i.push({name:s.name,typeReference:s.typeReference,value:f})}finally{this.errors.unscope()}}return i}associateByWireValueOrDefault({parameters:t,values:e}){let i=[];for(let a of t){this.errors.scope(a.name.wireValue);try{let p=e[a.name.wireValue];if(p==null)if(a.typeReference.type==="primitive"&&a.typeReference.value==="STRING")p=`<${a.name.wireValue}>`;else{this.errors.add({severity:L.Critical,message:this.newParameterNotRecognizedError(a.name.wireValue).message});continue}i.push({name:a.name,typeReference:a.typeReference,value:p})}finally{this.errors.unscope()}}return i}associateByWireValue({parameters:t,values:e,ignoreMissingParameters:i}){let a=[];for(let[p,s]of Object.entries(e))if(s!==void 0){this.errors.scope(p);try{let f=t.find(I=>I.name.wireValue===p);if(f==null){if(i)continue;this.errors.add({severity:L.Critical,message:this.newParameterNotRecognizedError(p).message});continue}a.push({name:f.name,typeReference:f.typeReference,value:s})}finally{this.errors.unscope()}}return a}getExampleObjectProperties({parameters:t,snippetObject:e}){let i=typeof e=="object"&&e!==null&&!Array.isArray(e)?e:{},a=[];for(let p of t){let s=i[p.name.wireValue];s==null?p.typeReference.type==="nullable"&&a.push({name:p.name,typeReference:p.typeReference,value:null}):a.push({name:p.name,typeReference:p.typeReference,value:s})}return a}getSingleFileValue({property:t,record:e}){let i=e[t.wireValue];if(i!=null){if(typeof i!="string"){this.errors.add({severity:L.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:L.Critical,message:`Expected file array value to be an array of strings, got ${typeof i}`});return}let a=[];for(let p of i){if(typeof p!="string"){this.errors.add({severity:L.Critical,message:`Expected file array value to be an array of strings, got ${typeof p}`});return}a.push(p)}return a}getRecord(t){if(typeof t!="object"||Array.isArray(t)){this.errors.add({severity:L.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:L.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 a=t.discriminant.wireValue,p=i[a];if(p==null){this.errors.add({severity:L.Critical,message:this.newParameterNotRecognizedError(a).message});return}if(typeof p!="string"){this.errors.add({severity:L.Critical,message:`Expected discriminant value to be a string but got: ${typeof p}`});return}let s=t.types[p];if(s==null){this.errors.add({severity:L.Critical,message:`No type found for discriminant value "${p}"`});return}let{[a]:f,...I}=i;return{singleDiscriminatedUnionType:s,discriminantValue:s.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:U(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(a=>a.id===t);return i?.name}case"multipleBaseUrls":{let i=e.environments.find(a=>a.id===t);return i?.name}default:U(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:L.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:L.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:L.Critical,message:"Multiple environments are not supported; use the baseUrl option instead"}),!1;let a=new Set(Oe(i.urls));for(let p of Oe(t))a.has(p)&&a.delete(p);return a.size>0?(this.errors.add({severity:L.Critical,message:`The provided environments are invalid; got: [${Object.keys(t).join(", ")}], expected: [${Oe(i.urls).join(", ")}]`}),!1):!0}}}getValueAsNumber({value:t}){if(typeof t!="number"){this.errors.add({severity:L.Critical,message:this.newTypeMismatchError({expected:"number",value:t}).message});return}return t}getValueAsBoolean({value:t}){if(typeof t!="boolean"){this.errors.add({severity:L.Critical,message:this.newTypeMismatchError({expected:"boolean",value:t}).message});return}return t}getValueAsString({value:t}){if(typeof t!="string"){this.errors.add({severity:L.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:U(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:U(e)}})}fileUploadHasFileProperties({fileUpload:t}){return t.properties.some(e=>{switch(e.type){case"file":case"fileArray":return!0;case"bodyProperty":return!1;default:U(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}};y();w();g();var re={PathParameters:"pathParameters",QueryParameters:"queryParameters",Headers:"headers",RequestBody:"requestBody"};y();w();g();y();w();g();function Ke(u,t){let e=u.split("/").pop()??u;return t?.stripExtension?e.split(".")[0]??e:e}y();w();g();y();w();g();y();w();g();y();w();g();var ci=/[^0-9a-zA-Z_]/g,ui=["break","case","chan","const","continue","default","defer","else","fallthrough","for","func","go","goto","if","import","interface","map","package","range","return","select","struct","switch","type","var"],Ie=class extends qe{constructor({packageName:e,rootImportPath:i,importPath:a,customConfig:p,formatter:s}){super();m(this,"packageName");m(this,"rootImportPath");m(this,"importPath");m(this,"customConfig");m(this,"formatter");m(this,"imports",{});this.packageName=e,this.rootImportPath=i,this.importPath=a,this.customConfig=p,this.formatter=s??new We}addImport(e){let i=this.imports[e];if(i!=null)return i;if(e===this.rootImportPath&&this.customConfig.packageName!=null)return this.imports[e]=this.customConfig.packageName,this.customConfig.packageName;let a=new Set(Object.values(this.imports)),p=e.split("/");for(let f=1;f<=p.length;f++){let I=p.slice(-f),x=this.getValidAlias(I.join(""));if(!a.has(x))return this.imports[e]=x,x}let s=this.getValidAlias(Ke(e));for(;a.has(s);)s="_"+s;return this.imports[e]=s,s}isValidAlias(e){return!ui.includes(e)}getValidAlias(e){let i=e.split("-");if(i[0]==null)return e;let a=i.map(p=>p.replace(ci,"")).join("");return this.isValidAlias(a)?a:"_"+a}};var Je=class extends Ie{constructor({packageName:t,rootImportPath:e,importPath:i,customConfig:a,formatter:p}){super({packageName:t,rootImportPath:e,importPath:i,customConfig:a,formatter:p})}async toStringAsync(){let t=this.getContent();if(this.formatter!=null)try{return this.formatter.format(t)}catch(e){throw new Error(`Failed to format Go 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 Go file: ${e}
|
|
17
17
|
${t}`)}return t}getContent(){let t=`package ${this.packageName}
|
|
18
18
|
|