@zenstackhq/sdk 3.4.5 → 3.5.0-beta.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/dist/index.d.cts CHANGED
@@ -145,12 +145,16 @@ type TsSchemaGeneratorOptions = {
145
145
  };
146
146
  declare class TsSchemaGenerator {
147
147
  private usedExpressionUtils;
148
+ private usedAttributeApplication;
149
+ private usedFieldDefault;
148
150
  generate(model: Model, options: TsSchemaGeneratorOptions): Promise<void>;
149
151
  private generateSchema;
150
152
  private generateSchemaStatements;
151
153
  private createExpressionUtilsCall;
152
154
  private createSchemaClass;
153
155
  private createAsConst;
156
+ private createAttributesTypeAssertion;
157
+ private createDefaultTypeAssertion;
154
158
  private createProviderObject;
155
159
  private createModelsObject;
156
160
  private getAllDataModels;
package/dist/index.d.ts CHANGED
@@ -145,12 +145,16 @@ type TsSchemaGeneratorOptions = {
145
145
  };
146
146
  declare class TsSchemaGenerator {
147
147
  private usedExpressionUtils;
148
+ private usedAttributeApplication;
149
+ private usedFieldDefault;
148
150
  generate(model: Model, options: TsSchemaGeneratorOptions): Promise<void>;
149
151
  private generateSchema;
150
152
  private generateSchemaStatements;
151
153
  private createExpressionUtilsCall;
152
154
  private createSchemaClass;
153
155
  private createAsConst;
156
+ private createAttributesTypeAssertion;
157
+ private createDefaultTypeAssertion;
154
158
  private createProviderObject;
155
159
  private createModelsObject;
156
160
  private getAllDataModels;
package/dist/index.js CHANGED
@@ -855,11 +855,12 @@ var TsSchemaGenerator = class {
855
855
  __name(this, "TsSchemaGenerator");
856
856
  }
857
857
  usedExpressionUtils = false;
858
+ usedAttributeApplication = false;
859
+ usedFieldDefault = false;
858
860
  async generate(model, options) {
859
861
  fs.mkdirSync(options.outDir, {
860
862
  recursive: true
861
863
  });
862
- this.usedExpressionUtils = false;
863
864
  this.generateSchema(model, options);
864
865
  if (options.generateModelTypes !== false) {
865
866
  this.generateModelsAndTypeDefs(model, options);
@@ -883,6 +884,9 @@ var TsSchemaGenerator = class {
883
884
  });
884
885
  }
885
886
  for (const { lite, file } of targets) {
887
+ this.usedExpressionUtils = false;
888
+ this.usedAttributeApplication = false;
889
+ this.usedFieldDefault = false;
886
890
  const statements = [];
887
891
  this.generateSchemaStatements(model, statements, lite);
888
892
  this.generateBannerComments(statements);
@@ -897,6 +901,12 @@ var TsSchemaGenerator = class {
897
901
  const schemaClass = this.createSchemaClass(model, lite);
898
902
  const schemaImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(void 0, void 0, ts.factory.createNamedImports([
899
903
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
904
+ ...this.usedAttributeApplication ? [
905
+ ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("AttributeApplication"))
906
+ ] : [],
907
+ ...this.usedFieldDefault ? [
908
+ ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("FieldDefault"))
909
+ ] : [],
900
910
  ...this.usedExpressionUtils ? [
901
911
  ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
902
912
  ] : []
@@ -950,6 +960,14 @@ var TsSchemaGenerator = class {
950
960
  createAsConst(expr) {
951
961
  return ts.factory.createAsExpression(expr, ts.factory.createTypeReferenceNode("const"));
952
962
  }
963
+ createAttributesTypeAssertion(expr) {
964
+ this.usedAttributeApplication = true;
965
+ return ts.factory.createAsExpression(expr, ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, ts.factory.createArrayTypeNode(ts.factory.createTypeReferenceNode("AttributeApplication"))));
966
+ }
967
+ createDefaultTypeAssertion(expr) {
968
+ this.usedFieldDefault = true;
969
+ return ts.factory.createAsExpression(expr, ts.factory.createTypeReferenceNode("FieldDefault"));
970
+ }
953
971
  createProviderObject(model) {
954
972
  const dsProvider = this.getDataSourceProvider(model);
955
973
  const defaultSchema = this.getDataSourceDefaultSchema(model);
@@ -992,7 +1010,7 @@ var TsSchemaGenerator = class {
992
1010
  ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm, lite))), true)),
993
1011
  // attributes
994
1012
  ...allAttributes.length > 0 ? [
995
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
1013
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true)))
996
1014
  ] : [],
