@zenstackhq/sdk 3.0.0-alpha.6 → 3.0.0-alpha.8
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.d.cts
CHANGED
|
@@ -103,7 +103,8 @@ declare class TsSchemaGenerator {
|
|
|
103
103
|
private mapFieldTypeToTSType;
|
|
104
104
|
private createDataModelFieldObject;
|
|
105
105
|
private getDataSourceProvider;
|
|
106
|
-
private
|
|
106
|
+
private getFieldMappedDefault;
|
|
107
|
+
private getMappedValue;
|
|
107
108
|
private getMemberAccessChain;
|
|
108
109
|
private isAuthMemberAccess;
|
|
109
110
|
private isAuthInvocation;
|
|
@@ -114,6 +115,7 @@ declare class TsSchemaGenerator {
|
|
|
114
115
|
private getRelationName;
|
|
115
116
|
private getIdFields;
|
|
116
117
|
private createUniqueFieldsObject;
|
|
118
|
+
private generateFieldTypeLiteral;
|
|
117
119
|
private createEnumObject;
|
|
118
120
|
private getLiteral;
|
|
119
121
|
private createLiteralNode;
|
package/dist/index.d.ts
CHANGED
|
@@ -103,7 +103,8 @@ declare class TsSchemaGenerator {
|
|
|
103
103
|
private mapFieldTypeToTSType;
|
|
104
104
|
private createDataModelFieldObject;
|
|
105
105
|
private getDataSourceProvider;
|
|
106
|
-
private
|
|
106
|
+
private getFieldMappedDefault;
|
|
107
|
+
private getMappedValue;
|
|
107
108
|
private getMemberAccessChain;
|
|
108
109
|
private isAuthMemberAccess;
|
|
109
110
|
private isAuthInvocation;
|
|
@@ -114,6 +115,7 @@ declare class TsSchemaGenerator {
|
|
|
114
115
|
private getRelationName;
|
|
115
116
|
private getIdFields;
|
|
116
117
|
private createUniqueFieldsObject;
|
|
118
|
+
private generateFieldTypeLiteral;
|
|
117
119
|
private createEnumObject;
|
|
118
120
|
private getLiteral;
|
|
119
121
|
private createLiteralNode;
|
package/dist/index.js
CHANGED
|
@@ -900,7 +900,7 @@ var TsSchemaGenerator = class {
|
|
|
900
900
|
}
|
|
901
901
|
createDataModelFieldObject(field) {
|
|
902
902
|
const objectFields = [
|
|
903
|
-
ts.factory.createPropertyAssignment("type",
|
|
903
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
904
904
|
];
|
|
905
905
|
if (isIdField(field)) {
|
|
906
906
|
objectFields.push(ts.factory.createPropertyAssignment("id", ts.factory.createTrue()));
|
|
@@ -920,9 +920,9 @@ var TsSchemaGenerator = class {
|
|
|
920
920
|
if (field.attributes.length > 0) {
|
|
921
921
|
objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
|
|
922
922
|
}
|
|
923
|
-
const defaultValue = this.
|
|
923
|
+
const defaultValue = this.getFieldMappedDefault(field);
|
|
924
924
|
if (defaultValue !== void 0) {
|
|
925
|
-
if (typeof defaultValue === "object") {
|
|
925
|
+
if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
|
|
926
926
|
if ("call" in defaultValue) {
|
|
927
927
|
objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
|
|
928
928
|
ts.factory.createStringLiteral(defaultValue.call),
|
|
@@ -941,7 +941,11 @@ var TsSchemaGenerator = class {
|
|
|
941
941
|
throw new Error(`Unsupported default value type for field ${field.name}`);
|
|
942
942
|
}
|
|
943
943
|
} else {
|
|
944
|
-
|
|
944
|
+
if (Array.isArray(defaultValue)) {
|
|
945
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)))));
|
|
946
|
+
} else {
|
|
947
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", this.createLiteralNode(defaultValue)));
|
|
948
|
+
}
|
|
945
949
|
}
|
|
946
950
|
}
|
|
947
951
|
if (hasAttribute(field, "@computed")) {
|
|
@@ -982,34 +986,39 @@ var TsSchemaGenerator = class {
|
|
|
982
986
|
throw new Error("Unsupported URL type");
|
|
983
987
|
}
|
|
984
988
|
}
|
|
985
|
-
|
|
989
|
+
getFieldMappedDefault(field) {
|
|
986
990
|
const defaultAttr = getAttribute(field, "@default");
|
|
987
991
|
if (!defaultAttr) {
|
|
988
992
|
return void 0;
|
|
989
993
|
}
|
|
990
994
|
const defaultValue = defaultAttr.args[0]?.value;
|
|
991
995
|
invariant(defaultValue, "Expected a default value");
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
996
|
+
return this.getMappedValue(defaultValue, field.type);
|
|
997
|
+
}
|
|
998
|
+
getMappedValue(expr, fieldType) {
|
|
999
|
+
if (isLiteralExpr3(expr)) {
|
|
1000
|
+
const lit = expr.value;
|
|
1001
|
+
return fieldType.type === "Boolean" ? lit : [
|
|
995
1002
|
"Int",
|
|
996
1003
|
"Float",
|
|
997
1004
|
"Decimal",
|
|
998
1005
|
"BigInt"
|
|
999
|
-
].includes(
|
|
1000
|
-
} else if (
|
|
1001
|
-
return
|
|
1002
|
-
} else if (
|
|
1006
|
+
].includes(fieldType.type) ? Number(lit) : lit;
|
|
1007
|
+
} else if (isArrayExpr3(expr)) {
|
|
1008
|
+
return expr.items.map((item) => this.getMappedValue(item, fieldType));
|
|
1009
|
+
} else if (isReferenceExpr3(expr) && isEnumField(expr.target.ref)) {
|
|
1010
|
+
return expr.target.ref.name;
|
|
1011
|
+
} else if (isInvocationExpr2(expr)) {
|
|
1003
1012
|
return {
|
|
1004
|
-
call:
|
|
1005
|
-
args:
|
|
1013
|
+
call: expr.function.$refText,
|
|
1014
|
+
args: expr.args.map((arg) => this.getLiteral(arg.value))
|
|
1006
1015
|
};
|
|
1007
|
-
} else if (this.isAuthMemberAccess(
|
|
1016
|
+
} else if (this.isAuthMemberAccess(expr)) {
|
|
1008
1017
|
return {
|
|
1009
|
-
authMember: this.getMemberAccessChain(
|
|
1018
|
+
authMember: this.getMemberAccessChain(expr)
|
|
1010
1019
|
};
|
|
1011
1020
|
} else {
|
|
1012
|
-
throw new Error(`Unsupported default value type for
|
|
1021
|
+
throw new Error(`Unsupported default value type for ${expr.$type}`);
|
|
1013
1022
|
}
|
|
1014
1023
|
}
|
|
1015
1024
|
getMemberAccessChain(expr) {
|
|
@@ -1122,10 +1131,11 @@ var TsSchemaGenerator = class {
|
|
|
1122
1131
|
for (const field of dm.fields) {
|
|
1123
1132
|
if (hasAttribute(field, "@id") || hasAttribute(field, "@unique")) {
|
|
1124
1133
|
properties.push(ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
|
|
1125
|
-
ts.factory.createPropertyAssignment("type",
|
|
1134
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
1126
1135
|
])));
|
|
1127
1136
|
}
|
|
1128
1137
|
}
|
|
1138
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
1129
1139
|
for (const attr of dm.attributes) {
|
|
1130
1140
|
if (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@unique") {
|
|
1131
1141
|
const fieldNames = this.getReferenceNames(attr.args[0].value);
|
|
@@ -1135,13 +1145,18 @@ var TsSchemaGenerator = class {
|
|
|
1135
1145
|
if (fieldNames.length === 1) {
|
|
1136
1146
|
const fieldDef = dm.fields.find((f) => f.name === fieldNames[0]);
|
|
1137
1147
|
properties.push(ts.factory.createPropertyAssignment(fieldNames[0], ts.factory.createObjectLiteralExpression([
|
|
1138
|
-
ts.factory.createPropertyAssignment("type",
|
|
1148
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1139
1149
|
])));
|
|
1140
1150
|
} else {
|
|
1151
|
+
const key = fieldNames.join("_");
|
|
1152
|
+
if (seenKeys.has(key)) {
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
seenKeys.add(key);
|
|
1141
1156
|
properties.push(ts.factory.createPropertyAssignment(fieldNames.join("_"), ts.factory.createObjectLiteralExpression(fieldNames.map((field) => {
|
|
1142
1157
|
const fieldDef = dm.fields.find((f) => f.name === field);
|
|
1143
1158
|
return ts.factory.createPropertyAssignment(field, ts.factory.createObjectLiteralExpression([
|
|
1144
|
-
ts.factory.createPropertyAssignment("type",
|
|
1159
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1145
1160
|
]));
|
|
1146
1161
|
}))));
|
|
1147
1162
|
}
|
|
@@ -1149,6 +1164,10 @@ var TsSchemaGenerator = class {
|
|
|
1149
1164
|
}
|
|
1150
1165
|
return ts.factory.createObjectLiteralExpression(properties, true);
|
|
1151
1166
|
}
|
|
1167
|
+
generateFieldTypeLiteral(field) {
|
|
1168
|
+
invariant(field.type.type || field.type.reference || field.type.unsupported, "Field type must be a primitive, reference, or Unsupported");
|
|
1169
|
+
return field.type.type ? ts.factory.createStringLiteral(field.type.type) : field.type.reference ? ts.factory.createStringLiteral(field.type.reference.$refText) : ts.factory.createStringLiteral("unknown");
|
|
1170
|
+
}
|
|
1152
1171
|
createEnumObject(e) {
|
|
1153
1172
|
return ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createStringLiteral(field.name))), true);
|
|
1154
1173
|
}
|