@fern-api/ruby-dynamic-snippets 1.0.0 → 1.0.1
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.js
CHANGED
|
@@ -22,7 +22,7 @@ ${this.buffer}`:this.buffer}stringifyRequires(){return Array.from(this.requires)
|
|
|
22
22
|
`,`
|
|
23
23
|
`);i.includes('"')&&!i.includes("'")&&!i.includes("#")?e.write(`'${i}'`):e.write(`"${i.replaceAll("\\","\\\\").replaceAll('"','\\"')}"`);break}case"int":{e.write(this.internalType.value.toString());break}case"float":{e.write(this.internalType.value.toString());break}case"bool":{e.write(this.internalType.value?"true":"false");break}case"hash":{let i=this.internalType.entries.filter(s=>!l.isNop(s.key)&&!l.isNop(s.value));if(i.length===0){e.write("{}");break}e.write(`{
|
|
24
24
|
`),i.forEach((s,f)=>{f>0&&e.writeLine(","),e.indent();let u=!1;if(s.key instanceof l&&s.key.internalType.type==="str"){let E=s.key.internalType.value;/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(E)&&(e.write(`${E}:`),u=!0)}u||(s.key.write(e),e.write(" =>")),e.write(" "),s.value.write(e),e.dedent()}),e.write(`
|
|
25
|
-
}`);break}case"set":{let i=this.internalType.values.filter(s=>!l.isNop(s));if(i.length===0){e.write("Set.new([])");break}e.write("Set.new(["),i.forEach((s,f)=>{f>0&&e.write(", "),s.write(e)}),e.write("])");break}case"list":{let i=this.internalType.values.filter(s=>!l.isNop(s));if(i.length===0){e.write("[]");break}if(i.length>=2&&i.every(s=>s instanceof l&&s.internalType.type==="str"&&!/[\s\\[\]]/.test(s.internalType.value))){let s=i.map(f=>f.internalType.value);e.write(`%w[${s.join(" ")}]`);break}e.write("["),i.forEach((s,f)=>{f>0&&e.write(", "),s.write(e)}),e.write("]");break}case"nop":break;case"nil":e.write("nil");break;default:L(this.internalType)}}};c();m();d();var tt;(function(l){l[l.Invariant=0]="Invariant",l.Covariant="out",l.Contravariant="in"})(tt||(tt={}));var be=class extends N{constructor({name:e,unchecked:i,bound:s,variance:f,defaultType:u}){super();y(this,"name");y(this,"unchecked");y(this,"bound");y(this,"variance");y(this,"defaultType");this.name=e,this.unchecked=i??!1,this.bound=s,this.variance=f??tt.Invariant,this.defaultType=u}write(e){}writeTypeDefinition(e){this.unchecked&&e.write("unchecked "),this.variance!=tt.Invariant&&e.write(`${this.variance} `),e.write(this.name),this.bound&&(e.write(" < "),this.bound.writeTypeDefinition(e)),this.defaultType&&(e.write(" = "),this.defaultType.writeTypeDefinition(e))}};c();m();d();var qe=class extends W{constructor(){super(...arguments);y(this,"initializer")}write(e){e.write(`&${this.name}`)}};c();m();d();var rt=class extends N{constructor({errorClass:e,message:i}){super();y(this,"errorClass");y(this,"message");this.errorClass=e,this.message=i}write(e){e.write("raise"),this.errorClass&&(e.write(" "),this.errorClass.write(e)),this.message&&(this.errorClass&&e.write(", "),this.message.write(e))}};function nt(l){return new ye(l)}function io(){return nt("true")}function oo(){return nt("false")}function so(){return nt("nil")}var ao={positional(l){return new _e(l)},keyword(l){return new xe(l)},positionalSplat(l){return new je(l)},keywordSplat(l){return new De(l)},yield(l){return new qe(l)}};function uo(l){return new ue(l)}function lo(l){return new ae(l)}function po(l){return new se(l)}function fo(l){return new X(l)}function co(l){return new be(l)}function mo(l){return new ne(l)}function ho(l){return new he(l)}function yo(l){return new ge(l)}function go(l){return new we(l)}function xo(l){return new Oe(l)}function wo(l){return new et(l)}function bo(l){return new Ze(l)}function vo(l){return new rt(l)}function Ao(l,t){let e=l;for(let i of t.toReversed())i.addStatement(e),e=i;return e}c();m();d();var it=class l{constructor({context:t}){y(this,"context");this.context=t}convert(t){if(t.value===null)return this.context.isNullable(t.typeReference)?x.TypeLiteral.nil():(this.context.errors.add({severity:S.Critical,message:"Expected non-null value, but got null"}),x.TypeLiteral.nop());if(t.value===void 0)return x.TypeLiteral.nop();switch(t.typeReference.type){case"list":return this.convertList({list:t.typeReference.value,value:t.value});case"literal":return this.convertLiteral({literalType:t.typeReference.value,value:t.value});case"map":return this.convertMap({map:t.typeReference,value:t.value});case"named":{let e=this.context.resolveNamedType({typeId:t.typeReference.value});return e==null?x.TypeLiteral.nop():this.convertNamed({named:e,value:t.value,as:t.as})}case"optional":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"nullable":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"primitive":return this.convertPrimitive({primitive:t.typeReference.value,value:t.value,as:t.as});case"set":return this.convertSet({set:t.typeReference.value,value:t.value});case"unknown":return x.TypeLiteral.nop();default:L(t.typeReference)}}convertLiteral({literalType:t,value:e}){switch(t.type){case"boolean":{let i=this.context.getValueAsBoolean({value:e});return i==null?x.TypeLiteral.nop():x.TypeLiteral.boolean(i)}case"string":{let i=this.context.getValueAsString({value:e});return i==null?x.TypeLiteral.nop():x.TypeLiteral.string(i)}default:L(t)}}convertList({list:t,value:e}){return Array.isArray(e)?x.TypeLiteral.list(e.map((i,s)=>{this.context.errors.scope({index:s});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}})):(this.context.errors.add({severity:S.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeLiteral.nop())}convertSet({set:t,value:e}){return Array.isArray(e)?x.TypeLiteral.set(e.map((i,s)=>{this.context.errors.scope({index:s});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}})):(this.context.errors.add({severity:S.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeLiteral.nop())}convertMap({map:t,value:e}){return typeof e!="object"||e==null?(this.context.errors.add({severity:S.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),x.TypeLiteral.nop()):x.TypeLiteral.hash(Object.entries(e).map(([i,s])=>{this.context.errors.scope(i);try{return{key:this.convert({typeReference:t.key,value:i,as:"key"}),value:this.convert({typeReference:t.value,value:s})}}finally{this.context.errors.unscope()}}))}convertNamed({named:t,value:e,as:i}){switch(t.type){case"alias":return this.convert({typeReference:t.typeReference,value:e,as:i});case"discriminatedUnion":return this.convertDiscriminatedUnion({discriminatedUnion:t,value:e});case"object":return this.convertObject({object:t,value:e});case"enum":return this.convertEnum({enum_:t,value:e});case"undiscriminatedUnion":return this.convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e});default:L(t)}}convertDiscriminatedUnion({discriminatedUnion:t,value:e}){let i=this.context.resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e});if(i==null)return x.TypeLiteral.nop();let s=i.singleDiscriminatedUnionType,f=this.convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:i,unionVariant:s});return f==null?x.TypeLiteral.nop():x.TypeLiteral.hash(f)}convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:t,unionVariant:e}){switch(e.type){case"samePropertiesAsObject":{let i=this.context.resolveNamedType({typeId:e.typeId});if(i==null)return;let s=this.convertNamed({named:i,value:t.value});return this.extractHashEntries(s)}case"singleProperty":try{this.context.errors.scope(e.discriminantValue.wireValue);let i=this.context.getRecord(t.value);return i==null?[{key:x.TypeLiteral.string(this.context.getPropertyName(e.discriminantValue.name)),value:this.convert({typeReference:e.typeReference,value:t.value})}]:[{key:x.TypeLiteral.string(this.context.getPropertyName(e.discriminantValue.name)),value:this.convert({typeReference:e.typeReference,value:i[e.discriminantValue.wireValue]})}]}finally{this.context.errors.unscope()}case"noProperties":return[];default:L(e)}}extractHashEntries(t){t instanceof x.TypeLiteral}convertEnum({enum_:t,value:e}){let i=this.getEnumValueName({enum_:t,value:e});return i==null?x.TypeLiteral.nop():x.TypeLiteral.string(i)}getEnumValueName({enum_:t,value:e}){if(typeof e!="string"){this.context.errors.add({severity:S.Critical,message:`Expected enum value string, got: ${typeof e}`});return}let i=t.values.find(s=>s.wireValue===e);if(i==null){this.context.errors.add({severity:S.Critical,message:`An enum value named "${e}" does not exist in this context`});return}return i.wireValue}convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e}){let i=this.findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e});return i??x.TypeLiteral.nop()}findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e}){for(let i of t.types){let s=this.context.clone(),u=new l({context:s}).convert({typeReference:i,value:e});if(s.errors.empty())return u}this.context.errors.add({severity:S.Critical,message:`None of the types in the undiscriminated union matched the given "${typeof e}" value`})}convertPrimitive({primitive:t,value:e,as:i}){switch(t){case"INTEGER":case"LONG":case"UINT":case"UINT_64":{let s=this.getValueAsNumber({value:e,as:i});return s==null?x.TypeLiteral.nop():x.TypeLiteral.integer(s)}case"FLOAT":case"DOUBLE":{let s=this.getValueAsNumber({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.float(s)}case"BOOLEAN":{let s=this.getValueAsBoolean({value:e,as:i});return s==null?x.TypeLiteral.nop():x.TypeLiteral.boolean(s)}case"BASE_64":case"BIG_INTEGER":case"STRING":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}case"UUID":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}case"DATE":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}case"DATE_TIME":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}default:L(t)}}convertObject({object:t,value:e}){return typeof e!="object"||e==null?(this.context.errors.add({severity:S.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),x.TypeLiteral.nop()):x.TypeLiteral.hash(Object.entries(e).map(([i,s])=>{this.context.errors.scope(i);let f=t.properties.find(k=>k.name.wireValue===i),u=f?.typeReference??{type:"unknown"},E=f?.name.name.snakeCase.safeName??i,w={key:x.TypeLiteral.string(E),value:this.convert({typeReference:u,value:s})};return this.context.errors.unscope(),w}))}getValueAsNumber({value:t,as:e}){let i=e==="key"&&typeof t=="string"?Number(t):t;return this.context.getValueAsNumber({value:i})}getValueAsBoolean({value:t,as:e}){let i=e==="key"?typeof t=="string"?t==="true":!!t:t;return this.context.getValueAsBoolean({value:i})}};var ot=class l extends $e{constructor({ir:e,config:i}){super({ir:e,config:i});y(this,"ir");y(this,"customConfig");y(this,"dynamicTypeLiteralMapper");this.ir=e,this.customConfig=i.customConfig!=null?i.customConfig:void 0,this.dynamicTypeLiteralMapper=new it({context:this})}clone(){return new l({ir:this.ir,config:this.config})}getRootClientClassReference(){return x.classReference({name:this.getRootClientClassName(),modules:[this.getRootModuleName()]})}getRootClientClassName(){return this.customConfig?.clientModuleName??"Client"}getRootModuleName(){return ht(this.customConfig?.moduleName??this.customConfig?.clientModuleName??this.config.organization)}isSingleEnvironmentID(e){return typeof e=="string"}isMultiEnvironmentValues(e){return typeof e=="object"}getEnvironmentTypeReferenceFromID(e){let i=this.resolveEnvironmentName(e);if(i!=null)return x.codeblock(s=>{s.writeNode(this.getEnvironmentClassReference()),s.write("::"),s.write(this.getEnumName(i))})}getEnvironmentClassReference(){return x.classReference({name:"Environment",modules:[this.getRootModuleName()]})}getEnumName(e){return this.getName(e.screamingSnakeCase.safeName)}getMethodName(e){return this.getName(e.snakeCase.safeName)}getPropertyName(e){return this.getName(e.snakeCase.safeName)}getName(e){return e}};c();m();d();var Un="client",Eo="@client",st=class{constructor({context:t,formatter:e}){y(this,"context");y(this,"formatter");this.context=t,this.formatter=e}async generateSnippet({endpoint:t,request:e}){return await this.buildCodeBlock({endpoint:t,snippet:e}).toStringAsync({customConfig:this.context.customConfig??{},formatter:this.formatter})}generateSnippetSync({endpoint:t,request:e}){return this.buildCodeBlock({endpoint:t,snippet:e}).toString({customConfig:this.context.customConfig??{},formatter:this.formatter})}async generateSnippetAst({endpoint:t,request:e,options:i}){return i?.skipClientInstantiation?this.buildCodeBlockWithoutClient({endpoint:t,snippet:e}):this.buildCodeBlock({endpoint:t,snippet:e})}buildCodeBlock({endpoint:t,snippet:e}){return x.codeblock(i=>{i.writeNodeStatement(this.constructClient({endpoint:t,snippet:e})),i.newLine(),i.writeNodeStatement(this.callMethod({endpoint:t,snippet:e}))})}buildCodeBlockWithoutClient({endpoint:t,snippet:e}){return x.codeblock(i=>{i.writeNodeStatement(this.callMethodOnExistingClient({endpoint:t,snippet:e}))})}constructClient({endpoint:t,snippet:e}){return x.codeblock(i=>{i.addRequire(this.context.getRootModuleName().toLowerCase());let s=this.context.getRootClientClassReference(),f=this.getRootClientBuilderArgs({endpoint:t,snippet:e});i.write(`${Un} = `),i.writeNode(x.instantiateClass({classReference:s,arguments_:f}))})}getRootClientBaseUrlArg({baseUrl:t,environment:e}){if(t!=null&&e!=null)return this.context.errors.add({severity:"CRITICAL",message:"Cannot specify both baseUrl and environment options"}),[];if(t!=null)return[x.keywordArgument({name:"base_url",value:x.TypeLiteral.string(t)})];if(e!=null){if(this.context.isSingleEnvironmentID(e)){let i=this.context.getEnvironmentTypeReferenceFromID(e);return i==null?(this.context.errors.add({severity:"CRITICAL",message:`Environment ID ${e} not found`}),[]):[x.keywordArgument({name:"environment",value:i})]}if(this.context.isMultiEnvironmentValues(e))return this.context.errors.add({severity:"CRITICAL",message:"Multi-environment values are not supported in Ruby snippets yet"}),[]}return[]}getRootClientAuthArgs({auth:t,values:e}){if(e.type!==t.type)return this.addError(this.context.newAuthMismatchError({auth:t,values:e}).message),[];switch(t.type){case"basic":return e.type==="basic"?this.getRootClientBasicAuthArgs({auth:t,values:e}):[];case"bearer":return e.type==="bearer"?this.getRootClientBearerAuthArgs({auth:t,values:e}):[];case"header":return e.type==="header"?this.getRootClientHeaderAuthArgs({auth:t,values:e}):[];case"oauth":return e.type==="oauth"?this.getRootClientOAuthArgs({auth:t,values:e}):[];case"inferred":return[];default:L(t)}}addError(t){this.context.errors.add({severity:S.Critical,message:t})}addWarning(t){this.context.errors.add({severity:S.Warning,message:t})}getRootClientBasicAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.username.snakeCase.safeName,value:x.TypeLiteral.string(e.username)}),x.keywordArgument({name:t.password.snakeCase.safeName,value:x.TypeLiteral.string(e.password)})]}getRootClientBearerAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.token.snakeCase.safeName,value:x.TypeLiteral.string(e.token)})]}getRootClientHeaderAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.header.name.name.snakeCase.safeName,value:x.TypeLiteral.string(e.value)})]}getRootClientOAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.clientId.snakeCase.safeName,value:x.TypeLiteral.string(e.clientId)}),x.keywordArgument({name:t.clientSecret.snakeCase.safeName,value:x.TypeLiteral.string(e.clientSecret)})]}getRootClientHeaderArgs({headers:t,values:e}){let i=[];for(let s of t){let f=e[s.name.name.originalName];f!=null&&typeof f=="string"&&i.push(x.keywordArgument({name:s.name.name.snakeCase.safeName,value:x.TypeLiteral.string(f)}))}return i}getRootClientBuilderArgs({endpoint:t,snippet:e}){let i=[];t.auth!=null&&(e.auth!=null?i.push(...this.getRootClientAuthArgs({auth:t.auth,values:e.auth})):this.context.errors.add({severity:"WARNING",message:`Auth with ${t.auth.type} configuration is required for this endpoint`}));let s=this.getRootClientBaseUrlArg({baseUrl:e.baseURL,environment:e.environment});return s.length>0&&i.push(...s),this.context.errors.scope("Headers"),this.context.ir.headers!=null&&e.headers!=null&&i.push(...this.getRootClientHeaderArgs({headers:this.context.ir.headers,values:e.headers})),this.context.errors.unscope(),i}callMethod({endpoint:t,snippet:e}){let i={on:x.codeblock(Un),method:this.getMethod({endpoint:t}),arguments_:[]};switch(t.request.type){case"inlined":i.keywordArguments=this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});break;case"body":i.keywordArguments=this.getMethodArgsForBodyRequest({request:t.request,snippet:e});break;default:L(t.request)}let s=this.getRequestOptions({endpoint:t,snippet:e});return s!=null&&(i.keywordArguments=i.keywordArguments??[],i.keywordArguments.push(s)),x.invokeMethod(i)}callMethodOnExistingClient({endpoint:t,snippet:e}){let i={on:x.codeblock(Eo),method:this.getMethod({endpoint:t}),arguments_:[]};switch(t.request.type){case"inlined":i.keywordArguments=this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});break;case"body":i.keywordArguments=this.getMethodArgsForBodyRequest({request:t.request,snippet:e});break;default:L(t.request)}let s=this.getRequestOptions({endpoint:t,snippet:e});return s!=null&&(i.keywordArguments=i.keywordArguments??[],i.keywordArguments.push(s)),x.invokeMethod(i)}getRequestOptions({endpoint:t,snippet:e}){let i=e.headers??{},s=Object.entries(i);if(s.length===0)return;let f=new Set;if(this.context.ir.headers!=null)for(let w of this.context.ir.headers)f.add(w.name.wireValue.toLowerCase());if(t.request.type==="inlined"&&t.request.headers!=null)for(let w of t.request.headers)f.add(w.name.wireValue.toLowerCase());let u=s.filter(([w])=>!f.has(w.toLowerCase()));if(u.length===0)return;let E=u.map(([w,k])=>({key:x.TypeLiteral.string(w),value:x.TypeLiteral.string(String(k))}));return x.keywordArgument({name:"request_options",value:x.TypeLiteral.hash([{key:x.TypeLiteral.string("additional_headers"),value:x.TypeLiteral.hash(E)}])})}getMethodArgsForInlinedRequest({request:t,snippet:e}){let i=[];if(i.push(...this.getNamedParameterArgs({kind:"PathParameters",namedParameters:t.pathParameters,values:e.pathParameters})),i.push(...this.getNamedParameterArgs({kind:"QueryParameters",namedParameters:t.queryParameters,values:e.queryParameters})),i.push(...this.getNamedParameterArgs({kind:"Headers",namedParameters:t.headers,values:e.headers})),t.body!=null&&e.requestBody!=null)switch(t.body.type){case"properties":i.push(...this.getMethodArgsForPropertiesRequest({request:t.body,snippet:e}));break;case"referenced":{let s=t.body.bodyType;if(s.type==="typeReference"){let f=s.value;if(f.type==="named"){let u=this.context.resolveNamedType({typeId:f.value});if(u!=null&&u.type==="object"){let E=this.context.getRecord(e.requestBody);if(E!=null){let w=this.getBodyFieldsAsKeywordArgs({namedType:u,bodyRecord:E});i.push(...w)}}else if(u!=null){let E=this.getBodyArgsForNonObjectType({namedType:u,typeRef:f,bodyValue:e.requestBody});i.push(...E)}}else{let u=this.context.dynamicTypeLiteralMapper.convert({typeReference:f,value:e.requestBody});x.TypeLiteral.isNop(u)||i.push(x.keywordArgument({name:t.body.bodyKey.snakeCase.safeName,value:u}))}}break}case"fileUpload":break;default:L(t.body)}return i}getNamedParameterArgs({kind:t,namedParameters:e,values:i}){let s=[];if(this.context.errors.scope(t),e!=null){let f=this.context.associateByWireValue({parameters:e,values:i??{},ignoreMissingParameters:!0});for(let u of f){let E=this.context.dynamicTypeLiteralMapper.convert(u);x.TypeLiteral.isNop(E)||s.push(x.keywordArgument({name:this.context.getPropertyName(u.name.name),value:E}))}}return this.context.errors.unscope(),s}getMethodArgsForPropertiesRequest({request:t,snippet:e}){let i=[],s=this.context.associateByWireValue({parameters:t.value,values:this.context.getRecord(e.requestBody)??{}});for(let f of s){let u=this.context.dynamicTypeLiteralMapper.convert(f);x.TypeLiteral.isNop(u)||i.push(x.keywordArgument({name:this.context.getPropertyName(f.name.name),value:u}))}return i}getMethodArgsForBodyRequest({request:t,snippet:e}){let i=[];if(i.push(...this.getNamedParameterArgs({kind:"PathParameters",namedParameters:t.pathParameters,values:e.pathParameters})),t.body!=null&&e.requestBody!=null)switch(t.body.type){case"bytes":this.context.errors.add({severity:"CRITICAL",message:"Bytes request body is not supported in Ruby snippets yet"});break;case"typeReference":{let s=t.body.value;if(s.type==="named"){let f=this.context.resolveNamedType({typeId:s.value});if(f!=null&&f.type==="object"){let u=this.context.getRecord(e.requestBody);if(u!=null){let E=this.getBodyFieldsAsKeywordArgs({namedType:f,bodyRecord:u});i.push(...E)}}else if(f!=null){let u=this.getBodyArgsForNonObjectType({namedType:f,typeRef:s,bodyValue:e.requestBody});i.push(...u)}}else{let f=this.context.dynamicTypeLiteralMapper.convert({typeReference:s,value:e.requestBody});x.TypeLiteral.isNop(f)||i.push(x.keywordArgument({name:"request",value:f}))}break}default:L(t.body)}return i}getBodyArgsForNonObjectType({namedType:t,typeRef:e,bodyValue:i}){let s=[];switch(t.type){case"undiscriminatedUnion":{let f=this.context.getRecord(i);if(f!=null){for(let E of t.types)if(E.type==="named"){let w=this.context.resolveNamedType({typeId:E.value});if(w!=null&&w.type==="object"){let k=new Set(w.properties.map(D=>D.name.wireValue)),R=Object.keys(f);if(R.every(D=>k.has(D))&&R.length>0){let D=this.getBodyFieldsAsKeywordArgs({namedType:w,bodyRecord:f});return s.push(...D),s}}}}let u=this.context.dynamicTypeLiteralMapper.convert({typeReference:e,value:i});x.TypeLiteral.isNop(u)||s.push(x.keywordArgument({name:"request",value:u}));break}case"alias":{let f=t.typeReference;if(f.type==="named"){let E=this.context.resolveNamedType({typeId:f.value});if(E!=null&&E.type==="object"){let w=this.context.getRecord(i);if(w!=null){let k=this.getBodyFieldsAsKeywordArgs({namedType:E,bodyRecord:w});return s.push(...k),s}}}let u=this.context.dynamicTypeLiteralMapper.convert({typeReference:e,value:i});x.TypeLiteral.isNop(u)||s.push(x.keywordArgument({name:"request",value:u}));break}case"discriminatedUnion":case"enum":{let f=this.context.dynamicTypeLiteralMapper.convert({typeReference:e,value:i});x.TypeLiteral.isNop(f)||s.push(x.keywordArgument({name:"request",value:f}));break}case"object":break;default:L(t)}return s}getBodyFieldsAsKeywordArgs({namedType:t,bodyRecord:e}){let i=[];switch(t.type){case"object":{for(let s of t.properties){let f=s.name.wireValue,u=e[f];if(u!==void 0){this.context.errors.scope(f);let E=this.context.dynamicTypeLiteralMapper.convert({typeReference:s.typeReference,value:u});this.context.errors.unscope(),x.TypeLiteral.isNop(E)||i.push(x.keywordArgument({name:this.context.getPropertyName(s.name.name),value:E}))}}break}case"alias":case"discriminatedUnion":case"undiscriminatedUnion":case"enum":break;default:L(t)}return i}getMethod({endpoint:t}){return t.declaration.fernFilepath.allParts.length>0?`${t.declaration.fernFilepath.allParts.map(e=>`${this.context.getMethodName(e)}`).join(".")}.${this.context.getMethodName(t.declaration.name)}`:this.context.getMethodName(t.declaration.name)}};var wt=class extends Pe{constructor({ir:e,config:i,formatter:s}){super(new ot({ir:e,config:i}));y(this,"formatter");this.formatter=s}async generate(e){return super.generate(e)}generateSync(e){return super.generateSync(e)}async generateSnippetAst(e,i){return super.generateSnippetAst(e,i)}createSnippetGenerator(e){return new st({context:e,formatter:this.formatter})}};export{wt as DynamicSnippetsGenerator};
|
|
25
|
+
}`);break}case"set":{let i=this.internalType.values.filter(s=>!l.isNop(s));if(i.length===0){e.write("Set.new([])");break}e.write("Set.new(["),i.forEach((s,f)=>{f>0&&e.write(", "),s.write(e)}),e.write("])");break}case"list":{let i=this.internalType.values.filter(s=>!l.isNop(s));if(i.length===0){e.write("[]");break}if(i.length>=2&&i.every(s=>s instanceof l&&s.internalType.type==="str"&&!/[\s\\[\]]/.test(s.internalType.value))){let s=i.map(f=>f.internalType.value);e.write(`%w[${s.join(" ")}]`);break}e.write("["),i.forEach((s,f)=>{f>0&&e.write(", "),s.write(e)}),e.write("]");break}case"nop":break;case"nil":e.write("nil");break;default:L(this.internalType)}}};c();m();d();var tt;(function(l){l[l.Invariant=0]="Invariant",l.Covariant="out",l.Contravariant="in"})(tt||(tt={}));var be=class extends N{constructor({name:e,unchecked:i,bound:s,variance:f,defaultType:u}){super();y(this,"name");y(this,"unchecked");y(this,"bound");y(this,"variance");y(this,"defaultType");this.name=e,this.unchecked=i??!1,this.bound=s,this.variance=f??tt.Invariant,this.defaultType=u}write(e){}writeTypeDefinition(e){this.unchecked&&e.write("unchecked "),this.variance!=tt.Invariant&&e.write(`${this.variance} `),e.write(this.name),this.bound&&(e.write(" < "),this.bound.writeTypeDefinition(e)),this.defaultType&&(e.write(" = "),this.defaultType.writeTypeDefinition(e))}};c();m();d();var qe=class extends W{constructor(){super(...arguments);y(this,"initializer")}write(e){e.write(`&${this.name}`)}};c();m();d();var rt=class extends N{constructor({errorClass:e,message:i}){super();y(this,"errorClass");y(this,"message");this.errorClass=e,this.message=i}write(e){e.write("raise"),this.errorClass&&(e.write(" "),this.errorClass.write(e)),this.message&&(this.errorClass&&e.write(", "),this.message.write(e))}};function nt(l){return new ye(l)}function io(){return nt("true")}function oo(){return nt("false")}function so(){return nt("nil")}var ao={positional(l){return new _e(l)},keyword(l){return new xe(l)},positionalSplat(l){return new je(l)},keywordSplat(l){return new De(l)},yield(l){return new qe(l)}};function uo(l){return new ue(l)}function lo(l){return new ae(l)}function po(l){return new se(l)}function fo(l){return new X(l)}function co(l){return new be(l)}function mo(l){return new ne(l)}function ho(l){return new he(l)}function yo(l){return new ge(l)}function go(l){return new we(l)}function xo(l){return new Oe(l)}function wo(l){return new et(l)}function bo(l){return new Ze(l)}function vo(l){return new rt(l)}function Ao(l,t){let e=l;for(let i of t.toReversed())i.addStatement(e),e=i;return e}c();m();d();var it=class l{constructor({context:t}){y(this,"context");this.context=t}convert(t){if(t.value===null)return this.context.isNullable(t.typeReference)?x.TypeLiteral.nil():(this.context.errors.add({severity:S.Critical,message:"Expected non-null value, but got null"}),x.TypeLiteral.nop());if(t.value===void 0)return x.TypeLiteral.nop();switch(t.typeReference.type){case"list":return this.convertList({list:t.typeReference.value,value:t.value});case"literal":return this.convertLiteral({literalType:t.typeReference.value,value:t.value});case"map":return this.convertMap({map:t.typeReference,value:t.value});case"named":{let e=this.context.resolveNamedType({typeId:t.typeReference.value});return e==null?x.TypeLiteral.nop():this.convertNamed({named:e,value:t.value,as:t.as})}case"optional":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"nullable":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"primitive":return this.convertPrimitive({primitive:t.typeReference.value,value:t.value,as:t.as});case"set":return this.convertSet({set:t.typeReference.value,value:t.value});case"unknown":return x.TypeLiteral.nop();default:L(t.typeReference)}}convertLiteral({literalType:t,value:e}){switch(t.type){case"boolean":{let i=this.context.getValueAsBoolean({value:e});return i==null?x.TypeLiteral.nop():x.TypeLiteral.boolean(i)}case"string":{let i=this.context.getValueAsString({value:e});return i==null?x.TypeLiteral.nop():x.TypeLiteral.string(i)}default:L(t)}}convertList({list:t,value:e}){return Array.isArray(e)?x.TypeLiteral.list(e.map((i,s)=>{this.context.errors.scope({index:s});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}})):(this.context.errors.add({severity:S.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeLiteral.nop())}convertSet({set:t,value:e}){return Array.isArray(e)?x.TypeLiteral.set(e.map((i,s)=>{this.context.errors.scope({index:s});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}})):(this.context.errors.add({severity:S.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeLiteral.nop())}convertMap({map:t,value:e}){return typeof e!="object"||e==null?(this.context.errors.add({severity:S.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),x.TypeLiteral.nop()):x.TypeLiteral.hash(Object.entries(e).map(([i,s])=>{this.context.errors.scope(i);try{return{key:this.convert({typeReference:t.key,value:i,as:"key"}),value:this.convert({typeReference:t.value,value:s})}}finally{this.context.errors.unscope()}}))}convertNamed({named:t,value:e,as:i}){switch(t.type){case"alias":return this.convert({typeReference:t.typeReference,value:e,as:i});case"discriminatedUnion":return this.convertDiscriminatedUnion({discriminatedUnion:t,value:e});case"object":return this.convertObject({object:t,value:e});case"enum":return this.convertEnum({enum_:t,value:e});case"undiscriminatedUnion":return this.convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e});default:L(t)}}convertDiscriminatedUnion({discriminatedUnion:t,value:e}){let i=this.context.resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e});if(i==null)return x.TypeLiteral.nop();let s=i.singleDiscriminatedUnionType,f=this.convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:i,unionVariant:s});return f==null?x.TypeLiteral.nop():x.TypeLiteral.hash(f)}convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:t,unionVariant:e}){switch(e.type){case"samePropertiesAsObject":{let i=this.context.resolveNamedType({typeId:e.typeId});if(i==null)return;let s=this.convertNamed({named:i,value:t.value});return this.extractHashEntries(s)}case"singleProperty":try{this.context.errors.scope(e.discriminantValue.wireValue);let i=this.context.getRecord(t.value);return i==null?[{key:x.TypeLiteral.string(this.context.getPropertyName(e.discriminantValue.name)),value:this.convert({typeReference:e.typeReference,value:t.value})}]:[{key:x.TypeLiteral.string(this.context.getPropertyName(e.discriminantValue.name)),value:this.convert({typeReference:e.typeReference,value:i[e.discriminantValue.wireValue]})}]}finally{this.context.errors.unscope()}case"noProperties":return[];default:L(e)}}extractHashEntries(t){t instanceof x.TypeLiteral}convertEnum({enum_:t,value:e}){let i=this.getEnumValueName({enum_:t,value:e});return i==null?x.TypeLiteral.nop():x.TypeLiteral.string(i)}getEnumValueName({enum_:t,value:e}){if(typeof e!="string"){this.context.errors.add({severity:S.Critical,message:`Expected enum value string, got: ${typeof e}`});return}let i=t.values.find(s=>s.wireValue===e);if(i==null){this.context.errors.add({severity:S.Critical,message:`An enum value named "${e}" does not exist in this context`});return}return i.wireValue}convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e}){let i=this.findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e});return i??x.TypeLiteral.nop()}findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e}){for(let i of t.types){let s=this.context.clone(),u=new l({context:s}).convert({typeReference:i,value:e});if(s.errors.empty())return u}this.context.errors.add({severity:S.Critical,message:`None of the types in the undiscriminated union matched the given "${typeof e}" value`})}convertPrimitive({primitive:t,value:e,as:i}){switch(t){case"INTEGER":case"LONG":case"UINT":case"UINT_64":{let s=this.getValueAsNumber({value:e,as:i});return s==null?x.TypeLiteral.nop():x.TypeLiteral.integer(s)}case"FLOAT":case"DOUBLE":{let s=this.getValueAsNumber({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.float(s)}case"BOOLEAN":{let s=this.getValueAsBoolean({value:e,as:i});return s==null?x.TypeLiteral.nop():x.TypeLiteral.boolean(s)}case"BASE_64":case"BIG_INTEGER":case"STRING":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}case"UUID":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}case"DATE":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}case"DATE_TIME":{let s=this.context.getValueAsString({value:e});return s==null?x.TypeLiteral.nop():x.TypeLiteral.string(s)}default:L(t)}}convertObject({object:t,value:e}){return typeof e!="object"||e==null?(this.context.errors.add({severity:S.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),x.TypeLiteral.nop()):x.TypeLiteral.hash(Object.entries(e).map(([i,s])=>{this.context.errors.scope(i);let f=t.properties.find(k=>k.name.wireValue===i),u=f?.typeReference??{type:"unknown"},E=f?.name.name.snakeCase.safeName??i,w={key:x.TypeLiteral.string(E),value:this.convert({typeReference:u,value:s})};return this.context.errors.unscope(),w}))}getValueAsNumber({value:t,as:e}){let i=e==="key"&&typeof t=="string"?Number(t):t;return this.context.getValueAsNumber({value:i})}getValueAsBoolean({value:t,as:e}){let i=e==="key"?typeof t=="string"?t==="true":!!t:t;return this.context.getValueAsBoolean({value:i})}};var ot=class l extends $e{constructor({ir:e,config:i}){super({ir:e,config:i});y(this,"ir");y(this,"customConfig");y(this,"dynamicTypeLiteralMapper");this.ir=e,this.customConfig=i.customConfig!=null?i.customConfig:void 0,this.dynamicTypeLiteralMapper=new it({context:this})}clone(){return new l({ir:this.ir,config:this.config})}getRootClientClassReference(){return x.classReference({name:this.getRootClientClassName(),modules:[this.getRootModuleName()]})}getRootClientClassName(){return this.customConfig?.clientModuleName??"Client"}getRootModuleName(){return ht(this.customConfig?.moduleName??this.config.organization)}isSingleEnvironmentID(e){return typeof e=="string"}isMultiEnvironmentValues(e){return typeof e=="object"}getEnvironmentTypeReferenceFromID(e){let i=this.resolveEnvironmentName(e);if(i!=null)return x.codeblock(s=>{s.writeNode(this.getEnvironmentClassReference()),s.write("::"),s.write(this.getEnumName(i))})}getEnvironmentClassReference(){return x.classReference({name:"Environment",modules:[this.getRootModuleName()]})}getEnumName(e){return this.getName(e.screamingSnakeCase.safeName)}getMethodName(e){return this.getName(e.snakeCase.safeName)}getPropertyName(e){return this.getName(e.snakeCase.safeName)}getName(e){return e}};c();m();d();var Un="client",Eo="@client",st=class{constructor({context:t,formatter:e}){y(this,"context");y(this,"formatter");this.context=t,this.formatter=e}async generateSnippet({endpoint:t,request:e}){return await this.buildCodeBlock({endpoint:t,snippet:e}).toStringAsync({customConfig:this.context.customConfig??{},formatter:this.formatter})}generateSnippetSync({endpoint:t,request:e}){return this.buildCodeBlock({endpoint:t,snippet:e}).toString({customConfig:this.context.customConfig??{},formatter:this.formatter})}async generateSnippetAst({endpoint:t,request:e,options:i}){return i?.skipClientInstantiation?this.buildCodeBlockWithoutClient({endpoint:t,snippet:e}):this.buildCodeBlock({endpoint:t,snippet:e})}buildCodeBlock({endpoint:t,snippet:e}){return x.codeblock(i=>{i.writeNodeStatement(this.constructClient({endpoint:t,snippet:e})),i.newLine(),i.writeNodeStatement(this.callMethod({endpoint:t,snippet:e}))})}buildCodeBlockWithoutClient({endpoint:t,snippet:e}){return x.codeblock(i=>{i.writeNodeStatement(this.callMethodOnExistingClient({endpoint:t,snippet:e}))})}constructClient({endpoint:t,snippet:e}){return x.codeblock(i=>{i.addRequire(this.context.getRootModuleName().toLowerCase());let s=this.context.getRootClientClassReference(),f=this.getRootClientBuilderArgs({endpoint:t,snippet:e});i.write(`${Un} = `),i.writeNode(x.instantiateClass({classReference:s,arguments_:f}))})}getRootClientBaseUrlArg({baseUrl:t,environment:e}){if(t!=null&&e!=null)return this.context.errors.add({severity:"CRITICAL",message:"Cannot specify both baseUrl and environment options"}),[];if(t!=null)return[x.keywordArgument({name:"base_url",value:x.TypeLiteral.string(t)})];if(e!=null){if(this.context.isSingleEnvironmentID(e)){let i=this.context.getEnvironmentTypeReferenceFromID(e);return i==null?(this.context.errors.add({severity:"CRITICAL",message:`Environment ID ${e} not found`}),[]):[x.keywordArgument({name:"environment",value:i})]}if(this.context.isMultiEnvironmentValues(e))return this.context.errors.add({severity:"CRITICAL",message:"Multi-environment values are not supported in Ruby snippets yet"}),[]}return[]}getRootClientAuthArgs({auth:t,values:e}){if(e.type!==t.type)return this.addError(this.context.newAuthMismatchError({auth:t,values:e}).message),[];switch(t.type){case"basic":return e.type==="basic"?this.getRootClientBasicAuthArgs({auth:t,values:e}):[];case"bearer":return e.type==="bearer"?this.getRootClientBearerAuthArgs({auth:t,values:e}):[];case"header":return e.type==="header"?this.getRootClientHeaderAuthArgs({auth:t,values:e}):[];case"oauth":return e.type==="oauth"?this.getRootClientOAuthArgs({auth:t,values:e}):[];case"inferred":return[];default:L(t)}}addError(t){this.context.errors.add({severity:S.Critical,message:t})}addWarning(t){this.context.errors.add({severity:S.Warning,message:t})}getRootClientBasicAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.username.snakeCase.safeName,value:x.TypeLiteral.string(e.username)}),x.keywordArgument({name:t.password.snakeCase.safeName,value:x.TypeLiteral.string(e.password)})]}getRootClientBearerAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.token.snakeCase.safeName,value:x.TypeLiteral.string(e.token)})]}getRootClientHeaderAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.header.name.name.snakeCase.safeName,value:x.TypeLiteral.string(e.value)})]}getRootClientOAuthArgs({auth:t,values:e}){return[x.keywordArgument({name:t.clientId.snakeCase.safeName,value:x.TypeLiteral.string(e.clientId)}),x.keywordArgument({name:t.clientSecret.snakeCase.safeName,value:x.TypeLiteral.string(e.clientSecret)})]}getRootClientHeaderArgs({headers:t,values:e}){let i=[];for(let s of t){let f=e[s.name.name.originalName];f!=null&&typeof f=="string"&&i.push(x.keywordArgument({name:s.name.name.snakeCase.safeName,value:x.TypeLiteral.string(f)}))}return i}getRootClientBuilderArgs({endpoint:t,snippet:e}){let i=[];t.auth!=null&&(e.auth!=null?i.push(...this.getRootClientAuthArgs({auth:t.auth,values:e.auth})):this.context.errors.add({severity:"WARNING",message:`Auth with ${t.auth.type} configuration is required for this endpoint`}));let s=this.getRootClientBaseUrlArg({baseUrl:e.baseURL,environment:e.environment});return s.length>0&&i.push(...s),this.context.errors.scope("Headers"),this.context.ir.headers!=null&&e.headers!=null&&i.push(...this.getRootClientHeaderArgs({headers:this.context.ir.headers,values:e.headers})),this.context.errors.unscope(),i}callMethod({endpoint:t,snippet:e}){let i={on:x.codeblock(Un),method:this.getMethod({endpoint:t}),arguments_:[]};switch(t.request.type){case"inlined":i.keywordArguments=this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});break;case"body":i.keywordArguments=this.getMethodArgsForBodyRequest({request:t.request,snippet:e});break;default:L(t.request)}let s=this.getRequestOptions({endpoint:t,snippet:e});return s!=null&&(i.keywordArguments=i.keywordArguments??[],i.keywordArguments.push(s)),x.invokeMethod(i)}callMethodOnExistingClient({endpoint:t,snippet:e}){let i={on:x.codeblock(Eo),method:this.getMethod({endpoint:t}),arguments_:[]};switch(t.request.type){case"inlined":i.keywordArguments=this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});break;case"body":i.keywordArguments=this.getMethodArgsForBodyRequest({request:t.request,snippet:e});break;default:L(t.request)}let s=this.getRequestOptions({endpoint:t,snippet:e});return s!=null&&(i.keywordArguments=i.keywordArguments??[],i.keywordArguments.push(s)),x.invokeMethod(i)}getRequestOptions({endpoint:t,snippet:e}){let i=e.headers??{},s=Object.entries(i);if(s.length===0)return;let f=new Set;if(this.context.ir.headers!=null)for(let w of this.context.ir.headers)f.add(w.name.wireValue.toLowerCase());if(t.request.type==="inlined"&&t.request.headers!=null)for(let w of t.request.headers)f.add(w.name.wireValue.toLowerCase());let u=s.filter(([w])=>!f.has(w.toLowerCase()));if(u.length===0)return;let E=u.map(([w,k])=>({key:x.TypeLiteral.string(w),value:x.TypeLiteral.string(String(k))}));return x.keywordArgument({name:"request_options",value:x.TypeLiteral.hash([{key:x.TypeLiteral.string("additional_headers"),value:x.TypeLiteral.hash(E)}])})}getMethodArgsForInlinedRequest({request:t,snippet:e}){let i=[];if(i.push(...this.getNamedParameterArgs({kind:"PathParameters",namedParameters:t.pathParameters,values:e.pathParameters})),i.push(...this.getNamedParameterArgs({kind:"QueryParameters",namedParameters:t.queryParameters,values:e.queryParameters})),i.push(...this.getNamedParameterArgs({kind:"Headers",namedParameters:t.headers,values:e.headers})),t.body!=null&&e.requestBody!=null)switch(t.body.type){case"properties":i.push(...this.getMethodArgsForPropertiesRequest({request:t.body,snippet:e}));break;case"referenced":{let s=t.body.bodyType;if(s.type==="typeReference"){let f=s.value;if(f.type==="named"){let u=this.context.resolveNamedType({typeId:f.value});if(u!=null&&u.type==="object"){let E=this.context.getRecord(e.requestBody);if(E!=null){let w=this.getBodyFieldsAsKeywordArgs({namedType:u,bodyRecord:E});i.push(...w)}}else if(u!=null){let E=this.getBodyArgsForNonObjectType({namedType:u,typeRef:f,bodyValue:e.requestBody});i.push(...E)}}else{let u=this.context.dynamicTypeLiteralMapper.convert({typeReference:f,value:e.requestBody});x.TypeLiteral.isNop(u)||i.push(x.keywordArgument({name:t.body.bodyKey.snakeCase.safeName,value:u}))}}break}case"fileUpload":break;default:L(t.body)}return i}getNamedParameterArgs({kind:t,namedParameters:e,values:i}){let s=[];if(this.context.errors.scope(t),e!=null){let f=this.context.associateByWireValue({parameters:e,values:i??{},ignoreMissingParameters:!0});for(let u of f){let E=this.context.dynamicTypeLiteralMapper.convert(u);x.TypeLiteral.isNop(E)||s.push(x.keywordArgument({name:this.context.getPropertyName(u.name.name),value:E}))}}return this.context.errors.unscope(),s}getMethodArgsForPropertiesRequest({request:t,snippet:e}){let i=[],s=this.context.associateByWireValue({parameters:t.value,values:this.context.getRecord(e.requestBody)??{}});for(let f of s){let u=this.context.dynamicTypeLiteralMapper.convert(f);x.TypeLiteral.isNop(u)||i.push(x.keywordArgument({name:this.context.getPropertyName(f.name.name),value:u}))}return i}getMethodArgsForBodyRequest({request:t,snippet:e}){let i=[];if(i.push(...this.getNamedParameterArgs({kind:"PathParameters",namedParameters:t.pathParameters,values:e.pathParameters})),t.body!=null&&e.requestBody!=null)switch(t.body.type){case"bytes":this.context.errors.add({severity:"CRITICAL",message:"Bytes request body is not supported in Ruby snippets yet"});break;case"typeReference":{let s=t.body.value;if(s.type==="named"){let f=this.context.resolveNamedType({typeId:s.value});if(f!=null&&f.type==="object"){let u=this.context.getRecord(e.requestBody);if(u!=null){let E=this.getBodyFieldsAsKeywordArgs({namedType:f,bodyRecord:u});i.push(...E)}}else if(f!=null){let u=this.getBodyArgsForNonObjectType({namedType:f,typeRef:s,bodyValue:e.requestBody});i.push(...u)}}else{let f=this.context.dynamicTypeLiteralMapper.convert({typeReference:s,value:e.requestBody});x.TypeLiteral.isNop(f)||i.push(x.keywordArgument({name:"request",value:f}))}break}default:L(t.body)}return i}getBodyArgsForNonObjectType({namedType:t,typeRef:e,bodyValue:i}){let s=[];switch(t.type){case"undiscriminatedUnion":{let f=this.context.getRecord(i);if(f!=null){for(let E of t.types)if(E.type==="named"){let w=this.context.resolveNamedType({typeId:E.value});if(w!=null&&w.type==="object"){let k=new Set(w.properties.map(D=>D.name.wireValue)),R=Object.keys(f);if(R.every(D=>k.has(D))&&R.length>0){let D=this.getBodyFieldsAsKeywordArgs({namedType:w,bodyRecord:f});return s.push(...D),s}}}}let u=this.context.dynamicTypeLiteralMapper.convert({typeReference:e,value:i});x.TypeLiteral.isNop(u)||s.push(x.keywordArgument({name:"request",value:u}));break}case"alias":{let f=t.typeReference;if(f.type==="named"){let E=this.context.resolveNamedType({typeId:f.value});if(E!=null&&E.type==="object"){let w=this.context.getRecord(i);if(w!=null){let k=this.getBodyFieldsAsKeywordArgs({namedType:E,bodyRecord:w});return s.push(...k),s}}}let u=this.context.dynamicTypeLiteralMapper.convert({typeReference:e,value:i});x.TypeLiteral.isNop(u)||s.push(x.keywordArgument({name:"request",value:u}));break}case"discriminatedUnion":case"enum":{let f=this.context.dynamicTypeLiteralMapper.convert({typeReference:e,value:i});x.TypeLiteral.isNop(f)||s.push(x.keywordArgument({name:"request",value:f}));break}case"object":break;default:L(t)}return s}getBodyFieldsAsKeywordArgs({namedType:t,bodyRecord:e}){let i=[];switch(t.type){case"object":{for(let s of t.properties){let f=s.name.wireValue,u=e[f];if(u!==void 0){this.context.errors.scope(f);let E=this.context.dynamicTypeLiteralMapper.convert({typeReference:s.typeReference,value:u});this.context.errors.unscope(),x.TypeLiteral.isNop(E)||i.push(x.keywordArgument({name:this.context.getPropertyName(s.name.name),value:E}))}}break}case"alias":case"discriminatedUnion":case"undiscriminatedUnion":case"enum":break;default:L(t)}return i}getMethod({endpoint:t}){return t.declaration.fernFilepath.allParts.length>0?`${t.declaration.fernFilepath.allParts.map(e=>`${this.context.getMethodName(e)}`).join(".")}.${this.context.getMethodName(t.declaration.name)}`:this.context.getMethodName(t.declaration.name)}};var wt=class extends Pe{constructor({ir:e,config:i,formatter:s}){super(new ot({ir:e,config:i}));y(this,"formatter");this.formatter=s}async generate(e){return super.generate(e)}generateSync(e){return super.generateSync(e)}async generateSnippetAst(e,i){return super.generateSnippetAst(e,i)}createSnippetGenerator(e){return new st({context:e,formatter:this.formatter})}};export{wt as DynamicSnippetsGenerator};
|
|
26
26
|
/*! Bundled license information:
|
|
27
27
|
|
|
28
28
|
@jspm/core/nodelibs/browser/chunk-DtuTasat.js:
|