cogsbox-shape 0.5.201 → 0.5.202

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.
@@ -0,0 +1 @@
1
+ export declare function Example(): null;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Minimal end-to-end example — open and hover inside Example.
3
+ * Run from cogsbox-shape repo after: pnpm build
4
+ */
5
+ import { createCogsState } from "cogsbox-state";
6
+ import { createSchemaBox, s, schema } from "cogsbox-shape";
7
+ import { z } from "zod";
8
+ import { createShapePlugin } from "../plugin.js";
9
+ const taskItemSchema = z.object({
10
+ id: z.number(),
11
+ title: z.string(),
12
+ done: z.boolean(),
13
+ });
14
+ const taskManagerSchema = schema({
15
+ _tableName: "client",
16
+ tasks: s.clientInput({
17
+ value: [],
18
+ schema: z.array(taskItemSchema),
19
+ }),
20
+ filter: s.clientInput({
21
+ value: "all",
22
+ schema: z.string(),
23
+ }),
24
+ });
25
+ const demoBox = createSchemaBox({
26
+ taskManager: taskManagerSchema,
27
+ }, {});
28
+ const shapePlugin = createShapePlugin(demoBox);
29
+ const { useCogsState } = createCogsState({}, { plugins: [shapePlugin] });
30
+ export function Example() {
31
+ const taskManager = useCogsState("taskManager");
32
+ // hover ↓
33
+ const tasks = taskManager.tasks;
34
+ const filter = taskManager.filter;
35
+ void tasks;
36
+ void filter;
37
+ return null;
38
+ }
@@ -1 +1 @@
1
- export { createShapePlugin } from "./plugin.js";
1
+ export { createShapePlugin, type InferShapeBoxState, type ShapeSchemaBox, type ShapeSchemaBoxEntry, } from "./plugin.js";
@@ -1 +1 @@
1
- export { createShapePlugin } from "./plugin.js";
1
+ export { createShapePlugin, } from "./plugin.js";
@@ -1,11 +1,12 @@
1
- type ShapeBoxEntry = {
2
- generateDefaults: () => any;
3
- stateType?: any;
1
+ /** Minimal shape of a createSchemaBox entry — matches journalSchemaBox.journalTechnical etc. */
2
+ export type ShapeSchemaBoxEntry = {
3
+ /** Field-key → value map from DeriveStateType (not z.infer on a flattened client object). */
4
+ stateType: Record<string, unknown>;
5
+ generateDefaults: () => unknown;
4
6
  };
5
- type InferShapeEntryState<TEntry> = TEntry extends {
6
- stateType: infer S;
7
- } ? S : TEntry extends {
8
- generateDefaults: () => infer R;
9
- } ? R : never;
10
- export declare function createShapePlugin<const TBox extends Record<string, ShapeBoxEntry>>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", undefined, unknown, unknown, never, {}, false, false, false, false, false, true, { [K in keyof TBox]: InferShapeEntryState<TBox[K]>; }>;
11
- export {};
7
+ export type ShapeSchemaBox = Record<string, ShapeSchemaBoxEntry>;
8
+ /** Per-box-key state: each entry's field keys stay typed via stateType. */
9
+ export type InferShapeBoxState<TBox extends ShapeSchemaBox> = {
10
+ [K in keyof TBox]: TBox[K]["stateType"];
11
+ };
12
+ export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", undefined, unknown, unknown, never, {}, false, false, false, false, false, true, InferShapeBoxState<TBox>>;
@@ -1,14 +1,15 @@
1
1
  import { createPluginContext } from "cogsbox-state";
2
2
  const { createPlugin } = createPluginContext();
3
+ function buildInitialState(box) {
4
+ const state = {};
5
+ for (const key of Object.keys(box)) {
6
+ const entry = box[key];
7
+ if (!entry)
8
+ continue;
9
+ state[key] = entry.generateDefaults();
10
+ }
11
+ return state;
12
+ }
3
13
  export function createShapePlugin(box) {
4
- return createPlugin("shape").initialState(() => {
5
- const state = {};
6
- for (const key of Object.keys(box)) {
7
- const entry = box[key];
8
- if (!entry)
9
- continue;
10
- state[key] = entry.generateDefaults();
11
- }
12
- return state;
13
- });
14
+ return createPlugin("shape").initialState(() => buildInitialState(box));
14
15
  }
package/dist/schema.d.ts CHANGED
@@ -179,9 +179,28 @@ export type Reference<TGetter extends () => any> = {
179
179
  getter: TGetter;
180
180
  };
181
181
  interface ShapeAPI {
182
- clientInput: <const TValue>(value: TValue | ((tools: {
182
+ clientInput<const TValue, const TSchema extends z.ZodTypeAny>(options: {
183
+ value: TValue | ((tools: {
184
+ uuid: () => string;
185
+ }) => TValue);
186
+ schema: TSchema | ((base: ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>) => TSchema);
187
+ clientPk?: boolean | ((val: any) => boolean);
188
+ }): Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, TSchema, TSchema>;
189
+ clientInput<const TValue>(options: {
190
+ value: TValue | ((tools: {
191
+ uuid: () => string;
192
+ }) => TValue);
193
+ schema?: never;
194
+ clientPk?: boolean | ((val: any) => boolean);
195
+ }): Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
196
+ clientInput<const TSchema extends z.ZodTypeAny>(options: {
197
+ value?: never;
198
+ schema: TSchema;
199
+ clientPk?: boolean | ((val: any) => boolean);
200
+ }): Builder<"clientInput", null, z.ZodUndefined, z.infer<TSchema>, TSchema, TSchema>;
201
+ clientInput<const TValue>(value: TValue | ((tools: {
183
202
  uuid: () => string;
184
- }) => TValue)) => Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
203
+ }) => TValue)): Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
185
204
  sqlite: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "sqlite">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
186
205
  postgres: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "postgres">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
187
206
  mysql: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "mysql">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
package/dist/schema.js CHANGED
@@ -49,8 +49,29 @@ function createSqlBuilder(dialect, sqlConfig) {
49
49
  validationZod: sqlZodType,
50
50
  });
51
51
  }
52
+ function isClientInputOptions(value) {
53
+ return (value !== undefined &&
54
+ typeof value === "object" &&
55
+ value !== null &&
56
+ !isFunction(value) &&
57
+ !("_def" in value) &&
58
+ !("parse" in value) &&
59
+ ("value" in value || "schema" in value || "clientPk" in value));
60
+ }
52
61
  export const s = {
53
- clientInput: (value) => {
62
+ clientInput: (...args) => {
63
+ const first = args[0];
64
+ if (isClientInputOptions(first)) {
65
+ return createBuilder({
66
+ stage: "sql",
67
+ sqlConfig: null,
68
+ sqlZod: z.undefined(),
69
+ initialValue: undefined,
70
+ clientZod: z.undefined(),
71
+ validationZod: z.undefined(),
72
+ }).clientInput(first);
73
+ }
74
+ const value = first;
54
75
  const sample = isFunction(value) ? value({ uuid }) : value;
55
76
  let inferredZodType;
56
77
  if (typeof sample === "string") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.201",
3
+ "version": "0.5.202",
4
4
  "description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",