[{"data":1,"prerenderedAt":1135},["ShallowReactive",2],{"page:\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fgenerators":3},{"id":4,"title":5,"body":6,"description":1118,"extension":1119,"frontmatter":1120,"heading":5,"meta":1125,"navigation":1127,"order":1110,"path":1128,"seo":1129,"sitemap":1130,"source":1131,"stem":1132,"__hash__":1133,"hasPopups":1134},"docs\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fgenerators.md","Generators",{"type":7,"value":8,"toc":1105},"minimark",[9,17,29,47,407,412,498,504,530,762,772],[10,11,13],"h2",{"id":12},"definegenerator",[14,15,16],"code",{},"defineGenerator",[18,19,20,22,23,28],"p",{},[14,21,16],{}," declares a named generator unit consumed by a plugin. Generators walk the ",[24,25,27],"a",{"href":26},"\u002Fdocs\u002F5.x\u002Fexplanation\u002Farchitecture#ast","AST"," and emit files. Kubb calls each method for the matching node type.",[18,30,31,32,35,36,42,43,46],{},"Each generator method returns ",[14,33,34],{},"TElement | Array\u003CFileNode> | void",". Returning a renderer element (for example JSX from ",[24,37,39],{"href":38},"\u002Fdocs\u002F5.x\u002Freference\u002Fjsx",[14,40,41],{},"kubb\u002Fjsx",") requires a ",[14,44,45],{},"renderer"," factory on the generator.",[48,49,55],"pre",{"language":50,"meta":51,"code":52,"filename":53,"class":54},"typescript","twoslash","import { ast, defineGenerator } from 'kubb\u002Fkit'\n\nconst myGenerator = defineGenerator({\n  name: 'my-generator',\n  operation(node, ctx) {\n    return [\n      ast.factory.createFile({\n        baseName: `${node.operationId}.ts`,\n        path: `.\u002F${node.operationId}.ts`,\n        sources: [\n          ast.factory.createSource({\n            nodes: [ast.factory.createText(`export const op = '${node.operationId}'`)],\n          }),\n        ],\n      }),\n    ]\n  },\n})","my-generator.ts","shiki shiki-themes github-light github-dark twoslash lsp dark:github-dark",[14,56,58,96,97,96,99,96,128,96,148,96,179,96,187,96,216,96,250,96,281,96,294,96,321,96,377,96,382,96,387,96,392,96,397,96,402],{"class":57},"language-typescript",[59,60,63,67,71,78,81,86,89,92],"span",{"class":61,"style":62},"line","display: inline",[59,64,66],{"style":65},"color:#D73A49;--shiki-dark:#F97583","import",[59,68,70],{"style":69},"color:#24292E;--shiki-dark:#E1E4E8"," { ",[59,72,73],{"style":69},[59,74,77],{"class":75,"data-popup":76},"twoslash-hover","0","ast",[59,79,80],{"style":69},", ",[59,82,83],{"style":69},[59,84,16],{"class":75,"data-popup":85},"1",[59,87,88],{"style":69}," } ",[59,90,91],{"style":65},"from",[59,93,95],{"style":94},"color:#032F62;--shiki-dark:#9ECBFF"," 'kubb\u002Fkit'","\n",[59,98],{"class":61,"style":62},[59,100,101,104,108,114,117,120,125],{"class":61,"style":62},[59,102,103],{"style":65},"const",[59,105,107],{"style":106},"color:#005CC5;--shiki-dark:#79B8FF"," ",[59,109,110],{"style":106},[59,111,113],{"class":75,"data-popup":112},"2","myGenerator",[59,115,116],{"style":65}," =",[59,118,107],{"style":119},"color:#6F42C1;--shiki-dark:#B392F0",[59,121,122],{"style":119},[59,123,16],{"class":75,"data-popup":124},"3",[59,126,127],{"style":69},"({",[59,129,130,133,139,142,145],{"class":61,"style":62},[59,131,132],{"style":69},"  ",[59,134,135],{"style":69},[59,136,138],{"class":75,"data-popup":137},"4","name",[59,140,141],{"style":69},": ",[59,143,144],{"style":94},"'my-generator'",[59,146,147],{"style":69},",",[59,149,150,152,158,161,168,170,176],{"class":61,"style":62},[59,151,132],{"style":119},[59,153,154],{"style":119},[59,155,157],{"class":75,"data-popup":156},"5","operation",[59,159,160],{"style":69},"(",[59,162,164],{"style":163},"color:#E36209;--shiki-dark:#FFAB70",[59,165,167],{"class":75,"data-popup":166},"6","node",[59,169,80],{"style":69},[59,171,172],{"style":163},[59,173,175],{"class":75,"data-popup":174},"7","ctx",[59,177,178],{"style":69},") {",[59,180,181,184],{"class":61,"style":62},[59,182,183],{"style":65},"    return",[59,185,186],{"style":69}," [",[59,188,189,192,197,200,206,208,214],{"class":61,"style":62},[59,190,191],{"style":69},"      ",[59,193,194],{"style":69},[59,195,77],{"class":75,"data-popup":196},"8",[59,198,199],{"style":69},".",[59,201,202],{"style":69},[59,203,205],{"class":75,"data-popup":204},"9","factory",[59,207,199],{"style":69},[59,209,210],{"style":119},[59,211,213],{"class":75,"data-popup":212},"10","createFile",[59,215,127],{"style":69},[59,217,218,221,227,229,232,237,239,245,248],{"class":61,"style":62},[59,219,220],{"style":69},"        ",[59,222,223],{"style":69},[59,224,226],{"class":75,"data-popup":225},"11","baseName",[59,228,141],{"style":69},[59,230,231],{"style":94},"`${",[59,233,234],{"style":69},[59,235,167],{"class":75,"data-popup":236},"12",[59,238,199],{"style":94},[59,240,241],{"style":69},[59,242,244],{"class":75,"data-popup":243},"13","operationId",[59,246,247],{"style":94},"}.ts`",[59,249,147],{"style":69},[59,251,252,254,260,262,265,270,272,277,279],{"class":61,"style":62},[59,253,220],{"style":69},[59,255,256],{"style":69},[59,257,259],{"class":75,"data-popup":258},"14","path",[59,261,141],{"style":69},[59,263,264],{"style":94},"`.\u002F${",[59,266,267],{"style":69},[59,268,167],{"class":75,"data-popup":269},"15",[59,271,199],{"style":94},[59,273,274],{"style":69},[59,275,244],{"class":75,"data-popup":276},"16",[59,278,247],{"style":94},[59,280,147],{"style":69},[59,282,283,285,291],{"class":61,"style":62},[59,284,220],{"style":69},[59,286,287],{"style":69},[59,288,290],{"class":75,"data-popup":289},"17","sources",[59,292,293],{"style":69},": [",[59,295,296,299,304,306,311,313,319],{"class":61,"style":62},[59,297,298],{"style":69},"          ",[59,300,301],{"style":69},[59,302,77],{"class":75,"data-popup":303},"18",[59,305,199],{"style":69},[59,307,308],{"style":69},[59,309,205],{"class":75,"data-popup":310},"19",[59,312,199],{"style":69},[59,314,315],{"style":119},[59,316,318],{"class":75,"data-popup":317},"20","createSource",[59,320,127],{"style":69},[59,322,323,326,332,334,339,341,346,348,354,356,359,364,366,371,374],{"class":61,"style":62},[59,324,325],{"style":69},"            ",[59,327,328],{"style":69},[59,329,331],{"class":75,"data-popup":330},"21","nodes",[59,333,293],{"style":69},[59,335,336],{"style":69},[59,337,77],{"class":75,"data-popup":338},"22",[59,340,199],{"style":69},[59,342,343],{"style":69},[59,344,205],{"class":75,"data-popup":345},"23",[59,347,199],{"style":69},[59,349,350],{"style":119},[59,351,353],{"class":75,"data-popup":352},"24","createText",[59,355,160],{"style":69},[59,357,358],{"style":94},"`export const op = '${",[59,360,361],{"style":69},[59,362,167],{"class":75,"data-popup":363},"25",[59,365,199],{"style":94},[59,367,368],{"style":69},[59,369,244],{"class":75,"data-popup":370},"26",[59,372,373],{"style":94},"}'`",[59,375,376],{"style":69},")],",[59,378,379],{"class":61,"style":62},[59,380,381],{"style":69},"          }),",[59,383,384],{"class":61,"style":62},[59,385,386],{"style":69},"        ],",[59,388,389],{"class":61,"style":62},[59,390,391],{"style":69},"      }),",[59,393,394],{"class":61,"style":62},[59,395,396],{"style":69},"    ]",[59,398,399],{"class":61,"style":62},[59,400,401],{"style":69},"  },",[59,403,404],{"class":61,"style":62},[59,405,406],{"style":69},"})",[408,409,411],"h3",{"id":410},"generator-methods","Generator methods",[413,414,415,434],"table",{},[416,417,418],"thead",{},[419,420,421,425,428,431],"tr",{},[422,423,424],"th",{},"Method",[422,426,427],{},"Input",[422,429,430],{},"Output",[422,432,433],{},"When to use",[435,436,437,458,478],"tbody",{},[419,438,439,445,451,455],{},[440,441,442],"td",{},[14,443,444],{},"schema()",[440,446,447,450],{},[14,448,449],{},"SchemaNode"," (per data schema)",[440,452,453],{},[14,454,34],{},[440,456,457],{},"Generate types, validators, factories. Called once per schema",[419,459,460,465,471,475],{},[440,461,462],{},[14,463,464],{},"operation()",[440,466,467,470],{},[14,468,469],{},"OperationNode"," (per API operation)",[440,472,473],{},[14,474,34],{},[440,476,477],{},"Generate hooks, clients, handlers. Called once per operation",[419,479,480,485,491,495],{},[440,481,482],{},[14,483,484],{},"operations()",[440,486,487,490],{},[14,488,489],{},"Array\u003COperationNode>"," (all operations)",[440,492,493],{},[14,494,34],{},[440,496,497],{},"Generate index or barrel files. Called once after all operations",[408,499,501,502],{"id":500},"match","Scoping with ",[14,503,500],{},[18,505,506,507,510,511,514,515,517,518,521,522,524,525,527,528,199],{},"Add a ",[14,508,509],{},"match(node, ctx)"," predicate to skip ",[14,512,513],{},"schema"," or ",[14,516,157],{}," for nodes a generator does not apply to. A ",[14,519,520],{},"false"," result skips that node. Omitting ",[14,523,500],{}," runs the generator for every node. ",[14,526,500],{}," does not filter ",[14,529,484],{},[48,531,534],{"language":50,"meta":51,"code":532,"filename":533,"class":54},"import { ast, defineGenerator } from 'kubb\u002Fkit'\n\nconst getOnlyGenerator = defineGenerator({\n  name: 'get-only-generator',\n  match(node, ctx) {\n    return node.kind === 'Operation' && ast.isHttpOperationNode(node) && node.method.toLowerCase() === 'get'\n  },\n  operation(node, ctx) {\n    \u002F\u002F node is already known to be a GET operation here\n    return\n  },\n})","scoped-generator.ts",[14,535,536,96,560,96,562,96,585,96,601,96,626,96,715,96,719,96,744,96,750,96,754,96,758],{"class":57},[59,537,538,540,542,547,549,554,556,558],{"class":61,"style":62},[59,539,66],{"style":65},[59,541,70],{"style":69},[59,543,544],{"style":69},[59,545,77],{"class":75,"data-popup":546},"27",[59,548,80],{"style":69},[59,550,551],{"style":69},[59,552,16],{"class":75,"data-popup":553},"28",[59,555,88],{"style":69},[59,557,91],{"style":65},[59,559,95],{"style":94},[59,561],{"class":61,"style":62},[59,563,564,566,568,574,576,578,583],{"class":61,"style":62},[59,565,103],{"style":65},[59,567,107],{"style":106},[59,569,570],{"style":106},[59,571,573],{"class":75,"data-popup":572},"29","getOnlyGenerator",[59,575,116],{"style":65},[59,577,107],{"style":119},[59,579,580],{"style":119},[59,581,16],{"class":75,"data-popup":582},"30",[59,584,127],{"style":69},[59,586,587,589,594,596,599],{"class":61,"style":62},[59,588,132],{"style":69},[59,590,591],{"style":69},[59,592,138],{"class":75,"data-popup":593},"31",[59,595,141],{"style":69},[59,597,598],{"style":94},"'get-only-generator'",[59,600,147],{"style":69},[59,602,603,605,610,612,617,619,624],{"class":61,"style":62},[59,604,132],{"style":119},[59,606,607],{"style":119},[59,608,500],{"class":75,"data-popup":609},"32",[59,611,160],{"style":69},[59,613,614],{"style":163},[59,615,167],{"class":75,"data-popup":616},"33",[59,618,80],{"style":69},[59,620,621],{"style":163},[59,622,175],{"class":75,"data-popup":623},"34",[59,625,178],{"style":69},[59,627,628,630,632,637,639,645,647,650,653,656,658,663,665,671,673,678,681,684,686,691,693,699,701,707,710,712],{"class":61,"style":62},[59,629,183],{"style":65},[59,631,107],{"style":69},[59,633,634],{"style":69},[59,635,167],{"class":75,"data-popup":636},"35",[59,638,199],{"style":69},[59,640,641],{"style":69},[59,642,644],{"class":75,"data-popup":643},"36","kind",[59,646,107],{"style":69},[59,648,649],{"style":65},"===",[59,651,652],{"style":94}," 'Operation'",[59,654,655],{"style":65}," &&",[59,657,107],{"style":69},[59,659,660],{"style":69},[59,661,77],{"class":75,"data-popup":662},"37",[59,664,199],{"style":69},[59,666,667],{"style":119},[59,668,670],{"class":75,"data-popup":669},"38","isHttpOperationNode",[59,672,160],{"style":69},[59,674,675],{"style":69},[59,676,167],{"class":75,"data-popup":677},"39",[59,679,680],{"style":69},") ",[59,682,683],{"style":65},"&&",[59,685,107],{"style":69},[59,687,688],{"style":69},[59,689,167],{"class":75,"data-popup":690},"40",[59,692,199],{"style":69},[59,694,695],{"style":69},[59,696,698],{"class":75,"data-popup":697},"41","method",[59,700,199],{"style":69},[59,702,703],{"style":119},[59,704,706],{"class":75,"data-popup":705},"42","toLowerCase",[59,708,709],{"style":69},"() ",[59,711,649],{"style":65},[59,713,714],{"style":94}," 'get'",[59,716,717],{"class":61,"style":62},[59,718,401],{"style":69},[59,720,721,723,728,730,735,737,742],{"class":61,"style":62},[59,722,132],{"style":119},[59,724,725],{"style":119},[59,726,157],{"class":75,"data-popup":727},"43",[59,729,160],{"style":69},[59,731,732],{"style":163},[59,733,167],{"class":75,"data-popup":734},"44",[59,736,80],{"style":69},[59,738,739],{"style":163},[59,740,175],{"class":75,"data-popup":741},"45",[59,743,178],{"style":69},[59,745,746],{"class":61,"style":62},[59,747,749],{"style":748},"color:#6A737D;--shiki-dark:#6A737D","    \u002F\u002F node is already known to be a GET operation here",[59,751,752],{"class":61,"style":62},[59,753,183],{"style":65},[59,755,756],{"class":61,"style":62},[59,757,401],{"style":69},[59,759,760],{"class":61,"style":62},[59,761,406],{"style":69},[408,763,765,768,769,771],{"id":764},"generatorcontext-properties-the-ctx-argument-passed-to-each-method",[14,766,767],{},"GeneratorContext"," properties (the ",[14,770,175],{}," argument passed to each method)",[413,773,774,787],{},[416,775,776],{},[419,777,778,781,784],{},[422,779,780],{},"Property",[422,782,783],{},"Type",[422,785,786],{},"Purpose",[435,788,789,804,819,834,849,864,879,898,913,954,979,994,1008,1023,1038,1053,1068,1086],{},[419,790,791,796,801],{},[440,792,793],{},[14,794,795],{},"ctx.config",[440,797,798],{},[14,799,800],{},"Config",[440,802,803],{},"Resolved Kubb configuration",[419,805,806,811,816],{},[440,807,808],{},[14,809,810],{},"ctx.root",[440,812,813],{},[14,814,815],{},"string",[440,817,818],{},"Absolute path to the output directory for the current plugin",[419,820,821,826,831],{},[440,822,823],{},[14,824,825],{},"ctx.options",[440,827,828],{},[14,829,830],{},"TResolvedOptions",[440,832,833],{},"Per-node resolved options (after exclude\u002Finclude\u002Foverride filtering)",[419,835,836,841,846],{},[440,837,838],{},[14,839,840],{},"ctx.plugin",[440,842,843],{},[14,844,845],{},"Plugin",[440,847,848],{},"The owning plugin descriptor",[419,850,851,856,861],{},[440,852,853],{},[14,854,855],{},"ctx.resolver",[440,857,858],{},[14,859,860],{},"Resolver",[440,862,863],{},"Resolver for the current plugin",[419,865,866,871,876],{},[440,867,868],{},[14,869,870],{},"ctx.driver",[440,872,873],{},[14,874,875],{},"KubbDriver",[440,877,878],{},"Plugin driver for cross-plugin access",[419,880,881,886,891],{},[440,882,883],{},[14,884,885],{},"ctx.hooks",[440,887,888],{},[14,889,890],{},"Hookable\u003CKubbHooks>",[440,892,893,894,897],{},"Event bus for ",[14,895,896],{},"KubbHooks"," events",[419,899,900,905,910],{},[440,901,902],{},[14,903,904],{},"ctx.adapter",[440,906,907],{},[14,908,909],{},"Adapter",[440,911,912],{},"The adapter that parsed the input spec",[419,914,915,920,925],{},[440,916,917],{},[14,918,919],{},"ctx.meta",[440,921,922],{},[14,923,924],{},"InputMeta",[440,926,927,928,80,931,80,934,937,938,941,942,945,946,949,950,953],{},"Document metadata from the adapter. Carries ",[14,929,930],{},"title",[14,932,933],{},"version",[14,935,936],{},"baseURL",", every declared ",[14,939,940],{},"servers"," entry and ",[14,943,944],{},"securitySchemes"," entry, and the pre-computed ",[14,947,948],{},"circularNames"," and ",[14,951,952],{},"enumNames"," arrays.",[419,955,956,961,966],{},[440,957,958],{},[14,959,960],{},"ctx.cache",[440,962,963],{},[14,964,965],{},"NodeCache",[440,967,968,969,80,972,975,976],{},"Cache scoped to the node being generated, shared by every plugin generating from that node in the current pass. Exposes ",[14,970,971],{},"readItem",[14,973,974],{},"writeItem",", and ",[14,977,978],{},"ensureItem",[419,980,981,986,991],{},[440,982,983],{},[14,984,985],{},"ctx.addFile()",[440,987,988],{},[14,989,990],{},"(...files: FileNode[]) => Promise\u003Cvoid>",[440,992,993],{},"Add files, skipping any that already exist",[419,995,996,1001,1005],{},[440,997,998],{},[14,999,1000],{},"ctx.upsertFile()",[440,1002,1003],{},[14,1004,990],{},[440,1006,1007],{},"Add or merge files (concatenates sources and imports)",[419,1009,1010,1015,1020],{},[440,1011,1012],{},[14,1013,1014],{},"ctx.getPlugin()",[440,1016,1017],{},[14,1018,1019],{},"(name: string) => Plugin | undefined",[440,1021,1022],{},"Get a plugin by name",[419,1024,1025,1030,1035],{},[440,1026,1027],{},[14,1028,1029],{},"ctx.requirePlugin()",[440,1031,1032],{},[14,1033,1034],{},"(name: string) => Plugin",[440,1036,1037],{},"Get a plugin by name or throw a descriptive error",[419,1039,1040,1045,1050],{},[440,1041,1042],{},[14,1043,1044],{},"ctx.getResolver()",[440,1046,1047],{},[14,1048,1049],{},"(name: string) => Resolver",[440,1051,1052],{},"Get a resolver by plugin name",[419,1054,1055,1060,1065],{},[440,1056,1057],{},[14,1058,1059],{},"ctx.info()",[440,1061,1062],{},[14,1063,1064],{},"(message: string, options?: { code?, help?, location? }) => void",[440,1066,1067],{},"Emit an info message via the build event system",[419,1069,1070,1075,1079],{},[440,1071,1072],{},[14,1073,1074],{},"ctx.warn()",[440,1076,1077],{},[14,1078,1064],{},[440,1080,1081,1082,1085],{},"Emit a warning via the build event system. ",[14,1083,1084],{},"options"," sets the fix and the source pointer",[419,1087,1088,1093,1098],{},[440,1089,1090],{},[14,1091,1092],{},"ctx.error()",[440,1094,1095],{},[14,1096,1097],{},"(error: string | Error, options?: { code?, help?, location? }) => void",[440,1099,1100,1101,1104],{},"Emit an error via the build event system. A ",[14,1102,1103],{},"Diagnostics.Error"," keeps its code, help, and location",{"title":1106,"searchDepth":1107,"depth":1107,"links":1108},"",5,[1109],{"id":12,"depth":1110,"text":16,"children":1111},2,[1112,1114,1116],{"id":410,"depth":1113,"text":411},3,{"id":500,"depth":1113,"text":1115},"Scoping with match",{"id":764,"depth":1113,"text":1117},"GeneratorContext properties (the ctx argument passed to each method)","defineGenerator declares a named generator unit that walks the AST and emits files. Covers the schema, operation, and operations methods and the GeneratorContext passed to each.","md",{"layout":1121,"title":5,"description":1118,"outline":1122,"order":1110,"navigation":1123},"doc",[1110,1113],{"title":5,"icon":1124},"i-iconoir-code-brackets",{"layout":1121,"outline":1126},[1110,1113],{"title":5,"icon":1124},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fgenerators",{"title":5,"description":1118},{"loc":1128},"kubb-labs\u002Fkubb\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fgenerators.md","docs\u002F5.x\u002Freference\u002Fkit\u002Fgenerators","mr1PcoGlCfELrhgo8qVFdJT0Ih4fILGhyZs7-ZQ9srU",true,1792352128363]