@constela/core 0.11.0 → 0.12.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.
package/README.md CHANGED
@@ -40,9 +40,27 @@ All fields except `version`, `state`, `actions`, and `view` are optional.
40
40
  }
41
41
  ```
42
42
 
43
+ ### Cookie Expression for Initial Value
44
+
45
+ String state can use a cookie expression to read initial value from cookies (SSR/SSG-safe):
46
+
47
+ ```json
48
+ {
49
+ "theme": {
50
+ "type": "string",
51
+ "initial": { "expr": "cookie", "key": "theme", "default": "dark" }
52
+ }
53
+ }
54
+ ```
55
+
56
+ - `key`: Cookie name to read
57
+ - `default`: Fallback value when cookie is not set
58
+ - Works in both SSR and client-side rendering
59
+ - Useful for theme persistence, user preferences, etc.
60
+
43
61
  ## Expression Types
44
62
 
45
- 14 expression types for constrained computation:
63
+ 15 expression types for constrained computation:
46
64
 
47
65
  | Type | JSON Example | Description |
48
66
  |------|-------------|-------------|
@@ -60,6 +78,7 @@ All fields except `version`, `state`, `actions`, and `view` are optional.
60
78
  | `ref` | `{ "expr": "ref", "name": "inputEl" }` | DOM element ref |
61
79
  | `style` | `{ "expr": "style", "name": "button", "variants": {...} }` | Style reference |
62
80
  | `concat` | `{ "expr": "concat", "items": [...] }` | String concatenation |
81
+ | `cookie` | `{ "expr": "cookie", "key": "theme", "default": "dark" }` | Cookie value (SSR-safe) |
63
82
 
64
83
  **Binary Operators:** `+`, `-`, `*`, `/`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`
65
84
 
package/dist/index.d.ts CHANGED
@@ -155,6 +155,14 @@ interface ValidityExpr {
155
155
  property?: ValidityProperty;
156
156
  }
157
157
  type Expression = LitExpr | StateExpr | VarExpr | BinExpr | NotExpr | ParamExpr | CondExpr | GetExpr | RouteExpr | ImportExpr | DataExpr | RefExpr | IndexExpr | StyleExpr | ConcatExpr | ValidityExpr;
158
+ /**
159
+ * Cookie expression for state initial value
160
+ */
161
+ interface CookieInitialExpr {
162
+ expr: 'cookie';
163
+ key: string;
164
+ default: string;
165
+ }
158
166
  /**
159
167
  * Number state field
160
168
  */
@@ -167,7 +175,7 @@ interface NumberField {
167
175
  */
168
176
  interface StringField {
169
177
  type: 'string';
170
- initial: string;
178
+ initial: string | CookieInitialExpr;
171
179
  }
172
180
  /**
173
181
  * List state field
@@ -824,6 +832,10 @@ declare function isLocalActionDefinition(value: unknown): value is LocalActionDe
824
832
  * Checks if value is a number field
825
833
  */
826
834
  declare function isNumberField(value: unknown): value is NumberField;
835
+ /**
836
+ * Checks if value is a cookie initial expression
837
+ */
838
+ declare function isCookieInitialExpr(value: unknown): value is CookieInitialExpr;
827
839
  /**
828
840
  * Checks if value is a string field
829
841
  */
@@ -1473,6 +1485,27 @@ declare const astSchema: {
1473
1485
  readonly const: "string";
1474
1486
  };
1475
1487
  readonly initial: {
1488
+ readonly oneOf: readonly [{
1489
+ readonly type: "string";
1490
+ }, {
1491
+ readonly $ref: "#/$defs/CookieInitialExpr";
1492
+ }];
1493
+ };
1494
+ };
1495
+ };
1496
+ readonly CookieInitialExpr: {
1497
+ readonly type: "object";
1498
+ readonly required: readonly ["expr", "key", "default"];
1499
+ readonly additionalProperties: false;
1500
+ readonly properties: {
1501
+ readonly expr: {
1502
+ readonly type: "string";
1503
+ readonly const: "cookie";
1504
+ };
1505
+ readonly key: {
1506
+ readonly type: "string";
1507
+ };
1508
+ readonly default: {
1476
1509
  readonly type: "string";
1477
1510
  };
1478
1511
  };
@@ -1991,4 +2024,4 @@ declare const astSchema: {
1991
2024
  };
1992
2025
  };
1993
2026
 
