@fragments-sdk/core 0.1.0
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 +84 -0
- package/dist/index.d.ts +2873 -0
- package/dist/index.js +1431 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
- package/src/__tests__/preview-runtime.test.tsx +111 -0
- package/src/composition.test.ts +262 -0
- package/src/composition.ts +318 -0
- package/src/constants.ts +114 -0
- package/src/context.ts +2 -0
- package/src/defineFragment.ts +141 -0
- package/src/figma.ts +263 -0
- package/src/fragment-types.ts +214 -0
- package/src/index.ts +207 -0
- package/src/performance-presets.ts +142 -0
- package/src/preview-runtime.tsx +144 -0
- package/src/schema.ts +229 -0
- package/src/storyAdapter.test.ts +571 -0
- package/src/storyAdapter.ts +761 -0
- package/src/storyFilters.test.ts +350 -0
- package/src/storyFilters.ts +253 -0
- package/src/storybook-csf.ts +11 -0
- package/src/token-parser.ts +321 -0
- package/src/token-types.ts +287 -0
- package/src/types.ts +784 -0
package/src/schema.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Zod schemas for runtime validation of fragment definitions
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Figma property mapping schemas
|
|
8
|
+
const figmaStringMappingSchema = z.object({
|
|
9
|
+
__type: z.literal('figma-string'),
|
|
10
|
+
figmaProperty: z.string().min(1),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const figmaBooleanMappingSchema = z.object({
|
|
14
|
+
__type: z.literal('figma-boolean'),
|
|
15
|
+
figmaProperty: z.string().min(1),
|
|
16
|
+
valueMapping: z.object({ true: z.unknown(), false: z.unknown() }).optional(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const figmaEnumMappingSchema = z.object({
|
|
20
|
+
__type: z.literal('figma-enum'),
|
|
21
|
+
figmaProperty: z.string().min(1),
|
|
22
|
+
valueMapping: z.record(z.unknown()),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const figmaInstanceMappingSchema = z.object({
|
|
26
|
+
__type: z.literal('figma-instance'),
|
|
27
|
+
figmaProperty: z.string().min(1),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const figmaChildrenMappingSchema = z.object({
|
|
31
|
+
__type: z.literal('figma-children'),
|
|
32
|
+
layers: z.array(z.string().min(1)),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const figmaTextContentMappingSchema = z.object({
|
|
36
|
+
__type: z.literal('figma-text-content'),
|
|
37
|
+
layer: z.string().min(1),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const figmaPropMappingSchema = z.discriminatedUnion('__type', [
|
|
41
|
+
figmaStringMappingSchema,
|
|
42
|
+
figmaBooleanMappingSchema,
|
|
43
|
+
figmaEnumMappingSchema,
|
|
44
|
+
figmaInstanceMappingSchema,
|
|
45
|
+
figmaChildrenMappingSchema,
|
|
46
|
+
figmaTextContentMappingSchema,
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
export const fragmentMetaSchema = z.object({
|
|
50
|
+
name: z.string().min(1),
|
|
51
|
+
description: z.string().min(1),
|
|
52
|
+
category: z.string().min(1),
|
|
53
|
+
tags: z.array(z.string()).optional(),
|
|
54
|
+
status: z.enum(['stable', 'beta', 'deprecated', 'experimental']).optional(),
|
|
55
|
+
since: z.string().optional(),
|
|
56
|
+
dependencies: z.array(z.object({
|
|
57
|
+
name: z.string().min(1),
|
|
58
|
+
version: z.string().min(1),
|
|
59
|
+
reason: z.string().optional(),
|
|
60
|
+
})).optional(),
|
|
61
|
+
figma: z.string().url().optional(),
|
|
62
|
+
figmaProps: z.record(figmaPropMappingSchema).optional(),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export const fragmentUsageSchema = z.object({
|
|
66
|
+
when: z.array(z.string()),
|
|
67
|
+
whenNot: z.array(z.string()),
|
|
68
|
+
guidelines: z.array(z.string()).optional(),
|
|
69
|
+
accessibility: z.array(z.string()).optional(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export const propTypeSchema: z.ZodType<string> = z.enum([
|
|
73
|
+
'string',
|
|
74
|
+
'number',
|
|
75
|
+
'boolean',
|
|
76
|
+
'enum',
|
|
77
|
+
'function',
|
|
78
|
+
'node',
|
|
79
|
+
'element',
|
|
80
|
+
'object',
|
|
81
|
+
'array',
|
|
82
|
+
'union',
|
|
83
|
+
'custom',
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
export const propDefinitionSchema = z.object({
|
|
87
|
+
type: propTypeSchema,
|
|
88
|
+
values: z.array(z.string()).readonly().optional(),
|
|
89
|
+
default: z.unknown().optional(),
|
|
90
|
+
description: z.string().optional(),
|
|
91
|
+
required: z.boolean().optional(),
|
|
92
|
+
constraints: z.array(z.string()).optional(),
|
|
93
|
+
typeDetails: z.record(z.unknown()).optional(),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export const relationshipTypeSchema = z.enum([
|
|
97
|
+
'alternative',
|
|
98
|
+
'sibling',
|
|
99
|
+
'parent',
|
|
100
|
+
'child',
|
|
101
|
+
'composition',
|
|
102
|
+
'complementary',
|
|
103
|
+
'used-by',
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
export const componentRelationSchema = z.object({
|
|
107
|
+
component: z.string().min(1),
|
|
108
|
+
relationship: relationshipTypeSchema,
|
|
109
|
+
note: z.string().min(1),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
export const fragmentVariantSchema = z.object({
|
|
113
|
+
name: z.string().min(1),
|
|
114
|
+
description: z.string().min(1),
|
|
115
|
+
render: z.function().returns(z.unknown()),
|
|
116
|
+
code: z.string().optional(),
|
|
117
|
+
figma: z.string().url().optional(),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Schema for banned patterns in codebase
|
|
122
|
+
*/
|
|
123
|
+
export const fragmentBanSchema = z.object({
|
|
124
|
+
pattern: z.string().min(1),
|
|
125
|
+
message: z.string().min(1),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Schema for agent-optimized contract metadata
|
|
130
|
+
*/
|
|
131
|
+
export const fragmentContractSchema = z.object({
|
|
132
|
+
propsSummary: z.array(z.string()).optional(),
|
|
133
|
+
a11yRules: z.array(z.string()).optional(),
|
|
134
|
+
bans: z.array(fragmentBanSchema).optional(),
|
|
135
|
+
scenarioTags: z.array(z.string()).optional(),
|
|
136
|
+
performanceBudget: z.number().positive().optional(),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Schema for provenance tracking of generated fragments
|
|
141
|
+
*/
|
|
142
|
+
export const fragmentGeneratedSchema = z.object({
|
|
143
|
+
source: z.enum(['storybook', 'manual', 'ai']),
|
|
144
|
+
sourceFile: z.string().optional(),
|
|
145
|
+
confidence: z.number().min(0).max(1).optional(),
|
|
146
|
+
timestamp: z.string().datetime().optional(),
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Schema for AI-specific metadata for playground context generation
|
|
151
|
+
*/
|
|
152
|
+
export const aiMetadataSchema = z.object({
|
|
153
|
+
compositionPattern: z.enum(['compound', 'simple', 'controlled', 'wrapper']).optional(),
|
|
154
|
+
subComponents: z.array(z.string()).optional(),
|
|
155
|
+
requiredChildren: z.array(z.string()).optional(),
|
|
156
|
+
commonPatterns: z.array(z.string()).optional(),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Schema for block definitions
|
|
161
|
+
*/
|
|
162
|
+
export const blockDefinitionSchema = z.object({
|
|
163
|
+
name: z.string().min(1),
|
|
164
|
+
description: z.string().min(1),
|
|
165
|
+
category: z.string().min(1),
|
|
166
|
+
components: z.array(z.string().min(1)).min(1),
|
|
167
|
+
code: z.string().min(1),
|
|
168
|
+
tags: z.array(z.string()).optional(),
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
export const fragmentDefinitionSchema = z.object({
|
|
172
|
+
component: z.any(), // Allow any component type (function, class, forwardRef, etc.)
|
|
173
|
+
meta: fragmentMetaSchema,
|
|
174
|
+
usage: fragmentUsageSchema,
|
|
175
|
+
props: z.record(propDefinitionSchema),
|
|
176
|
+
relations: z.array(componentRelationSchema).optional(),
|
|
177
|
+
variants: z.array(fragmentVariantSchema), // Allow empty variants array
|
|
178
|
+
contract: fragmentContractSchema.optional(),
|
|
179
|
+
ai: aiMetadataSchema.optional(),
|
|
180
|
+
_generated: fragmentGeneratedSchema.optional(),
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Config schema - validates required fields, passes through optional config objects.
|
|
185
|
+
* Type definitions are in types.ts - schema just ensures basic structure.
|
|
186
|
+
*/
|
|
187
|
+
export const fragmentsConfigSchema = z.object({
|
|
188
|
+
include: z.array(z.string()).min(1),
|
|
189
|
+
exclude: z.array(z.string()).optional(),
|
|
190
|
+
components: z.array(z.string()).optional(),
|
|
191
|
+
outFile: z.string().optional(),
|
|
192
|
+
framework: z.enum(['react', 'vue', 'svelte']).optional(),
|
|
193
|
+
figmaFile: z.string().url().optional(),
|
|
194
|
+
figmaToken: z.string().optional(),
|
|
195
|
+
screenshots: z.object({}).passthrough().optional(),
|
|
196
|
+
service: z.object({}).passthrough().optional(),
|
|
197
|
+
registry: z.object({}).passthrough().optional(),
|
|
198
|
+
tokens: z.object({
|
|
199
|
+
include: z.array(z.string()).min(1),
|
|
200
|
+
}).passthrough().optional(),
|
|
201
|
+
snippets: z.object({
|
|
202
|
+
mode: z.enum(['warn', 'error']).optional(),
|
|
203
|
+
scope: z.enum(['snippet', 'snippet+render']).optional(),
|
|
204
|
+
requireFullSnippet: z.boolean().optional(),
|
|
205
|
+
allowedExternalModules: z.array(z.string().min(1)).optional(),
|
|
206
|
+
}).optional(),
|
|
207
|
+
performance: z.union([
|
|
208
|
+
z.enum(['strict', 'standard', 'relaxed']),
|
|
209
|
+
z.object({
|
|
210
|
+
preset: z.enum(['strict', 'standard', 'relaxed']).optional(),
|
|
211
|
+
budgets: z.object({
|
|
212
|
+
bundleSize: z.number().positive().optional(),
|
|
213
|
+
}).optional(),
|
|
214
|
+
}),
|
|
215
|
+
]).optional(),
|
|
216
|
+
storybook: z.object({
|
|
217
|
+
exclude: z.array(z.string()).optional(),
|
|
218
|
+
include: z.array(z.string()).optional(),
|
|
219
|
+
excludeDeprecated: z.boolean().optional(),
|
|
220
|
+
excludeTests: z.boolean().optional(),
|
|
221
|
+
excludeSvgIcons: z.boolean().optional(),
|
|
222
|
+
excludeSubComponents: z.boolean().optional(),
|
|
223
|
+
}).optional(),
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @deprecated Use blockDefinitionSchema instead
|
|
228
|
+
*/
|
|
229
|
+
export const recipeDefinitionSchema = blockDefinitionSchema;
|