@zenstackhq/sdk 3.0.0-beta.2 → 3.0.0-beta.4

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
@@ -1017,15 +1026,15 @@ var TsSchemaGenerator = class {
1017
1026
  if (defaultValue !== void 0) {
1018
1027
  if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
1019
1028
  if ("call" in defaultValue) {
1020
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1029
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1021
1030
  ts.factory.createStringLiteral(defaultValue.call),
1022
1031
  ...defaultValue.args.length > 0 ? [
1023
1032
  ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
1024
1033
  ] : []
1025
1034
  ])));
1026
1035
  } 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, [
1036
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
1037
+ this.createExpressionUtilsCall("call", [
1029
1038
  ts.factory.createStringLiteral("auth")
1030
1039
  ]),
1031
1040
  ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
@@ -1339,7 +1348,7 @@ var TsSchemaGenerator = class {
1339
1348
  });
1340
1349
  }
1341
1350
  createThisExpression() {
1342
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._this"), void 0, []);
1351
+ return this.createExpressionUtilsCall("_this");
1343
1352
  }
1344
1353
  createMemberExpression(expr) {
1345
1354
  const members = [];
@@ -1353,32 +1362,32 @@ var TsSchemaGenerator = class {
1353
1362
  this.createExpression(receiver),
1354
1363
  ts.factory.createArrayLiteralExpression(members.map((m) => ts.factory.createStringLiteral(m)))
1355
1364
  ];
1356
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, args);
1365
+ return this.createExpressionUtilsCall("member", args);
1357
1366
  }
1358
1367
  createNullExpression() {
1359
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._null"), void 0, []);
1368
+ return this.createExpressionUtilsCall("_null");
1360
1369
  }
1361
1370
  createBinaryExpression(expr) {
1362
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.binary"), void 0, [
1371
+ return this.createExpressionUtilsCall("binary", [
1363
1372
  this.createExpression(expr.left),
1364
1373
  this.createLiteralNode(expr.operator),
1365
1374
  this.createExpression(expr.right)
1366
1375
  ]);
1367
1376
  }
1368
1377
  createUnaryExpression(expr) {
1369
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.unary"), void 0, [
1378
+ return this.createExpressionUtilsCall("unary", [
1370
1379
  this.createLiteralNode(expr.operator),
1371
1380
  this.createExpression(expr.operand)
1372
1381
  ]);
1373
1382
  }
1374
1383
  createArrayExpression(expr) {
1375
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.array"), void 0, [
1384
+ return this.createExpressionUtilsCall("array", [
1376
1385
  ts.factory.createArrayLiteralExpression(expr.items.map((item) => this.createExpression(item)))
1377
1386
  ]);
1378
1387
  }
1379
1388
  createRefExpression(expr) {
1380
1389
  if ((0, import_ast3.isDataField)(expr.target.ref)) {
1381
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.field"), void 0, [
1390
+ return this.createExpressionUtilsCall("field", [
1382
1391
  this.createLiteralNode(expr.target.$refText)
1383
1392
  ]);
1384
1393
  } else if ((0, import_ast3.isEnumField)(expr.target.ref)) {
@@ -1388,7 +1397,7 @@ var TsSchemaGenerator = class {
1388
1397
  }
1389
1398
  }
1390
1399
  createCallExpression(expr) {
1391
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1400
+ return this.createExpressionUtilsCall("call", [
1392
1401
  ts.factory.createStringLiteral(expr.function.$refText),
1393
1402
  ...expr.args.length > 0 ? [
1394
1403
  ts.factory.createArrayLiteralExpression(expr.args.map((arg) => this.createExpression(arg.value)))
@@ -1396,11 +1405,11 @@ var TsSchemaGenerator = class {
1396
1405
  ]);
1397
1406
  }
1398
1407
  createLiteralExpression(type, value) {
1399
- return (0, import_ts_pattern2.match)(type).with("BooleanLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1408
+ return (0, import_ts_pattern2.match)(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1400
1409
  this.createLiteralNode(value)
1401
- ])).with("NumberLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1410
+ ])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
1402
1411
  ts.factory.createIdentifier(value)
1403
- ])).with("StringLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1412
+ ])).with("StringLiteral", () => this.createExpressionUtilsCall("literal", [
1404
1413
  this.createLiteralNode(value)
1405
1414
  ])).otherwise(() => {
1406
1415
  throw new Error(`Unsupported literal type: ${type}`);