1994
- export { type ActionDefinition, type ActionStep, BINARY_OPERATORS, type BinExpr, type BinaryOperator, type BooleanField, CLIPBOARD_OPERATIONS, type CallStep, type ClearTimerStep, type ClipboardOperation, type ClipboardStep, type CloseStep, type CodeNode, type ComponentDef, type ComponentNode, type ComponentsRef, type CompoundVariant, type ConcatExpr, type CondExpr, type ConstelaAst, ConstelaError, type ConstelaProgram, DATA_SOURCE_TYPES, DATA_TRANSFORMS, type DataExpr, type DataSource, type DataSourceType, type DataTransform, type DelayStep, type DisposeStep, type DomStep, type EachNode, type ElementNode, type ErrorCode, type ErrorOptions, type EventHandler, type EventHandlerOptions, type Expression, FOCUS_OPERATIONS, type FetchStep, type FocusOperation, type FocusStep, type GetExpr, HTTP_METHODS, type HttpMethod, type IfNode, type ImportExpr, type ImportStep, type IntervalStep, type LayoutProgram, type LifecycleHooks, type ListField, type LitExpr, type LocalActionDefinition, type LocalActionStep, type MarkdownNode, NAVIGATE_TARGETS, type NavigateStep, type NavigateTarget, type NotExpr, type NumberField, type ObjectField, PARAM_TYPES, type ParamDef, type ParamExpr, type ParamType, type PortalNode, type Program, type RefExpr, type RouteDefinition, type RouteExpr, STORAGE_OPERATIONS, STORAGE_TYPES, type SendStep, type SetPathStep, type SetStep, type SlotNode, type StateExpr, type StateField, type StaticPathsDefinition, type StorageOperation, type StorageStep, type StorageType, type StringField, type StyleExpr, type StylePreset, type SubscribeStep, type TextNode, UPDATE_OPERATIONS, type UpdateOperation, type UpdateStep, VALIDITY_PROPERTIES, type ValidationFailure, type ValidationResult, type ValidationSuccess, type ValidityExpr, type ValidityProperty, type VarExpr, type ViewNode, astSchema, createClipboardWriteMissingValueError, createComponentCycleError, createComponentNotFoundError, createComponentPropMissingError, createComponentPropTypeError, createCondElseRequiredError, createDataNotDefinedError, createDuplicateActionError, createDuplicateDefaultSlotError, createDuplicateSlotNameError, createImportsNotDefinedError, createInvalidClipboardOperationError, createInvalidDataSourceError, createInvalidNavigateTargetError, createInvalidSlotNameError, createInvalidStorageOperationError, createInvalidStorageTypeError, createLayoutMissingSlotError, createLayoutNotFoundError, createLocalActionInvalidStepError, createOperationInvalidForTypeError, createOperationMissingFieldError, createOperationUnknownError, createRouteNotDefinedError, createSchemaError, createSlotInLoopError, createStorageSetMissingValueError, createUndefinedActionError, createUndefinedDataError, createUndefinedDataSourceError, createUndefinedImportError, createUndefinedLocalStateError, createUndefinedParamError, createUndefinedRefError, createUndefinedRouteParamError, createUndefinedStateError, createUndefinedStyleError, createUndefinedVarError, createUndefinedVariantError, createUnsupportedVersionError, findSimilarNames, isActionStep, isBinExpr, isBooleanField, isCallStep, isClipboardStep, isCodeNode, isComponentNode, isConcatExpr, isCondExpr, isConstelaError, isDataExpr, isDataSource, isDisposeStep, isEachNode, isElementNode, isEventHandler, isExpression, isFetchStep, isFocusStep, isGetExpr, isIfNode, isImportExpr, isImportStep, isLayoutProgram, isLifecycleHooks, isListField, isLitExpr, isLocalActionDefinition, isLocalActionStep, isMarkdownNode, isNamedSlotNode, isNavigateStep, isNotExpr, isNumberField, isObjectField, isParamExpr, isPortalNode, isRefExpr, isRouteDefinition, isRouteExpr, isSetPathStep, isSetStep, isSlotNode, isStateExpr, isStateField, isStaticPathsDefinition, isStorageStep, isStringField, isStyleExpr, isSubscribeStep, isTextNode, isUpdateStep, isValidityExpr, isVarExpr, isViewNode, validateAst };
2027
+ export { type ActionDefinition, type ActionStep, BINARY_OPERATORS, type BinExpr, type BinaryOperator, type BooleanField, CLIPBOARD_OPERATIONS, type CallStep, type ClearTimerStep, type ClipboardOperation, type ClipboardStep, type CloseStep, type CodeNode, type ComponentDef, type ComponentNode, type ComponentsRef, type CompoundVariant, type ConcatExpr, type CondExpr, type ConstelaAst, ConstelaError, type ConstelaProgram, type CookieInitialExpr, DATA_SOURCE_TYPES, DATA_TRANSFORMS, type DataExpr, type DataSource, type DataSourceType, type DataTransform, type DelayStep, type DisposeStep, type DomStep, type EachNode, type ElementNode, type ErrorCode, type ErrorOptions, type EventHandler, type EventHandlerOptions, type Expression, FOCUS_OPERATIONS, type FetchStep, type FocusOperation, type FocusStep, type GetExpr, HTTP_METHODS, type HttpMethod, type IfNode, type ImportExpr, type ImportStep, type IntervalStep, type LayoutProgram, type LifecycleHooks, type ListField, type LitExpr, type LocalActionDefinition, type LocalActionStep, type MarkdownNode, NAVIGATE_TARGETS, type NavigateStep, type NavigateTarget, type NotExpr, type NumberField, type ObjectField, PARAM_TYPES, type ParamDef, type ParamExpr, type ParamType, type PortalNode, type Program, type RefExpr, type RouteDefinition, type RouteExpr, STORAGE_OPERATIONS, STORAGE_TYPES, type SendStep, type SetPathStep, type SetStep, type SlotNode, type StateExpr, type StateField, type StaticPathsDefinition, type StorageOperation, type StorageStep, type StorageType, type StringField, type StyleExpr, type StylePreset, type SubscribeStep, type TextNode, UPDATE_OPERATIONS, type UpdateOperation, type UpdateStep, VALIDITY_PROPERTIES, type ValidationFailure, type ValidationResult, type ValidationSuccess, type ValidityExpr, type ValidityProperty, type VarExpr, type ViewNode, astSchema, createClipboardWriteMissingValueError, createComponentCycleError, createComponentNotFoundError, createComponentPropMissingError, createComponentPropTypeError, createCondElseRequiredError, createDataNotDefinedError, createDuplicateActionError, createDuplicateDefaultSlotError, createDuplicateSlotNameError, createImportsNotDefinedError, createInvalidClipboardOperationError, createInvalidDataSourceError, createInvalidNavigateTargetError, createInvalidSlotNameError, createInvalidStorageOperationError, createInvalidStorageTypeError, createLayoutMissingSlotError, createLayoutNotFoundError, createLocalActionInvalidStepError, createOperationInvalidForTypeError, createOperationMissingFieldError, createOperationUnknownError, createRouteNotDefinedError, createSchemaError, createSlotInLoopError, createStorageSetMissingValueError, createUndefinedActionError, createUndefinedDataError, createUndefinedDataSourceError, createUndefinedImportError, createUndefinedLocalStateError, createUndefinedParamError, createUndefinedRefError, createUndefinedRouteParamError, createUndefinedStateError, createUndefinedStyleError, createUndefinedVarError, createUndefinedVariantError, createUnsupportedVersionError, findSimilarNames, isActionStep, isBinExpr, isBooleanField, isCallStep, isClipboardStep, isCodeNode, isComponentNode, isConcatExpr, isCondExpr, isConstelaError, isCookieInitialExpr, isDataExpr, isDataSource, isDisposeStep, isEachNode, isElementNode, isEventHandler, isExpression, isFetchStep, isFocusStep, isGetExpr, isIfNode, isImportExpr, isImportStep, isLayoutProgram, isLifecycleHooks, isListField, isLitExpr, isLocalActionDefinition, isLocalActionStep, isMarkdownNode, isNamedSlotNode, isNavigateStep, isNotExpr, isNumberField, isObjectField, isParamExpr, isPortalNode, isRefExpr, isRouteDefinition, isRouteExpr, isSetPathStep, isSetStep, isSlotNode, isStateExpr, isStateField, isStaticPathsDefinition, isStorageStep, isStringField, isStyleExpr, isSubscribeStep, isTextNode, isUpdateStep, isValidityExpr, isVarExpr, isViewNode, validateAst };
package/dist/index.js CHANGED
@@ -409,10 +409,13 @@ function isNumberField(value) {
409
409
  if (value["type"] !== "number") return false;
410
410
  return typeof value["initial"] === "number";
411
411
  }
