@vx-oss/docs-story 1.0.3
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/LICENSE +21 -0
- package/css/preset.css +1 -0
- package/dist/.translations/index.d.ts +18 -0
- package/dist/.translations/keys.js +18 -0
- package/dist/_virtual/_rolldown/runtime.js +4 -0
- package/dist/cache/fs.d.ts +5 -0
- package/dist/cache/fs.js +22 -0
- package/dist/cache/index.d.ts +11 -0
- package/dist/cache/index.js +15 -0
- package/dist/client/arg-form.js +357 -0
- package/dist/client/compiled.d.ts +52 -0
- package/dist/client/compiled.js +47 -0
- package/dist/client/components/input.js +13 -0
- package/dist/client/components/select.js +78 -0
- package/dist/client/with-control.d.ts +8 -0
- package/dist/client/with-control.js +104 -0
- package/dist/i18n.d.ts +6 -0
- package/dist/i18n.js +7 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +74 -0
- package/dist/next/client.d.ts +2 -0
- package/dist/next/client.js +2 -0
- package/dist/next/index.d.ts +14 -0
- package/dist/next/index.js +37 -0
- package/dist/node_modules/.pnpm/@fastify_deepmerge@3.2.1/node_modules/@fastify/deepmerge/index.js +110 -0
- package/dist/node_modules/.pnpm/@ungap_structured-clone@1.3.4/node_modules/@ungap/structured-clone/esm/deserialize.js +86 -0
- package/dist/node_modules/.pnpm/@ungap_structured-clone@1.3.4/node_modules/@ungap/structured-clone/esm/json.js +23 -0
- package/dist/node_modules/.pnpm/@ungap_structured-clone@1.3.4/node_modules/@ungap/structured-clone/esm/serialize.js +119 -0
- package/dist/node_modules/.pnpm/react-error-boundary@6.1.3_@types_react@19.2.18_react@19.2.8/node_modules/react-error-boundary/dist/react-error-boundary.js +61 -0
- package/dist/type-tree/builder.d.ts +41 -0
- package/dist/type-tree/builder.js +240 -0
- package/dist/type-tree/index.d.ts +6 -0
- package/dist/type-tree/index.js +5 -0
- package/dist/type-tree/sampler.d.ts +5 -0
- package/dist/type-tree/sampler.js +21 -0
- package/dist/type-tree/stringify.d.ts +9 -0
- package/dist/type-tree/stringify.js +20 -0
- package/dist/type-tree/types.d.ts +67 -0
- package/dist/type-tree/validator.d.ts +5 -0
- package/dist/type-tree/validator.js +21 -0
- package/dist/utils/cn.js +2 -0
- package/dist/utils/date.js +6 -0
- package/dist/utils/generate.js +24 -0
- package/dist/utils/parse.js +20 -0
- package/dist/utils/serialization.js +10 -0
- package/dist/utils/transform.js +27 -0
- package/dist/vite/client.d.ts +2 -0
- package/dist/vite/client.js +2 -0
- package/dist/vite/index.d.ts +19 -0
- package/dist/vite/index.js +25 -0
- package/dist/webpack/story.d.ts +13 -0
- package/dist/webpack/story.js +26 -0
- package/package.json +96 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Component, createContext, createElement } from "react";
|
|
3
|
+
//#region ../../node_modules/.pnpm/react-error-boundary@6.1.3_@types+react@19.2.18_react@19.2.8/node_modules/react-error-boundary/dist/react-error-boundary.js
|
|
4
|
+
const h = createContext(null);
|
|
5
|
+
const c = {
|
|
6
|
+
didCatch: !1,
|
|
7
|
+
error: null
|
|
8
|
+
};
|
|
9
|
+
var m = class extends Component {
|
|
10
|
+
constructor(e) {
|
|
11
|
+
super(e), this.resetErrorBoundary = this.resetErrorBoundary.bind(this), this.state = c;
|
|
12
|
+
}
|
|
13
|
+
static getDerivedStateFromError(e) {
|
|
14
|
+
return {
|
|
15
|
+
didCatch: !0,
|
|
16
|
+
error: e
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
resetErrorBoundary(...e) {
|
|
20
|
+
const { error: t } = this.state;
|
|
21
|
+
t !== null && (this.props.onReset?.({
|
|
22
|
+
args: e,
|
|
23
|
+
reason: "imperative-api"
|
|
24
|
+
}), this.setState(c));
|
|
25
|
+
}
|
|
26
|
+
componentDidCatch(e, t) {
|
|
27
|
+
this.props.onError?.(e, t);
|
|
28
|
+
}
|
|
29
|
+
componentDidUpdate(e, t) {
|
|
30
|
+
const { didCatch: o } = this.state, { resetKeys: s } = this.props;
|
|
31
|
+
o && t.error !== null && C(e.resetKeys, s) && (this.props.onReset?.({
|
|
32
|
+
next: s,
|
|
33
|
+
prev: e.resetKeys,
|
|
34
|
+
reason: "keys"
|
|
35
|
+
}), this.setState(c));
|
|
36
|
+
}
|
|
37
|
+
render() {
|
|
38
|
+
const { children: e, fallbackRender: t, FallbackComponent: o, fallback: s } = this.props, { didCatch: n, error: a } = this.state;
|
|
39
|
+
let i = e;
|
|
40
|
+
if (n) {
|
|
41
|
+
const u = {
|
|
42
|
+
error: a,
|
|
43
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
44
|
+
};
|
|
45
|
+
if (typeof t == "function") i = t(u);
|
|
46
|
+
else if (o) i = createElement(o, u);
|
|
47
|
+
else if (s !== void 0) i = s;
|
|
48
|
+
else throw a;
|
|
49
|
+
}
|
|
50
|
+
return createElement(h.Provider, { value: {
|
|
51
|
+
didCatch: n,
|
|
52
|
+
error: a,
|
|
53
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
54
|
+
} }, i);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
function C(r = [], e = []) {
|
|
58
|
+
return r.length !== e.length || r.some((t, o) => !Object.is(t, e[o]));
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { m as ErrorBoundary, h as ErrorBoundaryContext };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { TypeNode, UnionNode } from "./types.js";
|
|
2
|
+
import { Node, Project, Type } from "ts-morph";
|
|
3
|
+
//#region src/type-tree/builder.d.ts
|
|
4
|
+
declare enum TypeToNodeFlag {
|
|
5
|
+
None = 0,
|
|
6
|
+
NoIntersection = 1
|
|
7
|
+
}
|
|
8
|
+
interface TypeTreeBuilder {
|
|
9
|
+
typeToNode: (type: Type, location: Node, flag?: TypeToNodeFlag) => TypeNode;
|
|
10
|
+
}
|
|
11
|
+
type Handler = (options: {
|
|
12
|
+
type: Type;
|
|
13
|
+
location: Node;
|
|
14
|
+
flag: TypeToNodeFlag;
|
|
15
|
+
builder: TypeTreeBuilder;
|
|
16
|
+
project: Project;
|
|
17
|
+
cache: Map<TypeToNodeFlag, WeakMap<Type, TypeNode>>;
|
|
18
|
+
getCache: () => TypeNode | undefined;
|
|
19
|
+
setCache: (value: TypeNode) => void;
|
|
20
|
+
/**
|
|
21
|
+
* start from the first handler
|
|
22
|
+
*/
|
|
23
|
+
root: (type: Type, flag: TypeToNodeFlag, location?: Node) => TypeNode;
|
|
24
|
+
/**
|
|
25
|
+
* start from the next handler
|
|
26
|
+
*/
|
|
27
|
+
next: (type: Type, flag: TypeToNodeFlag, location?: Node) => TypeNode;
|
|
28
|
+
}) => TypeNode;
|
|
29
|
+
declare const literalEnumHandler: Handler;
|
|
30
|
+
declare function createTypeTreeBuilder(project: Project, customHandlers?: Handler[]): TypeTreeBuilder;
|
|
31
|
+
/**
|
|
32
|
+
* collapse controls into a deterministic state when certain values are known & immutable:
|
|
33
|
+
* - collapse determined unions
|
|
34
|
+
* - remove immutable controls
|
|
35
|
+
* @returns modified node (modification is in place)
|
|
36
|
+
*/
|
|
37
|
+
declare function collapse(node: TypeNode, value: unknown): TypeNode;
|
|
38
|
+
/** simplify union */
|
|
39
|
+
declare function unwrapUnion(union: UnionNode): TypeNode;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { Handler, TypeToNodeFlag, TypeTreeBuilder, collapse, createTypeTreeBuilder, literalEnumHandler, unwrapUnion };
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { validate } from "./validator.js";
|
|
2
|
+
import { Node, ts } from "ts-morph";
|
|
3
|
+
//#region src/type-tree/builder.ts
|
|
4
|
+
let TypeToNodeFlag = /* @__PURE__ */ function(TypeToNodeFlag) {
|
|
5
|
+
TypeToNodeFlag[TypeToNodeFlag["None"] = 0] = "None";
|
|
6
|
+
TypeToNodeFlag[TypeToNodeFlag["NoIntersection"] = 1] = "NoIntersection";
|
|
7
|
+
return TypeToNodeFlag;
|
|
8
|
+
}({});
|
|
9
|
+
const baseHandler = ({ type, flag, location, setCache, root }) => {
|
|
10
|
+
if (type.isString()) return { type: "string" };
|
|
11
|
+
if (type.isNumber()) return { type: "number" };
|
|
12
|
+
if (type.isBoolean()) return { type: "boolean" };
|
|
13
|
+
if (type.isBigInt()) return { type: "bigint" };
|
|
14
|
+
const symbol = type.getSymbol();
|
|
15
|
+
if (symbol && symbol.getName() === "Date") return { type: "date" };
|
|
16
|
+
if (type.isUndefined()) return { type: "undefined" };
|
|
17
|
+
if (type.isUnknown()) return { type: "unknown" };
|
|
18
|
+
if (type.isNull()) return { type: "null" };
|
|
19
|
+
if (type.isNever() || type.getCallSignatures().length > 0) return { type: "never" };
|
|
20
|
+
if (type.isUnion()) {
|
|
21
|
+
const result = {
|
|
22
|
+
type: "union",
|
|
23
|
+
types: []
|
|
24
|
+
};
|
|
25
|
+
setCache(result);
|
|
26
|
+
for (const t of type.getUnionTypes()) result.types.push(root(t, flag));
|
|
27
|
+
Object.assign(result, unwrapUnion(result));
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
if (type.isIntersection() && (flag & 1) === 0) {
|
|
31
|
+
const intersectionTypes = type.getIntersectionTypes();
|
|
32
|
+
const result = {
|
|
33
|
+
type: "intersection",
|
|
34
|
+
members: [],
|
|
35
|
+
intersection: { type: "never" }
|
|
36
|
+
};
|
|
37
|
+
setCache(result);
|
|
38
|
+
result.intersection = root(type, flag | 1);
|
|
39
|
+
for (const t of intersectionTypes) {
|
|
40
|
+
const member = root(t, flag);
|
|
41
|
+
if (member.type !== "never") result.members.push(member);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
if (type.isLiteral()) {
|
|
46
|
+
let value;
|
|
47
|
+
if (type.isBooleanLiteral()) value = type.getText() === "true";
|
|
48
|
+
else if (type.isBigIntLiteral()) value = BigInt(type.getLiteralValue().base10Value);
|
|
49
|
+
else value = type.getLiteralValue();
|
|
50
|
+
return {
|
|
51
|
+
type: "literal",
|
|
52
|
+
value
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (type.isArray() || type.isReadonlyArray()) {
|
|
56
|
+
const elementType = type.getArrayElementType();
|
|
57
|
+
const result = {
|
|
58
|
+
type: "array",
|
|
59
|
+
elementType: { type: "never" }
|
|
60
|
+
};
|
|
61
|
+
setCache(result);
|
|
62
|
+
result.elementType = elementType ? root(elementType, flag) : { type: "unknown" };
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
if (type.isTuple()) {
|
|
66
|
+
const elements = type.getTupleElements();
|
|
67
|
+
if (elements.length === 0) return {
|
|
68
|
+
type: "array",
|
|
69
|
+
elementType: { type: "unknown" }
|
|
70
|
+
};
|
|
71
|
+
const result = {
|
|
72
|
+
type: "array",
|
|
73
|
+
elementType: { type: "never" }
|
|
74
|
+
};
|
|
75
|
+
setCache(result);
|
|
76
|
+
result.elementType = unwrapUnion({
|
|
77
|
+
type: "union",
|
|
78
|
+
types: elements.map((t) => root(t, flag))
|
|
79
|
+
});
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
const enumSymbol = type.getSymbol();
|
|
83
|
+
if (enumSymbol && enumSymbol.getFlags() & ts.SymbolFlags.Enum) {
|
|
84
|
+
const enumDeclaration = enumSymbol.getValueDeclaration();
|
|
85
|
+
if (enumDeclaration && Node.isEnumDeclaration(enumDeclaration)) return {
|
|
86
|
+
type: "enum",
|
|
87
|
+
members: enumDeclaration.getMembers().map((member) => {
|
|
88
|
+
const name = member.getName();
|
|
89
|
+
const initializer = member.getInitializer();
|
|
90
|
+
let value = name;
|
|
91
|
+
if (initializer) {
|
|
92
|
+
if (Node.isStringLiteral(initializer)) value = initializer.getText().slice(1, -1);
|
|
93
|
+
else if (Node.isNumericLiteral(initializer)) value = Number.parseFloat(initializer.getText());
|
|
94
|
+
else if (Node.isTrueLiteral(initializer)) value = true;
|
|
95
|
+
else if (Node.isFalseLiteral(initializer)) value = false;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
label: name,
|
|
99
|
+
value
|
|
100
|
+
};
|
|
101
|
+
})
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (type.isObject() || type.isClassOrInterface() || type.getProperties().length > 0) {
|
|
105
|
+
const properties = type.getProperties();
|
|
106
|
+
const alias = type.getAliasSymbol();
|
|
107
|
+
const aliasTypeArguments = type.getAliasTypeArguments();
|
|
108
|
+
const result = {
|
|
109
|
+
type: "object",
|
|
110
|
+
displayName: alias && aliasTypeArguments.length === 0 ? alias.getName() : void 0,
|
|
111
|
+
properties: []
|
|
112
|
+
};
|
|
113
|
+
setCache(result);
|
|
114
|
+
for (const prop of properties) {
|
|
115
|
+
if (prop.getName().startsWith("#")) continue;
|
|
116
|
+
let child = root(prop.getTypeAtLocation(location), flag, prop.getValueDeclaration());
|
|
117
|
+
if (child.type === "union") child = unwrapUnion({
|
|
118
|
+
...child,
|
|
119
|
+
types: child.types.filter((t) => t.type !== "undefined")
|
|
120
|
+
});
|
|
121
|
+
else if (child.type === "undefined") continue;
|
|
122
|
+
if (child.type !== "never") result.properties.push({
|
|
123
|
+
name: prop.getName(),
|
|
124
|
+
type: child,
|
|
125
|
+
required: !prop.isOptional()
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
type: "never",
|
|
132
|
+
displayName: type.getText(location, ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope)
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
const literalEnumHandler = ({ type, flag, next }) => {
|
|
136
|
+
const result = next(type, flag);
|
|
137
|
+
if (result.type !== "union") return result;
|
|
138
|
+
const literalTypes = [];
|
|
139
|
+
const otherTypes = [];
|
|
140
|
+
for (const t of result.types) (t.type === "literal" ? literalTypes : otherTypes).push(t);
|
|
141
|
+
if (literalTypes.length <= 1) return result;
|
|
142
|
+
const enumNode = {
|
|
143
|
+
type: "enum",
|
|
144
|
+
members: literalTypes.map((t) => ({
|
|
145
|
+
label: JSON.stringify(t.value),
|
|
146
|
+
value: t.value
|
|
147
|
+
}))
|
|
148
|
+
};
|
|
149
|
+
if (otherTypes.length > 0) result.types = [enumNode, ...otherTypes];
|
|
150
|
+
else Object.assign(result, enumNode);
|
|
151
|
+
return result;
|
|
152
|
+
};
|
|
153
|
+
function createTypeTreeBuilder(project, customHandlers = []) {
|
|
154
|
+
const handlers = [...customHandlers, baseHandler];
|
|
155
|
+
function callHandler(type, location, flag, index = 0, cache = /* @__PURE__ */ new Map()) {
|
|
156
|
+
const cached = cache.get(flag)?.get(type);
|
|
157
|
+
if (cached) return cached;
|
|
158
|
+
const handler = handlers[index];
|
|
159
|
+
if (!handler) return { type: "never" };
|
|
160
|
+
return handler({
|
|
161
|
+
project,
|
|
162
|
+
builder,
|
|
163
|
+
cache,
|
|
164
|
+
type,
|
|
165
|
+
location,
|
|
166
|
+
flag,
|
|
167
|
+
getCache() {
|
|
168
|
+
return cache.get(flag)?.get(type);
|
|
169
|
+
},
|
|
170
|
+
setCache(value) {
|
|
171
|
+
let typeCache = cache.get(flag);
|
|
172
|
+
if (!typeCache) {
|
|
173
|
+
typeCache = /* @__PURE__ */ new WeakMap();
|
|
174
|
+
cache.set(flag, typeCache);
|
|
175
|
+
}
|
|
176
|
+
typeCache.set(type, value);
|
|
177
|
+
},
|
|
178
|
+
next(type, flag, l = location) {
|
|
179
|
+
return callHandler(type, l, flag, index + 1, cache);
|
|
180
|
+
},
|
|
181
|
+
root(type, flag, l = location) {
|
|
182
|
+
return callHandler(type, l, flag, 0, cache);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const builder = { typeToNode(type, location, flag = 0) {
|
|
187
|
+
return callHandler(type, location, flag);
|
|
188
|
+
} };
|
|
189
|
+
return builder;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* collapse controls into a deterministic state when certain values are known & immutable:
|
|
193
|
+
* - collapse determined unions
|
|
194
|
+
* - remove immutable controls
|
|
195
|
+
* @returns modified node (modification is in place)
|
|
196
|
+
*/
|
|
197
|
+
function collapse(node, value) {
|
|
198
|
+
if (node.type === "object") {
|
|
199
|
+
if (typeof value !== "object" || value === null) return node;
|
|
200
|
+
const newProps = [];
|
|
201
|
+
for (const prop of node.properties) {
|
|
202
|
+
if (!(prop.name in value)) {
|
|
203
|
+
newProps.push(prop);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
prop.type = collapse(prop.type, value[prop.name]);
|
|
207
|
+
if (prop.type.type !== "never") newProps.push(prop);
|
|
208
|
+
}
|
|
209
|
+
node.properties = newProps;
|
|
210
|
+
return node;
|
|
211
|
+
}
|
|
212
|
+
if (node.type === "union") {
|
|
213
|
+
const newTypes = [];
|
|
214
|
+
for (const member of node.types) if (validate(member, value)) newTypes.push(collapse(member, value));
|
|
215
|
+
node.types = newTypes;
|
|
216
|
+
return unwrapUnion(node);
|
|
217
|
+
}
|
|
218
|
+
return { type: "never" };
|
|
219
|
+
}
|
|
220
|
+
/** simplify union */
|
|
221
|
+
function unwrapUnion(union) {
|
|
222
|
+
const members = /* @__PURE__ */ new Set();
|
|
223
|
+
for (let t of union.types) {
|
|
224
|
+
if (t.type === "union") t = unwrapUnion(t);
|
|
225
|
+
switch (t.type) {
|
|
226
|
+
case "unknown": return t;
|
|
227
|
+
case "never": break;
|
|
228
|
+
case "union":
|
|
229
|
+
for (const child of t.types) members.add(child);
|
|
230
|
+
break;
|
|
231
|
+
default: members.add(t);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (members.size === 0) return { type: "never" };
|
|
235
|
+
if (members.size === 1) return members.values().next().value;
|
|
236
|
+
union.types = Array.from(members);
|
|
237
|
+
return union;
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
export { TypeToNodeFlag, collapse, createTypeTreeBuilder, literalEnumHandler, unwrapUnion };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ArrayNode, BigIntNode, BooleanNode, DateNode, EnumNode, IntersectionNode, LiteralNode, NeverNode, NullNode, NumberNode, ObjectNode, StringNode, TypeNode, UndefinedNode, UnionNode, UnknownNode } from "./types.js";
|
|
2
|
+
import { Handler, TypeToNodeFlag, TypeTreeBuilder, collapse, createTypeTreeBuilder, literalEnumHandler, unwrapUnion } from "./builder.js";
|
|
3
|
+
import { FormatFlags, typeToString } from "./stringify.js";
|
|
4
|
+
import { validate } from "./validator.js";
|
|
5
|
+
import { getDefaultValue } from "./sampler.js";
|
|
6
|
+
export { ArrayNode, BigIntNode, BooleanNode, DateNode, EnumNode, FormatFlags, Handler, IntersectionNode, LiteralNode, NeverNode, NullNode, NumberNode, ObjectNode, StringNode, TypeNode, TypeToNodeFlag, TypeTreeBuilder, UndefinedNode, UnionNode, UnknownNode, collapse, createTypeTreeBuilder, getDefaultValue, literalEnumHandler, typeToString, unwrapUnion, validate };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { validate } from "./validator.js";
|
|
2
|
+
import { TypeToNodeFlag, collapse, createTypeTreeBuilder, literalEnumHandler, unwrapUnion } from "./builder.js";
|
|
3
|
+
import { getDefaultValue } from "./sampler.js";
|
|
4
|
+
import { FormatFlags, typeToString } from "./stringify.js";
|
|
5
|
+
export { FormatFlags, TypeToNodeFlag, collapse, createTypeTreeBuilder, getDefaultValue, literalEnumHandler, typeToString, unwrapUnion, validate };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/type-tree/sampler.ts
|
|
2
|
+
function getDefaultValue(node) {
|
|
3
|
+
switch (node.type) {
|
|
4
|
+
case "object": return Object.fromEntries(node.properties.map((prop) => [prop.name, getDefaultValue(prop.type)]));
|
|
5
|
+
case "array": return [];
|
|
6
|
+
case "null": return null;
|
|
7
|
+
case "undefined": return;
|
|
8
|
+
case "string": return "";
|
|
9
|
+
case "bigint": return 0n;
|
|
10
|
+
case "number": return 0;
|
|
11
|
+
case "boolean": return false;
|
|
12
|
+
case "enum": return node.members[0]?.value ?? "";
|
|
13
|
+
case "literal": return node.value;
|
|
14
|
+
case "union": return node.types.length > 0 ? getDefaultValue(node.types[0]) : void 0;
|
|
15
|
+
case "intersection": return getDefaultValue(node.intersection);
|
|
16
|
+
case "date": return new Date(Date.now());
|
|
17
|
+
case "unknown": return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { getDefaultValue };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TypeNode } from "./types.js";
|
|
2
|
+
//#region src/type-tree/stringify.d.ts
|
|
3
|
+
declare enum FormatFlags {
|
|
4
|
+
None = 0,
|
|
5
|
+
UseAlias = 1
|
|
6
|
+
}
|
|
7
|
+
declare function typeToString(node: TypeNode, flags?: FormatFlags): string;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { FormatFlags, typeToString };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/type-tree/stringify.ts
|
|
2
|
+
let FormatFlags = /* @__PURE__ */ function(FormatFlags) {
|
|
3
|
+
FormatFlags[FormatFlags["None"] = 0] = "None";
|
|
4
|
+
FormatFlags[FormatFlags["UseAlias"] = 1] = "UseAlias";
|
|
5
|
+
return FormatFlags;
|
|
6
|
+
}({});
|
|
7
|
+
function typeToString(node, flags = 0) {
|
|
8
|
+
if (node.displayName) return node.displayName;
|
|
9
|
+
switch (node.type) {
|
|
10
|
+
case "array": return `Array<${typeToString(node.elementType, flags)}>`;
|
|
11
|
+
case "literal":
|
|
12
|
+
if (typeof node.value === "string") return `"${node.value}"`;
|
|
13
|
+
return String(node.value);
|
|
14
|
+
case "union": return node.types.map((t) => typeToString(t, flags)).join(" | ");
|
|
15
|
+
case "intersection": return node.members.map((t) => typeToString(t, flags)).join(" & ");
|
|
16
|
+
default: return node.type;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { FormatFlags, typeToString };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//#region src/type-tree/types.d.ts
|
|
2
|
+
type TypeNode = ObjectNode | EnumNode | StringNode | BigIntNode | NumberNode | BooleanNode | ArrayNode | UnionNode | IntersectionNode | LiteralNode | NullNode | UndefinedNode | DateNode | UnknownNode | NeverNode;
|
|
3
|
+
interface Node {
|
|
4
|
+
displayName?: string;
|
|
5
|
+
}
|
|
6
|
+
interface ObjectNode extends Node {
|
|
7
|
+
type: 'object';
|
|
8
|
+
properties: {
|
|
9
|
+
name: string;
|
|
10
|
+
type: TypeNode;
|
|
11
|
+
required: boolean;
|
|
12
|
+
}[];
|
|
13
|
+
}
|
|
14
|
+
interface EnumNode extends Node {
|
|
15
|
+
type: 'enum';
|
|
16
|
+
members: {
|
|
17
|
+
label: string;
|
|
18
|
+
value: unknown;
|
|
19
|
+
}[];
|
|
20
|
+
}
|
|
21
|
+
interface BigIntNode extends Node {
|
|
22
|
+
type: 'bigint';
|
|
23
|
+
}
|
|
24
|
+
interface StringNode extends Node {
|
|
25
|
+
type: 'string';
|
|
26
|
+
}
|
|
27
|
+
interface DateNode extends Node {
|
|
28
|
+
type: 'date';
|
|
29
|
+
}
|
|
30
|
+
interface NumberNode extends Node {
|
|
31
|
+
type: 'number';
|
|
32
|
+
}
|
|
33
|
+
interface BooleanNode extends Node {
|
|
34
|
+
type: 'boolean';
|
|
35
|
+
}
|
|
36
|
+
interface ArrayNode extends Node {
|
|
37
|
+
type: 'array';
|
|
38
|
+
elementType: TypeNode;
|
|
39
|
+
}
|
|
40
|
+
interface UnionNode extends Node {
|
|
41
|
+
type: 'union';
|
|
42
|
+
types: TypeNode[];
|
|
43
|
+
}
|
|
44
|
+
interface IntersectionNode extends Node {
|
|
45
|
+
type: 'intersection';
|
|
46
|
+
members: TypeNode[];
|
|
47
|
+
intersection: TypeNode;
|
|
48
|
+
}
|
|
49
|
+
interface LiteralNode extends Node {
|
|
50
|
+
type: 'literal';
|
|
51
|
+
value: string | number | boolean | bigint;
|
|
52
|
+
}
|
|
53
|
+
interface NullNode extends Node {
|
|
54
|
+
type: 'null';
|
|
55
|
+
}
|
|
56
|
+
interface UndefinedNode extends Node {
|
|
57
|
+
type: 'undefined';
|
|
58
|
+
}
|
|
59
|
+
interface UnknownNode extends Node {
|
|
60
|
+
type: 'unknown';
|
|
61
|
+
}
|
|
62
|
+
/** used for unsupported types too */
|
|
63
|
+
interface NeverNode extends Node {
|
|
64
|
+
type: 'never';
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { ArrayNode, BigIntNode, BooleanNode, DateNode, EnumNode, IntersectionNode, LiteralNode, NeverNode, NullNode, NumberNode, ObjectNode, StringNode, TypeNode, UndefinedNode, UnionNode, UnknownNode };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/type-tree/validator.ts
|
|
2
|
+
function validate(node, value) {
|
|
3
|
+
switch (node.type) {
|
|
4
|
+
case "array": return Array.isArray(value) && value.every((item) => validate(node.elementType, item));
|
|
5
|
+
case "enum": return node.members.some((member) => member.value === value);
|
|
6
|
+
case "intersection": return validate(node.intersection, value);
|
|
7
|
+
case "union": return node.types.some((t) => validate(t, value));
|
|
8
|
+
case "null": return value === "null";
|
|
9
|
+
case "literal": return node.value === value;
|
|
10
|
+
case "unknown":
|
|
11
|
+
case "never": return true;
|
|
12
|
+
case "date": return value instanceof Date;
|
|
13
|
+
case "object": return typeof value === "object" && value !== null && node.properties.every((prop) => {
|
|
14
|
+
const propValue = value[prop.name];
|
|
15
|
+
return !prop.required && propValue === void 0 || validate(prop.type, propValue);
|
|
16
|
+
});
|
|
17
|
+
default: return typeof value === node.type;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { validate };
|
package/dist/utils/cn.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createTypeTreeBuilder, literalEnumHandler } from "../type-tree/builder.js";
|
|
2
|
+
//#region src/utils/generate.ts
|
|
3
|
+
async function createControlsProject(tsconfigPath) {
|
|
4
|
+
const { Project } = await import("ts-morph");
|
|
5
|
+
return new Project({
|
|
6
|
+
tsConfigFilePath: tsconfigPath,
|
|
7
|
+
skipAddingFilesFromTsConfig: true
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function generateControls(mode, project, filePath, exportName, content) {
|
|
11
|
+
const aliasName = `_StoryProps_${exportName}_`;
|
|
12
|
+
const sourceFile = project.getSourceFile(filePath) ?? project.createSourceFile(filePath, content, { overwrite: true });
|
|
13
|
+
if (!sourceFile.getTypeAlias(aliasName)) sourceFile.addTypeAlias({
|
|
14
|
+
isExported: true,
|
|
15
|
+
name: aliasName,
|
|
16
|
+
type: `import('${mode}').GetProps<typeof ${exportName}>`
|
|
17
|
+
});
|
|
18
|
+
const declarations = sourceFile.getExportedDeclarations();
|
|
19
|
+
const declaration = declarations.get(aliasName)?.[0];
|
|
20
|
+
if (!declaration || !declarations.get(exportName)) throw new Error(`Export "${exportName}" not found in file "${filePath}"`);
|
|
21
|
+
return createTypeTreeBuilder(project, [literalEnumHandler]).typeToNode(declaration.getType(), declaration);
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { createControlsProject, generateControls };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Node, SyntaxKind } from "ts-morph";
|
|
2
|
+
//#region src/utils/parse.ts
|
|
3
|
+
function isDefineStoryCall(text) {
|
|
4
|
+
return text === "defineStory" || text.endsWith(".defineStory");
|
|
5
|
+
}
|
|
6
|
+
function findDefineStoryCalls(sourceFile) {
|
|
7
|
+
const stories = [];
|
|
8
|
+
for (const [exportName, declarations] of sourceFile.getExportedDeclarations()) for (const decl of declarations) {
|
|
9
|
+
if (!Node.isVariableDeclaration(decl)) continue;
|
|
10
|
+
const call = decl.getInitializer();
|
|
11
|
+
if (!call || !call.isKind(SyntaxKind.CallExpression) || !isDefineStoryCall(call.getExpression().getText())) continue;
|
|
12
|
+
stories.push({
|
|
13
|
+
call,
|
|
14
|
+
exportName
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return stories;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { findDefineStoryCalls };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { parse, stringify } from "../node_modules/.pnpm/@ungap_structured-clone@1.3.4/node_modules/@ungap/structured-clone/esm/json.js";
|
|
2
|
+
//#region src/utils/serialization.ts
|
|
3
|
+
function serialize(value) {
|
|
4
|
+
return stringify(value);
|
|
5
|
+
}
|
|
6
|
+
function deserialize(value) {
|
|
7
|
+
return parse(value);
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { deserialize, serialize };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { serialize } from "./serialization.js";
|
|
2
|
+
import { generateControls } from "./generate.js";
|
|
3
|
+
import { findDefineStoryCalls } from "./parse.js";
|
|
4
|
+
import { SyntaxKind } from "ts-morph";
|
|
5
|
+
//#region src/utils/transform.ts
|
|
6
|
+
function injectControls(parsed, controls) {
|
|
7
|
+
const [optionsArg] = parsed.call.getArguments();
|
|
8
|
+
if (optionsArg.getKind() !== SyntaxKind.ObjectLiteralExpression) throw new Error("defineStory() options must be an object literal to inject controls from @vx-oss/docs-story.");
|
|
9
|
+
const object = optionsArg.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
|
|
10
|
+
object.getProperty("_generated")?.remove();
|
|
11
|
+
object.addPropertyAssignment({
|
|
12
|
+
name: "_generated",
|
|
13
|
+
initializer: `{ exportName: ${JSON.stringify(parsed.exportName)}, controls: ${JSON.stringify(serialize(controls))} }`
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function transformStoryFile(mode, code, id, project) {
|
|
17
|
+
const sourceFile = project.createSourceFile(id, code, { overwrite: true });
|
|
18
|
+
const calls = findDefineStoryCalls(sourceFile);
|
|
19
|
+
if (calls.length === 0) return;
|
|
20
|
+
for (const parsed of calls) {
|
|
21
|
+
if (parsed.call.getArguments().length !== 1) continue;
|
|
22
|
+
injectControls(parsed, generateControls(mode, project, id, parsed.exportName, code));
|
|
23
|
+
}
|
|
24
|
+
return sourceFile.getFullText();
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { transformStoryFile };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
//#region src/vite/index.d.ts
|
|
3
|
+
interface StoryPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Path to `tsconfig.json`.
|
|
6
|
+
*
|
|
7
|
+
* @default "tsconfig.json"
|
|
8
|
+
*/
|
|
9
|
+
tsconfigPath?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Filter story files to transform by id (regex)
|
|
12
|
+
*
|
|
13
|
+
* @default `/\.story\.(js|jsx|ts|tsx)$/`
|
|
14
|
+
*/
|
|
15
|
+
filter?: RegExp;
|
|
16
|
+
}
|
|
17
|
+
declare function story(pluginOptions?: StoryPluginOptions): Plugin;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { StoryPluginOptions, story as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createControlsProject } from "../utils/generate.js";
|
|
2
|
+
import { transformStoryFile } from "../utils/transform.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
//#region src/vite/index.ts
|
|
5
|
+
function story(pluginOptions = {}) {
|
|
6
|
+
const { filter = /\.story\.(js|jsx|ts|tsx)$/ } = pluginOptions;
|
|
7
|
+
let tsconfigPath;
|
|
8
|
+
let projectPromise;
|
|
9
|
+
return {
|
|
10
|
+
name: "fumadocs:story",
|
|
11
|
+
enforce: "pre",
|
|
12
|
+
configResolved(resolved) {
|
|
13
|
+
tsconfigPath = pluginOptions.tsconfigPath ?? path.join(resolved.root, "tsconfig.json");
|
|
14
|
+
},
|
|
15
|
+
transform: {
|
|
16
|
+
filter: { id: filter },
|
|
17
|
+
async handler(code, id) {
|
|
18
|
+
projectPromise ??= createControlsProject(tsconfigPath);
|
|
19
|
+
return transformStoryFile("@vx-oss/docs-story/vite/client", code, id, await projectPromise);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { story as default };
|