@@ -319,6 +319,7 @@ function generate(spec, types, outpath, version) {
319319 moduleSpecifier : "openapi-typescript-server-runtime"
320320 } ) ;
321321 const operationsById = { } ;
322+ const allTags = /* @__PURE__ */ new Set ( ) ;
322323 for ( const path in spec . paths ) {
323324 const pathSpec = spec . paths [ path ] ;
324325 for ( const method in pathSpec ) {
@@ -408,13 +409,16 @@ function generate(spec, types, outpath, version) {
408409 isExported : true ,
409410 type : `Promise<${ responseVariantInterfaceNames . join ( " | " ) } >`
410411 } ) ;
412+ const operationTags = operation . tags || [ ] ;
413+ operationTags . forEach ( ( tag ) => allTags . add ( tag ) ) ;
411414 operationsById [ operationId ] = {
412415 path,
413416 method,
414417 args : argsInterface . getName ( ) ,
415418 result : resultType . getName ( ) ,
416419 summary : operation . summary ,
417- description : operation . description
420+ description : operation . description ,
421+ tags : operationTags
418422 } ;
419423 sourceFile . addFunction ( {
420424 name : `${ operationId } Unimplemented` ,
@@ -481,6 +485,75 @@ function generate(spec, types, outpath, version) {
481485 writer . writeLine ( "]" ) ;
482486 }
483487 } ) ;
488+ const tagValues = Array . from ( allTags ) . map ( ( tag ) => `"${ tag } "` ) ;
489+ const hasUntaggedOperations = Object . values ( operationsById ) . some (
490+ ( op ) => ! op . tags || op . tags . length === 0
491+ ) ;
492+ if ( hasUntaggedOperations ) {
493+ tagValues . push ( "null" ) ;
494+ }
495+ if ( tagValues . length > 0 ) {
496+ sourceFile . addTypeAlias ( {
497+ name : "Tag" ,
498+ isExported : true ,
499+ type : tagValues . join ( " | " )
500+ } ) ;
501+ }
502+ const tagToOperations = { } ;
503+ Object . entries ( operationsById ) . forEach ( ( [ operationId , { tags } ] ) => {
504+ if ( ! tags || tags . length === 0 ) {
505+ if ( ! tagToOperations [ "null" ] ) {
506+ tagToOperations [ "null" ] = [ ] ;
507+ }
508+ tagToOperations [ "null" ] . push ( operationId ) ;
509+ } else {
510+ tags . forEach ( ( tag ) => {
511+ if ( ! tagToOperations [ tag ] ) {
512+ tagToOperations [ tag ] = [ ] ;
513+ }
514+ tagToOperations [ tag ] . push ( operationId ) ;
515+ } ) ;
516+ }
517+ } ) ;
518+ sourceFile . addFunction ( {
519+ name : "registerRouteHandlersByTag" ,
520+ isExported : true ,
521+ parameters : [
522+ { name : "tag" , type : "Tag" } ,
523+ { name : "server" , type : "Partial<Server<Req, Res>>" }
524+ ] ,
525+ typeParameters : [ { name : "Req" } , { name : "Res" } ] ,
526+ returnType : "Route[]" ,
527+ statements : ( writer ) => {
528+ writer . writeLine ( "const routes: Route[] = [];" ) ;
529+ writer . writeLine ( "" ) ;
530+ if ( Object . keys ( tagToOperations ) . length > 0 ) {
531+ writer . writeLine ( "switch (tag) {" ) ;
532+ Object . entries ( tagToOperations ) . forEach ( ( [ tag , operations ] ) => {
533+ const caseValue = tag === "null" ? "null" : `"${ tag } "` ;
534+ writer . writeLine ( `case ${ caseValue } :` ) ;
535+ operations . forEach ( ( operationId ) => {
536+ const op = operationsById [ operationId ] ;
537+ if ( op ) {
538+ writer . writeLine ( `if (server.${ operationId } ) {` ) ;
539+ writer . writeLine ( "routes.push({" ) ;
540+ writer . writeLine ( `method: "${ op . method } ",` ) ;
541+ writer . writeLine ( `path: "${ op . path } ",` ) ;
542+ writer . writeLine (
543+ `handler: server.${ operationId } as Route["handler"],`
544+ ) ;
545+ writer . writeLine ( "});" ) ;
546+ writer . writeLine ( "}" ) ;
547+ }
548+ } ) ;
549+ writer . writeLine ( "break;" ) ;
550+ } ) ;
551+ writer . writeLine ( "}" ) ;
552+ }
553+ writer . writeLine ( "" ) ;
554+ writer . writeLine ( "return routes;" ) ;
555+ }
556+ } ) ;
484557 sourceFile . insertText (
485558 0 ,
486559 `/**
0 commit comments