ai-props 2.1.3 → 2.4.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/.dev.vars +2 -0
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +20 -0
- package/README.md +2 -0
- package/dist/ai.d.ts.map +1 -1
- package/dist/ai.js +4 -4
- package/dist/ai.js.map +1 -1
- package/dist/cascade.d.ts +329 -0
- package/dist/cascade.d.ts.map +1 -0
- package/dist/cascade.js +522 -0
- package/dist/cascade.js.map +1 -0
- package/dist/client.d.ts +233 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +191 -0
- package/dist/client.js.map +1 -0
- package/dist/durable-cascade.d.ts +280 -0
- package/dist/durable-cascade.d.ts.map +1 -0
- package/dist/durable-cascade.js +469 -0
- package/dist/durable-cascade.js.map +1 -0
- package/dist/event-bridge.d.ts +257 -0
- package/dist/event-bridge.d.ts.map +1 -0
- package/dist/event-bridge.js +317 -0
- package/dist/event-bridge.js.map +1 -0
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +12 -6
- package/dist/generate.js.map +1 -1
- package/dist/hoc.d.ts.map +1 -1
- package/dist/hoc.js +13 -13
- package/dist/hoc.js.map +1 -1
- package/dist/hono-jsx.d.ts +208 -0
- package/dist/hono-jsx.d.ts.map +1 -0
- package/dist/hono-jsx.js +459 -0
- package/dist/hono-jsx.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/mdx-types.d.ts +152 -0
- package/dist/mdx-types.d.ts.map +1 -0
- package/dist/mdx-types.js +9 -0
- package/dist/mdx-types.js.map +1 -0
- package/dist/mdx-utils.d.ts +106 -0
- package/dist/mdx-utils.d.ts.map +1 -0
- package/dist/mdx-utils.js +384 -0
- package/dist/mdx-utils.js.map +1 -0
- package/dist/mdx.d.ts +230 -0
- package/dist/mdx.d.ts.map +1 -0
- package/dist/mdx.js +820 -0
- package/dist/mdx.js.map +1 -0
- package/dist/rpc.d.ts +313 -0
- package/dist/rpc.d.ts.map +1 -0
- package/dist/rpc.js +359 -0
- package/dist/rpc.js.map +1 -0
- package/dist/streaming.d.ts +199 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +402 -0
- package/dist/streaming.js.map +1 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +11 -13
- package/dist/validate.js.map +1 -1
- package/dist/worker.d.ts +270 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +405 -0
- package/dist/worker.js.map +1 -0
- package/package.json +39 -13
- package/src/ai.ts +12 -31
- package/src/cascade.ts +795 -0
- package/src/client.ts +440 -0
- package/src/durable-cascade.ts +743 -0
- package/src/event-bridge.ts +478 -0
- package/src/generate.ts +14 -12
- package/src/hoc.ts +15 -19
- package/src/hono-jsx.ts +675 -0
- package/src/index.ts +30 -0
- package/src/mdx-types.ts +169 -0
- package/src/mdx-utils.ts +437 -0
- package/src/mdx.ts +1008 -0
- package/src/rpc.ts +614 -0
- package/src/streaming.ts +618 -0
- package/src/validate.ts +15 -29
- package/src/worker.ts +547 -0
- package/test/cascade.test.ts +338 -0
- package/test/durable-cascade.test.ts +319 -0
- package/test/event-bridge.test.ts +351 -0
- package/test/generate.test.ts +6 -16
- package/test/mdx.test.ts +817 -0
- package/test/worker/capnweb-rpc.test.ts +1084 -0
- package/test/worker/full-flow.integration.test.ts +1463 -0
- package/test/worker/hono-jsx.test.ts +1258 -0
- package/test/worker/mdx-parsing.test.ts +1148 -0
- package/test/worker/setup.ts +56 -0
- package/test/worker.test.ts +595 -0
- package/tsconfig.json +2 -1
- package/vitest.config.js +6 -0
- package/vitest.config.ts +15 -1
- package/vitest.workers.config.ts +58 -0
- package/wrangler.jsonc +27 -0
- package/LICENSE +0 -21
- package/src/ai.js +0 -198
- package/src/cache.js +0 -182
- package/src/generate.js +0 -220
- package/src/hoc.js +0 -235
- package/src/index.js +0 -20
- package/src/types.js +0 -6
- package/src/validate.js +0 -252
package/src/hoc.js
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Higher-Order Component (HOC) for React components
|
|
3
|
-
*
|
|
4
|
-
* Provides withAIProps HOC for wrapping React components
|
|
5
|
-
* with AI-powered prop generation.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
import { generateProps, mergeWithGenerated } from './generate.js';
|
|
10
|
-
/**
|
|
11
|
-
* Create props wrapper that can be used with any component system
|
|
12
|
-
*
|
|
13
|
-
* This is framework-agnostic and returns the enhanced props.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* const enhancer = createPropsEnhancer({
|
|
18
|
-
* schema: {
|
|
19
|
-
* title: 'Page title',
|
|
20
|
-
* description: 'Page description',
|
|
21
|
-
* },
|
|
22
|
-
* defaults: {
|
|
23
|
-
* title: 'Default Title',
|
|
24
|
-
* },
|
|
25
|
-
* })
|
|
26
|
-
*
|
|
27
|
-
* // Use with any component
|
|
28
|
-
* const props = await enhancer({ description: 'My page' })
|
|
29
|
-
* // { title: 'Default Title', description: 'My page' }
|
|
30
|
-
*
|
|
31
|
-
* const generatedProps = await enhancer({})
|
|
32
|
-
* // { title: 'AI-generated title', description: 'AI-generated description' }
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export function createPropsEnhancer(options) {
|
|
36
|
-
const { schema, defaults = {}, required = [], exclude = [], config = {}, fallback } = options;
|
|
37
|
-
return async (partialProps) => {
|
|
38
|
-
try {
|
|
39
|
-
// Merge with defaults
|
|
40
|
-
const propsWithDefaults = { ...defaults, ...partialProps };
|
|
41
|
-
// Check required props
|
|
42
|
-
const missingRequired = required.filter(key => propsWithDefaults[key] === undefined);
|
|
43
|
-
if (missingRequired.length > 0) {
|
|
44
|
-
throw new Error(`Missing required props: ${missingRequired.join(', ')}`);
|
|
45
|
-
}
|
|
46
|
-
// Filter out excluded props from schema
|
|
47
|
-
const filteredSchema = filterSchemaKeys(schema, exclude);
|
|
48
|
-
// Generate missing props
|
|
49
|
-
return await mergeWithGenerated(filteredSchema, propsWithDefaults, {
|
|
50
|
-
model: config.model,
|
|
51
|
-
system: config.system,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
if (fallback) {
|
|
56
|
-
return { ...defaults, ...fallback, ...partialProps };
|
|
57
|
-
}
|
|
58
|
-
throw error;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Filter schema to exclude certain keys
|
|
64
|
-
*/
|
|
65
|
-
function filterSchemaKeys(schema, exclude) {
|
|
66
|
-
if (typeof schema === 'string' || exclude.length === 0) {
|
|
67
|
-
return schema;
|
|
68
|
-
}
|
|
69
|
-
const filtered = {};
|
|
70
|
-
for (const [key, value] of Object.entries(schema)) {
|
|
71
|
-
if (!exclude.includes(key)) {
|
|
72
|
-
filtered[key] = value;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return filtered;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Create an async props provider
|
|
79
|
-
*
|
|
80
|
-
* Returns a function that generates props on each call.
|
|
81
|
-
* Useful for SSR and static generation.
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
* ```ts
|
|
85
|
-
* const getPageProps = createAsyncPropsProvider({
|
|
86
|
-
* schema: {
|
|
87
|
-
* title: 'SEO-optimized page title',
|
|
88
|
-
* meta: { description: 'Meta description' },
|
|
89
|
-
* },
|
|
90
|
-
* })
|
|
91
|
-
*
|
|
92
|
-
* // In getStaticProps or getServerSideProps
|
|
93
|
-
* export async function getStaticProps() {
|
|
94
|
-
* const props = await getPageProps({ slug: 'about' })
|
|
95
|
-
* return { props }
|
|
96
|
-
* }
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export function createAsyncPropsProvider(options) {
|
|
100
|
-
const enhancer = createPropsEnhancer(options);
|
|
101
|
-
return {
|
|
102
|
-
/**
|
|
103
|
-
* Get props with AI generation
|
|
104
|
-
*/
|
|
105
|
-
getProps: enhancer,
|
|
106
|
-
/**
|
|
107
|
-
* Get props for multiple items
|
|
108
|
-
*/
|
|
109
|
-
getManyProps: async (contexts) => {
|
|
110
|
-
return Promise.all(contexts.map(enhancer));
|
|
111
|
-
},
|
|
112
|
-
/**
|
|
113
|
-
* Get props with caching hint
|
|
114
|
-
*/
|
|
115
|
-
getCachedProps: async (context, revalidate) => {
|
|
116
|
-
const props = await enhancer(context);
|
|
117
|
-
return { props, revalidate };
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Create a props transformer
|
|
123
|
-
*
|
|
124
|
-
* Transforms existing props by filling in missing values.
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```ts
|
|
128
|
-
* const transformUserProps = createPropsTransformer({
|
|
129
|
-
* schema: {
|
|
130
|
-
* displayName: 'Display name for the user',
|
|
131
|
-
* initials: 'User initials (2 letters)',
|
|
132
|
-
* },
|
|
133
|
-
* })
|
|
134
|
-
*
|
|
135
|
-
* const user = await transformUserProps({
|
|
136
|
-
* username: 'johndoe',
|
|
137
|
-
* email: 'john@example.com',
|
|
138
|
-
* })
|
|
139
|
-
* // { username: 'johndoe', email: '...', displayName: 'John Doe', initials: 'JD' }
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
export function createPropsTransformer(options) {
|
|
143
|
-
const { schema, transform, config = {} } = options;
|
|
144
|
-
return async (input) => {
|
|
145
|
-
const result = await generateProps({
|
|
146
|
-
schema,
|
|
147
|
-
context: input,
|
|
148
|
-
model: config.model,
|
|
149
|
-
system: config.system,
|
|
150
|
-
});
|
|
151
|
-
const generated = result.props;
|
|
152
|
-
if (transform) {
|
|
153
|
-
return { ...input, ...transform(input, generated) };
|
|
154
|
-
}
|
|
155
|
-
return { ...input, ...generated };
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Create a conditional props generator
|
|
160
|
-
*
|
|
161
|
-
* Only generates props when a condition is met.
|
|
162
|
-
*
|
|
163
|
-
* @example
|
|
164
|
-
* ```ts
|
|
165
|
-
* const maybeGenerateProps = createConditionalGenerator({
|
|
166
|
-
* schema: { summary: 'Article summary' },
|
|
167
|
-
* condition: (props) => !props.summary && props.content?.length > 100,
|
|
168
|
-
* })
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
export function createConditionalGenerator(options) {
|
|
172
|
-
const { schema, condition, config = {} } = options;
|
|
173
|
-
return async (props) => {
|
|
174
|
-
if (!condition(props)) {
|
|
175
|
-
return props;
|
|
176
|
-
}
|
|
177
|
-
return mergeWithGenerated(schema, props, {
|
|
178
|
-
model: config.model,
|
|
179
|
-
system: config.system,
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Batch props generator for list rendering
|
|
185
|
-
*
|
|
186
|
-
* Efficiently generates props for multiple items.
|
|
187
|
-
*
|
|
188
|
-
* @example
|
|
189
|
-
* ```ts
|
|
190
|
-
* const batchGenerator = createBatchGenerator({
|
|
191
|
-
* schema: { title: 'Item title', description: 'Item description' },
|
|
192
|
-
* })
|
|
193
|
-
*
|
|
194
|
-
* const items = await batchGenerator.generate([
|
|
195
|
-
* { id: 1, category: 'tech' },
|
|
196
|
-
* { id: 2, category: 'science' },
|
|
197
|
-
* { id: 3, category: 'art' },
|
|
198
|
-
* ])
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
|
-
export function createBatchGenerator(options) {
|
|
202
|
-
const { schema, concurrency = 3, config = {} } = options;
|
|
203
|
-
return {
|
|
204
|
-
/**
|
|
205
|
-
* Generate props for multiple items
|
|
206
|
-
*/
|
|
207
|
-
generate: async (items) => {
|
|
208
|
-
const results = [];
|
|
209
|
-
// Process in batches based on concurrency
|
|
210
|
-
for (let i = 0; i < items.length; i += concurrency) {
|
|
211
|
-
const batch = items.slice(i, i + concurrency);
|
|
212
|
-
const batchResults = await Promise.all(batch.map(item => mergeWithGenerated(schema, item, {
|
|
213
|
-
model: config.model,
|
|
214
|
-
system: config.system,
|
|
215
|
-
})));
|
|
216
|
-
results.push(...batchResults);
|
|
217
|
-
}
|
|
218
|
-
return results;
|
|
219
|
-
},
|
|
220
|
-
/**
|
|
221
|
-
* Generate props one at a time (for rate limiting)
|
|
222
|
-
*/
|
|
223
|
-
generateSequential: async (items) => {
|
|
224
|
-
const results = [];
|
|
225
|
-
for (const item of items) {
|
|
226
|
-
const result = await mergeWithGenerated(schema, item, {
|
|
227
|
-
model: config.model,
|
|
228
|
-
system: config.system,
|
|
229
|
-
});
|
|
230
|
-
results.push(result);
|
|
231
|
-
}
|
|
232
|
-
return results;
|
|
233
|
-
},
|
|
234
|
-
};
|
|
235
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ai-props - AI-powered props for intelligent component properties
|
|
3
|
-
*
|
|
4
|
-
* This package provides utilities for automatically generating
|
|
5
|
-
* component props using AI based on schema definitions.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
// Export types
|
|
10
|
-
export * from './types.js';
|
|
11
|
-
// Export core functionality
|
|
12
|
-
export { AI, createAIComponent, definePropsSchema, createComponentFactory, composeAIComponents, } from './ai.js';
|
|
13
|
-
// Export generation functions
|
|
14
|
-
export { generateProps, getPropsSync, prefetchProps, generatePropsMany, mergeWithGenerated, configureAIProps, getConfig, resetConfig, } from './generate.js';
|
|
15
|
-
// Export cache utilities
|
|
16
|
-
export { MemoryPropsCache, LRUPropsCache, createCacheKey, getDefaultCache, configureCache, clearCache, DEFAULT_CACHE_TTL, } from './cache.js';
|
|
17
|
-
// Export HOC and enhancers
|
|
18
|
-
export { createPropsEnhancer, createAsyncPropsProvider, createPropsTransformer, createConditionalGenerator, createBatchGenerator, } from './hoc.js';
|
|
19
|
-
// Export validation utilities
|
|
20
|
-
export { validateProps, hasRequiredProps, getMissingProps, isComplete, getMissingFromSchema, sanitizeProps, mergeWithDefaults, createValidator, assertValidProps, } from './validate.js';
|
package/src/types.js
DELETED
package/src/validate.js
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities for ai-props
|
|
3
|
-
*
|
|
4
|
-
* Provides prop validation against schemas.
|
|
5
|
-
*
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Validate props against a schema
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* const result = validateProps(
|
|
14
|
-
* { name: 'John', age: '25' }, // props
|
|
15
|
-
* { name: 'Name', age: 'Age (number)' } // schema
|
|
16
|
-
* )
|
|
17
|
-
*
|
|
18
|
-
* if (!result.valid) {
|
|
19
|
-
* console.log(result.errors)
|
|
20
|
-
* // [{ path: 'age', message: 'Expected number, got string' }]
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export function validateProps(props, schema) {
|
|
25
|
-
const errors = [];
|
|
26
|
-
if (typeof schema === 'string') {
|
|
27
|
-
// Simple string schema - just check if value exists
|
|
28
|
-
if (!props.value) {
|
|
29
|
-
errors.push({
|
|
30
|
-
path: 'value',
|
|
31
|
-
message: 'Value is required',
|
|
32
|
-
expected: 'string',
|
|
33
|
-
received: props.value,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return { valid: errors.length === 0, errors };
|
|
37
|
-
}
|
|
38
|
-
// Object schema - validate each key
|
|
39
|
-
for (const [key, schemaDef] of Object.entries(schema)) {
|
|
40
|
-
const value = props[key];
|
|
41
|
-
const keyErrors = validateValue(key, value, schemaDef);
|
|
42
|
-
errors.push(...keyErrors);
|
|
43
|
-
}
|
|
44
|
-
return { valid: errors.length === 0, errors };
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Validate a single value against a schema definition
|
|
48
|
-
*/
|
|
49
|
-
function validateValue(path, value, schema) {
|
|
50
|
-
const errors = [];
|
|
51
|
-
// String schema with type hint
|
|
52
|
-
if (typeof schema === 'string') {
|
|
53
|
-
const expectedType = extractTypeFromSchema(schema);
|
|
54
|
-
if (value === undefined || value === null) {
|
|
55
|
-
// Optional unless marked required
|
|
56
|
-
return errors;
|
|
57
|
-
}
|
|
58
|
-
if (expectedType && !checkType(value, expectedType)) {
|
|
59
|
-
errors.push({
|
|
60
|
-
path,
|
|
61
|
-
message: `Expected ${expectedType}, got ${typeof value}`,
|
|
62
|
-
expected: expectedType,
|
|
63
|
-
received: value,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
return errors;
|
|
67
|
-
}
|
|
68
|
-
// Array schema
|
|
69
|
-
if (Array.isArray(schema)) {
|
|
70
|
-
if (!Array.isArray(value)) {
|
|
71
|
-
if (value !== undefined && value !== null) {
|
|
72
|
-
errors.push({
|
|
73
|
-
path,
|
|
74
|
-
message: `Expected array, got ${typeof value}`,
|
|
75
|
-
expected: 'array',
|
|
76
|
-
received: value,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
return errors;
|
|
80
|
-
}
|
|
81
|
-
// Validate array items if schema has item definition
|
|
82
|
-
if (schema.length > 0) {
|
|
83
|
-
const itemSchema = schema[0];
|
|
84
|
-
for (let i = 0; i < value.length; i++) {
|
|
85
|
-
const itemErrors = validateValue(`${path}[${i}]`, value[i], itemSchema);
|
|
86
|
-
errors.push(...itemErrors);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return errors;
|
|
90
|
-
}
|
|
91
|
-
// Object schema
|
|
92
|
-
if (typeof schema === 'object' && schema !== null) {
|
|
93
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
94
|
-
if (value !== undefined && value !== null) {
|
|
95
|
-
errors.push({
|
|
96
|
-
path,
|
|
97
|
-
message: `Expected object, got ${Array.isArray(value) ? 'array' : typeof value}`,
|
|
98
|
-
expected: 'object',
|
|
99
|
-
received: value,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
return errors;
|
|
103
|
-
}
|
|
104
|
-
// Recursively validate nested object
|
|
105
|
-
for (const [key, nestedSchema] of Object.entries(schema)) {
|
|
106
|
-
const nestedValue = value[key];
|
|
107
|
-
const nestedErrors = validateValue(`${path}.${key}`, nestedValue, nestedSchema);
|
|
108
|
-
errors.push(...nestedErrors);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return errors;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Extract type hint from schema string
|
|
115
|
-
* e.g., "Age (number)" -> "number"
|
|
116
|
-
*/
|
|
117
|
-
function extractTypeFromSchema(schema) {
|
|
118
|
-
const match = schema.match(/\((\w+)\)\s*$/);
|
|
119
|
-
if (match) {
|
|
120
|
-
return match[1].toLowerCase();
|
|
121
|
-
}
|
|
122
|
-
// Check for enum syntax
|
|
123
|
-
if (schema.includes(' | ')) {
|
|
124
|
-
return 'enum';
|
|
125
|
-
}
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Check if a value matches an expected type
|
|
130
|
-
*/
|
|
131
|
-
function checkType(value, expectedType) {
|
|
132
|
-
switch (expectedType) {
|
|
133
|
-
case 'string':
|
|
134
|
-
return typeof value === 'string';
|
|
135
|
-
case 'number':
|
|
136
|
-
case 'integer':
|
|
137
|
-
return typeof value === 'number';
|
|
138
|
-
case 'boolean':
|
|
139
|
-
return typeof value === 'boolean';
|
|
140
|
-
case 'array':
|
|
141
|
-
return Array.isArray(value);
|
|
142
|
-
case 'object':
|
|
143
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
144
|
-
case 'date':
|
|
145
|
-
return typeof value === 'string' || value instanceof Date;
|
|
146
|
-
case 'enum':
|
|
147
|
-
return typeof value === 'string';
|
|
148
|
-
default:
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Check if all required props are present
|
|
154
|
-
*/
|
|
155
|
-
export function hasRequiredProps(props, required) {
|
|
156
|
-
return required.every(key => props[key] !== undefined);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Get list of missing required props
|
|
160
|
-
*/
|
|
161
|
-
export function getMissingProps(props, required) {
|
|
162
|
-
return required.filter(key => props[key] === undefined);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Check if props are complete according to schema
|
|
166
|
-
*/
|
|
167
|
-
export function isComplete(props, schema) {
|
|
168
|
-
if (typeof schema === 'string') {
|
|
169
|
-
return props.value !== undefined;
|
|
170
|
-
}
|
|
171
|
-
return Object.keys(schema).every(key => props[key] !== undefined);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Get list of missing props according to schema
|
|
175
|
-
*/
|
|
176
|
-
export function getMissingFromSchema(props, schema) {
|
|
177
|
-
if (typeof schema === 'string') {
|
|
178
|
-
return props.value === undefined ? ['value'] : [];
|
|
179
|
-
}
|
|
180
|
-
return Object.keys(schema).filter(key => props[key] === undefined);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Sanitize props by removing extra keys not in schema
|
|
184
|
-
*/
|
|
185
|
-
export function sanitizeProps(props, schema) {
|
|
186
|
-
if (typeof schema === 'string') {
|
|
187
|
-
return { value: props.value };
|
|
188
|
-
}
|
|
189
|
-
const schemaKeys = new Set(Object.keys(schema));
|
|
190
|
-
const sanitized = {};
|
|
191
|
-
for (const [key, value] of Object.entries(props)) {
|
|
192
|
-
if (schemaKeys.has(key)) {
|
|
193
|
-
sanitized[key] = value;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return sanitized;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Merge props with defaults, respecting schema types
|
|
200
|
-
*/
|
|
201
|
-
export function mergeWithDefaults(props, defaults, schema) {
|
|
202
|
-
const result = { ...defaults, ...props };
|
|
203
|
-
// Ensure types match schema
|
|
204
|
-
if (typeof schema !== 'string') {
|
|
205
|
-
for (const [key, schemaDef] of Object.entries(schema)) {
|
|
206
|
-
if (result[key] === undefined)
|
|
207
|
-
continue;
|
|
208
|
-
const expectedType = typeof schemaDef === 'string'
|
|
209
|
-
? extractTypeFromSchema(schemaDef)
|
|
210
|
-
: null;
|
|
211
|
-
if (expectedType) {
|
|
212
|
-
result[key] = coerceType(result[key], expectedType);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return result;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Attempt to coerce a value to an expected type
|
|
220
|
-
*/
|
|
221
|
-
function coerceType(value, expectedType) {
|
|
222
|
-
if (value === undefined || value === null)
|
|
223
|
-
return value;
|
|
224
|
-
switch (expectedType) {
|
|
225
|
-
case 'string':
|
|
226
|
-
return String(value);
|
|
227
|
-
case 'number':
|
|
228
|
-
return typeof value === 'number' ? value : Number(value);
|
|
229
|
-
case 'integer':
|
|
230
|
-
return typeof value === 'number' ? Math.floor(value) : parseInt(String(value), 10);
|
|
231
|
-
case 'boolean':
|
|
232
|
-
return Boolean(value);
|
|
233
|
-
default:
|
|
234
|
-
return value;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Create a props validator function
|
|
239
|
-
*/
|
|
240
|
-
export function createValidator(schema) {
|
|
241
|
-
return (props) => validateProps(props, schema);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Assert props are valid, throwing on error
|
|
245
|
-
*/
|
|
246
|
-
export function assertValidProps(props, schema) {
|
|
247
|
-
const result = validateProps(props, schema);
|
|
248
|
-
if (!result.valid) {
|
|
249
|
-
const messages = result.errors.map(e => `${e.path}: ${e.message}`).join(', ');
|
|
250
|
-
throw new Error(`Invalid props: ${messages}`);
|
|
251
|
-
}
|
|
252
|
-
}
|