@fern-api/typescript-dynamic-snippets 3.63.1 → 3.63.3
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
|
@@ -21,7 +21,7 @@ ${t}`)}return t}getContent(){let t=this.stringifyImports();return t.length>0?t+`
|
|
|
21
21
|
`)?this.writeStringWithBackticks({writer:e,value:this.internalType.value}):e.write(`"${this.internalType.value.replaceAll('"','\\"')}"`);break}case"tuple":{this.writeIterable({writer:e,iterable:this.internalType});break}case"unknown":{this.writeUnknown({writer:e,value:this.internalType.value});break}case"null":{e.write("null");break}case"nop":break;default:P(this.internalType)}}isObject(){return this.internalType.type==="object"}asObjectOrThrow(){if(this.isObject())return this.internalType;throw new Error("Internal error; ts.TypeLiteral is not an object")}isSet(){return this.internalType.type==="set"}asSetOrThrow(){if(this.isSet())return this.internalType;throw new Error("Internal error; ts.TypeLiteral is not a set")}isSetOfObjects(){return this.isSet()&&this.asSetOrThrow().values.every(e=>e.isObject())}writeStringWithBackticks({writer:e,value:i}){e.write("`");let p=i.split(`
|
|
22
22
|
`),d=p[0]+`
|
|
23
23
|
`,u=p.slice(1).join(`
|
|
24
|
-
`);e.write(d.replaceAll("`","\\`")),e.writeNoIndent(u.replaceAll("`","\\`")),e.write("`")}writeIterable({writer:e,iterable:i}){let p=_a({values:i.values});if(p.length===0){e.write("[]");return}e.writeLine("["),e.indent();for(let d of p)d.write(e),e.writeLine(",");e.dedent(),e.write("]")}writeRecord({writer:e,record:i}){let p=ja({entries:i.entries});if(p.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let d of p)d.key.write(e),e.write(": "),d.value.write(e),e.writeLine(",");e.dedent(),e.write("}")}writeObject({writer:e,object:i}){let p=$a({fields:i.fields});if(p.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let d of p)e.write(`${d.name}: `),d.value.write(e),e.writeLine(",");e.dedent(),e.write("}")}static array({values:e}){return new this({type:"array",values:e})}static bigint(e){return new this({type:"bigint",value:e})}static blob(e){return new this({type:"blob",value:e})}static boolean(e){return new this({type:"boolean",value:e})}static datetime(e){return new this({type:"datetime",value:e})}static number(e){return new this({type:"number",value:e})}static object({fields:e}){return new this({type:"object",fields:e})}static record({entries:e}){return new this({type:"record",entries:e})}static reference(e){return new this({type:"reference",value:e})}static set({values:e}){return new this({type:"set",values:e})}static string(e){return new this({type:"string",value:e})}static tuple({values:e}){return new this({type:"tuple",values:e})}static unknown(e){return new this({type:"unknown",value:e})}static null(){return new this({type:"null"})}static nop(){return new this({type:"nop"})}static isNop(e){return e.internalType.type==="nop"}writeUnknown({writer:e,value:i}){switch(typeof i){case"boolean":e.write(i.toString());return;case"string":e.write(i.includes('"')?`\`${i}\``:`"${i}"`);return;case"number":e.write(i.toString());return;case"object":if(i==null){e.write("null");return}if(Array.isArray(i)){this.writeUnknownArray({writer:e,value:i});return}this.writeUnknownObject({writer:e,value:i});return;default:throw new Error(`Internal error; unsupported unknown type: ${typeof i}`)}}writeUnknownArray({writer:e,value:i}){if(i.length===0){e.write("[]");return}e.writeLine("["),e.indent();for(let p of i)e.writeNode(s.unknown(p)),e.writeLine(",");e.dedent(),e.write("]")}writeUnknownObject({writer:e,value:i}){let p=Object.entries(i);if(p.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let[d,u]of p)e.write(`${d}: `),e.writeNode(s.unknown(u)),e.writeLine(",");e.dedent(),e.write("}")}};function $a({fields:s}){return s.filter(t=>!re.isNop(t.value))}function ja({entries:s}){return s.filter(t=>!re.isNop(t.key)&&!re.isNop(t.value))}function _a({values:s}){return s.filter(t=>!re.isNop(t))}l();f();c();var Re=class extends k{constructor(e){super();v(this,"args");this.args=e}write(e){this.args.export&&e.write("export "),this.args.const?e.write("const "):e.write("let "),e.write(`${this.args.name} = `),e.writeNode(this.args.initializer)}};function Wa(s){return new be(s)}function Va(s){return new Ee(s)}function qa(s){return new ve(s)}function Ga(s){return new Ie(s)}function za(s){return new Te(s)}function Ha(s){return new Ae(s)}function Ya(s){return new de(s)}function Ja(s){return new Re(s)}l();f();c();l();f();c();function ai({generatorConfig:s,isPackagePrivate:t}){let e=s.output.mode;switch(e.type){case"downloadFiles":return;case"publish":return{packageName:e.registriesV2.npm.packageName,version:e.version,private:t,publishInfo:{registryUrl:e.registriesV2.npm.registryUrl,token:e.registriesV2.npm.token},license:void 0,repoUrl:void 0};case"github":if(e.publishInfo!=null&&e.publishInfo.type!=="npm")throw new Error(`Expected to receive npm publish info but received ${e.publishInfo.type} instead`);return{packageName:e.publishInfo!=null?e.publishInfo.packageName:"",version:e.version,private:t,publishInfo:void 0,repoUrl:Za(e.repoUrl),license:s.license?._visit({basic:i=>i.id,custom:i=>`See ${i.filename}`,_other:()=>{}})};default:throw new Error(`Encountered unknown output mode: ${e}`)}}function Za(s){if(s!=null)return s.startsWith("https://github.com/")?`git+${s}`:s.startsWith("ssh://github.com/")?`github:${De(s).replace("ssh://github.com/","")}`:s.startsWith("https://bitbucket.org/")?`bitbucket:${De(s).replace("https://bitbucket.org/","")}`:s.startsWith("ssh://bitbucket.org/")?`bitbucket:${De(s).replace("ssh://bitbucket.org/","")}`:s.startsWith("https://gitlab.com/")?`gitlab:${De(s).replace("https://gitlab.com/","")}`:s.startsWith("ssh://gitlab.com/")?`gitlab:${De(s).replace("ssh://gitlab.com/","")}`:(s.startsWith("git+")||(s=`git+${s}`),s.endsWith(".git")||(s=`${s}.git`),s)}function De(s){return s.endsWith(".git")?s.slice(0,-4):s}l();f();c();function ht({organization:s,workspaceName:t,namespaceExport:e,naming:i}){return(typeof i=="string"?i:i?.namespace)??e??`${le(Se(s))}${le(Se(t))}`}function yt({namespaceExport:s,naming:t}){let e=typeof t=="string"?{}:t,i=t!=null?le(Se(s)):s;return{namespace:s,client:e?.client??`${i}Client`,error:e?.error??`${i}Error`,timeoutError:e?.timeoutError??`${i}TimeoutError`,environment:e?.environment??`${i}Environment`,environmentUrls:e?.environmentUrls??`${i}EnvironmentUrls`,version:e?.version??`${i}Version`}}l();f();c();l();f();c();var We={};K(We,{commons:()=>si,dynamic:()=>vt,generatorExec:()=>Et});l();f();c();l();f();c();var si={};l();f();c();var vt={};K(vt,{ErrorSeverity:()=>hi,HttpMethod:()=>di,ObjectPropertyAccess:()=>yi,PrimitiveTypeV1:()=>xi,auth:()=>ui,commons:()=>pi,declaration:()=>li,endpoints:()=>ci,environment:()=>fi,http:()=>xt,ir:()=>mi,snippets:()=>gt,types:()=>wt,variables:()=>gi});l();f();c();l();f();c();var ui={};l();f();c();var pi={};l();f();c();var li={};l();f();c();var ci={};l();f();c();var fi={};l();f();c();var xt={};K(xt,{HttpMethod:()=>di});l();f();c();l();f();c();var di={Get:"GET",Post:"POST",Put:"PUT",Patch:"PATCH",Delete:"DELETE",Head:"HEAD"};var mi={};l();f();c();var gt={};K(gt,{ErrorSeverity:()=>hi});l();f();c();l();f();c();var hi={Critical:"CRITICAL",Warning:"WARNING"};var wt={};K(wt,{ObjectPropertyAccess:()=>yi,PrimitiveTypeV1:()=>xi});l();f();c();l();f();c();var yi={ReadOnly:"READ_ONLY",WriteOnly:"WRITE_ONLY"};l();f();c();var xi={Integer:"INTEGER",Long:"LONG",Uint:"UINT",Uint64:"UINT_64",Float:"FLOAT",Double:"DOUBLE",Boolean:"BOOLEAN",String:"STRING",Date:"DATE",DateTime:"DATE_TIME",DateTimeRfc2822:"DATE_TIME_RFC_2822",Uuid:"UUID",Base64:"BASE_64",BigInteger:"BIG_INTEGER"};var gi={};l();f();c();var Et={};K(Et,{LicenseId:()=>wi,config:()=>bt});l();f();c();l();f();c();var bt={};K(bt,{LicenseId:()=>wi});l();f();c();l();f();c();var wi={Mit:"MIT",Apache2:"Apache-2.0"};var Ka="value",Qe=class{constructor({context:t}){v(this,"context");this.context=t}convert(t){let e=t.convertOpts;if(t.value===null)return this.context.isNullable(t.typeReference)?b.TypeLiteral.null():(this.context.errors.add({severity:N.Critical,message:"Expected non-null value, but got null"}),b.TypeLiteral.nop());if(t.value===void 0)return b.TypeLiteral.nop();switch(t.typeReference.type){case"list":return this.convertList({list:t.typeReference.value,value:t.value,convertOpts:e});case"literal":return this.convertLiteral({literalType:t.typeReference.value,value:t.value,convertOpts:e});case"map":return this.convertMap({map:t.typeReference,value:t.value,convertOpts:e});case"named":{let i=this.context.resolveNamedType({typeId:t.typeReference.value});return i==null?b.TypeLiteral.nop():this.convertNamed({named:i,value:t.value,as:t.as,convertOpts:e})}case"optional":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as,convertOpts:e});case"nullable":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as,convertOpts:e});case"primitive":return this.convertPrimitive({primitive:t.typeReference.value,value:t.value,as:t.as,convertOpts:e});case"set":return this.convertSet({set:t.typeReference.value,value:t.value,convertOpts:e});case"unknown":return this.convertUnknown({value:t.value,convertOpts:e});default:P(t.typeReference)}}convertLiteral({literalType:t,value:e}){switch(t.type){case"boolean":{let i=this.context.getValueAsBoolean({value:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.boolean(i)}case"string":{let i=this.context.getValueAsString({value:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.string(i)}default:P(t)}}convertList({list:t,value:e,convertOpts:i}){return Array.isArray(e)?b.TypeLiteral.array({values:e.map((p,d)=>{this.context.errors.scope({index:d});try{return this.convert({typeReference:t,value:p,convertOpts:i})}finally{this.context.errors.unscope()}})}):(this.context.errors.add({severity:N.Critical,message:`Expected array but got: ${typeof e}`}),b.TypeLiteral.nop())}convertSet({set:t,value:e,convertOpts:i}){return Array.isArray(e)?b.TypeLiteral.set({values:e.map((p,d)=>{this.context.errors.scope({index:d});try{return this.convert({typeReference:t,value:p,convertOpts:i})}finally{this.context.errors.unscope()}})}):(this.context.errors.add({severity:N.Critical,message:`Expected array but got: ${typeof e}`}),b.TypeLiteral.nop())}convertMap({map:t,value:e,convertOpts:i}){return typeof e!="object"||e==null?(this.context.errors.add({severity:N.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),b.TypeLiteral.nop()):b.TypeLiteral.record({entries:Object.entries(e).map(([p,d])=>{this.context.errors.scope(p);try{return{key:this.convert({typeReference:t.key,value:p,as:"key",convertOpts:i}),value:this.convert({typeReference:t.value,value:d,convertOpts:i})}}finally{this.context.errors.unscope()}})})}convertNamed({named:t,value:e,as:i,convertOpts:p}){switch(t.type){case"alias":return this.context.customConfig?.useBrandedStringAliases?b.TypeLiteral.reference(b.codeblock(d=>{d.writeNode(b.reference({name:this.context.namespaceExport,importFrom:this.context.getModuleImport(),memberName:this.context.getFullyQualifiedReference({declaration:t.declaration})})),d.write("("),d.writeNode(this.convert({typeReference:t.typeReference,value:e,as:i,convertOpts:p})),d.write(")")})):this.convert({typeReference:t.typeReference,value:e,as:i,convertOpts:p});case"discriminatedUnion":return this.convertDiscriminatedUnion({discriminatedUnion:t,value:e,convertOpts:p});case"enum":return this.convertEnum({enum_:t,value:e,convertOpts:p});case"object":return this.convertObject({object_:t,value:e,convertOpts:p});case"undiscriminatedUnion":return this.convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e,convertOpts:p});default:P(t)}}convertDiscriminatedUnion({discriminatedUnion:t,value:e,convertOpts:i}){let p=this.context.resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e});if(p==null)return b.TypeLiteral.nop();let d=p.singleDiscriminatedUnionType,u=this.convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:p,unionVariant:d,convertOpts:i});if(u==null)return b.TypeLiteral.nop();if(this.context.customConfig?.includeUtilsOnUnionMembers)return b.TypeLiteral.reference(b.codeblock(I=>{I.writeNode(b.invokeMethod({on:b.reference({name:this.context.namespaceExport,importFrom:this.context.getModuleImport(),memberName:this.context.getFullyQualifiedReference({declaration:t.declaration})}),method:this.context.getMethodName(d.discriminantValue.name),arguments_:this.convertDiscriminatedUnionUtilsArgs({discriminatedUnionTypeInstance:p,unionVariant:d,unionProperties:u,convertOpts:i})}))}));let E={name:this.context.getPropertyName(t.discriminant.name),value:b.TypeLiteral.string(d.discriminantValue.wireValue)};return b.TypeLiteral.object({fields:[E,...u]})}convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:t,unionVariant:e,convertOpts:i}){let p=this.getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e});switch(e.type){case"samePropertiesAsObject":{let d=this.context.resolveNamedType({typeId:e.typeId});if(d==null)return;let u=this.convertNamed({named:d,value:t.value,convertOpts:i});if(!u.isObject()){this.context.errors.add({severity:N.Critical,message:"Internal error; expected union value to be an object"});return}let E=u.asObjectOrThrow();return[...p,...E.fields]}case"singleProperty":try{this.context.errors.scope(e.discriminantValue.wireValue);let d=this.context.getRecord(t.value);return d==null?[...p,{name:Ka,value:this.convert({typeReference:e.typeReference,value:t.value,convertOpts:i})}]:[...p,{name:this.context.getPropertyName(e.discriminantValue.name),value:this.convert({typeReference:e.typeReference,value:d[e.discriminantValue.wireValue],convertOpts:i})}]}finally{this.context.errors.unscope()}case"noProperties":return p;default:P(e)}}convertDiscriminatedUnionUtilsArgs({discriminatedUnionTypeInstance:t,unionVariant:e,unionProperties:i,convertOpts:p}){return e.type==="singleProperty"&&this.context.getRecord(t.value)==null&&i.length===1?[this.convert({typeReference:e.typeReference,value:t.value,convertOpts:p})]:i.length>0?[b.TypeLiteral.object({fields:i})]:[]}getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e,convertOpts:i}){return this.context.associateByWireValue({parameters:e.properties??[],values:this.context.getRecord(t.value)??{},ignoreMissingParameters:!0}).map(d=>{this.context.errors.scope(d.name.wireValue);try{return{name:this.context.getPropertyName(d.name.name),value:this.convert({...d,convertOpts:i})}}finally{this.context.errors.unscope()}})}convertObject({object_:t,value:e,convertOpts:i}){let p=this.context.associateByWireValue({parameters:this.filterReadWriteProperties(t.properties,i),values:this.context.getRecord(e)??{}});return b.TypeLiteral.object({fields:p.map(d=>{this.context.errors.scope(d.name.wireValue);try{return{name:this.context.getPropertyName(d.name.name),value:this.convert({...d,convertOpts:i})}}finally{this.context.errors.unscope()}})})}filterReadWriteProperties(t,e){return this.context.customConfig?.experimentalGenerateReadWriteOnlyTypes!==!0||typeof e>"u"||(e.isForRequest&&(t=t.filter(i=>i.propertyAccess!==We.dynamic.ObjectPropertyAccess.ReadOnly)),e.isForResponse&&(t=t.filter(i=>i.propertyAccess!==We.dynamic.ObjectPropertyAccess.WriteOnly))),t}convertEnum({enum_:t,value:e}){let i=this.getEnumValue({enum_:t,value:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.string(i)}getEnumValue({enum_:t,value:e}){if(typeof e!="string"){this.context.errors.add({severity:N.Critical,message:`Expected enum value string, got: ${typeof e}`});return}if(t.values.find(p=>p.wireValue===e)==null){this.context.errors.add({severity:N.Critical,message:`An enum value named "${e}" does not exist in this context`});return}return e}convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e,convertOpts:i}){let p=this.findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e,convertOpts:i});return p??b.TypeLiteral.nop()}findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e,convertOpts:i}){for(let p of t.types){let d=this.context.errors.size();try{let u=this.convert({typeReference:p,value:e,convertOpts:i});if(b.TypeLiteral.isNop(u)){this.context.errors.truncate(d);continue}return u}catch{this.context.errors.truncate(d);continue}}this.context.errors.add({severity:N.Critical,message:`None of the types in the undiscriminated union matched the given "${typeof e}" value`})}convertUnknown({value:t,convertOpts:e}){return b.TypeLiteral.unknown(t)}convertPrimitive({primitive:t,value:e,as:i,convertOpts:p}){switch(t){case"INTEGER":case"UINT":{let d=this.getValueAsNumber({value:e,as:i});return d==null?b.TypeLiteral.nop():b.TypeLiteral.number(d)}case"LONG":case"UINT_64":{let d=this.getValueAsNumber({value:e,as:i});return d==null?b.TypeLiteral.nop():this.context.customConfig?.useBigInt?b.TypeLiteral.bigint(BigInt(d)):b.TypeLiteral.number(d)}case"FLOAT":case"DOUBLE":{let d=this.getValueAsNumber({value:e});return d==null?b.TypeLiteral.nop():b.TypeLiteral.number(d)}case"BOOLEAN":{let d=this.getValueAsBoolean({value:e,as:i});return d==null?b.TypeLiteral.nop():b.TypeLiteral.boolean(d)}case"BASE_64":case"DATE":case"UUID":case"STRING":{let d=this.context.getValueAsString({value:e});return d==null?b.TypeLiteral.nop():b.TypeLiteral.string(d)}case"DATE_TIME":case"DATE_TIME_RFC_2822":{let d=this.context.getValueAsString({value:e});return d==null?b.TypeLiteral.nop():b.TypeLiteral.datetime(d)}case"BIG_INTEGER":{let d=this.context.getValueAsString({value:e});return d==null?b.TypeLiteral.nop():this.context.customConfig?.useBigInt?b.TypeLiteral.bigint(BigInt(d)):b.TypeLiteral.string(d)}default:P(t)}}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==="false"?!1:t:t;return this.context.getValueAsBoolean({value:i})}};l();f();c();var et=class{constructor({context:t}){v(this,"context");this.context=t}getFilePropertyInfo({body:t,value:e}){let i={fileFields:[],bodyPropertyFields:[]},p=this.context.getRecord(e)??{};for(let d of t.properties)switch(d.type){case"file":i.fileFields.push({name:this.context.getPropertyName(d.name),value:this.getSingleFileProperty({property:d,record:p})});break;case"fileArray":i.fileFields.push({name:this.context.getPropertyName(d.name),value:this.getArrayFileProperty({property:d,record:p})});break;case"bodyProperty":i.bodyPropertyFields.push({name:this.context.getPropertyName(d.name.name),value:this.getBodyProperty({property:d,record:p})});break;default:P(d)}return i}getSingleFileProperty({property:t,record:e}){let i=this.context.getSingleFileValue({property:t,record:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.blob(i)}getArrayFileProperty({property:t,record:e}){let i=this.context.getFileArrayValues({property:t,record:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.array({values:i.map(p=>b.TypeLiteral.blob(p))})}getBodyProperty({property:t,record:e}){let i=e[t.name.wireValue];return i==null?b.TypeLiteral.nop():this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:i})}};var tt=class s extends je{constructor({ir:e,config:i}){super({ir:e,config:i});v(this,"ir");v(this,"customConfig");v(this,"dynamicTypeLiteralMapper");v(this,"filePropertyMapper");v(this,"moduleName");v(this,"namespaceExport");v(this,"resolvedClientName");v(this,"resolvedEnvironmentName");this.ir=e,this.customConfig=i.customConfig!=null?i.customConfig:void 0,this.dynamicTypeLiteralMapper=new Qe({context:this}),this.filePropertyMapper=new et({context:this}),this.moduleName=Xa({config:i,customConfig:this.customConfig}),this.namespaceExport=ht({organization:i.organization,workspaceName:i.workspaceName,namespaceExport:this.customConfig?.namespaceExport,naming:this.customConfig?.naming});let p=yt({namespaceExport:this.namespaceExport,naming:this.customConfig?.naming});this.resolvedClientName=p.client,this.resolvedEnvironmentName=p.environment}clone(){return new s({ir:this.ir,config:this.config})}getModuleImport(){return{type:"named",moduleName:this.moduleName}}getRootClientName(){return this.resolvedClientName}getPropertyName(e){return this.customConfig?.retainOriginalCasing||this.customConfig?.noSerdeLayer?this.formatOriginalPropertyName(e.originalName):e.camelCase.unsafeName}getMethodName(e){return e.camelCase.unsafeName}getTypeName(e){return e.pascalCase.unsafeName}getEnvironmentTypeReferenceFromID(e){let i=this.resolveEnvironmentName(e);if(i!=null)return this.getEnvironmentsTypeReference(i)}getFullyQualifiedReference({declaration:e}){return e.fernFilepath.allParts.length>0?`${e.fernFilepath.allParts.map(i=>i.camelCase.unsafeName).join(".")}.${this.getTypeName(e.name)}`:`${this.getTypeName(e.name)}`}getEnvironmentsTypeReference(e){return b.reference({name:this.resolvedEnvironmentName,importFrom:this.getModuleImport(),memberName:this.getTypeName(e)})}formatOriginalPropertyName(e){return e.includes("-")?`"${e}"`:e}};function Xa({config:s,customConfig:t}){return ai({generatorConfig:s,isPackagePrivate:t?.private??!1})?.packageName??s.organization}l();f();c();var vi="client",bi="main",Qa={type:"primitive",value:"STRING"},rt=class{constructor({context:t}){v(this,"context");this.context=t}async generateSnippet({endpoint:t,request:e}){return await this.buildCodeBlock({endpoint:t,snippet:e}).toStringAsync({customConfig:this.context.customConfig})}generateSnippetSync({endpoint:t,request:e}){return this.buildCodeBlock({endpoint:t,snippet:e}).toString({customConfig:this.context.customConfig})}async generateSnippetAst({endpoint:t,request:e}){return this.buildCodeBlock({endpoint:t,snippet:e})}buildCodeBlock({endpoint:t,snippet:e}){return b.codeblock(i=>{i.writeNode(b.function_({name:bi,async:!0,parameters:[],body:b.codeblock(p=>{p.writeNodeStatement(this.constructClient({endpoint:t,snippet:e})),p.writeNodeStatement(this.callMethod({endpoint:t,snippet:e}))})})),i.writeNodeStatement(b.invokeFunction({function_:b.reference({name:bi}),arguments_:[]}))})}constructClient({endpoint:t,snippet:e}){return b.variable({name:vi,const:!0,initializer:b.instantiateClass({class_:b.reference({name:this.context.getRootClientName(),importFrom:this.context.getModuleImport()}),arguments_:[this.getConstructorArgs({endpoint:t,snippet:e})]})})}getConstructorArgs({endpoint:t,snippet:e}){let i=[],p=this.getConstructorEnvironmentArgs({baseUrl:e.baseURL,environment:e.environment});return p.length>0&&i.push(...p),t.auth!=null&&(e.auth!=null?i.push(...this.getConstructorAuthArgs({auth:t.auth,values:e.auth})):this.context.errors.add({severity:N.Warning,message:`Auth with ${t.auth.type} configuration is required for this endpoint`})),this.context.errors.scope(J.PathParameters),this.context.ir.pathParameters!=null&&i.push(...this.getPathParameters({namedParameters:this.context.ir.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(J.Headers),this.context.ir.headers!=null&&e.headers!=null&&i.push(...this.getConstructorHeaderArgs({headers:this.context.ir.headers,values:e.headers})),this.context.errors.unscope(),i.length===0?b.TypeLiteral.nop():b.TypeLiteral.object({fields:i})}getConstructorEnvironmentArgs({baseUrl:t,environment:e}){let i=this.getEnvironmentValue({baseUrl:t,environment:e});return i==null?[]:[{name:"environment",value:i}]}getEnvironmentValue({baseUrl:t,environment:e}){if(t!=null&&e!=null){this.context.errors.add({severity:N.Critical,message:"Cannot specify both baseUrl and environment options"});return}if(t!=null)return b.TypeLiteral.string(t);if(e!=null){if(this.context.isSingleEnvironmentID(e)){let i=this.context.getEnvironmentTypeReferenceFromID(e);if(i==null){this.context.errors.add({severity:N.Warning,message:`Environment ${JSON.stringify(e)} was not found`});return}return b.TypeLiteral.reference(i)}if(this.context.isMultiEnvironmentValues(e))return this.context.validateMultiEnvironmentUrlValues(e)?b.TypeLiteral.object({fields:Object.entries(e).map(([i,p])=>({name:i,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:Qa,value:p})}))}):void 0}}getConstructorAuthArgs({auth:t,values:e}){switch(t.type){case"basic":return e.type!=="basic"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBasicAuthArg({auth:t,values:e});case"bearer":return e.type!=="bearer"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBearerAuthArgs({auth:t,values:e});case"header":return e.type!=="header"?(this.addAuthMismatchError(t,e),[]):this.getConstructorHeaderAuthArgs({auth:t,values:e});case"oauth":return e.type!=="oauth"?(this.addAuthMismatchError(t,e),[]):this.getConstructorOAuthArgs({auth:t,values:e});case"inferred":return e.type!=="inferred"?(this.addAuthMismatchError(t,e),[]):(this.addWarning("The TypeScript SDK v2 Generator does not support Inferred auth scheme yet"),[]);default:P(t)}}addAuthMismatchError(t,e){this.context.errors.add({severity:N.Critical,message:this.context.newAuthMismatchError({auth:t,values:e}).message})}addWarning(t){this.context.errors.add({severity:N.Warning,message:t})}getConstructorBasicAuthArg({auth:t,values:e}){return[{name:this.context.getPropertyName(t.username),value:b.TypeLiteral.string(e.username)},{name:this.context.getPropertyName(t.password),value:b.TypeLiteral.string(e.password)}]}getConstructorBearerAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.token),value:b.TypeLiteral.string(e.token)}]}getConstructorHeaderAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.header.name.name),value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t.header.typeReference,value:e.value})}]}getConstructorOAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.clientId),value:b.TypeLiteral.string(e.clientId)},{name:this.context.getPropertyName(t.clientSecret),value:b.TypeLiteral.string(e.clientSecret)}]}getConstructorHeaderArgs({headers:t,values:e}){let i=[];for(let p of t){let d=e[p.name.wireValue],u=this.getConstructorHeaderArg({header:p,value:d});u!=null&&i.push(u)}return i}getConstructorHeaderArg({header:t,value:e}){let i=this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:e});if(!b.TypeLiteral.isNop(i))return{name:this.context.getPropertyName(t.name.name),value:i}}callMethod({endpoint:t,snippet:e}){return b.invokeMethod({on:b.reference({name:vi}),method:this.getMethod({endpoint:t}),async:!0,arguments_:this.getMethodArgs({endpoint:t,snippet:e})})}getMethodArgs({endpoint:t,snippet:e}){switch(t.request.type){case"inlined":return this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});case"body":return this.getMethodArgsForBodyRequest({request:t.request,snippet:e});default:P(t.request)}}getMethodArgsForBodyRequest({request:t,snippet:e}){let i=[];this.context.errors.scope(J.PathParameters);let p=[...this.context.ir.pathParameters??[],...t.pathParameters??[]];return p.length>0&&i.push(...this.getPathParameters({namedParameters:p,snippet:e}).map(d=>d.value)),this.context.errors.unscope(),this.context.errors.scope(J.RequestBody),t.body!=null&&i.push(this.getBodyRequestArg({body:t.body,value:e.requestBody})),this.context.errors.unscope(),i}getBodyRequestArg({body:t,value:e}){switch(t.type){case"bytes":return this.getBytesBodyRequestArg({value:e});case"typeReference":return this.context.dynamicTypeLiteralMapper.convert({typeReference:t.value,value:e,convertOpts:{isForRequest:!0}});default:P(t)}}getBytesBodyRequestArg({value:t}){return typeof t!="string"?(this.context.errors.add({severity:N.Critical,message:`Expected bytes value to be a string, got ${typeof t}`}),b.TypeLiteral.nop()):b.TypeLiteral.blob(t)}getMethodArgsForInlinedRequest({request:t,snippet:e}){let i=[],{inlinePathParameters:p,inlineFileProperties:d}={inlinePathParameters:this.context.customConfig?.inlinePathParameters??!1,inlineFileProperties:this.context.customConfig?.inlineFileProperties??!1};this.context.errors.scope(J.PathParameters);let u=[];t.pathParameters!=null&&u.push(...this.getPathParameters({namedParameters:t.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(J.RequestBody);let E=this.getFilePropertyInfo({request:t,snippet:e});return this.context.errors.unscope(),this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:p})||i.push(...u.map(I=>I.value)),d||i.push(...E.fileFields.map(I=>I.value)),this.context.needsRequestParameter({request:t,inlinePathParameters:p,inlineFileProperties:d})&&i.push(this.getInlinedRequestArg({request:t,snippet:e,pathParameterFields:this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:p})?u:[],filePropertyInfo:E})),i}getFilePropertyInfo({request:t,snippet:e}){return t.body==null||!this.context.isFileUploadRequestBody(t.body)?{fileFields:[],bodyPropertyFields:[]}:this.context.filePropertyMapper.getFilePropertyInfo({body:t.body,value:e.requestBody})}getInlinedRequestArg({request:t,snippet:e,pathParameterFields:i,filePropertyInfo:p}){this.context.errors.scope(J.QueryParameters);let u=this.context.associateQueryParametersByWireValue({parameters:t.queryParameters??[],values:e.queryParameters??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...R,convertOpts:{isForRequest:!0}})}));this.context.errors.unscope(),this.context.errors.scope(J.Headers);let I=this.context.associateByWireValue({parameters:t.headers??[],values:e.headers??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...R,convertOpts:{isForRequest:!0}})}));this.context.errors.unscope(),this.context.errors.scope(J.RequestBody);let F=t.body!=null?this.getInlinedRequestBodyObjectFields({body:t.body,value:e.requestBody,filePropertyInfo:p}):[];return this.context.errors.unscope(),b.TypeLiteral.object({fields:[...i,...u,...I,...F]})}getInlinedRequestBodyObjectFields({body:t,value:e,filePropertyInfo:i}){switch(t.type){case"properties":return this.getInlinedRequestBodyPropertyObjectFields({parameters:t.value,value:e});case"referenced":return[this.getReferencedRequestBodyPropertyObjectField({body:t,value:e})];case"fileUpload":return this.getFileUploadRequestBodyObjectFields({filePropertyInfo:i});default:P(t)}}getFileUploadRequestBodyObjectFields({filePropertyInfo:t}){return this.context.customConfig?.inlineFileProperties?[...t.fileFields,...t.bodyPropertyFields]:t.bodyPropertyFields}getReferencedRequestBodyPropertyObjectField({body:t,value:e}){return{name:this.context.getPropertyName(t.bodyKey),value:this.getReferencedRequestBodyPropertyTypeLiteral({body:t.bodyType,value:e})}}getReferencedRequestBodyPropertyTypeLiteral({body:t,value:e}){switch(t.type){case"bytes":return this.getBytesBodyRequestArg({value:e});case"typeReference":return this.context.dynamicTypeLiteralMapper.convert({typeReference:t.value,value:e,convertOpts:{isForRequest:!0}});default:P(t)}}getInlinedRequestBodyPropertyObjectFields({parameters:t,value:e}){let i=[],p=this.context.associateByWireValue({parameters:t,values:this.context.getRecord(e)??{}});for(let d of p)i.push({name:this.context.getPropertyName(d.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...d,convertOpts:{isForRequest:!0}})});return i}getPathParameters({namedParameters:t,snippet:e}){let i=[],p=this.context.associateByWireValue({parameters:t,values:e.pathParameters??{},ignoreMissingParameters:!0});for(let d of p)i.push({name:this.context.getPropertyName(d.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...d,convertOpts:{isForRequest:!0}})});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 nt=class extends Ue{constructor({ir:t,config:e}){super(new tt({ir:t,config:e}))}async generate(t,e={}){return super.generate(t,e)}generateSync(t){return super.generateSync(t)}async generateSnippetAst(t){return super.generateSnippetAst(t)}createSnippetGenerator(t){return new rt({context:t})}};0&&(module.exports={DynamicSnippetsGenerator});
|
|
24
|
+
`);e.write(d.replaceAll("`","\\`")),e.writeNoIndent(u.replaceAll("`","\\`")),e.write("`")}writeIterable({writer:e,iterable:i}){let p=_a({values:i.values});if(p.length===0){e.write("[]");return}e.writeLine("["),e.indent();for(let d of p)d.write(e),e.writeLine(",");e.dedent(),e.write("]")}writeRecord({writer:e,record:i}){let p=ja({entries:i.entries});if(p.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let d of p)d.key.write(e),e.write(": "),d.value.write(e),e.writeLine(",");e.dedent(),e.write("}")}writeObject({writer:e,object:i}){let p=$a({fields:i.fields});if(p.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let d of p)e.write(`${d.name}: `),d.value.write(e),e.writeLine(",");e.dedent(),e.write("}")}static array({values:e}){return new this({type:"array",values:e})}static bigint(e){return new this({type:"bigint",value:e})}static blob(e){return new this({type:"blob",value:e})}static boolean(e){return new this({type:"boolean",value:e})}static datetime(e){return new this({type:"datetime",value:e})}static number(e){return new this({type:"number",value:e})}static object({fields:e}){return new this({type:"object",fields:e})}static record({entries:e}){return new this({type:"record",entries:e})}static reference(e){return new this({type:"reference",value:e})}static set({values:e}){return new this({type:"set",values:e})}static string(e){return new this({type:"string",value:e})}static tuple({values:e}){return new this({type:"tuple",values:e})}static unknown(e){return new this({type:"unknown",value:e})}static null(){return new this({type:"null"})}static nop(){return new this({type:"nop"})}static isNop(e){return e.internalType.type==="nop"}writeUnknown({writer:e,value:i}){switch(typeof i){case"boolean":e.write(i.toString());return;case"string":e.write(i.includes('"')?`\`${i}\``:`"${i}"`);return;case"number":e.write(i.toString());return;case"object":if(i==null){e.write("null");return}if(Array.isArray(i)){this.writeUnknownArray({writer:e,value:i});return}this.writeUnknownObject({writer:e,value:i});return;default:throw new Error(`Internal error; unsupported unknown type: ${typeof i}`)}}writeUnknownArray({writer:e,value:i}){if(i.length===0){e.write("[]");return}e.writeLine("["),e.indent();for(let p of i)e.writeNode(s.unknown(p)),e.writeLine(",");e.dedent(),e.write("]")}writeUnknownObject({writer:e,value:i}){let p=Object.entries(i);if(p.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let[d,u]of p)e.write(`${d}: `),e.writeNode(s.unknown(u)),e.writeLine(",");e.dedent(),e.write("}")}};function $a({fields:s}){return s.filter(t=>!re.isNop(t.value))}function ja({entries:s}){return s.filter(t=>!re.isNop(t.key)&&!re.isNop(t.value))}function _a({values:s}){return s.filter(t=>!re.isNop(t))}l();f();c();var Re=class extends k{constructor(e){super();v(this,"args");this.args=e}write(e){this.args.export&&e.write("export "),this.args.const?e.write("const "):e.write("let "),e.write(`${this.args.name} = `),e.writeNode(this.args.initializer)}};function Wa(s){return new be(s)}function Va(s){return new Ee(s)}function qa(s){return new ve(s)}function Ga(s){return new Ie(s)}function za(s){return new Te(s)}function Ha(s){return new Ae(s)}function Ya(s){return new de(s)}function Ja(s){return new Re(s)}l();f();c();l();f();c();function ai({generatorConfig:s,isPackagePrivate:t}){let e=s.output.mode;switch(e.type){case"downloadFiles":return;case"publish":return{packageName:e.registriesV2.npm.packageName,version:e.version,private:t,publishInfo:{registryUrl:e.registriesV2.npm.registryUrl,token:e.registriesV2.npm.token},license:void 0,repoUrl:void 0};case"github":if(e.publishInfo!=null&&e.publishInfo.type!=="npm")throw new Error(`Expected to receive npm publish info but received ${e.publishInfo.type} instead`);return{packageName:e.publishInfo!=null?e.publishInfo.packageName:"",version:e.version,private:t,publishInfo:void 0,repoUrl:Za(e.repoUrl),license:s.license?._visit({basic:i=>i.id,custom:i=>`See ${i.filename}`,_other:()=>{}})};default:throw new Error(`Encountered unknown output mode: ${e}`)}}function Za(s){if(s!=null)return s.startsWith("https://github.com/")?`git+${s}`:s.startsWith("ssh://github.com/")?`github:${De(s).replace("ssh://github.com/","")}`:s.startsWith("https://bitbucket.org/")?`bitbucket:${De(s).replace("https://bitbucket.org/","")}`:s.startsWith("ssh://bitbucket.org/")?`bitbucket:${De(s).replace("ssh://bitbucket.org/","")}`:s.startsWith("https://gitlab.com/")?`gitlab:${De(s).replace("https://gitlab.com/","")}`:s.startsWith("ssh://gitlab.com/")?`gitlab:${De(s).replace("ssh://gitlab.com/","")}`:(s.startsWith("git+")||(s=`git+${s}`),s.endsWith(".git")||(s=`${s}.git`),s)}function De(s){return s.endsWith(".git")?s.slice(0,-4):s}l();f();c();function ht({organization:s,workspaceName:t,namespaceExport:e,naming:i}){return(typeof i=="string"?i:i?.namespace)??e??`${le(Se(s))}${le(Se(t))}`}function yt({namespaceExport:s,naming:t}){let e=typeof t=="string"?{}:t,i=t!=null?le(Se(s)):s;return{namespace:s,client:e?.client??`${i}Client`,error:e?.error??`${i}Error`,timeoutError:e?.timeoutError??`${i}TimeoutError`,environment:e?.environment??`${i}Environment`,environmentUrls:e?.environmentUrls??`${i}EnvironmentUrls`,version:e?.version??`${i}Version`}}l();f();c();l();f();c();var We={};K(We,{commons:()=>si,dynamic:()=>vt,generatorExec:()=>Et});l();f();c();l();f();c();var si={};l();f();c();var vt={};K(vt,{ErrorSeverity:()=>hi,HttpMethod:()=>di,ObjectPropertyAccess:()=>yi,PrimitiveTypeV1:()=>xi,auth:()=>ui,commons:()=>pi,declaration:()=>li,endpoints:()=>ci,environment:()=>fi,http:()=>xt,ir:()=>mi,snippets:()=>gt,types:()=>wt,variables:()=>gi});l();f();c();l();f();c();var ui={};l();f();c();var pi={};l();f();c();var li={};l();f();c();var ci={};l();f();c();var fi={};l();f();c();var xt={};K(xt,{HttpMethod:()=>di});l();f();c();l();f();c();var di={Get:"GET",Post:"POST",Put:"PUT",Patch:"PATCH",Delete:"DELETE",Head:"HEAD"};var mi={};l();f();c();var gt={};K(gt,{ErrorSeverity:()=>hi});l();f();c();l();f();c();var hi={Critical:"CRITICAL",Warning:"WARNING"};var wt={};K(wt,{ObjectPropertyAccess:()=>yi,PrimitiveTypeV1:()=>xi});l();f();c();l();f();c();var yi={ReadOnly:"READ_ONLY",WriteOnly:"WRITE_ONLY"};l();f();c();var xi={Integer:"INTEGER",Long:"LONG",Uint:"UINT",Uint64:"UINT_64",Float:"FLOAT",Double:"DOUBLE",Boolean:"BOOLEAN",String:"STRING",Date:"DATE",DateTime:"DATE_TIME",DateTimeRfc2822:"DATE_TIME_RFC_2822",Uuid:"UUID",Base64:"BASE_64",BigInteger:"BIG_INTEGER"};var gi={};l();f();c();var Et={};K(Et,{LicenseId:()=>wi,config:()=>bt});l();f();c();l();f();c();var bt={};K(bt,{LicenseId:()=>wi});l();f();c();l();f();c();var wi={Mit:"MIT",Apache2:"Apache-2.0"};var Ka="value",Qe=class{constructor({context:t}){v(this,"context");this.context=t}convert(t){let e=t.convertOpts;if(t.value===null)return this.context.isNullable(t.typeReference)?b.TypeLiteral.null():(this.context.errors.add({severity:N.Critical,message:"Expected non-null value, but got null"}),b.TypeLiteral.nop());if(t.value===void 0)return b.TypeLiteral.nop();switch(t.typeReference.type){case"list":return this.convertList({list:t.typeReference.value,value:t.value,convertOpts:e});case"literal":return this.convertLiteral({literalType:t.typeReference.value,value:t.value,convertOpts:e});case"map":return this.convertMap({map:t.typeReference,value:t.value,convertOpts:e});case"named":{let i=this.context.resolveNamedType({typeId:t.typeReference.value});return i==null?b.TypeLiteral.nop():this.convertNamed({named:i,value:t.value,as:t.as,convertOpts:e})}case"optional":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as,convertOpts:e});case"nullable":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as,convertOpts:e});case"primitive":return this.convertPrimitive({primitive:t.typeReference.value,value:t.value,as:t.as,convertOpts:e});case"set":return this.convertSet({set:t.typeReference.value,value:t.value,convertOpts:e});case"unknown":return this.convertUnknown({value:t.value,convertOpts:e});default:P(t.typeReference)}}convertLiteral({literalType:t,value:e}){switch(t.type){case"boolean":{let i=this.context.getValueAsBoolean({value:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.boolean(i)}case"string":{let i=this.context.getValueAsString({value:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.string(i)}default:P(t)}}convertList({list:t,value:e,convertOpts:i}){return Array.isArray(e)?b.TypeLiteral.array({values:e.map((p,d)=>{this.context.errors.scope({index:d});try{return this.convert({typeReference:t,value:p,convertOpts:i})}finally{this.context.errors.unscope()}})}):(this.context.errors.add({severity:N.Critical,message:`Expected array but got: ${typeof e}`}),b.TypeLiteral.nop())}convertSet({set:t,value:e,convertOpts:i}){return Array.isArray(e)?b.TypeLiteral.set({values:e.map((p,d)=>{this.context.errors.scope({index:d});try{return this.convert({typeReference:t,value:p,convertOpts:i})}finally{this.context.errors.unscope()}})}):(this.context.errors.add({severity:N.Critical,message:`Expected array but got: ${typeof e}`}),b.TypeLiteral.nop())}convertMap({map:t,value:e,convertOpts:i}){return typeof e!="object"||e==null?(this.context.errors.add({severity:N.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),b.TypeLiteral.nop()):b.TypeLiteral.record({entries:Object.entries(e).map(([p,d])=>{this.context.errors.scope(p);try{return{key:this.convert({typeReference:t.key,value:p,as:"key",convertOpts:i}),value:this.convert({typeReference:t.value,value:d,convertOpts:i})}}finally{this.context.errors.unscope()}})})}convertNamed({named:t,value:e,as:i,convertOpts:p}){switch(t.type){case"alias":return this.context.customConfig?.useBrandedStringAliases?b.TypeLiteral.reference(b.codeblock(d=>{d.writeNode(b.reference({name:this.context.namespaceExport,importFrom:this.context.getModuleImport(),memberName:this.context.getFullyQualifiedReference({declaration:t.declaration})})),d.write("("),d.writeNode(this.convert({typeReference:t.typeReference,value:e,as:i,convertOpts:p})),d.write(")")})):this.convert({typeReference:t.typeReference,value:e,as:i,convertOpts:p});case"discriminatedUnion":return this.convertDiscriminatedUnion({discriminatedUnion:t,value:e,convertOpts:p});case"enum":return this.convertEnum({enum_:t,value:e,convertOpts:p});case"object":return this.convertObject({object_:t,value:e,convertOpts:p});case"undiscriminatedUnion":return this.convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e,convertOpts:p});default:P(t)}}convertDiscriminatedUnion({discriminatedUnion:t,value:e,convertOpts:i}){let p=this.context.resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e});if(p==null)return b.TypeLiteral.nop();let d=p.singleDiscriminatedUnionType,u=this.convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:p,unionVariant:d,convertOpts:i});if(u==null)return b.TypeLiteral.nop();if(this.context.customConfig?.includeUtilsOnUnionMembers)return b.TypeLiteral.reference(b.codeblock(I=>{I.writeNode(b.invokeMethod({on:b.reference({name:this.context.namespaceExport,importFrom:this.context.getModuleImport(),memberName:this.context.getFullyQualifiedReference({declaration:t.declaration})}),method:this.context.getMethodName(d.discriminantValue.name),arguments_:this.convertDiscriminatedUnionUtilsArgs({discriminatedUnionTypeInstance:p,unionVariant:d,unionProperties:u,convertOpts:i})}))}));let E={name:this.context.getPropertyName(t.discriminant.name),value:b.TypeLiteral.string(d.discriminantValue.wireValue)};return b.TypeLiteral.object({fields:[E,...u]})}convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:t,unionVariant:e,convertOpts:i}){let p=this.getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e});switch(e.type){case"samePropertiesAsObject":{let d=this.context.resolveNamedType({typeId:e.typeId});if(d==null)return;let u=this.convertNamed({named:d,value:t.value,convertOpts:i});if(!u.isObject()){this.context.errors.add({severity:N.Critical,message:"Internal error; expected union value to be an object"});return}let E=u.asObjectOrThrow();return[...p,...E.fields]}case"singleProperty":try{this.context.errors.scope(e.discriminantValue.wireValue);let d=this.context.getRecord(t.value);return d==null?[...p,{name:Ka,value:this.convert({typeReference:e.typeReference,value:t.value,convertOpts:i})}]:[...p,{name:this.context.getPropertyName(e.discriminantValue.name),value:this.convert({typeReference:e.typeReference,value:d[e.discriminantValue.wireValue],convertOpts:i})}]}finally{this.context.errors.unscope()}case"noProperties":return p;default:P(e)}}convertDiscriminatedUnionUtilsArgs({discriminatedUnionTypeInstance:t,unionVariant:e,unionProperties:i,convertOpts:p}){return e.type==="singleProperty"&&this.context.getRecord(t.value)==null&&i.length===1?[this.convert({typeReference:e.typeReference,value:t.value,convertOpts:p})]:i.length>0?[b.TypeLiteral.object({fields:i})]:[]}getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e,convertOpts:i}){return this.context.associateByWireValue({parameters:e.properties??[],values:this.context.getRecord(t.value)??{},ignoreMissingParameters:!0}).map(d=>{this.context.errors.scope(d.name.wireValue);try{return{name:this.context.getPropertyName(d.name.name),value:this.convert({...d,convertOpts:i})}}finally{this.context.errors.unscope()}})}convertObject({object_:t,value:e,convertOpts:i}){let p=this.context.associateByWireValue({parameters:this.filterReadWriteProperties(t.properties,i),values:this.context.getRecord(e)??{}});return b.TypeLiteral.object({fields:p.map(d=>{this.context.errors.scope(d.name.wireValue);try{return{name:this.context.getPropertyName(d.name.name),value:this.convert({...d,convertOpts:i})}}finally{this.context.errors.unscope()}})})}filterReadWriteProperties(t,e){return this.context.customConfig?.experimentalGenerateReadWriteOnlyTypes!==!0||typeof e>"u"||(e.isForRequest&&(t=t.filter(i=>i.propertyAccess!==We.dynamic.ObjectPropertyAccess.ReadOnly)),e.isForResponse&&(t=t.filter(i=>i.propertyAccess!==We.dynamic.ObjectPropertyAccess.WriteOnly))),t}convertEnum({enum_:t,value:e}){let i=this.getEnumValue({enum_:t,value:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.string(i)}getEnumValue({enum_:t,value:e}){if(typeof e!="string"){this.context.errors.add({severity:N.Critical,message:`Expected enum value string, got: ${typeof e}`});return}if(t.values.find(p=>p.wireValue===e)==null){this.context.errors.add({severity:N.Critical,message:`An enum value named "${e}" does not exist in this context`});return}return e}convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e,convertOpts:i}){let p=this.findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e,convertOpts:i});return p??b.TypeLiteral.nop()}findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e,convertOpts:i}){for(let p of t.types){let d=this.context.errors.size();try{let u=this.convert({typeReference:p,value:e,convertOpts:i});if(b.TypeLiteral.isNop(u)||this.context.errors.size()>d){this.context.errors.truncate(d);continue}return u}catch{this.context.errors.truncate(d);continue}}this.context.errors.add({severity:N.Critical,message:`None of the types in the undiscriminated union matched the given "${typeof e}" value`})}convertUnknown({value:t,convertOpts:e}){return b.TypeLiteral.unknown(t)}convertPrimitive({primitive:t,value:e,as:i,convertOpts:p}){switch(t){case"INTEGER":case"UINT":{let d=this.getValueAsNumber({value:e,as:i});return d==null?b.TypeLiteral.nop():b.TypeLiteral.number(d)}case"LONG":case"UINT_64":{let d=this.getValueAsNumber({value:e,as:i});return d==null?b.TypeLiteral.nop():this.context.customConfig?.useBigInt?b.TypeLiteral.bigint(BigInt(d)):b.TypeLiteral.number(d)}case"FLOAT":case"DOUBLE":{let d=this.getValueAsNumber({value:e});return d==null?b.TypeLiteral.nop():b.TypeLiteral.number(d)}case"BOOLEAN":{let d=this.getValueAsBoolean({value:e,as:i});return d==null?b.TypeLiteral.nop():b.TypeLiteral.boolean(d)}case"BASE_64":case"DATE":case"UUID":case"STRING":{let d=this.context.getValueAsString({value:e});return d==null?b.TypeLiteral.nop():b.TypeLiteral.string(d)}case"DATE_TIME":case"DATE_TIME_RFC_2822":{let d=this.context.getValueAsString({value:e});return d==null?b.TypeLiteral.nop():b.TypeLiteral.datetime(d)}case"BIG_INTEGER":{let d=this.context.getValueAsString({value:e});return d==null?b.TypeLiteral.nop():this.context.customConfig?.useBigInt?b.TypeLiteral.bigint(BigInt(d)):b.TypeLiteral.string(d)}default:P(t)}}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==="false"?!1:t:t;return this.context.getValueAsBoolean({value:i})}};l();f();c();var et=class{constructor({context:t}){v(this,"context");this.context=t}getFilePropertyInfo({body:t,value:e}){let i={fileFields:[],bodyPropertyFields:[]},p=this.context.getRecord(e)??{};for(let d of t.properties)switch(d.type){case"file":i.fileFields.push({name:this.context.getPropertyName(d.name),value:this.getSingleFileProperty({property:d,record:p})});break;case"fileArray":i.fileFields.push({name:this.context.getPropertyName(d.name),value:this.getArrayFileProperty({property:d,record:p})});break;case"bodyProperty":i.bodyPropertyFields.push({name:this.context.getPropertyName(d.name.name),value:this.getBodyProperty({property:d,record:p})});break;default:P(d)}return i}getSingleFileProperty({property:t,record:e}){let i=this.context.getSingleFileValue({property:t,record:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.blob(i)}getArrayFileProperty({property:t,record:e}){let i=this.context.getFileArrayValues({property:t,record:e});return i==null?b.TypeLiteral.nop():b.TypeLiteral.array({values:i.map(p=>b.TypeLiteral.blob(p))})}getBodyProperty({property:t,record:e}){let i=e[t.name.wireValue];return i==null?b.TypeLiteral.nop():this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:i})}};var tt=class s extends je{constructor({ir:e,config:i}){super({ir:e,config:i});v(this,"ir");v(this,"customConfig");v(this,"dynamicTypeLiteralMapper");v(this,"filePropertyMapper");v(this,"moduleName");v(this,"namespaceExport");v(this,"resolvedClientName");v(this,"resolvedEnvironmentName");this.ir=e,this.customConfig=i.customConfig!=null?i.customConfig:void 0,this.dynamicTypeLiteralMapper=new Qe({context:this}),this.filePropertyMapper=new et({context:this}),this.moduleName=Xa({config:i,customConfig:this.customConfig}),this.namespaceExport=ht({organization:i.organization,workspaceName:i.workspaceName,namespaceExport:this.customConfig?.namespaceExport,naming:this.customConfig?.naming});let p=yt({namespaceExport:this.namespaceExport,naming:this.customConfig?.naming});this.resolvedClientName=p.client,this.resolvedEnvironmentName=p.environment}clone(){return new s({ir:this.ir,config:this.config})}getModuleImport(){return{type:"named",moduleName:this.moduleName}}getRootClientName(){return this.resolvedClientName}getPropertyName(e){return this.customConfig?.retainOriginalCasing||this.customConfig?.noSerdeLayer?this.formatOriginalPropertyName(e.originalName):e.camelCase.unsafeName}getMethodName(e){return e.camelCase.unsafeName}getTypeName(e){return e.pascalCase.unsafeName}getEnvironmentTypeReferenceFromID(e){let i=this.resolveEnvironmentName(e);if(i!=null)return this.getEnvironmentsTypeReference(i)}getFullyQualifiedReference({declaration:e}){return e.fernFilepath.allParts.length>0?`${e.fernFilepath.allParts.map(i=>i.camelCase.unsafeName).join(".")}.${this.getTypeName(e.name)}`:`${this.getTypeName(e.name)}`}getEnvironmentsTypeReference(e){return b.reference({name:this.resolvedEnvironmentName,importFrom:this.getModuleImport(),memberName:this.getTypeName(e)})}formatOriginalPropertyName(e){return e.includes("-")?`"${e}"`:e}};function Xa({config:s,customConfig:t}){return ai({generatorConfig:s,isPackagePrivate:t?.private??!1})?.packageName??s.organization}l();f();c();var vi="client",bi="main",Qa={type:"primitive",value:"STRING"},rt=class{constructor({context:t}){v(this,"context");this.context=t}async generateSnippet({endpoint:t,request:e}){return await this.buildCodeBlock({endpoint:t,snippet:e}).toStringAsync({customConfig:this.context.customConfig})}generateSnippetSync({endpoint:t,request:e}){return this.buildCodeBlock({endpoint:t,snippet:e}).toString({customConfig:this.context.customConfig})}async generateSnippetAst({endpoint:t,request:e}){return this.buildCodeBlock({endpoint:t,snippet:e})}buildCodeBlock({endpoint:t,snippet:e}){return b.codeblock(i=>{i.writeNode(b.function_({name:bi,async:!0,parameters:[],body:b.codeblock(p=>{p.writeNodeStatement(this.constructClient({endpoint:t,snippet:e})),p.writeNodeStatement(this.callMethod({endpoint:t,snippet:e}))})})),i.writeNodeStatement(b.invokeFunction({function_:b.reference({name:bi}),arguments_:[]}))})}constructClient({endpoint:t,snippet:e}){return b.variable({name:vi,const:!0,initializer:b.instantiateClass({class_:b.reference({name:this.context.getRootClientName(),importFrom:this.context.getModuleImport()}),arguments_:[this.getConstructorArgs({endpoint:t,snippet:e})]})})}getConstructorArgs({endpoint:t,snippet:e}){let i=[],p=this.getConstructorEnvironmentArgs({baseUrl:e.baseURL,environment:e.environment});return p.length>0&&i.push(...p),t.auth!=null&&(e.auth!=null?i.push(...this.getConstructorAuthArgs({auth:t.auth,values:e.auth})):this.context.errors.add({severity:N.Warning,message:`Auth with ${t.auth.type} configuration is required for this endpoint`})),this.context.errors.scope(J.PathParameters),this.context.ir.pathParameters!=null&&i.push(...this.getPathParameters({namedParameters:this.context.ir.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(J.Headers),this.context.ir.headers!=null&&e.headers!=null&&i.push(...this.getConstructorHeaderArgs({headers:this.context.ir.headers,values:e.headers})),this.context.errors.unscope(),i.length===0?b.TypeLiteral.nop():b.TypeLiteral.object({fields:i})}getConstructorEnvironmentArgs({baseUrl:t,environment:e}){let i=this.getEnvironmentValue({baseUrl:t,environment:e});return i==null?[]:[{name:"environment",value:i}]}getEnvironmentValue({baseUrl:t,environment:e}){if(t!=null&&e!=null){this.context.errors.add({severity:N.Critical,message:"Cannot specify both baseUrl and environment options"});return}if(t!=null)return b.TypeLiteral.string(t);if(e!=null){if(this.context.isSingleEnvironmentID(e)){let i=this.context.getEnvironmentTypeReferenceFromID(e);if(i==null){this.context.errors.add({severity:N.Warning,message:`Environment ${JSON.stringify(e)} was not found`});return}return b.TypeLiteral.reference(i)}if(this.context.isMultiEnvironmentValues(e))return this.context.validateMultiEnvironmentUrlValues(e)?b.TypeLiteral.object({fields:Object.entries(e).map(([i,p])=>({name:i,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:Qa,value:p})}))}):void 0}}getConstructorAuthArgs({auth:t,values:e}){switch(t.type){case"basic":return e.type!=="basic"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBasicAuthArg({auth:t,values:e});case"bearer":return e.type!=="bearer"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBearerAuthArgs({auth:t,values:e});case"header":return e.type!=="header"?(this.addAuthMismatchError(t,e),[]):this.getConstructorHeaderAuthArgs({auth:t,values:e});case"oauth":return e.type!=="oauth"?(this.addAuthMismatchError(t,e),[]):this.getConstructorOAuthArgs({auth:t,values:e});case"inferred":return e.type!=="inferred"?(this.addAuthMismatchError(t,e),[]):(this.addWarning("The TypeScript SDK v2 Generator does not support Inferred auth scheme yet"),[]);default:P(t)}}addAuthMismatchError(t,e){this.context.errors.add({severity:N.Critical,message:this.context.newAuthMismatchError({auth:t,values:e}).message})}addWarning(t){this.context.errors.add({severity:N.Warning,message:t})}getConstructorBasicAuthArg({auth:t,values:e}){return[{name:this.context.getPropertyName(t.username),value:b.TypeLiteral.string(e.username)},{name:this.context.getPropertyName(t.password),value:b.TypeLiteral.string(e.password)}]}getConstructorBearerAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.token),value:b.TypeLiteral.string(e.token)}]}getConstructorHeaderAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.header.name.name),value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t.header.typeReference,value:e.value})}]}getConstructorOAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.clientId),value:b.TypeLiteral.string(e.clientId)},{name:this.context.getPropertyName(t.clientSecret),value:b.TypeLiteral.string(e.clientSecret)}]}getConstructorHeaderArgs({headers:t,values:e}){let i=[];for(let p of t){let d=e[p.name.wireValue],u=this.getConstructorHeaderArg({header:p,value:d});u!=null&&i.push(u)}return i}getConstructorHeaderArg({header:t,value:e}){let i=this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:e});if(!b.TypeLiteral.isNop(i))return{name:this.context.getPropertyName(t.name.name),value:i}}callMethod({endpoint:t,snippet:e}){return b.invokeMethod({on:b.reference({name:vi}),method:this.getMethod({endpoint:t}),async:!0,arguments_:this.getMethodArgs({endpoint:t,snippet:e})})}getMethodArgs({endpoint:t,snippet:e}){switch(t.request.type){case"inlined":return this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});case"body":return this.getMethodArgsForBodyRequest({request:t.request,snippet:e});default:P(t.request)}}getMethodArgsForBodyRequest({request:t,snippet:e}){let i=[];this.context.errors.scope(J.PathParameters);let p=[...this.context.ir.pathParameters??[],...t.pathParameters??[]];return p.length>0&&i.push(...this.getPathParameters({namedParameters:p,snippet:e}).map(d=>d.value)),this.context.errors.unscope(),this.context.errors.scope(J.RequestBody),t.body!=null&&i.push(this.getBodyRequestArg({body:t.body,value:e.requestBody})),this.context.errors.unscope(),i}getBodyRequestArg({body:t,value:e}){switch(t.type){case"bytes":return this.getBytesBodyRequestArg({value:e});case"typeReference":return this.context.dynamicTypeLiteralMapper.convert({typeReference:t.value,value:e,convertOpts:{isForRequest:!0}});default:P(t)}}getBytesBodyRequestArg({value:t}){return typeof t!="string"?(this.context.errors.add({severity:N.Critical,message:`Expected bytes value to be a string, got ${typeof t}`}),b.TypeLiteral.nop()):b.TypeLiteral.blob(t)}getMethodArgsForInlinedRequest({request:t,snippet:e}){let i=[],{inlinePathParameters:p,inlineFileProperties:d}={inlinePathParameters:this.context.customConfig?.inlinePathParameters??!1,inlineFileProperties:this.context.customConfig?.inlineFileProperties??!1};this.context.errors.scope(J.PathParameters);let u=[];t.pathParameters!=null&&u.push(...this.getPathParameters({namedParameters:t.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(J.RequestBody);let E=this.getFilePropertyInfo({request:t,snippet:e});return this.context.errors.unscope(),this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:p})||i.push(...u.map(I=>I.value)),d||i.push(...E.fileFields.map(I=>I.value)),this.context.needsRequestParameter({request:t,inlinePathParameters:p,inlineFileProperties:d})&&i.push(this.getInlinedRequestArg({request:t,snippet:e,pathParameterFields:this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:p})?u:[],filePropertyInfo:E})),i}getFilePropertyInfo({request:t,snippet:e}){return t.body==null||!this.context.isFileUploadRequestBody(t.body)?{fileFields:[],bodyPropertyFields:[]}:this.context.filePropertyMapper.getFilePropertyInfo({body:t.body,value:e.requestBody})}getInlinedRequestArg({request:t,snippet:e,pathParameterFields:i,filePropertyInfo:p}){this.context.errors.scope(J.QueryParameters);let u=this.context.associateQueryParametersByWireValue({parameters:t.queryParameters??[],values:e.queryParameters??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...R,convertOpts:{isForRequest:!0}})}));this.context.errors.unscope(),this.context.errors.scope(J.Headers);let I=this.context.associateByWireValue({parameters:t.headers??[],values:e.headers??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...R,convertOpts:{isForRequest:!0}})}));this.context.errors.unscope(),this.context.errors.scope(J.RequestBody);let F=t.body!=null?this.getInlinedRequestBodyObjectFields({body:t.body,value:e.requestBody,filePropertyInfo:p}):[];return this.context.errors.unscope(),b.TypeLiteral.object({fields:[...i,...u,...I,...F]})}getInlinedRequestBodyObjectFields({body:t,value:e,filePropertyInfo:i}){switch(t.type){case"properties":return this.getInlinedRequestBodyPropertyObjectFields({parameters:t.value,value:e});case"referenced":return[this.getReferencedRequestBodyPropertyObjectField({body:t,value:e})];case"fileUpload":return this.getFileUploadRequestBodyObjectFields({filePropertyInfo:i});default:P(t)}}getFileUploadRequestBodyObjectFields({filePropertyInfo:t}){return this.context.customConfig?.inlineFileProperties?[...t.fileFields,...t.bodyPropertyFields]:t.bodyPropertyFields}getReferencedRequestBodyPropertyObjectField({body:t,value:e}){return{name:this.context.getPropertyName(t.bodyKey),value:this.getReferencedRequestBodyPropertyTypeLiteral({body:t.bodyType,value:e})}}getReferencedRequestBodyPropertyTypeLiteral({body:t,value:e}){switch(t.type){case"bytes":return this.getBytesBodyRequestArg({value:e});case"typeReference":return this.context.dynamicTypeLiteralMapper.convert({typeReference:t.value,value:e,convertOpts:{isForRequest:!0}});default:P(t)}}getInlinedRequestBodyPropertyObjectFields({parameters:t,value:e}){let i=[],p=this.context.associateByWireValue({parameters:t,values:this.context.getRecord(e)??{}});for(let d of p)i.push({name:this.context.getPropertyName(d.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...d,convertOpts:{isForRequest:!0}})});return i}getPathParameters({namedParameters:t,snippet:e}){let i=[],p=this.context.associateByWireValue({parameters:t,values:e.pathParameters??{},ignoreMissingParameters:!0});for(let d of p)i.push({name:this.context.getPropertyName(d.name.name),value:this.context.dynamicTypeLiteralMapper.convert({...d,convertOpts:{isForRequest:!0}})});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 nt=class extends Ue{constructor({ir:t,config:e}){super(new tt({ir:t,config:e}))}async generate(t,e={}){return super.generate(t,e)}generateSync(t){return super.generateSync(t)}async generateSnippetAst(t){return super.generateSnippetAst(t)}createSnippetGenerator(t){return new rt({context:t})}};0&&(module.exports={DynamicSnippetsGenerator});
|
|
25
25
|
/*! Bundled license information:
|
|
26
26
|
|
|
27
27
|
@jspm/core/nodelibs/browser/chunk-DtuTasat.js:
|