@zenstackhq/sdk 3.0.0-beta.3 → 3.0.0-beta.5

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/dist/index.cjs CHANGED
@@ -121,7 +121,7 @@ function resolved(ref) {
121
121
  }
122
122
  __name(resolved, "resolved");
123
123
  function getAuthDecl(model) {
124
- let found = model.declarations.find((d) => (0, import_ast.isDataModel)(d) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
124
+ let found = model.declarations.find((d) => ((0, import_ast.isDataModel)(d) || (0, import_ast.isTypeDef)(d)) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
125
125
  if (!found) {
126
126
  found = model.declarations.find((d) => (0, import_ast.isDataModel)(d) && d.name === "User");
127
127
  }
@@ -821,10 +821,12 @@ var TsSchemaGenerator = class {
821
821
  static {
822
822
  __name(this, "TsSchemaGenerator");
823
823
  }
824
+ usedExpressionUtils = false;
824
825
  async generate(model, outputDir) {
825
826
  import_node_fs.default.mkdirSync(outputDir, {
826
827
  recursive: true
827
828
  });
829
+ this.usedExpressionUtils = false;
828
830
  this.generateSchema(model, outputDir);
829
831
  this.generateModelsAndTypeDefs(model, outputDir);
830
832
  this.generateInputTypes(model, outputDir);
@@ -841,18 +843,21 @@ var TsSchemaGenerator = class {
841
843
  }
842
844
  generateSchemaStatements(model, statements) {
843
845
  const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
846
+ const schemaObject = this.createSchemaObject(model);
844
847
  const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
845
848
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
846
849
  ...hasComputedFields ? [
847
850
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("OperandExpression"))
848
851
  ] : [],
849
- ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
852
+ ...this.usedExpressionUtils ? [
853
+ ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
854
+ ] : []
850
855
  ])), ts.factory.createStringLiteral("@zenstackhq/runtime/schema"));
851
856
  statements.push(runtimeImportDecl);
852
857
  const declaration = ts.factory.createVariableStatement([
853
858
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
854
859
  ], ts.factory.createVariableDeclarationList([
855
- ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(this.createSchemaObject(model), ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
860
+ ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
856
861
  ], ts.NodeFlags.Const));
857
862
  statements.push(declaration);
858
863
  const typeDeclaration = ts.factory.createTypeAliasDeclaration([
@@ -860,6 +865,10 @@ var TsSchemaGenerator = class {
860
865
  ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
861
866
  statements.push(typeDeclaration);
862
867
  }
868
+ createExpressionUtilsCall(method, args) {
869
+ this.usedExpressionUtils = true;
870
+ return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
871
+ }
863
872
  createSchemaObject(model) {
864
873
  const properties = [
865
874
  // provider
@@ -931,6 +940,9 @@ var TsSchemaGenerator = class {
931
940
  // subModels
932
941
  ...subModels.length > 0 ? [
933
942
  ts.factory.createPropertyAssignment("subModels", ts.factory.createArrayLiteralExpression(subModels.map((subModel) => ts.factory.createStringLiteral(subModel))))
943
+ ] : [],
944
+ ...dm.isView ? [
945
+ ts.factory.createPropertyAssignment("isView", ts.factory.createTrue())
934
946
  ] : []
935
947
  ];
936
948
  const computedFields = dm.fields.filter((f) => hasAttribute(f, "@computed"));
@@ -959,9 +971,9 @@ var TsSchemaGenerator = class {
959
971
  }
960
972
  createComputedFieldsObject(fields) {
961
973
  return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [
962
- // parameter: `context: { currentModel: string }`
974
+ // parameter: `context: { modelAlias: string }`
963
975
  ts.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, ts.factory.createTypeLiteralNode([
964
- ts.factory.createPropertySignature(void 0, "currentModel", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
976
+ ts.factory.createPropertySignature(void 0, "modelAlias", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
965
977
  ]), void 0)
966
978
  ], ts.factory.createTypeReferenceNode("OperandExpression", [
967
979
  ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type))
@@ -1017,15 +1029,15 @@ var TsSchemaGenerator = class {
1017
1029
  if (defaultValue !== void 0) {
1018
1030
  if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
1019
1031
  if ("call" in defaultValue) {
1020
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1032
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1021
1033
  ts.factory.createStringLiteral(defaultValue.call),
1022
1034
  ...defaultValue.args.length > 0 ? [
1023
1035
  ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
1024
1036
  ] : []
1025
1037
  ])));
1026
1038
  } else if ("authMember" in defaultValue) {
1027
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, [
1028
- ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1039
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
1040
+ this.createExpressionUtilsCall("call", [
1029
1041
  ts.factory.createStringLiteral("auth")
1030
1042
  ]),
1031
1043
  ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
@@ -1339,7 +1351,7 @@ var TsSchemaGenerator = class {
1339
1351
  });
1340
1352
  }
1341
1353
  createThisExpression() {
1342
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._this"), void 0, []);
1354
+ return this.createExpressionUtilsCall("_this");
1343
1355
  }
1344
1356
  createMemberExpression(expr) {
1345
1357
  const members = [];
@@ -1353,32 +1365,32 @@ var TsSchemaGenerator = class {
1353
1365
  this.createExpression(receiver),
1354
1366
  ts.factory.createArrayLiteralExpression(members.map((m) => ts.factory.createStringLiteral(m)))
1355
1367
  ];
1356
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, args);
1368
+ return this.createExpressionUtilsCall("member", args);
1357
1369
  }
1358
1370
  createNullExpression() {
1359
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._null"), void 0, []);
1371
+ return this.createExpressionUtilsCall("_null");
1360
1372
  }
1361
1373
  createBinaryExpression(expr) {
1362
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.binary"), void 0, [
1374
+ return this.createExpressionUtilsCall("binary", [
1363
1375
  this.createExpression(expr.left),
1364
1376
  this.createLiteralNode(expr.operator),
1365
1377
  this.createExpression(expr.right)
1366
1378
  ]);
1367
1379
  }
1368
1380
  createUnaryExpression(expr) {
1369
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.unary"), void 0, [
1381
+ return this.createExpressionUtilsCall("unary", [
1370
1382
  this.createLiteralNode(expr.operator),
1371
1383
  this.createExpression(expr.operand)
1372
1384
  ]);
1373
1385
  }
1374
1386
  createArrayExpression(expr) {
1375
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.array"), void 0, [
1387
+ return this.createExpressionUtilsCall("array", [
1376
1388
  ts.factory.createArrayLiteralExpression(expr.items.map((item) => this.createExpression(item)))
1377
1389
  ]);
1378
1390
  }
1379
1391
  createRefExpression(expr) {
1380
1392
  if ((0, import_ast3.isDataField)(expr.target.ref)) {
1381
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.field"), void 0, [
1393
+ return this.createExpressionUtilsCall("field", [
1382
1394
  this.createLiteralNode(expr.target.$refText)
1383
1395
  ]);
1384
1396
  } else if ((0, import_ast3.isEnumField)(expr.target.ref)) {
@@ -1388,7 +1400,7 @@ var TsSchemaGenerator = class {
1388
1400
  }
1389
1401
  }
1390
1402
  createCallExpression(expr) {
1391
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1403
+ return this.createExpressionUtilsCall("call", [
1392
1404
  ts.factory.createStringLiteral(expr.function.$refText),
1393
1405
  ...expr.args.length > 0 ? [
1394
1406
  ts.factory.createArrayLiteralExpression(expr.args.map((arg) => this.createExpression(arg.value)))
@@ -1396,11 +1408,11 @@ var TsSchemaGenerator = class {
1396
1408
  ]);
1397
1409
  }
1398
1410
  createLiteralExpression(type, value) {
1399
- return (0, import_ts_pattern2.match)(type).with("BooleanLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1411
+ return (0, import_ts_pattern2.match)(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1400
1412
  this.createLiteralNode(value)
1401
- ])).with("NumberLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1413
+ ])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
1402
1414
  ts.factory.createIdentifier(value)
1403
- ])).with("StringLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1415
+ ])).with("StringLiteral", () => this.createExpressionUtilsCall("literal", [
1404
1416
  this.createLiteralNode(value)
1405
1417
  ])).otherwise(() => {
1406
1418
  throw new Error(`Unsupported literal type: ${type}`);