@kubb/renderer-jsx 5.0.0-alpha.34 → 5.0.0-alpha.36
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/{chunk-CErwXX-a.js → chunk-Bb7HlUDG.js} +1 -1
- package/dist/index.cjs +271 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +336 -47
- package/dist/index.js +273 -110
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.cjs +1 -1
- package/dist/jsx-dev-runtime.d.ts +3 -3
- package/dist/jsx-dev-runtime.js +2 -2
- package/dist/{jsx-namespace-Cx1KMEbe.d.ts → jsx-namespace-DdqdFHqm.d.ts} +3 -3
- package/dist/{jsx-runtime-Dmf9wTKR.js → jsx-runtime-7z5lYBqC.js} +2 -2
- package/dist/{jsx-runtime-Dmf9wTKR.js.map → jsx-runtime-7z5lYBqC.js.map} +1 -1
- package/dist/{jsx-runtime-CeMde2cR.cjs → jsx-runtime-Dnd1wztY.cjs} +2 -2
- package/dist/{jsx-runtime-CeMde2cR.cjs.map → jsx-runtime-Dnd1wztY.cjs.map} +1 -1
- package/dist/jsx-runtime.cjs +1 -1
- package/dist/jsx-runtime.d.ts +3 -3
- package/dist/jsx-runtime.js +2 -2
- package/dist/types-BEu94Tb3.d.ts +176 -0
- package/dist/types.d.ts +2 -2
- package/package.json +2 -2
- package/src/Runtime.tsx +3 -4
- package/src/components/Const.tsx +35 -7
- package/src/components/File.tsx +96 -16
- package/src/components/Function.tsx +59 -14
- package/src/components/Root.tsx +11 -5
- package/src/components/Type.tsx +32 -6
- package/src/constants.ts +28 -0
- package/src/context/KubbContext.ts +1 -1
- package/src/createRenderer.tsx +64 -3
- package/src/dom.ts +35 -21
- package/src/index.ts +1 -1
- package/src/types.ts +107 -5
- package/src/utils.ts +74 -108
- package/dist/types-C7FD9BLg.d.ts +0 -119
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { n as __name } from "./chunk-Bb7HlUDG.js";
|
|
2
|
+
import { ArrowFunctionNode, ConstNode, ExportNode, FileNode, FunctionNode, ImportNode, SourceNode, TypeNode } from "@kubb/ast";
|
|
3
|
+
import React, { JSX, ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/types.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* React reconciliation key type.
|
|
8
|
+
* Used to uniquely identify elements in lists and conditional renders.
|
|
9
|
+
*/
|
|
10
|
+
type Key = string | number | bigint;
|
|
11
|
+
/**
|
|
12
|
+
* All custom element names recognized by the Kubb JSX renderer.
|
|
13
|
+
* Each name maps to a corresponding AST node type emitted during code generation.
|
|
14
|
+
*/
|
|
15
|
+
type ElementNames = 'br' | 'div' | 'indent' | 'dedent' | 'kubb-jsx' | 'kubb-text' | 'kubb-file' | 'kubb-source' | 'kubb-import' | 'kubb-export' | 'kubb-function' | 'kubb-arrow-function' | 'kubb-const' | 'kubb-type' | 'kubb-root' | 'kubb-app';
|
|
16
|
+
type Node = {
|
|
17
|
+
parentNode: DOMElement | undefined;
|
|
18
|
+
internal_static?: boolean;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Allowed attribute value types stored on a {@link DOMElement}.
|
|
22
|
+
* Corresponds to the range of prop values JSX components may pass to intrinsic elements.
|
|
23
|
+
*/
|
|
24
|
+
type DOMNodeAttribute = boolean | string | number | Record<string, unknown> | Array<unknown>;
|
|
25
|
+
type TextName = '#text';
|
|
26
|
+
/**
|
|
27
|
+
* A leaf DOM node that holds a raw text string.
|
|
28
|
+
* Created by the renderer whenever a JSX expression produces a plain string child.
|
|
29
|
+
*/
|
|
30
|
+
type TextNode = {
|
|
31
|
+
nodeName: TextName;
|
|
32
|
+
nodeValue: string;
|
|
33
|
+
} & Node;
|
|
34
|
+
/**
|
|
35
|
+
* Discriminated union of all virtual DOM nodes.
|
|
36
|
+
* Resolves to {@link TextNode} when `nodeName` is `'#text'`, or {@link DOMElement} for
|
|
37
|
+
* any named Kubb element.
|
|
38
|
+
*/
|
|
39
|
+
type DOMNode<T = {
|
|
40
|
+
nodeName: NodeNames;
|
|
41
|
+
}> = T extends {
|
|
42
|
+
nodeName: infer U;
|
|
43
|
+
} ? U extends '#text' ? TextNode : DOMElement : never;
|
|
44
|
+
type OutputTransformer = (s: string, index: number) => string;
|
|
45
|
+
/**
|
|
46
|
+
* A named element node in the Kubb virtual DOM tree.
|
|
47
|
+
* Holds attributes, child nodes, and optional lifecycle callbacks used by the renderer.
|
|
48
|
+
*/
|
|
49
|
+
type DOMElement = {
|
|
50
|
+
nodeName: ElementNames;
|
|
51
|
+
/**
|
|
52
|
+
* Key/value attributes passed as JSX props to this element.
|
|
53
|
+
*/
|
|
54
|
+
attributes: Map<string, DOMNodeAttribute>;
|
|
55
|
+
/**
|
|
56
|
+
* Ordered list of child nodes attached to this element.
|
|
57
|
+
*/
|
|
58
|
+
childNodes: DOMNode[];
|
|
59
|
+
internal_transform?: OutputTransformer;
|
|
60
|
+
isStaticDirty?: boolean;
|
|
61
|
+
staticNode?: DOMElement;
|
|
62
|
+
onComputeLayout?: () => void;
|
|
63
|
+
onRender?: () => void;
|
|
64
|
+
onImmediateRender?: () => void;
|
|
65
|
+
} & Node;
|
|
66
|
+
type NodeNames = ElementNames | TextName;
|
|
67
|
+
/**
|
|
68
|
+
* React node type used throughout Kubb JSX components.
|
|
69
|
+
* Alias for React's `ReactNode`.
|
|
70
|
+
*/
|
|
71
|
+
type KubbReactNode = ReactNode;
|
|
72
|
+
/**
|
|
73
|
+
* React element type returned by Kubb JSX components.
|
|
74
|
+
* Alias for `JSX.Element`.
|
|
75
|
+
*/
|
|
76
|
+
type KubbReactElement = JSX.Element;
|
|
77
|
+
/**
|
|
78
|
+
* Props for the `<kubb-jsx>` intrinsic element.
|
|
79
|
+
* Embeds a raw JSX string verbatim in the generated source.
|
|
80
|
+
*/
|
|
81
|
+
type KubbJsxProps = {
|
|
82
|
+
children?: string;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Props for the `<kubb-text>` intrinsic element.
|
|
86
|
+
* Wraps arbitrary React children as plain text in the output.
|
|
87
|
+
*/
|
|
88
|
+
type KubbTextProps = {
|
|
89
|
+
children?: KubbReactNode;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Props for the `<kubb-file>` intrinsic element.
|
|
93
|
+
* Represents a generated file entry collected by the renderer.
|
|
94
|
+
*/
|
|
95
|
+
type KubbFileProps = {
|
|
96
|
+
id?: string;
|
|
97
|
+
children?: KubbReactNode;
|
|
98
|
+
baseName: string;
|
|
99
|
+
path: string;
|
|
100
|
+
override?: boolean;
|
|
101
|
+
meta?: FileNode['meta'];
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Props for the `<kubb-source>` intrinsic element.
|
|
105
|
+
* Marks a block of source text associated with the enclosing file.
|
|
106
|
+
*/
|
|
107
|
+
type KubbSourceProps = Omit<SourceNode, 'kind'> & {
|
|
108
|
+
children?: KubbReactNode;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Props for the `<kubb-import>` intrinsic element.
|
|
112
|
+
* Declares an import entry for the enclosing file.
|
|
113
|
+
*/
|
|
114
|
+
type KubbImportProps = Omit<ImportNode, 'kind'> & {};
|
|
115
|
+
/**
|
|
116
|
+
* Props for the `<kubb-export>` intrinsic element.
|
|
117
|
+
* Declares an export entry for the enclosing file.
|
|
118
|
+
*/
|
|
119
|
+
type KubbExportProps = Omit<ExportNode, 'kind'> & {};
|
|
120
|
+
/**
|
|
121
|
+
* Props for the `<kubb-function>` intrinsic element.
|
|
122
|
+
* Describes a function declaration node in the generated output.
|
|
123
|
+
*/
|
|
124
|
+
type KubbFunctionProps = Omit<FunctionNode, 'kind'> & {
|
|
125
|
+
children?: KubbReactNode;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Props for the `<kubb-arrow-function>` intrinsic element.
|
|
129
|
+
* Describes an arrow function declaration node in the generated output.
|
|
130
|
+
*/
|
|
131
|
+
type KubbArrowFunctionProps = Omit<ArrowFunctionNode, 'kind'> & {
|
|
132
|
+
children?: KubbReactNode;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Props for the `<kubb-const>` intrinsic element.
|
|
136
|
+
* Describes a constant declaration node in the generated output.
|
|
137
|
+
*/
|
|
138
|
+
type KubbConstProps = Omit<ConstNode, 'kind'> & {
|
|
139
|
+
children?: KubbReactNode;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Props for the `<kubb-type>` intrinsic element.
|
|
143
|
+
* Describes a TypeScript type alias declaration node in the generated output.
|
|
144
|
+
*/
|
|
145
|
+
type KubbTypeProps = Omit<TypeNode, 'kind'> & {
|
|
146
|
+
children?: KubbReactNode;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Props forwarded to the HTML `<br>` element within intrinsic element declarations.
|
|
150
|
+
*/
|
|
151
|
+
type LineBreakProps = React.DetailedHTMLProps<React.HTMLAttributes<HTMLBRElement>, HTMLBRElement>;
|
|
152
|
+
/**
|
|
153
|
+
* JSDoc block to attach to a generated declaration.
|
|
154
|
+
*
|
|
155
|
+
* Each string in `comments` becomes one line inside the emitted `/** … *\/` block.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* { comments: ['@description A pet object.', '@deprecated Use PetV2 instead.'] }
|
|
160
|
+
* // Emits:
|
|
161
|
+
* // /**
|
|
162
|
+
* // * @description A pet object.
|
|
163
|
+
* // * @deprecated Use PetV2 instead.
|
|
164
|
+
* // *\/
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
type JSDoc = {
|
|
168
|
+
/**
|
|
169
|
+
* Lines to emit inside the JSDoc block, in source order.
|
|
170
|
+
* Use standard JSDoc tags such as `@description`, `@deprecated`, `@see`, etc.
|
|
171
|
+
*/
|
|
172
|
+
comments: Array<string>;
|
|
173
|
+
};
|
|
174
|
+
//#endregion
|
|
175
|
+
export { KubbTextProps as _, JSDoc as a, TextNode as b, KubbConstProps as c, KubbFunctionProps as d, KubbImportProps as f, KubbSourceProps as g, KubbReactNode as h, ElementNames as i, KubbExportProps as l, KubbReactElement as m, DOMNode as n, Key as o, KubbJsxProps as p, DOMNodeAttribute as r, KubbArrowFunctionProps as s, DOMElement as t, KubbFileProps as u, KubbTypeProps as v, LineBreakProps as y };
|
|
176
|
+
//# sourceMappingURL=types-BEu94Tb3.d.ts.map
|
package/dist/types.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as KubbTextProps, a as JSDoc, b as TextNode, c as KubbConstProps, d as KubbFunctionProps, f as KubbImportProps, g as KubbSourceProps, h as KubbReactNode, i as ElementNames, l as KubbExportProps, m as KubbReactElement, n as DOMNode, o as Key, p as KubbJsxProps, r as DOMNodeAttribute, s as KubbArrowFunctionProps, t as DOMElement, u as KubbFileProps, v as KubbTypeProps,
|
|
2
|
-
export {
|
|
1
|
+
import { _ as KubbTextProps, a as JSDoc, b as TextNode, c as KubbConstProps, d as KubbFunctionProps, f as KubbImportProps, g as KubbSourceProps, h as KubbReactNode, i as ElementNames, l as KubbExportProps, m as KubbReactElement, n as DOMNode, o as Key, p as KubbJsxProps, r as DOMNodeAttribute, s as KubbArrowFunctionProps, t as DOMElement, u as KubbFileProps, v as KubbTypeProps, y as LineBreakProps } from "./types-BEu94Tb3.js";
|
|
2
|
+
export { DOMElement, DOMNode, DOMNodeAttribute, ElementNames, JSDoc, Key, KubbArrowFunctionProps, KubbConstProps, KubbExportProps, KubbFileProps, KubbFunctionProps, KubbImportProps, KubbJsxProps, KubbReactElement, KubbReactNode, KubbSourceProps, KubbTextProps, KubbTypeProps, LineBreakProps, TextNode };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/renderer-jsx",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.36",
|
|
4
4
|
"description": "React integration for Kubb - JSX runtime and component-based code generation with React reconciler for building type-safe generators",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
}
|
|
85
85
|
],
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
87
|
+
"@kubb/ast": "5.0.0-alpha.36"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@types/react": "^19.2.14",
|
package/src/Runtime.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { onProcessExit } from '@internals/utils'
|
|
2
|
-
import type { FileNode } from '@kubb/ast
|
|
2
|
+
import type { FileNode } from '@kubb/ast'
|
|
3
3
|
import { ConcurrentRoot } from 'react-reconciler/constants.js'
|
|
4
4
|
import { Root } from './components/Root.tsx'
|
|
5
5
|
import { createNode } from './dom.ts'
|
|
@@ -65,7 +65,6 @@ export class Runtime {
|
|
|
65
65
|
null,
|
|
66
66
|
)
|
|
67
67
|
|
|
68
|
-
// Unmount when process exits
|
|
69
68
|
// Unmount when process exits
|
|
70
69
|
this.unsubscribeExit = onProcessExit((code) => {
|
|
71
70
|
this.unmount(code)
|
|
@@ -82,12 +81,12 @@ export class Runtime {
|
|
|
82
81
|
|
|
83
82
|
const task = previous
|
|
84
83
|
.catch(() => {})
|
|
85
|
-
.then(
|
|
84
|
+
.then(() => {
|
|
86
85
|
if (this.#isUnmounted) {
|
|
87
86
|
return
|
|
88
87
|
}
|
|
89
88
|
|
|
90
|
-
const files =
|
|
89
|
+
const files = processFiles(this.#rootNode)
|
|
91
90
|
|
|
92
91
|
this.nodes.push(...files)
|
|
93
92
|
|
package/src/components/Const.tsx
CHANGED
|
@@ -1,35 +1,63 @@
|
|
|
1
1
|
import type { JSDoc, Key, KubbReactElement, KubbReactNode } from '../types.ts'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type ConstProps = {
|
|
4
4
|
key?: Key
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Identifier of the generated constant declaration.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* `name: 'petSchema'`
|
|
7
10
|
*/
|
|
8
11
|
name: string
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* Emit the `export` keyword before the `const` declaration.
|
|
14
|
+
* - `true` generates `export const name = …`
|
|
15
|
+
* - `false` generates `const name = …`
|
|
16
|
+
* @default false
|
|
11
17
|
*/
|
|
12
18
|
export?: boolean
|
|
13
19
|
/**
|
|
14
|
-
*
|
|
20
|
+
* TypeScript type annotation for the constant, written verbatim after `const name:`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* `type: 'Pet'` → `const pet: Pet = …`
|
|
15
24
|
*/
|
|
16
25
|
type?: string
|
|
17
26
|
/**
|
|
18
|
-
*
|
|
27
|
+
* JSDoc block to prepend to the constant declaration.
|
|
28
|
+
* Each entry in `comments` becomes one line inside the emitted `/** … *\/` block.
|
|
19
29
|
*/
|
|
20
30
|
JSDoc?: JSDoc
|
|
21
31
|
/**
|
|
22
|
-
*
|
|
32
|
+
* Append `as const` after the initialiser, enabling TypeScript const assertions.
|
|
33
|
+
* - `true` generates `const name = … as const`
|
|
34
|
+
* - `false` generates `const name = …`
|
|
35
|
+
* @default false
|
|
23
36
|
*/
|
|
24
37
|
asConst?: boolean
|
|
25
38
|
/**
|
|
26
|
-
*
|
|
39
|
+
* Child nodes rendered as the initialiser expression of the constant.
|
|
27
40
|
*/
|
|
28
41
|
children?: KubbReactNode
|
|
29
42
|
}
|
|
30
43
|
|
|
31
44
|
/**
|
|
32
45
|
* Generates a TypeScript constant declaration.
|
|
46
|
+
*
|
|
47
|
+
* @example Named export with type annotation
|
|
48
|
+
* ```tsx
|
|
49
|
+
* <Const export name="petSchema" type="z.ZodType<Pet>">
|
|
50
|
+
* {`z.object({ id: z.number() })`}
|
|
51
|
+
* </Const>
|
|
52
|
+
* // export const petSchema: z.ZodType<Pet> = z.object({ id: z.number() })
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example With JSDoc and const assertion
|
|
56
|
+
* ```tsx
|
|
57
|
+
* <Const name="HTTP_METHODS" asConst JSDoc={{ comments: ['@description Supported HTTP methods.'] }}>
|
|
58
|
+
* {`['GET', 'POST', 'PUT', 'DELETE']`}
|
|
59
|
+
* </Const>
|
|
60
|
+
* ```
|
|
33
61
|
*/
|
|
34
62
|
export function Const({ children, ...props }: ConstProps): KubbReactElement {
|
|
35
63
|
const { name, export: canExport, type, JSDoc, asConst } = props
|
package/src/components/File.tsx
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import type { ExportNode, ImportNode, SourceNode } from '@kubb/ast
|
|
1
|
+
import type { ExportNode, ImportNode, SourceNode } from '@kubb/ast'
|
|
2
2
|
import type { Key, KubbReactElement, KubbReactNode } from '../types.ts'
|
|
3
3
|
|
|
4
4
|
type BasePropsWithBaseName = {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* Based on UNIX basename
|
|
6
|
+
* Base file name including extension, derived from the input path.
|
|
7
|
+
* Based on UNIX basename convention: `${name}${extname}`.
|
|
8
|
+
*
|
|
8
9
|
* @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* `baseName: 'petStore.ts'`
|
|
9
13
|
*/
|
|
10
14
|
baseName: `${string}.${string}`
|
|
11
15
|
/**
|
|
12
|
-
*
|
|
16
|
+
* Fully qualified path to the generated file.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* `path: 'src/models/petStore.ts'`
|
|
13
20
|
*/
|
|
14
21
|
path: string
|
|
15
22
|
}
|
|
@@ -17,7 +24,8 @@ type BasePropsWithBaseName = {
|
|
|
17
24
|
type BasePropsWithoutBaseName = {
|
|
18
25
|
baseName?: never
|
|
19
26
|
/**
|
|
20
|
-
*
|
|
27
|
+
* Fully qualified path to the generated file.
|
|
28
|
+
* Optional when `baseName` is omitted — the component renders its children inline.
|
|
21
29
|
*/
|
|
22
30
|
path?: string
|
|
23
31
|
}
|
|
@@ -26,14 +34,40 @@ type BaseProps = BasePropsWithBaseName | BasePropsWithoutBaseName
|
|
|
26
34
|
|
|
27
35
|
type Props<TMeta> = BaseProps & {
|
|
28
36
|
key?: Key
|
|
37
|
+
/**
|
|
38
|
+
* Arbitrary metadata attached to the file node.
|
|
39
|
+
* Used by plugins for barrel generation and custom post-processing.
|
|
40
|
+
*/
|
|
29
41
|
meta?: TMeta
|
|
42
|
+
/**
|
|
43
|
+
* Text prepended to the generated file content before any source blocks.
|
|
44
|
+
*/
|
|
30
45
|
banner?: string
|
|
46
|
+
/**
|
|
47
|
+
* Text appended to the generated file content after all source blocks.
|
|
48
|
+
*/
|
|
31
49
|
footer?: string
|
|
50
|
+
/**
|
|
51
|
+
* Child nodes rendered as the content of this file (source blocks, imports, exports).
|
|
52
|
+
*/
|
|
32
53
|
children?: KubbReactNode
|
|
33
54
|
}
|
|
34
55
|
|
|
35
56
|
/**
|
|
36
|
-
*
|
|
57
|
+
* Declares a generated file entry to be collected by the renderer.
|
|
58
|
+
*
|
|
59
|
+
* When both `baseName` and `path` are provided, the component registers a new
|
|
60
|
+
* `FileNode` and passes its children through as source blocks.
|
|
61
|
+
* When either is omitted the children are rendered inline without creating a file entry.
|
|
62
|
+
*
|
|
63
|
+
* @example Basic file with a source block
|
|
64
|
+
* ```tsx
|
|
65
|
+
* <File baseName="petStore.ts" path="src/models/petStore.ts">
|
|
66
|
+
* <File.Source name="Pet" isExportable isIndexable>
|
|
67
|
+
* {`export type Pet = { id: number; name: string }`}
|
|
68
|
+
* </File.Source>
|
|
69
|
+
* </File>
|
|
70
|
+
* ```
|
|
37
71
|
*/
|
|
38
72
|
export function File<TMeta extends object = object>({ children, ...props }: Props<TMeta>): KubbReactElement {
|
|
39
73
|
const { baseName, path } = props
|
|
@@ -49,14 +83,31 @@ File.displayName = 'File'
|
|
|
49
83
|
|
|
50
84
|
type FileSourceProps = Omit<SourceNode, 'kind' | 'value'> & {
|
|
51
85
|
key?: Key
|
|
86
|
+
/**
|
|
87
|
+
* Child nodes rendered as the source content of this block.
|
|
88
|
+
*/
|
|
52
89
|
children?: KubbReactNode
|
|
53
90
|
}
|
|
54
91
|
|
|
55
92
|
/**
|
|
56
|
-
* File.
|
|
93
|
+
* Marks a block of source text to be associated with the enclosing {@link File}.
|
|
94
|
+
*
|
|
95
|
+
* Children are treated as the source string. When `isExportable` is `true` the
|
|
96
|
+
* `name` is used for deduplication and barrel generation.
|
|
57
97
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
98
|
+
* @example Exportable, indexable source block
|
|
99
|
+
* ```tsx
|
|
100
|
+
* <File.Source name="Pet" isExportable isIndexable>
|
|
101
|
+
* {`export type Pet = { id: number; name: string }`}
|
|
102
|
+
* </File.Source>
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example Type-only source block
|
|
106
|
+
* ```tsx
|
|
107
|
+
* <File.Source name="PetId" isTypeOnly isExportable>
|
|
108
|
+
* {`export type PetId = string`}
|
|
109
|
+
* </File.Source>
|
|
110
|
+
* ```
|
|
60
111
|
*/
|
|
61
112
|
function FileSource({ children, ...props }: FileSourceProps): KubbReactElement {
|
|
62
113
|
const { name, isExportable, isIndexable, isTypeOnly } = props
|
|
@@ -70,13 +121,24 @@ function FileSource({ children, ...props }: FileSourceProps): KubbReactElement {
|
|
|
70
121
|
|
|
71
122
|
FileSource.displayName = 'FileSource'
|
|
72
123
|
|
|
73
|
-
|
|
124
|
+
type FileExportProps = Omit<ExportNode, 'kind'> & { key?: Key }
|
|
74
125
|
|
|
75
126
|
/**
|
|
76
|
-
* File.
|
|
127
|
+
* Declares an export entry for the enclosing {@link File}.
|
|
128
|
+
*
|
|
129
|
+
* The export is collected by the renderer and emitted at the top of the generated file.
|
|
77
130
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
131
|
+
* @example Named export
|
|
132
|
+
* ```tsx
|
|
133
|
+
* <File.Export name={['Pet']} path="./models/petStore" />
|
|
134
|
+
* // export { Pet } from './models/petStore'
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example Type-only wildcard export
|
|
138
|
+
* ```tsx
|
|
139
|
+
* <File.Export path="./models/petStore" isTypeOnly />
|
|
140
|
+
* // export type * from './models/petStore'
|
|
141
|
+
* ```
|
|
80
142
|
*/
|
|
81
143
|
function FileExport(props: FileExportProps): KubbReactElement {
|
|
82
144
|
const { name, path, isTypeOnly, asAlias } = props
|
|
@@ -86,12 +148,30 @@ function FileExport(props: FileExportProps): KubbReactElement {
|
|
|
86
148
|
|
|
87
149
|
FileExport.displayName = 'FileExport'
|
|
88
150
|
|
|
89
|
-
|
|
151
|
+
type FileImportProps = Omit<ImportNode, 'kind'> & { key?: Key }
|
|
90
152
|
|
|
91
153
|
/**
|
|
92
|
-
* File.
|
|
154
|
+
* Declares an import entry for the enclosing {@link File}.
|
|
155
|
+
*
|
|
156
|
+
* The import is collected by the renderer and emitted at the top of the generated file.
|
|
157
|
+
*
|
|
158
|
+
* @example Named import
|
|
159
|
+
* ```tsx
|
|
160
|
+
* <File.Import name={['useState']} path="react" />
|
|
161
|
+
* // import { useState } from 'react'
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* @example Type-only import
|
|
165
|
+
* ```tsx
|
|
166
|
+
* <File.Import name={['Pet']} path="./models" isTypeOnly />
|
|
167
|
+
* // import type { Pet } from './models'
|
|
168
|
+
* ```
|
|
93
169
|
*
|
|
94
|
-
*
|
|
170
|
+
* @example Namespace import
|
|
171
|
+
* ```tsx
|
|
172
|
+
* <File.Import name="z" path="zod" isNameSpace />
|
|
173
|
+
* // import * as z from 'zod'
|
|
174
|
+
* ```
|
|
95
175
|
*/
|
|
96
176
|
function FileImport(props: FileImportProps): KubbReactElement {
|
|
97
177
|
const { name, root, path, isTypeOnly, isNameSpace } = props
|
|
@@ -3,46 +3,81 @@ import type { JSDoc, Key, KubbReactElement, KubbReactNode } from '../types.ts'
|
|
|
3
3
|
type Props = {
|
|
4
4
|
key?: Key
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Identifier of the generated function declaration.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* `name: 'getPet'`
|
|
7
10
|
*/
|
|
8
11
|
name: string
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* Emit `default` after the `export` keyword, making this the module's default export.
|
|
14
|
+
* Requires `export` to also be `true`.
|
|
15
|
+
* @default false
|
|
11
16
|
*/
|
|
12
17
|
default?: boolean
|
|
13
18
|
/**
|
|
14
|
-
*
|
|
19
|
+
* Parameter list written verbatim between the function's parentheses.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* `params: 'petId: string, options?: RequestOptions'`
|
|
15
23
|
*/
|
|
16
24
|
params?: string
|
|
17
25
|
/**
|
|
18
|
-
*
|
|
26
|
+
* Emit the `export` keyword before the function declaration.
|
|
27
|
+
* - `true` generates `export function name(…) { … }`
|
|
28
|
+
* - `false` generates `function name(…) { … }`
|
|
29
|
+
* @default false
|
|
19
30
|
*/
|
|
20
31
|
export?: boolean
|
|
21
32
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
33
|
+
* Emit the `async` keyword, making this an async function.
|
|
34
|
+
* The return type is automatically wrapped in `Promise<returnType>` when both
|
|
35
|
+
* `async` and `returnType` are set.
|
|
36
|
+
* @default false
|
|
24
37
|
*/
|
|
25
38
|
async?: boolean
|
|
26
39
|
/**
|
|
27
|
-
*
|
|
40
|
+
* TypeScript generic type parameters written verbatim between `<` and `>`.
|
|
41
|
+
* Pass an array to emit multiple parameters separated by commas.
|
|
42
|
+
*
|
|
43
|
+
* @example Single generic
|
|
44
|
+
* `generics: 'TData'`
|
|
45
|
+
*
|
|
46
|
+
* @example Multiple generics
|
|
47
|
+
* `generics: ['TData', 'TError = unknown']`
|
|
28
48
|
*/
|
|
29
49
|
generics?: string | string[]
|
|
30
50
|
/**
|
|
31
|
-
*
|
|
51
|
+
* TypeScript return type annotation written verbatim after `:`.
|
|
52
|
+
* When `async` is `true`, the value is automatically wrapped in `Promise<…>`.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* `returnType: 'Pet'`
|
|
32
56
|
*/
|
|
33
57
|
returnType?: string
|
|
34
58
|
/**
|
|
35
|
-
*
|
|
59
|
+
* JSDoc block to prepend to the function declaration.
|
|
60
|
+
* Each entry in `comments` becomes one line inside the emitted `/** … *\/` block.
|
|
36
61
|
*/
|
|
37
62
|
JSDoc?: JSDoc
|
|
38
63
|
/**
|
|
39
|
-
*
|
|
64
|
+
* Child nodes rendered as the body of the function.
|
|
40
65
|
*/
|
|
41
66
|
children?: KubbReactNode
|
|
42
67
|
}
|
|
43
68
|
|
|
44
69
|
/**
|
|
45
70
|
* Generates a TypeScript function declaration.
|
|
71
|
+
*
|
|
72
|
+
* @example Async exported function with generics
|
|
73
|
+
* ```tsx
|
|
74
|
+
* <Function export async name="getPet" generics={['TData = Pet']} params="petId: string" returnType="TData">
|
|
75
|
+
* {`return client.get(\`/pets/\${petId}\`)`}
|
|
76
|
+
* </Function>
|
|
77
|
+
* // export async function getPet<TData = Pet>(petId: string): Promise<TData> {
|
|
78
|
+
* // return client.get(`/pets/${petId}`)
|
|
79
|
+
* // }
|
|
80
|
+
* ```
|
|
46
81
|
*/
|
|
47
82
|
export function Function({ children, ...props }: Props): KubbReactElement {
|
|
48
83
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc } = props
|
|
@@ -70,16 +105,26 @@ Function.displayName = 'Function'
|
|
|
70
105
|
|
|
71
106
|
type ArrowFunctionProps = Props & {
|
|
72
107
|
/**
|
|
73
|
-
*
|
|
108
|
+
* Render the arrow function as a single-line expression (no braces around the body).
|
|
109
|
+
* - `true` generates `const name = (…) => expression`
|
|
110
|
+
* - `false` generates `const name = (…) => { … }`
|
|
111
|
+
* @default false
|
|
74
112
|
*/
|
|
75
113
|
singleLine?: boolean
|
|
76
114
|
}
|
|
77
115
|
|
|
78
116
|
/**
|
|
79
|
-
*
|
|
117
|
+
* Generates an arrow function expression assigned to a `const`.
|
|
118
|
+
* Supports the same flags as {@link Function}.
|
|
119
|
+
* Use `singleLine` to render the body as a concise expression rather than a block.
|
|
80
120
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
121
|
+
* @example Single-line arrow function
|
|
122
|
+
* ```tsx
|
|
123
|
+
* <Function.Arrow export name="double" params="n: number" returnType="number" singleLine>
|
|
124
|
+
* {`n * 2`}
|
|
125
|
+
* </Function.Arrow>
|
|
126
|
+
* // export const double = (n: number): number => n * 2
|
|
127
|
+
* ```
|
|
83
128
|
*/
|
|
84
129
|
function ArrowFunction({ children, ...props }: ArrowFunctionProps) {
|
|
85
130
|
const { name, default: isDefault, export: canExport, async, generics, params, returnType, JSDoc, singleLine } = props
|
package/src/components/Root.tsx
CHANGED
|
@@ -31,23 +31,29 @@ class ErrorBoundary extends Component<{
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
type RootProps = {
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* Callback invoked to unmount the entire renderer tree.
|
|
37
|
+
* Called with an `Error` when the exit is caused by a render error,
|
|
38
|
+
* or with `undefined` for a clean shutdown.
|
|
37
39
|
*/
|
|
38
40
|
onExit: (error?: Error) => void
|
|
39
41
|
/**
|
|
40
|
-
*
|
|
42
|
+
* Callback invoked whenever a render error is caught by the error boundary.
|
|
43
|
+
* Use this to propagate errors up to the caller of {@link createRenderer}.
|
|
41
44
|
*/
|
|
42
45
|
onError: (error: Error) => void
|
|
43
46
|
/**
|
|
44
|
-
*
|
|
47
|
+
* Child nodes rendered inside the error boundary.
|
|
45
48
|
*/
|
|
46
49
|
children?: KubbReactNode
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
/**
|
|
50
|
-
*
|
|
53
|
+
* Root component for the Kubb renderer tree.
|
|
54
|
+
*
|
|
55
|
+
* Wraps all children in an `ErrorBoundary` so that render errors are caught
|
|
56
|
+
* and forwarded to `onError` rather than crashing the process.
|
|
51
57
|
*/
|
|
52
58
|
export function Root({ onError, children }: RootProps): KubbReactElement {
|
|
53
59
|
return (
|