66 OperationParameterBase ,
77 OperationQueryParameterSchema ,
88 type QueryParamSchemaSchema ,
9+ recordToObject ,
10+ refToName ,
911 type Schema ,
1012 SchemaSchema ,
1113 SchemaSchemaBoolean ,
@@ -38,15 +40,17 @@ const OpenApiSchemaPaths = z.record(OpenApiSchemaPath, SchemaSchemaRef);
3840
3941const OpenApiSchemaComponents = z . object ( ) ;
4042
43+ const OpenApiServersSchema = z . tuple ( [
44+ z . object ( { url : z . literal ( "https://lichess.org" ) } ) ,
45+ z . object ( { url : z . literal ( "https://lichess.dev" ) } ) ,
46+ z . object ( { url : z . literal ( "http://localhost:{port}" ) } ) ,
47+ z . object ( { url : z . literal ( "http://l.org" ) } ) ,
48+ ] ) ;
49+
4150const OpenApiSchemaSchema = z . object ( {
4251 openapi : z . literal ( "3.1.0" ) ,
4352 info : OpenApiSchemaInfo ,
44- servers : z . tuple ( [
45- z . object ( { url : z . literal ( "https://lichess.org" ) } ) ,
46- z . object ( { url : z . literal ( "https://lichess.dev" ) } ) ,
47- z . object ( { url : z . literal ( "http://localhost:{port}" ) } ) ,
48- z . object ( { url : z . literal ( "http://l.org" ) } ) ,
49- ] ) ,
53+ servers : OpenApiServersSchema ,
5054 tags : z . array ( z . object ( { name : z . string ( ) , description : z . string ( ) } ) ) ,
5155 paths : OpenApiSchemaPaths ,
5256 components : OpenApiSchemaComponents ,
@@ -248,10 +252,30 @@ const ResponseStatus = z.string();
248252
249253const OperationResponses = z . record ( ResponseStatus , ResponseSchema ) ;
250254
255+ const LichessServerSchema = z . union ( [
256+ z . object ( {
257+ url : z . literal ( [
258+ "https://engine.lichess.ovh" ,
259+ "https://explorer.lichess.org" ,
260+ "https://tablebase.lichess.org" ,
261+ ] ) ,
262+ } ) ,
263+ z . object ( {
264+ url : z . literal ( "http://localhost:{port}" ) ,
265+ variables : z . object ( { port : z . object ( { default : z . string ( ) } ) } ) ,
266+ } ) ,
267+ ] ) ;
268+
269+ const LichessServersSchema = z
270+ . tuple ( [ LichessServerSchema ] )
271+ . rest ( LichessServerSchema )
272+ . transform ( ( s ) => ( { url : s [ 0 ] . url , __id : "__servers" as const } ) ) ;
273+
251274const BaseTagSchemaOperation = z . object ( {
252275 operationId : z . string ( ) ,
253276 summary : z . string ( ) ,
254277 description : z . string ( ) ,
278+ servers : LichessServersSchema . optional ( ) ,
255279 tags : z . array ( z . string ( ) ) ,
256280 security : SecuritySchema ,
257281 parameters : OperationParameters ,
@@ -312,18 +336,7 @@ const TagSchemaSchemaPut = BaseTagSchemaOperation.extend({
312336
313337const TagSchemaSchema = z
314338 . object ( {
315- servers : z
316- . tuple ( [
317- z . object ( {
318- url : z . literal ( [
319- "https://engine.lichess.ovh" ,
320- "https://explorer.lichess.org" ,
321- "https://tablebase.lichess.org" ,
322- ] ) ,
323- } ) ,
324- ] )
325- . transform ( ( s ) => ( { url : s [ 0 ] . url , __id : "__servers" as const } ) )
326- . optional ( ) ,
339+ servers : LichessServersSchema . optional ( ) ,
327340 parameters : z
328341 . array ( OperationPathParameterSchema )
329342 . transform ( ( s ) => ( { parameters : s , __id : "__parameters" as const } ) )
@@ -453,13 +466,13 @@ function schemaToTypescriptTypes(
453466 case "$ref" :
454467 case "notverified:reftoprimitive" : {
455468 const ref = schema . $ref ;
456- const name = ref . split ( "/" ) . pop ( ) ! . replace ( ".yaml" , "" ) ;
469+ const name = refToName ( ref ) ;
457470 const typescriptSchema = `schemas.${ name } ` as const ;
458471 return typescriptSchema ;
459472 }
460473 case "notverified:reftoprimitive:nullable" : {
461474 const ref = schema . allOf [ 0 ] . $ref ;
462- const name = ref . split ( "/" ) . pop ( ) ! . replace ( ".yaml" , "" ) ;
475+ const name = refToName ( ref ) ;
463476 const typescriptSchema = `schemas.${ name } | null` as const ;
464477 return typescriptSchema ;
465478 }
@@ -474,15 +487,7 @@ function schemaToTypescriptTypes(
474487 : ( `?: ${ typescriptSchema } ` as const ) ;
475488 objectRecord [ k ] = propStr ;
476489 }
477- const entries = Object . entries ( objectRecord ) ;
478- if ( entries . length === 1 ) {
479- return `{ "${ entries [ 0 ] ! [ 0 ] } " ${ entries [ 0 ] ! [ 1 ] } }` as const ;
480- }
481- return (
482- "{\n" +
483- entries . map ( ( [ k , v ] ) => ` "${ k } " ${ v } ,` as const ) . join ( "\n" ) +
484- "\n}"
485- ) ;
490+ return recordToObject ( objectRecord , { colon : false } ) ;
486491 }
487492 case "boolean" :
488493 case "boolean-like" : {
@@ -524,7 +529,7 @@ function schemaToTypescriptTypes(
524529 }
525530 case "array:notverified:reftoprimitive" : {
526531 const ref = schema . items . $ref ;
527- const name = ref . split ( "/" ) . pop ( ) ! . replace ( ".yaml" , "" ) ;
532+ const name = refToName ( ref ) ;
528533 const typescriptSchema = `schemas.${ name } ` as const ;
529534 return `(${ typescriptSchema } )[]` as const ;
530535 }
@@ -552,32 +557,15 @@ function extractQueryParams(queryParams: OperationQueryParameter[]) {
552557 ? `: ${ typescriptSchema } `
553558 : `?: ${ typescriptSchema } ` ;
554559 }
555- const entries = Object . entries ( params ) ;
556- if ( entries . length === 1 ) {
557- return `{ "${ entries [ 0 ] ! [ 0 ] } " ${ entries [ 0 ] ! [ 1 ] } }` as const ;
558- }
559- return (
560- "{\n" +
561- entries . map ( ( [ k , v ] ) => ` "${ k } " ${ v } ,` as const ) . join ( "\n" ) +
562- "\n}"
563- ) ;
560+ return recordToObject ( params , { colon : false } ) ;
564561}
565562
566563function extractPathParams ( pathParams : OperationPathParameter [ ] ) {
567564 const params : Record < string , string > = { } ;
568565 for ( const param of pathParams ) {
569- const typescriptSchema = schemaToTypescriptTypes ( param . schema ) ;
570- params [ param . name ] = `: ${ typescriptSchema } ` as const ;
571- }
572- const entries = Object . entries ( params ) ;
573- if ( entries . length === 1 ) {
574- return `{ "${ entries [ 0 ] ! [ 0 ] } " ${ entries [ 0 ] ! [ 1 ] } }` as const ;
566+ params [ param . name ] = schemaToTypescriptTypes ( param . schema ) ;
575567 }
576- return (
577- "{\n" +
578- entries . map ( ( [ k , v ] ) => ` "${ k } " ${ v } ,` as const ) . join ( "\n" ) +
579- "\n}"
580- ) ;
568+ return recordToObject ( params ) ;
581569}
582570
583571function extractBodyTypes ( bodySchema : Schema ) {
@@ -665,10 +653,12 @@ function processOperation(
665653 requestObjCode = `${ requestPieces . join ( ", " ) } ` ;
666654 }
667655
656+ const baseUrl = options ?. baseUrl || operation . servers ?. url ;
657+
668658 let baseUrlLine = "" ;
669659 let baseUrlArg = "" ;
670- if ( options ?. baseUrl ) {
671- baseUrlLine = ` const baseUrl = "${ options . baseUrl } ";\n` ;
660+ if ( baseUrl ) {
661+ baseUrlLine = ` const baseUrl = "${ baseUrl } ";\n` ;
672662 baseUrlArg = ", baseUrl" ;
673663 }
674664
@@ -692,8 +682,8 @@ ${baseUrlLine}\
692682
693683function processTag ( tagSchema : TagSchema , rawApiPath : string ) {
694684 const methodsCode : string [ ] = [ ] ;
695- let sharedPathParams : OperationPathParameter [ ] | undefined = undefined ;
696- let baseUrl : string | undefined = undefined ;
685+ let sharedPathParams : OperationPathParameter [ ] | undefined ;
686+ let baseUrl : string | undefined ;
697687
698688 for ( const operation of Object . values ( tagSchema ) ) {
699689 const processedOperation = processOperation ( operation , rawApiPath , {
0 commit comments