412
+ function isCookieInitialExpr(value) {
413
+ return typeof value === "object" && value !== null && "expr" in value && value.expr === "cookie" && "key" in value && typeof value.key === "string" && "default" in value && typeof value.default === "string";
414
+ }
412
415
  function isStringField(value) {
413
416
  if (!isObject(value)) return false;
414
417
  if (value["type"] !== "string") return false;
415
- return typeof value["initial"] === "string";
418
+ return typeof value["initial"] === "string" || isCookieInitialExpr(value["initial"]);
416
419
  }
417
420
  function isListField(value) {
418
421
  if (!isObject(value)) return false;
@@ -836,10 +839,10 @@ function findSimilarNames(target, candidates, maxDistance = 2) {
836
839
  function isObject2(value) {
837
840
  return typeof value === "object" && value !== null && !Array.isArray(value);
838
841
  }
839
- var VALID_VIEW_KINDS = ["element", "text", "if", "each", "component", "slot", "markdown", "code"];
840
- var VALID_EXPR_TYPES = ["lit", "state", "var", "bin", "not", "param", "cond", "get", "style"];
842
+ var VALID_VIEW_KINDS = ["element", "text", "if", "each", "component", "slot", "markdown", "code", "portal"];
843
+ var VALID_EXPR_TYPES = ["lit", "state", "var", "bin", "not", "param", "cond", "get", "style", "validity", "index"];
841
844
  var VALID_PARAM_TYPES = ["string", "number", "boolean", "json"];
842
- var VALID_ACTION_TYPES = ["set", "update", "fetch"];
845
+ var VALID_ACTION_TYPES = ["set", "update", "fetch", "delay", "interval", "clearTimer", "focus"];
843
846
  var VALID_STATE_TYPES = ["number", "string", "list", "boolean", "object"];
844
847
  var VALID_BIN_OPS = BINARY_OPERATORS;
845
848
  var VALID_UPDATE_OPS = UPDATE_OPERATIONS;
@@ -853,7 +856,7 @@ function validateViewNode(node, path) {
853
856
  return { path: path + "/kind", message: "kind is required" };
854
857
  }
855
858
  if (!VALID_VIEW_KINDS.includes(kind)) {
856
- return { path: path + "/kind", message: "must be one of: element, text, if, each, component, slot, markdown, code" };
859
+ return { path: path + "/kind", message: "must be one of: element, text, if, each, component, slot, markdown, code, portal" };
857
860
  }
858
861
  switch (kind) {
859
862
  case "element":
@@ -952,6 +955,17 @@ function validateViewNode(node, path) {
952
955
  if (langError) return langError;
953
956
  return validateExpression(node["content"], path + "/content");
954
957
  }
958
+ case "portal":
959
+ if (typeof node["target"] !== "string") {
960
+ return { path: path + "/target", message: "target is required" };
961
+ }
962
+ if (Array.isArray(node["children"])) {
963
+ for (let i = 0; i < node["children"].length; i++) {
964
+ const error = validateViewNode(node["children"][i], path + "/children/" + i);
965
+ if (error) return error;
966
+ }
967
+ }
968
+ break;
955
969
  }
956
970
  return null;
957
971
  }
@@ -964,7 +978,7 @@ function validateExpression(expr, path) {
964
978
  return { path: path + "/expr", message: "expr is required" };
965
979
  }
966
980
  if (!VALID_EXPR_TYPES.includes(exprType)) {
967
- return { path: path + "/expr", message: "must be one of: lit, state, var, bin, not, param, cond, get, style" };
981
+ return { path: path + "/expr", message: "must be one of: lit, state, var, bin, not, param, cond, get, style, validity, index" };
968
982
  }
969
983
  switch (exprType) {
970
984
  case "lit":
@@ -1052,6 +1066,25 @@ function validateExpression(expr, path) {
1052
1066
  }
1053
1067
  }
1054
1068
  break;
1069
+ case "validity":
1070
+ if (typeof expr["ref"] !== "string") {
1071
+ return { path: path + "/ref", message: "ref is required" };
1072
+ }
1073
+ break;
1074
+ case "index":
1075
+ if (!("base" in expr)) {
1076
+ return { path: path + "/base", message: "base is required" };
1077
+ }
1078
+ if (!("key" in expr)) {
1079
+ return { path: path + "/key", message: "key is required" };
1080
+ }
1081
+ {
1082
+ const baseError = validateExpression(expr["base"], path + "/base");
1083
+ if (baseError) return baseError;
1084
+ const keyError = validateExpression(expr["key"], path + "/key");
1085
+ if (keyError) return keyError;
1086
+ }
1087
+ break;
1055
1088
  }
