Skip to content

Commit

Permalink
fix wrong relation field type and make code more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
maltejur committed Feb 14, 2023
1 parent 7725a81 commit eb83e90
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/lib/generateTypes/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default async function generateTsTypes(
ret += `export type ${typeName} = {\n`;
collection.fields.forEach((field) => {
if (field.meta?.interface?.startsWith("presentation-")) return;
ret += ` ${
field.field.includes("-") ? `"${field.field}"` : field.field
}${field.relation || field.schema?.is_nullable ? "?" : ""}: ${getType(
field,
useIntersectionTypes
)};\n`;
ret += " ";
ret += field.field.includes("-") ? `"${field.field}"` : field.field;
if (field.relation || field.schema?.is_nullable) ret += "?";
ret += ": ";
ret += getType(field, useIntersectionTypes);
ret += ";\n";
});
ret += "};\n\n";
});
Expand All @@ -47,15 +47,21 @@ function pascalCase(str: string) {

function getType(field: Field, useIntersectionTypes = false) {
let type: string;
if (["integer", "bigInteger", "float", "decimal"].includes(field.type))
type = "number";
else if (["boolean"].includes(field.type)) type = "boolean";
else if (["json", "csv"].includes(field.type)) type = "unknown";
else type = "string";
if (field.relation && field.relation.type === "many") {
type = "any[]";
} else {
if (["integer", "bigInteger", "float", "decimal"].includes(field.type))
type = "number";
else if (["boolean"].includes(field.type)) type = "boolean";
else if (["json", "csv"].includes(field.type)) type = "unknown";
else type = "string";
}
if (field.relation) {
type += ` ${useIntersectionTypes ? "&" : "|"} ${
field.relation.collection ? pascalCase(field.relation.collection) : "any"
}${field.relation.type === "many" ? "[]" : ""}`;
type += useIntersectionTypes ? " & " : " | ";
type += field.relation.collection
? pascalCase(field.relation.collection)
: "any";
if (field.relation.type === "many") type += "[]";
}
if (field.relation || field.schema?.is_nullable) {
if (field.relation && useIntersectionTypes) {
Expand Down

0 comments on commit eb83e90

Please sign in to comment.