@decocms/bindings 1.0.1-alpha.4 → 1.0.2

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.
@@ -1,4 +1,4 @@
1
- import { z } from "zod";
1
+ import { z, type ZodType } from "zod";
2
2
  import type { ToolBinder } from "../core/binder";
3
3
 
4
4
  /**
@@ -23,6 +23,7 @@ import type { ToolBinder } from "../core/binder";
23
23
  export const BaseCollectionEntitySchema = z.object({
24
24
  id: z.string().describe("Unique identifier for the entity"),
25
25
  title: z.string().describe("Human-readable title for the entity"),
26
+ description: z.string().nullish().describe("Description of the entity"),
26
27
  created_at: z.string().datetime(),
27
28
  updated_at: z.string().datetime(),
28
29
  created_by: z.string().optional(),
@@ -44,22 +45,30 @@ const ComparisonExpressionSchema = z.object({
44
45
  });
45
46
 
46
47
  /**
47
- * Where expression schema for filtering
48
- * Supports TanStack DB predicate push-down patterns
48
+ * Where expression type for filtering
49
+ * Recursive to allow nested logical operators
49
50
  */
50
- export const WhereExpressionSchema = z.union([
51
- ComparisonExpressionSchema,
52
- z.object({
53
- operator: z.enum(["and", "or", "not"]),
54
- conditions: z.array(ComparisonExpressionSchema),
55
- }),
56
- ]);
51
+ export type WhereExpression =
52
+ | z.infer<typeof ComparisonExpressionSchema>
53
+ | {
54
+ operator: "and" | "or" | "not";
55
+ conditions: WhereExpression[];
56
+ };
57
57
 
58
58
  /**
59
- * Where expression type for filtering
60
- * Derived from WhereExpressionSchema
59
+ * Where expression schema for filtering
60
+ * Supports TanStack DB predicate push-down patterns
61
+ * Recursive to allow nested logical operators
61
62
  */
62
- export type WhereExpression = z.infer<typeof WhereExpressionSchema>;
63
+ export const WhereExpressionSchema: z.ZodType<WhereExpression> = z.lazy(() =>
64
+ z.union([
65
+ ComparisonExpressionSchema,
66
+ z.object({
67
+ operator: z.enum(["and", "or", "not"]),
68
+ conditions: z.array(WhereExpressionSchema),
69
+ }),
70
+ ]),
71
+ );
63
72
 
64
73
  /**
65
74
  * Order by expression for sorting
@@ -139,14 +148,14 @@ export function createCollectionGetOutputSchema<T extends z.ZodTypeAny>(
139
148
  /**
140
149
  * Factory function to create insert input schema
141
150
  */
