@zenstackhq/sdk 3.0.0-alpha.6 → 3.0.0-alpha.7
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 +39 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +39 -20
- package/dist/index.js.map +1 -1
- package/dist/schema.d.cts +1 -1
- package/dist/schema.d.ts +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -933,7 +933,7 @@ var TsSchemaGenerator = class {
|
|
|
933
933
|
}
|
|
934
934
|
createDataModelFieldObject(field) {
|
|
935
935
|
const objectFields = [
|
|
936
|
-
ts.factory.createPropertyAssignment("type",
|
|
936
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
937
937
|
];
|
|
938
938
|
if (isIdField(field)) {
|
|
939
939
|
objectFields.push(ts.factory.createPropertyAssignment("id", ts.factory.createTrue()));
|
|
@@ -953,9 +953,9 @@ var TsSchemaGenerator = class {
|
|
|
953
953
|
if (field.attributes.length > 0) {
|
|
954
954
|
objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
|
|
955
955
|
}
|
|
956
|
-
const defaultValue = this.
|
|
956
|
+
const defaultValue = this.getFieldMappedDefault(field);
|
|
957
957
|
if (defaultValue !== void 0) {
|
|
958
|
-
if (typeof defaultValue === "object") {
|
|
958
|
+
if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
|
|
959
959
|
if ("call" in defaultValue) {
|
|
960
960
|
objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
|
|
961
961
|
ts.factory.createStringLiteral(defaultValue.call),
|
|
@@ -974,7 +974,11 @@ var TsSchemaGenerator = class {
|
|
|
974
974
|
throw new Error(`Unsupported default value type for field ${field.name}`);
|
|
975
975
|
}
|
|
976
976
|
} else {
|
|
977
|
-
|
|
977
|
+
if (Array.isArray(defaultValue)) {
|
|
978
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)))));
|
|
979
|
+
} else {
|
|
980
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", this.createLiteralNode(defaultValue)));
|
|
981
|
+
}
|
|
978
982
|
}
|
|
979
983
|
}
|
|
980
984
|
if (hasAttribute(field, "@computed")) {
|
|
@@ -1015,34 +1019,39 @@ var TsSchemaGenerator = class {
|
|
|
1015
1019
|
throw new Error("Unsupported URL type");
|
|
1016
1020
|
}
|
|
1017
1021
|
}
|
|
1018
|
-
|
|
1022
|
+
getFieldMappedDefault(field) {
|
|
1019
1023
|
const defaultAttr = getAttribute(field, "@default");
|
|
1020
1024
|
if (!defaultAttr) {
|
|
1021
1025
|
return void 0;
|
|
1022
1026
|
}
|
|
1023
1027
|
const defaultValue = defaultAttr.args[0]?.value;
|
|
1024
1028
|
(0, import_common_helpers.invariant)(defaultValue, "Expected a default value");
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1029
|
+
return this.getMappedValue(defaultValue, field.type);
|
|
1030
|
+
}
|
|
1031
|
+
getMappedValue(expr, fieldType) {
|
|
1032
|
+
if ((0, import_ast3.isLiteralExpr)(expr)) {
|
|
1033
|
+
const lit = expr.value;
|
|
1034
|
+
return fieldType.type === "Boolean" ? lit : [
|
|
1028
1035
|
"Int",
|
|
1029
1036
|
"Float",
|
|
1030
1037
|
"Decimal",
|
|
1031
1038
|
"BigInt"
|
|
1032
|
-
].includes(
|
|
1033
|
-
} else if ((0, import_ast3.
|
|
1034
|
-
return
|
|
1035
|
-
} else if ((0, import_ast3.
|
|
1039
|
+
].includes(fieldType.type) ? Number(lit) : lit;
|
|
1040
|
+
} else if ((0, import_ast3.isArrayExpr)(expr)) {
|
|
1041
|
+
return expr.items.map((item) => this.getMappedValue(item, fieldType));
|
|
1042
|
+
} else if ((0, import_ast3.isReferenceExpr)(expr) && (0, import_ast3.isEnumField)(expr.target.ref)) {
|
|
1043
|
+
return expr.target.ref.name;
|
|
1044
|
+
} else if ((0, import_ast3.isInvocationExpr)(expr)) {
|
|
1036
1045
|
return {
|
|
1037
|
-
call:
|
|
1038
|
-
args:
|
|
1046
|
+
call: expr.function.$refText,
|
|
1047
|
+
args: expr.args.map((arg) => this.getLiteral(arg.value))
|
|
1039
1048
|
};
|
|
1040
|
-
} else if (this.isAuthMemberAccess(
|
|
1049
|
+
} else if (this.isAuthMemberAccess(expr)) {
|
|
1041
1050
|
return {
|
|
1042
|
-
authMember: this.getMemberAccessChain(
|
|
1051
|
+
authMember: this.getMemberAccessChain(expr)
|
|
1043
1052
|
};
|
|
1044
1053
|
} else {
|
|
1045
|
-
throw new Error(`Unsupported default value type for
|
|
1054
|
+
throw new Error(`Unsupported default value type for ${expr.$type}`);
|
|
1046
1055
|
}
|
|
1047
1056
|
}
|
|
1048
1057
|
getMemberAccessChain(expr) {
|
|
@@ -1155,10 +1164,11 @@ var TsSchemaGenerator = class {
|
|
|
1155
1164
|
for (const field of dm.fields) {
|
|
1156
1165
|
if (hasAttribute(field, "@id") || hasAttribute(field, "@unique")) {
|
|
1157
1166
|
properties.push(ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
|
|
1158
|
-
ts.factory.createPropertyAssignment("type",
|
|
1167
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
1159
1168
|
])));
|
|
1160
1169
|
}
|
|
1161
1170
|
}
|
|
1171
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
1162
1172
|
for (const attr of dm.attributes) {
|
|
1163
1173
|
if (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@unique") {
|
|
1164
1174
|
const fieldNames = this.getReferenceNames(attr.args[0].value);
|
|
@@ -1168,13 +1178,18 @@ var TsSchemaGenerator = class {
|
|
|
1168
1178
|
if (fieldNames.length === 1) {
|
|
1169
1179
|
const fieldDef = dm.fields.find((f) => f.name === fieldNames[0]);
|
|
1170
1180
|
properties.push(ts.factory.createPropertyAssignment(fieldNames[0], ts.factory.createObjectLiteralExpression([
|
|
1171
|
-
ts.factory.createPropertyAssignment("type",
|
|
1181
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1172
1182
|
])));
|
|
1173
1183
|
} else {
|
|
1184
|
+
const key = fieldNames.join("_");
|
|
1185
|
+
if (seenKeys.has(key)) {
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
seenKeys.add(key);
|
|
1174
1189
|
properties.push(ts.factory.createPropertyAssignment(fieldNames.join("_"), ts.factory.createObjectLiteralExpression(fieldNames.map((field) => {
|
|
1175
1190
|
const fieldDef = dm.fields.find((f) => f.name === field);
|
|
1176
1191
|
return ts.factory.createPropertyAssignment(field, ts.factory.createObjectLiteralExpression([
|
|
1177
|
-
ts.factory.createPropertyAssignment("type",
|
|
1192
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1178
1193
|
]));
|
|
1179
1194
|
}))));
|
|
1180
1195
|
}
|
|
@@ -1182,6 +1197,10 @@ var TsSchemaGenerator = class {
|
|
|
1182
1197
|
}
|
|
1183
1198
|
return ts.factory.createObjectLiteralExpression(properties, true);
|
|
1184
1199
|
}
|
|
1200
|
+
generateFieldTypeLiteral(field) {
|
|
1201
|
+
(0, import_common_helpers.invariant)(field.type.type || field.type.reference || field.type.unsupported, "Field type must be a primitive, reference, or Unsupported");
|
|
1202
|
+
return field.type.type ? ts.factory.createStringLiteral(field.type.type) : field.type.reference ? ts.factory.createStringLiteral(field.type.reference.$refText) : ts.factory.createStringLiteral("unknown");
|
|
1203
|
+
}
|
|
1185
1204
|
createEnumObject(e) {
|
|
1186
1205
|
return ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createStringLiteral(field.name))), true);
|
|
1187
1206
|
}
|