@decocms/bindings 1.0.1-alpha.3 → 1.0.1

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,
@@ -261,19 +270,18 @@ export function createCollectionBindings<
261
270
  const upperName = collectionName.toUpperCase() as Uppercase<TName>;
262
271
  const readOnly = options?.readOnly ?? false;
263
272
 
264
- const bindings: CollectionBinding<TEntitySchema, Uppercase<TName>>[number][] =
265
- [
266
- {
267
- name: `COLLECTION_${upperName}_LIST` as const,
268
- inputSchema: CollectionListInputSchema,
269
- outputSchema: createCollectionListOutputSchema(entitySchema),
270
- },
271
- {
272
- name: `COLLECTION_${upperName}_GET` as const,
273
- inputSchema: CollectionGetInputSchema,
274
- outputSchema: createCollectionGetOutputSchema(entitySchema),
275
- },
276
- ];
273
+ const bindings: ToolBinder[] = [
274
+ {
275
+ name: `COLLECTION_${upperName}_LIST` as const,
276
+ inputSchema: CollectionListInputSchema,
277
+ outputSchema: createCollectionListOutputSchema(entitySchema),
278
+ },
279
+ {
280
+ name: `COLLECTION_${upperName}_GET` as const,
281
+ inputSchema: CollectionGetInputSchema,
282
+ outputSchema: createCollectionGetOutputSchema(entitySchema),
283
+ },
284
+ ];
277
285
 
278
286
  // Only include mutation operations if not read-only
279
287
  if (!readOnly) {
@@ -299,71 +307,69 @@ export function createCollectionBindings<
299
307
  );
300
308
  }
301
309
 
302
- return bindings satisfies readonly ToolBinder[];
310
+ return bindings as unknown as CollectionBinding<
311
+ TEntitySchema,
312
+ Uppercase<TName>
313
+ >;
303
314
  }
304
315
 
305
- export type ReadOnlyCollectionBinding<
306
- TEntitySchema extends BaseCollectionEntitySchemaType,
307
- TUpperName extends Uppercase<string> = Uppercase<string>,
308
- > = [
309
- {
310
- name: `COLLECTION_${TUpperName}_LIST`;
311
- inputSchema: typeof CollectionListInputSchema;
312
- outputSchema: ReturnType<
313
- typeof createCollectionListOutputSchema<TEntitySchema>
314
- >;
315
- },
316
- {
317
- name: `COLLECTION_${TUpperName}_GET`;
318
- inputSchema: typeof CollectionGetInputSchema;
319
- outputSchema: ReturnType<
320
- typeof createCollectionGetOutputSchema<TEntitySchema>
321
- >;
322
- },
323
- ];
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
+
324
328
  /**
325
329
  * Type helper to extract the collection binding type
326
330
  */
327
331
  export type CollectionBinding<
328
- TEntitySchema extends BaseCollectionEntitySchemaType,
332
+ TEntitySchema,
329
333
  TUpperName extends Uppercase<string> = Uppercase<string>,
334
+ TEntity = TEntitySchema extends z.AnyZodObject
335
+ ? z.infer<TEntitySchema>
336
+ : TEntitySchema,
330
337
  > = [
331
- ...ReadOnlyCollectionBinding<TEntitySchema, TUpperName>,
332
- {
333
- name: `COLLECTION_${TUpperName}_CREATE`;
334
- inputSchema: ReturnType<
335
- typeof createCollectionInsertInputSchema<TEntitySchema>
336
- >;
337
- outputSchema: ReturnType<
338
- typeof createCollectionInsertOutputSchema<TEntitySchema>
339
- >;
340
- opt: true;
341
- },
342
- {
343
- name: `COLLECTION_${TUpperName}_UPDATE`;
344
- inputSchema: ReturnType<
345
- typeof createCollectionUpdateInputSchema<TEntitySchema>
346
- >;
347
- outputSchema: ReturnType<
348
- typeof createCollectionUpdateOutputSchema<TEntitySchema>
349
- >;
350
- opt: true;
351
- },
352
- {
353
- name: `COLLECTION_${TUpperName}_DELETE`;
354
- inputSchema: typeof CollectionDeleteInputSchema;
355
- outputSchema: ReturnType<
356
- typeof createCollectionDeleteOutputSchema<TEntitySchema>
357
- >;
358
- opt: true;
359
- },
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
+ >,
360
366
  ];
361
367
 
362
368
  /**
363
369
  * Type helper to extract tool names from a collection binding
364
370
  */
365
371
  export type CollectionTools<
366
- TEntitySchema extends BaseCollectionEntitySchemaType,
372
+ TEntitySchema extends z.AnyZodObject,
367
373
  TUpperName extends Uppercase<string> = Uppercase<string>,
368
374
  > = CollectionBinding<TEntitySchema, TUpperName>[number]["name"];
369
375
 
@@ -373,6 +379,21 @@ export type CollectionGetInput = z.infer<typeof CollectionGetInputSchema>;
373
379
  export type CollectionDeleteInput = z.infer<typeof CollectionDeleteInputSchema>;
374
380
  export type OrderByExpression = z.infer<typeof OrderByExpressionSchema>;
375
381
 
382
+ /**
383
+ * Type helper for insert input with generic item type
384
+ */
385
+ export type CollectionInsertInput<T> = {
386
+ data: Partial<T>;
387
+ };
388
+
389
+ /**
390
+ * Type helper for update input with generic item type
391
+ */
392
+ export type CollectionUpdateInput<T> = {
393
+ id: string;
394
+ data: Partial<T>;
395
+ };
396
+
376
397
  /**
377
398
  * Type helper for list output with generic item type
378
399
  */