1056
1089
  return null;
1057
1090
  }
@@ -1064,7 +1097,7 @@ function validateActionStep(step, path) {
1064
1097
  return { path: path + "/do", message: "do is required" };
1065
1098
  }
1066
1099
  if (!VALID_ACTION_TYPES.includes(doType)) {
1067
- return { path: path + "/do", message: "must be one of: set, update, fetch" };
1100
+ return { path: path + "/do", message: "must be one of: set, update, fetch, delay, interval, clearTimer, focus" };
1068
1101
  }
1069
1102
  switch (doType) {
1070
1103
  case "set":
@@ -1107,6 +1140,50 @@ function validateActionStep(step, path) {
1107
1140
  if (bodyError) return bodyError;
1108
1141
  }
1109
1142
  break;
1143
+ case "delay":
1144
+ if (!("ms" in step)) {
1145
+ return { path: path + "/ms", message: "ms is required" };
1146
+ }
1147
+ if (!("then" in step)) {
1148
+ return { path: path + "/then", message: "then is required" };
1149
+ }
1150
+ {
1151
+ const msError = validateExpression(step["ms"], path + "/ms");
1152
+ if (msError) return msError;
1153
+ if (!Array.isArray(step["then"])) {
1154
+ return { path: path + "/then", message: "then must be an array" };
1155
+ }
1156
+ for (let i = 0; i < step["then"].length; i++) {
1157
+ const thenError = validateActionStep(step["then"][i], path + "/then/" + i);
1158
+ if (thenError) return thenError;
1159
+ }
1160
+ }
1161
+ break;
1162
+ case "interval":
1163
+ if (!("ms" in step)) {
1164
+ return { path: path + "/ms", message: "ms is required" };
1165
+ }
1166
+ if (typeof step["action"] !== "string") {
1167
+ return { path: path + "/action", message: "action is required" };
1168
+ }
1169
+ {
1170
+ const msError = validateExpression(step["ms"], path + "/ms");
1171
+ if (msError) return msError;
1172
+ }
1173
+ break;
1174
+ case "clearTimer":
1175
+ if (!("target" in step)) {
1176
+ return { path: path + "/target", message: "target is required" };
1177
+ }
1178
+ return validateExpression(step["target"], path + "/target");
1179
+ case "focus":
1180
+ if (!("target" in step)) {
1181
+ return { path: path + "/target", message: "target is required" };
1182
+ }
1183
+ if (typeof step["operation"] !== "string") {
1184
+ return { path: path + "/operation", message: "operation is required" };
1185
+ }
1186
+ return validateExpression(step["target"], path + "/target");
1110
1187
  }
