cms-renderer 0.3.0 → 0.3.2
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/lib/cms-api.js.map +1 -1
- package/dist/lib/custom-schemas.js +165 -545
- package/dist/lib/custom-schemas.js.map +1 -1
- package/dist/lib/refresher.d.ts +9 -0
- package/dist/lib/refresher.js +47 -0
- package/dist/lib/refresher.js.map +1 -0
- package/dist/lib/renderer.d.ts +1 -6
- package/dist/lib/renderer.js +0 -4
- package/dist/lib/renderer.js.map +1 -1
- package/dist/lib/schema.js +1 -1
- package/dist/lib/schema.js.map +1 -1
- package/package.json +6 -1
|
@@ -4,13 +4,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var
|
|
8
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
-
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
-
});
|
|
13
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
14
8
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
9
|
};
|
|
16
10
|
var __copyProps = (to, from, except, desc) => {
|
|
@@ -801,387 +795,174 @@ var require_safe_regex2 = __commonJS({
|
|
|
801
795
|
}
|
|
802
796
|
});
|
|
803
797
|
|
|
804
|
-
//
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
}
|
|
849
|
-
for (var i = 0; i < hashes.length; ++i) {
|
|
850
|
-
if (hashes[i].toLowerCase() === options.algorithm.toLowerCase()) {
|
|
851
|
-
options.algorithm = hashes[i];
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
if (hashes.indexOf(options.algorithm) === -1) {
|
|
855
|
-
throw new Error('Algorithm "' + options.algorithm + '" not supported. supported values: ' + hashes.join(", "));
|
|
856
|
-
}
|
|
857
|
-
if (encodings.indexOf(options.encoding) === -1 && options.algorithm !== "passthrough") {
|
|
858
|
-
throw new Error('Encoding "' + options.encoding + '" not supported. supported values: ' + encodings.join(", "));
|
|
859
|
-
}
|
|
860
|
-
return options;
|
|
861
|
-
}
|
|
862
|
-
function isNativeFunction(f) {
|
|
863
|
-
if (typeof f !== "function") {
|
|
864
|
-
return false;
|
|
865
|
-
}
|
|
866
|
-
var exp = /^function\s+\w*\s*\(\s*\)\s*{\s+\[native code\]\s+}$/i;
|
|
867
|
-
return exp.exec(Function.prototype.toString.call(f)) != null;
|
|
868
|
-
}
|
|
869
|
-
function hash2(object, options) {
|
|
870
|
-
var hashingStream;
|
|
871
|
-
if (options.algorithm !== "passthrough") {
|
|
872
|
-
hashingStream = crypto.createHash(options.algorithm);
|
|
873
|
-
} else {
|
|
874
|
-
hashingStream = new PassThrough();
|
|
875
|
-
}
|
|
876
|
-
if (typeof hashingStream.write === "undefined") {
|
|
877
|
-
hashingStream.write = hashingStream.update;
|
|
878
|
-
hashingStream.end = hashingStream.update;
|
|
879
|
-
}
|
|
880
|
-
var hasher = typeHasher(options, hashingStream);
|
|
881
|
-
hasher.dispatch(object);
|
|
882
|
-
if (!hashingStream.update) {
|
|
883
|
-
hashingStream.end("");
|
|
884
|
-
}
|
|
885
|
-
if (hashingStream.digest) {
|
|
886
|
-
return hashingStream.digest(options.encoding === "buffer" ? void 0 : options.encoding);
|
|
887
|
-
}
|
|
888
|
-
var buf = hashingStream.read();
|
|
889
|
-
if (options.encoding === "buffer") {
|
|
890
|
-
return buf;
|
|
891
|
-
}
|
|
892
|
-
return buf.toString(options.encoding);
|
|
798
|
+
// lib/custom-schemas.ts
|
|
799
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
800
|
+
import { dirname } from "path";
|
|
801
|
+
|
|
802
|
+
// ../../packages/cms-schema/src/documents/generate-zod-code.ts
|
|
803
|
+
function generateZodSchemaCode(schemaName, fields) {
|
|
804
|
+
const lines = [];
|
|
805
|
+
lines.push(`import { z } from 'zod';`);
|
|
806
|
+
lines.push("");
|
|
807
|
+
const hasImageField = fields.some((f) => f.type === "image");
|
|
808
|
+
if (hasImageField) {
|
|
809
|
+
appendImageReferenceSchema(lines);
|
|
810
|
+
lines.push("");
|
|
811
|
+
}
|
|
812
|
+
appendSchemaBody(lines, schemaName, fields);
|
|
813
|
+
return lines.join("\n");
|
|
814
|
+
}
|
|
815
|
+
function generateCombinedZodSchemaCode(schemas) {
|
|
816
|
+
const lines = [];
|
|
817
|
+
lines.push("/* eslint-disable */");
|
|
818
|
+
lines.push("/* prettier-ignore */");
|
|
819
|
+
lines.push("/* biome-ignore format: auto-generated */");
|
|
820
|
+
lines.push("// This file is auto-generated by cms-renderer. Do not edit manually.");
|
|
821
|
+
lines.push("");
|
|
822
|
+
lines.push(`import { z } from 'zod';`);
|
|
823
|
+
lines.push("");
|
|
824
|
+
const hasImageField = schemas.some((s) => s.fields.some((f) => f.type === "image"));
|
|
825
|
+
if (hasImageField) {
|
|
826
|
+
appendImageReferenceSchema(lines);
|
|
827
|
+
lines.push("");
|
|
828
|
+
}
|
|
829
|
+
for (const [i, schema] of schemas.entries()) {
|
|
830
|
+
if (i > 0) lines.push("");
|
|
831
|
+
appendSchemaBody(lines, schema.name, schema.fields);
|
|
832
|
+
}
|
|
833
|
+
return lines.join("\n");
|
|
834
|
+
}
|
|
835
|
+
function appendSchemaBody(lines, schemaName, fields) {
|
|
836
|
+
const varName = `${toCamelCase(schemaName)}Schema`;
|
|
837
|
+
lines.push(`export const ${varName} = z.object({`);
|
|
838
|
+
for (const field of fields) {
|
|
839
|
+
const fieldCode = generateFieldCode(field);
|
|
840
|
+
if (field.description) {
|
|
841
|
+
lines.push(` /** ${field.description} */`);
|
|
893
842
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
843
|
+
lines.push(` ${field.name}: ${fieldCode},`);
|
|
844
|
+
}
|
|
845
|
+
lines.push("});");
|
|
846
|
+
lines.push("");
|
|
847
|
+
lines.push(`export type ${toPascalCase(schemaName)} = z.infer<typeof ${varName}>;`);
|
|
848
|
+
lines.push("");
|
|
849
|
+
}
|
|
850
|
+
function appendImageReferenceSchema(lines) {
|
|
851
|
+
lines.push("const ImageReferenceSchema = z.object({");
|
|
852
|
+
lines.push(" alt: z.string().min(1, 'Alt text is required for accessibility'),");
|
|
853
|
+
lines.push(" caption: z.string().max(500).optional(),");
|
|
854
|
+
lines.push(" attribution: z.string().max(255).optional(),");
|
|
855
|
+
lines.push(" _asset: z.object({");
|
|
856
|
+
lines.push(" id: z.string().uuid(),");
|
|
857
|
+
lines.push(" }),");
|
|
858
|
+
lines.push("});");
|
|
859
|
+
}
|
|
860
|
+
function generateFieldCode(field) {
|
|
861
|
+
let code = generateBaseTypeCode(field.type, field.constraints);
|
|
862
|
+
if (!field.required) {
|
|
863
|
+
code += ".optional()";
|
|
864
|
+
}
|
|
865
|
+
return code;
|
|
866
|
+
}
|
|
867
|
+
function generateBaseTypeCode(type, constraints) {
|
|
868
|
+
switch (type) {
|
|
869
|
+
case "string":
|
|
870
|
+
return applyStringConstraintCode("z.string()", constraints);
|
|
871
|
+
case "number":
|
|
872
|
+
return applyNumberConstraintCode("z.number()", constraints);
|
|
873
|
+
case "boolean":
|
|
874
|
+
return "z.boolean()";
|
|
875
|
+
case "image":
|
|
876
|
+
return "ImageReferenceSchema";
|
|
877
|
+
case "date":
|
|
878
|
+
return "z.string().date('Invalid date format. Expected YYYY-MM-DD')";
|
|
879
|
+
case "datetime":
|
|
880
|
+
return "z.string().datetime({ offset: true, message: 'Invalid datetime format. Expected ISO 8601' })";
|
|
881
|
+
case "url":
|
|
882
|
+
return applyStringConstraintCode("z.string().url('Invalid URL format')", constraints);
|
|
883
|
+
case "email":
|
|
884
|
+
return applyStringConstraintCode("z.string().email('Invalid email format')", constraints);
|
|
885
|
+
case "enum": {
|
|
886
|
+
const values = constraints?.enumValues;
|
|
887
|
+
if (!values || values.length === 0) {
|
|
888
|
+
return "z.enum([''])";
|
|
898
889
|
}
|
|
899
|
-
|
|
900
|
-
return
|
|
901
|
-
};
|
|
902
|
-
function typeHasher(options, writeTo, context) {
|
|
903
|
-
context = context || [];
|
|
904
|
-
var write = function(str) {
|
|
905
|
-
if (writeTo.update) {
|
|
906
|
-
return writeTo.update(str, "utf8");
|
|
907
|
-
} else {
|
|
908
|
-
return writeTo.write(str, "utf8");
|
|
909
|
-
}
|
|
910
|
-
};
|
|
911
|
-
return {
|
|
912
|
-
dispatch: function(value) {
|
|
913
|
-
if (options.replacer) {
|
|
914
|
-
value = options.replacer(value);
|
|
915
|
-
}
|
|
916
|
-
var type = typeof value;
|
|
917
|
-
if (value === null) {
|
|
918
|
-
type = "null";
|
|
919
|
-
}
|
|
920
|
-
return this["_" + type](value);
|
|
921
|
-
},
|
|
922
|
-
_object: function(object) {
|
|
923
|
-
var pattern = /\[object (.*)\]/i;
|
|
924
|
-
var objString = Object.prototype.toString.call(object);
|
|
925
|
-
var objType = pattern.exec(objString);
|
|
926
|
-
if (!objType) {
|
|
927
|
-
objType = "unknown:[" + objString + "]";
|
|
928
|
-
} else {
|
|
929
|
-
objType = objType[1];
|
|
930
|
-
}
|
|
931
|
-
objType = objType.toLowerCase();
|
|
932
|
-
var objectNumber = null;
|
|
933
|
-
if ((objectNumber = context.indexOf(object)) >= 0) {
|
|
934
|
-
return this.dispatch("[CIRCULAR:" + objectNumber + "]");
|
|
935
|
-
} else {
|
|
936
|
-
context.push(object);
|
|
937
|
-
}
|
|
938
|
-
if (typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(object)) {
|
|
939
|
-
write("buffer:");
|
|
940
|
-
return write(object);
|
|
941
|
-
}
|
|
942
|
-
if (objType !== "object" && objType !== "function" && objType !== "asyncfunction") {
|
|
943
|
-
if (this["_" + objType]) {
|
|
944
|
-
this["_" + objType](object);
|
|
945
|
-
} else if (options.ignoreUnknown) {
|
|
946
|
-
return write("[" + objType + "]");
|
|
947
|
-
} else {
|
|
948
|
-
throw new Error('Unknown object type "' + objType + '"');
|
|
949
|
-
}
|
|
950
|
-
} else {
|
|
951
|
-
var keys = Object.keys(object);
|
|
952
|
-
if (options.unorderedObjects) {
|
|
953
|
-
keys = keys.sort();
|
|
954
|
-
}
|
|
955
|
-
if (options.respectType !== false && !isNativeFunction(object)) {
|
|
956
|
-
keys.splice(0, 0, "prototype", "__proto__", "constructor");
|
|
957
|
-
}
|
|
958
|
-
if (options.excludeKeys) {
|
|
959
|
-
keys = keys.filter(function(key) {
|
|
960
|
-
return !options.excludeKeys(key);
|
|
961
|
-
});
|
|
962
|
-
}
|
|
963
|
-
write("object:" + keys.length + ":");
|
|
964
|
-
var self = this;
|
|
965
|
-
return keys.forEach(function(key) {
|
|
966
|
-
self.dispatch(key);
|
|
967
|
-
write(":");
|
|
968
|
-
if (!options.excludeValues) {
|
|
969
|
-
self.dispatch(object[key]);
|
|
970
|
-
}
|
|
971
|
-
write(",");
|
|
972
|
-
});
|
|
973
|
-
}
|
|
974
|
-
},
|
|
975
|
-
_array: function(arr, unordered) {
|
|
976
|
-
unordered = typeof unordered !== "undefined" ? unordered : options.unorderedArrays !== false;
|
|
977
|
-
var self = this;
|
|
978
|
-
write("array:" + arr.length + ":");
|
|
979
|
-
if (!unordered || arr.length <= 1) {
|
|
980
|
-
return arr.forEach(function(entry) {
|
|
981
|
-
return self.dispatch(entry);
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
var contextAdditions = [];
|
|
985
|
-
var entries = arr.map(function(entry) {
|
|
986
|
-
var strm = new PassThrough();
|
|
987
|
-
var localContext = context.slice();
|
|
988
|
-
var hasher = typeHasher(options, strm, localContext);
|
|
989
|
-
hasher.dispatch(entry);
|
|
990
|
-
contextAdditions = contextAdditions.concat(localContext.slice(context.length));
|
|
991
|
-
return strm.read().toString();
|
|
992
|
-
});
|
|
993
|
-
context = context.concat(contextAdditions);
|
|
994
|
-
entries.sort();
|
|
995
|
-
return this._array(entries, false);
|
|
996
|
-
},
|
|
997
|
-
_date: function(date) {
|
|
998
|
-
return write("date:" + date.toJSON());
|
|
999
|
-
},
|
|
1000
|
-
_symbol: function(sym) {
|
|
1001
|
-
return write("symbol:" + sym.toString());
|
|
1002
|
-
},
|
|
1003
|
-
_error: function(err2) {
|
|
1004
|
-
return write("error:" + err2.toString());
|
|
1005
|
-
},
|
|
1006
|
-
_boolean: function(bool) {
|
|
1007
|
-
return write("bool:" + bool.toString());
|
|
1008
|
-
},
|
|
1009
|
-
_string: function(string) {
|
|
1010
|
-
write("string:" + string.length + ":");
|
|
1011
|
-
write(string.toString());
|
|
1012
|
-
},
|
|
1013
|
-
_function: function(fn) {
|
|
1014
|
-
write("fn:");
|
|
1015
|
-
if (isNativeFunction(fn)) {
|
|
1016
|
-
this.dispatch("[native]");
|
|
1017
|
-
} else {
|
|
1018
|
-
this.dispatch(fn.toString());
|
|
1019
|
-
}
|
|
1020
|
-
if (options.respectFunctionNames !== false) {
|
|
1021
|
-
this.dispatch("function-name:" + String(fn.name));
|
|
1022
|
-
}
|
|
1023
|
-
if (options.respectFunctionProperties) {
|
|
1024
|
-
this._object(fn);
|
|
1025
|
-
}
|
|
1026
|
-
},
|
|
1027
|
-
_number: function(number) {
|
|
1028
|
-
return write("number:" + number.toString());
|
|
1029
|
-
},
|
|
1030
|
-
_xml: function(xml) {
|
|
1031
|
-
return write("xml:" + xml.toString());
|
|
1032
|
-
},
|
|
1033
|
-
_null: function() {
|
|
1034
|
-
return write("Null");
|
|
1035
|
-
},
|
|
1036
|
-
_undefined: function() {
|
|
1037
|
-
return write("Undefined");
|
|
1038
|
-
},
|
|
1039
|
-
_regexp: function(regex) {
|
|
1040
|
-
return write("regex:" + regex.toString());
|
|
1041
|
-
},
|
|
1042
|
-
_uint8array: function(arr) {
|
|
1043
|
-
write("uint8array:");
|
|
1044
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1045
|
-
},
|
|
1046
|
-
_uint8clampedarray: function(arr) {
|
|
1047
|
-
write("uint8clampedarray:");
|
|
1048
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1049
|
-
},
|
|
1050
|
-
_int8array: function(arr) {
|
|
1051
|
-
write("int8array:");
|
|
1052
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1053
|
-
},
|
|
1054
|
-
_uint16array: function(arr) {
|
|
1055
|
-
write("uint16array:");
|
|
1056
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1057
|
-
},
|
|
1058
|
-
_int16array: function(arr) {
|
|
1059
|
-
write("int16array:");
|
|
1060
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1061
|
-
},
|
|
1062
|
-
_uint32array: function(arr) {
|
|
1063
|
-
write("uint32array:");
|
|
1064
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1065
|
-
},
|
|
1066
|
-
_int32array: function(arr) {
|
|
1067
|
-
write("int32array:");
|
|
1068
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1069
|
-
},
|
|
1070
|
-
_float32array: function(arr) {
|
|
1071
|
-
write("float32array:");
|
|
1072
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1073
|
-
},
|
|
1074
|
-
_float64array: function(arr) {
|
|
1075
|
-
write("float64array:");
|
|
1076
|
-
return this.dispatch(Array.prototype.slice.call(arr));
|
|
1077
|
-
},
|
|
1078
|
-
_arraybuffer: function(arr) {
|
|
1079
|
-
write("arraybuffer:");
|
|
1080
|
-
return this.dispatch(new Uint8Array(arr));
|
|
1081
|
-
},
|
|
1082
|
-
_url: function(url) {
|
|
1083
|
-
return write("url:" + url.toString(), "utf8");
|
|
1084
|
-
},
|
|
1085
|
-
_map: function(map) {
|
|
1086
|
-
write("map:");
|
|
1087
|
-
var arr = Array.from(map);
|
|
1088
|
-
return this._array(arr, options.unorderedSets !== false);
|
|
1089
|
-
},
|
|
1090
|
-
_set: function(set) {
|
|
1091
|
-
write("set:");
|
|
1092
|
-
var arr = Array.from(set);
|
|
1093
|
-
return this._array(arr, options.unorderedSets !== false);
|
|
1094
|
-
},
|
|
1095
|
-
_file: function(file) {
|
|
1096
|
-
write("file:");
|
|
1097
|
-
return this.dispatch([file.name, file.size, file.type, file.lastModfied]);
|
|
1098
|
-
},
|
|
1099
|
-
_blob: function() {
|
|
1100
|
-
if (options.ignoreUnknown) {
|
|
1101
|
-
return write("[blob]");
|
|
1102
|
-
}
|
|
1103
|
-
throw Error('Hashing Blob objects is currently not supported\n(see https://github.com/puleos/object-hash/issues/26)\nUse "options.replacer" or "options.ignoreUnknown"\n');
|
|
1104
|
-
},
|
|
1105
|
-
_domwindow: function() {
|
|
1106
|
-
return write("domwindow");
|
|
1107
|
-
},
|
|
1108
|
-
_bigint: function(number) {
|
|
1109
|
-
return write("bigint:" + number.toString());
|
|
1110
|
-
},
|
|
1111
|
-
/* Node.js standard native objects */
|
|
1112
|
-
_process: function() {
|
|
1113
|
-
return write("process");
|
|
1114
|
-
},
|
|
1115
|
-
_timer: function() {
|
|
1116
|
-
return write("timer");
|
|
1117
|
-
},
|
|
1118
|
-
_pipe: function() {
|
|
1119
|
-
return write("pipe");
|
|
1120
|
-
},
|
|
1121
|
-
_tcp: function() {
|
|
1122
|
-
return write("tcp");
|
|
1123
|
-
},
|
|
1124
|
-
_udp: function() {
|
|
1125
|
-
return write("udp");
|
|
1126
|
-
},
|
|
1127
|
-
_tty: function() {
|
|
1128
|
-
return write("tty");
|
|
1129
|
-
},
|
|
1130
|
-
_statwatcher: function() {
|
|
1131
|
-
return write("statwatcher");
|
|
1132
|
-
},
|
|
1133
|
-
_securecontext: function() {
|
|
1134
|
-
return write("securecontext");
|
|
1135
|
-
},
|
|
1136
|
-
_connection: function() {
|
|
1137
|
-
return write("connection");
|
|
1138
|
-
},
|
|
1139
|
-
_zlib: function() {
|
|
1140
|
-
return write("zlib");
|
|
1141
|
-
},
|
|
1142
|
-
_context: function() {
|
|
1143
|
-
return write("context");
|
|
1144
|
-
},
|
|
1145
|
-
_nodescript: function() {
|
|
1146
|
-
return write("nodescript");
|
|
1147
|
-
},
|
|
1148
|
-
_httpparser: function() {
|
|
1149
|
-
return write("httpparser");
|
|
1150
|
-
},
|
|
1151
|
-
_dataview: function() {
|
|
1152
|
-
return write("dataview");
|
|
1153
|
-
},
|
|
1154
|
-
_signal: function() {
|
|
1155
|
-
return write("signal");
|
|
1156
|
-
},
|
|
1157
|
-
_fsevent: function() {
|
|
1158
|
-
return write("fsevent");
|
|
1159
|
-
},
|
|
1160
|
-
_tlswrap: function() {
|
|
1161
|
-
return write("tlswrap");
|
|
1162
|
-
}
|
|
1163
|
-
};
|
|
890
|
+
const formatted = values.map((v) => `'${escapeString(v)}'`).join(", ");
|
|
891
|
+
return `z.enum([${formatted}])`;
|
|
1164
892
|
}
|
|
1165
|
-
|
|
1166
|
-
return
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
end: function(b) {
|
|
1172
|
-
this.buf += b;
|
|
1173
|
-
},
|
|
1174
|
-
read: function() {
|
|
1175
|
-
return this.buf;
|
|
1176
|
-
}
|
|
1177
|
-
};
|
|
893
|
+
case "reference":
|
|
894
|
+
return "z.record(z.string(), z.unknown())";
|
|
895
|
+
case "array": {
|
|
896
|
+
const itemType = constraints?.arrayItemType ?? "string";
|
|
897
|
+
const itemCode = generatePrimitiveCode(itemType);
|
|
898
|
+
return applyArrayConstraintCode(`z.array(${itemCode})`, constraints);
|
|
1178
899
|
}
|
|
900
|
+
default:
|
|
901
|
+
return "z.unknown()";
|
|
1179
902
|
}
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
903
|
+
}
|
|
904
|
+
function generatePrimitiveCode(type) {
|
|
905
|
+
switch (type) {
|
|
906
|
+
case "string":
|
|
907
|
+
return "z.string()";
|
|
908
|
+
case "number":
|
|
909
|
+
return "z.number()";
|
|
910
|
+
case "boolean":
|
|
911
|
+
return "z.boolean()";
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
function applyStringConstraintCode(base, constraints) {
|
|
915
|
+
if (!constraints) return base;
|
|
916
|
+
let code = base;
|
|
917
|
+
if (constraints.minLength !== void 0) {
|
|
918
|
+
code += `.min(${constraints.minLength}, 'Must be at least ${constraints.minLength} characters')`;
|
|
919
|
+
}
|
|
920
|
+
if (constraints.maxLength !== void 0) {
|
|
921
|
+
code += `.max(${constraints.maxLength}, 'Must be at most ${constraints.maxLength} characters')`;
|
|
922
|
+
}
|
|
923
|
+
if (constraints.pattern) {
|
|
924
|
+
code += `.regex(/${escapeRegex(constraints.pattern)}/, 'Invalid format')`;
|
|
925
|
+
}
|
|
926
|
+
return code;
|
|
927
|
+
}
|
|
928
|
+
function applyNumberConstraintCode(base, constraints) {
|
|
929
|
+
if (!constraints) return base;
|
|
930
|
+
let code = base;
|
|
931
|
+
if (constraints.integer) {
|
|
932
|
+
code += `.int('Must be an integer')`;
|
|
933
|
+
}
|
|
934
|
+
if (constraints.min !== void 0) {
|
|
935
|
+
code += `.min(${constraints.min}, 'Must be at least ${constraints.min}')`;
|
|
936
|
+
}
|
|
937
|
+
if (constraints.max !== void 0) {
|
|
938
|
+
code += `.max(${constraints.max}, 'Must be at most ${constraints.max}')`;
|
|
939
|
+
}
|
|
940
|
+
return code;
|
|
941
|
+
}
|
|
942
|
+
function applyArrayConstraintCode(base, constraints) {
|
|
943
|
+
if (!constraints) return base;
|
|
944
|
+
let code = base;
|
|
945
|
+
if (constraints.minItems !== void 0) {
|
|
946
|
+
code += `.min(${constraints.minItems}, 'Must have at least ${constraints.minItems} items')`;
|
|
947
|
+
}
|
|
948
|
+
if (constraints.maxItems !== void 0) {
|
|
949
|
+
code += `.max(${constraints.maxItems}, 'Must have at most ${constraints.maxItems} items')`;
|
|
950
|
+
}
|
|
951
|
+
return code;
|
|
952
|
+
}
|
|
953
|
+
function escapeString(str) {
|
|
954
|
+
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
955
|
+
}
|
|
956
|
+
function escapeRegex(pattern) {
|
|
957
|
+
return pattern.replace(/\//g, "\\/");
|
|
958
|
+
}
|
|
959
|
+
function toCamelCase(str) {
|
|
960
|
+
return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
961
|
+
}
|
|
962
|
+
function toPascalCase(str) {
|
|
963
|
+
const camel = toCamelCase(str);
|
|
964
|
+
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
965
|
+
}
|
|
1185
966
|
|
|
1186
967
|
// ../../packages/cms-schema/src/documents/registry.ts
|
|
1187
968
|
import { z } from "zod";
|
|
@@ -1493,172 +1274,11 @@ function rehydrateSchema(fields) {
|
|
|
1493
1274
|
}
|
|
1494
1275
|
|
|
1495
1276
|
// ../../packages/cms-schema/src/documents/schema-hash.ts
|
|
1496
|
-
|
|
1277
|
+
import hash from "object-hash";
|
|
1497
1278
|
|
|
1498
1279
|
// ../../packages/cms-schema/src/documents/validations/country.ts
|
|
1499
1280
|
import { TRPCError } from "@trpc/server";
|
|
1500
1281
|
|
|
1501
|
-
// ../../packages/cms-schema/src/documents/generate-zod-code.ts
|
|
1502
|
-
function generateZodSchemaCode(schemaName, fields) {
|
|
1503
|
-
const lines = [];
|
|
1504
|
-
lines.push(`import { z } from 'zod';`);
|
|
1505
|
-
lines.push("");
|
|
1506
|
-
const hasImageField = fields.some((f) => f.type === "image");
|
|
1507
|
-
if (hasImageField) {
|
|
1508
|
-
appendImageReferenceSchema(lines);
|
|
1509
|
-
lines.push("");
|
|
1510
|
-
}
|
|
1511
|
-
appendSchemaBody(lines, schemaName, fields);
|
|
1512
|
-
return lines.join("\n");
|
|
1513
|
-
}
|
|
1514
|
-
function generateCombinedZodSchemaCode(schemas) {
|
|
1515
|
-
const lines = [];
|
|
1516
|
-
lines.push(`import { z } from 'zod';`);
|
|
1517
|
-
lines.push("");
|
|
1518
|
-
const hasImageField = schemas.some((s) => s.fields.some((f) => f.type === "image"));
|
|
1519
|
-
if (hasImageField) {
|
|
1520
|
-
appendImageReferenceSchema(lines);
|
|
1521
|
-
lines.push("");
|
|
1522
|
-
}
|
|
1523
|
-
for (let i = 0; i < schemas.length; i++) {
|
|
1524
|
-
const schema = schemas[i];
|
|
1525
|
-
if (i > 0) lines.push("");
|
|
1526
|
-
appendSchemaBody(lines, schema.name, schema.fields);
|
|
1527
|
-
}
|
|
1528
|
-
return lines.join("\n");
|
|
1529
|
-
}
|
|
1530
|
-
function appendSchemaBody(lines, schemaName, fields) {
|
|
1531
|
-
const varName = toCamelCase(schemaName) + "Schema";
|
|
1532
|
-
lines.push(`export const ${varName} = z.object({`);
|
|
1533
|
-
for (const field of fields) {
|
|
1534
|
-
const fieldCode = generateFieldCode(field);
|
|
1535
|
-
if (field.description) {
|
|
1536
|
-
lines.push(` /** ${field.description} */`);
|
|
1537
|
-
}
|
|
1538
|
-
lines.push(` ${field.name}: ${fieldCode},`);
|
|
1539
|
-
}
|
|
1540
|
-
lines.push("});");
|
|
1541
|
-
lines.push("");
|
|
1542
|
-
lines.push(`export type ${toPascalCase(schemaName)} = z.infer<typeof ${varName}>;`);
|
|
1543
|
-
lines.push("");
|
|
1544
|
-
}
|
|
1545
|
-
function appendImageReferenceSchema(lines) {
|
|
1546
|
-
lines.push("const ImageReferenceSchema = z.object({");
|
|
1547
|
-
lines.push(" alt: z.string().min(1, 'Alt text is required for accessibility'),");
|
|
1548
|
-
lines.push(" caption: z.string().max(500).optional(),");
|
|
1549
|
-
lines.push(" attribution: z.string().max(255).optional(),");
|
|
1550
|
-
lines.push(" _asset: z.object({");
|
|
1551
|
-
lines.push(" id: z.string().uuid(),");
|
|
1552
|
-
lines.push(" }),");
|
|
1553
|
-
lines.push("});");
|
|
1554
|
-
}
|
|
1555
|
-
function generateFieldCode(field) {
|
|
1556
|
-
let code = generateBaseTypeCode(field.type, field.constraints);
|
|
1557
|
-
if (!field.required) {
|
|
1558
|
-
code += ".optional()";
|
|
1559
|
-
}
|
|
1560
|
-
return code;
|
|
1561
|
-
}
|
|
1562
|
-
function generateBaseTypeCode(type, constraints) {
|
|
1563
|
-
switch (type) {
|
|
1564
|
-
case "string":
|
|
1565
|
-
return applyStringConstraintCode("z.string()", constraints);
|
|
1566
|
-
case "number":
|
|
1567
|
-
return applyNumberConstraintCode("z.number()", constraints);
|
|
1568
|
-
case "boolean":
|
|
1569
|
-
return "z.boolean()";
|
|
1570
|
-
case "image":
|
|
1571
|
-
return "ImageReferenceSchema";
|
|
1572
|
-
case "date":
|
|
1573
|
-
return "z.string().date('Invalid date format. Expected YYYY-MM-DD')";
|
|
1574
|
-
case "datetime":
|
|
1575
|
-
return "z.string().datetime({ offset: true, message: 'Invalid datetime format. Expected ISO 8601' })";
|
|
1576
|
-
case "url":
|
|
1577
|
-
return applyStringConstraintCode("z.string().url('Invalid URL format')", constraints);
|
|
1578
|
-
case "email":
|
|
1579
|
-
return applyStringConstraintCode("z.string().email('Invalid email format')", constraints);
|
|
1580
|
-
case "enum": {
|
|
1581
|
-
const values = constraints?.enumValues;
|
|
1582
|
-
if (!values || values.length === 0) {
|
|
1583
|
-
return "z.enum([''])";
|
|
1584
|
-
}
|
|
1585
|
-
const formatted = values.map((v) => `'${escapeString(v)}'`).join(", ");
|
|
1586
|
-
return `z.enum([${formatted}])`;
|
|
1587
|
-
}
|
|
1588
|
-
case "reference":
|
|
1589
|
-
return "z.record(z.string(), z.unknown())";
|
|
1590
|
-
case "array": {
|
|
1591
|
-
const itemType = constraints?.arrayItemType ?? "string";
|
|
1592
|
-
const itemCode = generatePrimitiveCode(itemType);
|
|
1593
|
-
return applyArrayConstraintCode(`z.array(${itemCode})`, constraints);
|
|
1594
|
-
}
|
|
1595
|
-
default:
|
|
1596
|
-
return "z.unknown()";
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
function generatePrimitiveCode(type) {
|
|
1600
|
-
switch (type) {
|
|
1601
|
-
case "string":
|
|
1602
|
-
return "z.string()";
|
|
1603
|
-
case "number":
|
|
1604
|
-
return "z.number()";
|
|
1605
|
-
case "boolean":
|
|
1606
|
-
return "z.boolean()";
|
|
1607
|
-
}
|
|
1608
|
-
}
|
|
1609
|
-
function applyStringConstraintCode(base, constraints) {
|
|
1610
|
-
if (!constraints) return base;
|
|
1611
|
-
let code = base;
|
|
1612
|
-
if (constraints.minLength !== void 0) {
|
|
1613
|
-
code += `.min(${constraints.minLength}, 'Must be at least ${constraints.minLength} characters')`;
|
|
1614
|
-
}
|
|
1615
|
-
if (constraints.maxLength !== void 0) {
|
|
1616
|
-
code += `.max(${constraints.maxLength}, 'Must be at most ${constraints.maxLength} characters')`;
|
|
1617
|
-
}
|
|
1618
|
-
if (constraints.pattern) {
|
|
1619
|
-
code += `.regex(/${escapeRegex(constraints.pattern)}/, 'Invalid format')`;
|
|
1620
|
-
}
|
|
1621
|
-
return code;
|
|
1622
|
-
}
|
|
1623
|
-
function applyNumberConstraintCode(base, constraints) {
|
|
1624
|
-
if (!constraints) return base;
|
|
1625
|
-
let code = base;
|
|
1626
|
-
if (constraints.integer) {
|
|
1627
|
-
code += `.int('Must be an integer')`;
|
|
1628
|
-
}
|
|
1629
|
-
if (constraints.min !== void 0) {
|
|
1630
|
-
code += `.min(${constraints.min}, 'Must be at least ${constraints.min}')`;
|
|
1631
|
-
}
|
|
1632
|
-
if (constraints.max !== void 0) {
|
|
1633
|
-
code += `.max(${constraints.max}, 'Must be at most ${constraints.max}')`;
|
|
1634
|
-
}
|
|
1635
|
-
return code;
|
|
1636
|
-
}
|
|
1637
|
-
function applyArrayConstraintCode(base, constraints) {
|
|
1638
|
-
if (!constraints) return base;
|
|
1639
|
-
let code = base;
|
|
1640
|
-
if (constraints.minItems !== void 0) {
|
|
1641
|
-
code += `.min(${constraints.minItems}, 'Must have at least ${constraints.minItems} items')`;
|
|
1642
|
-
}
|
|
1643
|
-
if (constraints.maxItems !== void 0) {
|
|
1644
|
-
code += `.max(${constraints.maxItems}, 'Must have at most ${constraints.maxItems} items')`;
|
|
1645
|
-
}
|
|
1646
|
-
return code;
|
|
1647
|
-
}
|
|
1648
|
-
function escapeString(str) {
|
|
1649
|
-
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
1650
|
-
}
|
|
1651
|
-
function escapeRegex(pattern) {
|
|
1652
|
-
return pattern.replace(/\//g, "\\/");
|
|
1653
|
-
}
|
|
1654
|
-
function toCamelCase(str) {
|
|
1655
|
-
return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
1656
|
-
}
|
|
1657
|
-
function toPascalCase(str) {
|
|
1658
|
-
const camel = toCamelCase(str);
|
|
1659
|
-
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
1660
|
-
}
|
|
1661
|
-
|
|
1662
1282
|
// lib/cms-api.ts
|
|
1663
1283
|
import { createTRPCClient, httpBatchLink } from "@trpc/client";
|
|
1664
1284
|
import superjson from "superjson";
|