142
- export function createCollectionInsertInputSchema<T extends z.ZodTypeAny>(
143
- entitySchema: T,
144
- ) {
151
+ export function createCollectionInsertInputSchema<
152
+ T extends z.ZodObject<z.ZodRawShape>,
153
+ >(entitySchema: T) {
145
154
  // Remove id field since it may be auto-generated by the server
146
155
  return z.object({
147
- data: entitySchema.describe(
148
- "Data for the new entity (id may be auto-generated)",
149
- ),
156
+ data: entitySchema
157
+ .partial()
158
+ .describe("Data for the new entity (id may be auto-generated)"),
150
159
  });
151
160
  }
152
161
 
@@ -251,7 +260,7 @@ export interface CollectionBindingOptions {
251
260
  * ```
252
261
  */
253
262
  export function createCollectionBindings<
254
- TEntitySchema extends BaseCollectionEntitySchemaType,
263
+ TEntitySchema extends z.ZodObject<z.ZodRawShape>,
255
264
  TName extends string,
256
265
  >(
257
266
  collectionName: TName,
@@ -265,16 +274,12 @@ export function createCollectionBindings<
265
274
  {
266
275
  name: `COLLECTION_${upperName}_LIST` as const,
267
276
  inputSchema: CollectionListInputSchema,
268
- outputSchema: createCollectionListOutputSchema(entitySchema) as z.ZodType<
269
- CollectionListOutput<z.infer<TEntitySchema>>
270
- >,
277
+ outputSchema: createCollectionListOutputSchema(entitySchema),
271
278
  },
272
279
  {
273
280
  name: `COLLECTION_${upperName}_GET` as const,
274
281
  inputSchema: CollectionGetInputSchema,
275
- outputSchema: createCollectionGetOutputSchema(entitySchema) as z.ZodType<
276
- CollectionGetOutput<z.infer<TEntitySchema>>
277
- >,
282
+ outputSchema: createCollectionGetOutputSchema(entitySchema),
278
283
  },
279
284
  ];
280
285
 
@@ -283,108 +288,88 @@ export function createCollectionBindings<
283
288
  bindings.push(
284
289
  {
285
290
  name: `COLLECTION_${upperName}_CREATE` as const,
286
- inputSchema: createCollectionInsertInputSchema(
287
- entitySchema,
288
- ) as unknown as z.ZodType<
289
- CollectionInsertInput<z.infer<TEntitySchema>>
290
- >,
291
- outputSchema: createCollectionInsertOutputSchema(
292
- entitySchema,
293
- ) as unknown as z.ZodType<
294
- CollectionInsertOutput<z.infer<TEntitySchema>>
295
- >,
291
+ inputSchema: createCollectionInsertInputSchema(entitySchema),
292
+ outputSchema: createCollectionInsertOutputSchema(entitySchema),
296
293
  opt: true,
297
294
  },
298
295
  {
299
296
  name: `COLLECTION_${upperName}_UPDATE` as const,
300
- inputSchema: createCollectionUpdateInputSchema(
301
- entitySchema,
302
- ) as unknown as z.ZodType<
303
- CollectionUpdateInput<z.infer<TEntitySchema>>
304
- >,
305
- outputSchema: createCollectionUpdateOutputSchema(
306
- entitySchema,
307
- ) as unknown as z.ZodType<
308
- CollectionUpdateOutput<z.infer<TEntitySchema>>
309
- >,
297
+ inputSchema: createCollectionUpdateInputSchema(entitySchema),
298
+ outputSchema: createCollectionUpdateOutputSchema(entitySchema),
310
299
  opt: true,
311
300
  },
312
301
  {
313
302
  name: `COLLECTION_${upperName}_DELETE` as const,
314
303
  inputSchema: CollectionDeleteInputSchema,
315
- outputSchema: createCollectionDeleteOutputSchema(
316
- entitySchema,
317
- ) as unknown as z.ZodType<
318
- CollectionDeleteOutput<z.infer<TEntitySchema>>
319
- >,
304
+ outputSchema: createCollectionDeleteOutputSchema(entitySchema),
320
305
  opt: true,
321
306
  },
322
307
  );
323
308
  }
324
309
 
325
- return bindings as unknown as readonly CollectionBinding<
310
+ return bindings as unknown as CollectionBinding<
326
311
  TEntitySchema,
327
312
  Uppercase<TName>
328
- >[number][];
313
+ >;
329
314
  }
330
315
 
331
- export type ReadOnlyCollectionBinding<
332
- TEntitySchema extends BaseCollectionEntitySchemaType,
333
- TUpperName extends Uppercase<string> = Uppercase<string>,
334
- > = [
335
- {
336
- name: `COLLECTION_${TUpperName}_LIST`;
337
- inputSchema: typeof CollectionListInputSchema;
338
- outputSchema: z.ZodType<CollectionListOutput<z.infer<TEntitySchema>>>;
339
- },
340
- {
341
- name: `COLLECTION_${TUpperName}_GET`;
342
- inputSchema: typeof CollectionGetInputSchema;
343
- outputSchema: z.ZodType<CollectionGetOutput<z.infer<TEntitySchema>>>;
344
- },
345
- ];
316
+ // Helper type for tool definition
317
+ type ToolBinding<
318
+ Name extends string,
319
+ Input,
320
+ Output,
321
+ Opt extends boolean = false,
322
+ > = {
323
+ name: Name;
324
+ inputSchema: ZodType<Input>;
325
+ outputSchema: ZodType<Output>;
326
+ } & (Opt extends true ? { opt: true } : unknown);
327
+
346
328
  /**
347
329
  * Type helper to extract the collection binding type
348
330
  */
349
331
  export type CollectionBinding<
350
- TEntitySchema extends BaseCollectionEntitySchemaType,
332
+ TEntitySchema,
351
333
  TUpperName extends Uppercase<string> = Uppercase<string>,
334
+ TEntity = TEntitySchema extends z.AnyZodObject
335
+ ? z.infer<TEntitySchema>
336
+ : TEntitySchema,
352
337
  > = [
353
- {
354
- name: `COLLECTION_${TUpperName}_LIST`;
355
- inputSchema: typeof CollectionListInputSchema;
356
- outputSchema: z.ZodType<CollectionListOutput<z.infer<TEntitySchema>>>;
357
- },
358
- {
359
- name: `COLLECTION_${TUpperName}_GET`;
360
- inputSchema: typeof CollectionGetInputSchema;
361
- outputSchema: z.ZodType<CollectionGetOutput<z.infer<TEntitySchema>>>;
362
- },
363
- {
364
- name: `COLLECTION_${TUpperName}_CREATE`;
365
- inputSchema: z.ZodType<CollectionInsertInput<z.infer<TEntitySchema>>>;
366
- outputSchema: z.ZodType<CollectionInsertOutput<z.infer<TEntitySchema>>>;
367
- opt: true;
368
- },
369
- {
370
- name: `COLLECTION_${TUpperName}_UPDATE`;
371
- inputSchema: z.ZodType<CollectionUpdateInput<z.infer<TEntitySchema>>>;
372
- outputSchema: z.ZodType<CollectionUpdateOutput<z.infer<TEntitySchema>>>;
373
- opt: true;
374
- },
375
- {
376
- name: `COLLECTION_${TUpperName}_DELETE`;
377
- inputSchema: typeof CollectionDeleteInputSchema;
378
- outputSchema: z.ZodType<CollectionDeleteOutput<z.infer<TEntitySchema>>>;
379
- opt: true;
380
- },
338
+ ToolBinding<
339
+ `COLLECTION_${TUpperName}_LIST`,
340
+ CollectionListInput,
341
+ CollectionListOutput<TEntity>
342
+ >,
343
+ ToolBinding<
344
+ `COLLECTION_${TUpperName}_GET`,
345
+ CollectionGetInput,
346
+ CollectionGetOutput<TEntity>
347
+ >,
348
+ ToolBinding<
349
+ `COLLECTION_${TUpperName}_CREATE`,
350
+ CollectionInsertInput<TEntity>,
351
+ CollectionInsertOutput<TEntity>,
352
+ true
353
+ >,
354
+ ToolBinding<
355
+ `COLLECTION_${TUpperName}_UPDATE`,
356
+ CollectionUpdateInput<TEntity>,
357
+ CollectionUpdateOutput<TEntity>,
358
+ true
359
+ >,
360
+ ToolBinding<
361
+ `COLLECTION_${TUpperName}_DELETE`,
362
+ CollectionDeleteInput,
363
+ CollectionDeleteOutput<TEntity>,
364
+ true
365
+ >,
381
366
  ];
382
367
 
383
368
  /**
384
369
  * Type helper to extract tool names from a collection binding
385
370
  */
386
371
  export type CollectionTools<
387
- TEntitySchema extends BaseCollectionEntitySchemaType,
372
+ TEntitySchema extends z.AnyZodObject,
388
373
  TUpperName extends Uppercase<string> = Uppercase<string>,
389
374
  > = CollectionBinding<TEntitySchema, TUpperName>[number]["name"];
390
375
 
@@ -398,7 +383,7 @@ export type OrderByExpression = z.infer<typeof OrderByExpressionSchema>;
398
383
  * Type helper for insert input with generic item type
399
384
  */
400
385
  export type CollectionInsertInput<T> = {
401
- data: T;
386
+ data: Partial<T>;
402
387
  };
403
388
 
404
389
  /**