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