1111
1188
  return null;
1112
1189
  }
@@ -1707,7 +1784,22 @@ var astSchema = {
1707
1784
  additionalProperties: false,
1708
1785
  properties: {
1709
1786
  type: { type: "string", const: "string" },
1710
- initial: { type: "string" }
1787
+ initial: {
1788
+ oneOf: [
1789
+ { type: "string" },
1790
+ { $ref: "#/$defs/CookieInitialExpr" }
1791
+ ]
1792
+ }
1793
+ }
1794
+ },
1795
+ CookieInitialExpr: {
1796
+ type: "object",
1797
+ required: ["expr", "key", "default"],
1798
+ additionalProperties: false,
1799
+ properties: {
1800
+ expr: { type: "string", const: "cookie" },
1801
+ key: { type: "string" },
1802
+ default: { type: "string" }
1711
1803
  }
1712
1804
  },
1713
1805
  ListField: {
@@ -2108,6 +2200,7 @@ export {
2108
2200
  isConcatExpr,
2109
2201
  isCondExpr,
2110
2202
  isConstelaError,
2203
+ isCookieInitialExpr,
2111
2204
  isDataExpr,
2112
2205
  isDataSource,
2113
2206
  isDisposeStep,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/core",
3
- "version": "0.11.0",
3
+ "version": "0.12.1",
4
4
  "description": "Core types, schema, and validator for Constela UI framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",