997
1015
  // idFields
998
1016
  ts.factory.createPropertyAssignment("idFields", ts.factory.createArrayLiteralExpression(getIdFields(dm).map((idField) => ts.factory.createStringLiteral(idField)))),
@@ -1029,7 +1047,7 @@ var TsSchemaGenerator = class {
1029
1047
  ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0, lite))), true)),
1030
1048
  // attributes
1031
1049
  ...allAttributes.length > 0 ? [
1032
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
1050
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true)))
1033
1051
  ] : []
1034
1052
  ];
1035
1053
  return ts.factory.createObjectLiteralExpression(fields, true);
@@ -1099,39 +1117,39 @@ var TsSchemaGenerator = class {
1099
1117
  objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
1100
1118
  }
1101
1119
  if (!lite && field.attributes.length > 0) {
1102
- objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
1120
+ objectFields.push(ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr))))));
1103
1121
  }
1104
1122
  const defaultValue = this.getFieldMappedDefault(field);
1105
1123
  if (defaultValue !== void 0) {
1124
+ let defaultExpr;
1106
1125
  if (defaultValue === null) {
1107
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("_null")));
1126
+ defaultExpr = this.createExpressionUtilsCall("_null");
1108
1127
  } else if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
1109
1128
  if ("call" in defaultValue) {
1110
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1129
+ defaultExpr = this.createExpressionUtilsCall("call", [
1111
1130
  ts.factory.createStringLiteral(defaultValue.call),
1112
1131
  ...defaultValue.args.length > 0 ? [
1113
1132
  ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createExpressionUtilsCall("literal", [
1114
1133
  this.createLiteralNode(arg)
1115
1134
  ])))
1116
1135
  ] : []
1117
- ])));
1136
+ ]);
1118
1137
  } else if ("authMember" in defaultValue) {
1119
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
1138
+ defaultExpr = this.createExpressionUtilsCall("member", [
1120
1139
  this.createExpressionUtilsCall("call", [
1121
1140
  ts.factory.createStringLiteral("auth")
1122
1141
  ]),
1123
1142
  ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
1124
- ])));
1143
+ ]);
1125
1144
  } else {
1126
1145
  throw new Error(`Unsupported default value type for field ${field.name}`);
1127
1146
  }
1147
+ } else if (Array.isArray(defaultValue)) {
1148
+ defaultExpr = ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)));
1128
1149
  } else {
1129
- if (Array.isArray(defaultValue)) {
1130
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)))));
1131
- } else {
1132
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createLiteralNode(defaultValue)));
1133
- }
1150
+ defaultExpr = this.createLiteralNode(defaultValue);
1134
1151
  }
1152
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createDefaultTypeAssertion(defaultExpr)));
1135
1153
  }
1136
1154
  if (hasAttribute(field, "@computed")) {
1137
1155
  objectFields.push(ts.factory.createPropertyAssignment("computed", ts.factory.createTrue()));
@@ -1141,7 +1159,7 @@ var TsSchemaGenerator = class {
1141
1159
  }
1142
1160
  const fkFor = this.getForeignKeyFor(field);
1143
1161
  if (fkFor && fkFor.length > 0) {
1144
- objectFields.push(ts.factory.createPropertyAssignment("foreignKeyFor", ts.factory.createArrayLiteralExpression(fkFor.map((fk) => ts.factory.createStringLiteral(fk)), true)));
1162
+ objectFields.push(ts.factory.createPropertyAssignment("foreignKeyFor", ts.factory.createAsExpression(ts.factory.createArrayLiteralExpression(fkFor.map((fk) => ts.factory.createStringLiteral(fk)), true), ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))))));
1145
1163
  }
1146
1164
  return ts.factory.createObjectLiteralExpression(objectFields, true);
1147
1165
  }
@@ -1383,12 +1401,12 @@ var TsSchemaGenerator = class {
1383
1401
  ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
1384
1402
  ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
1385
1403
  ...field.attributes.length > 0 ? [
1386
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes?.map((attr) => this.createAttributeObject(attr)) ?? [], true))
1404
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(field.attributes?.map((attr) => this.createAttributeObject(attr)) ?? [], true)))
1387
1405
  ] : []
1388
1406
  ], true))), true))
1389
1407
  ] : [],
1390
1408
  ...e.attributes.length > 0 ? [
1391
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(e.attributes.map((attr) => this.createAttributeObject(attr)), true))
1409
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(e.attributes.map((attr) => this.createAttributeObject(attr)), true)))
1392
1410
  ] : []
1393
1411
  ], true);
1394
1412
  }