Skip to content

Commit

Permalink
fixed invalid type names with dashes (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
maltejur committed Sep 24, 2022
1 parent 6894780 commit 556413c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/lib/generateTypes/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export default async function generateTsTypes(
types.push(`${collectionName}: ${typeName}`);
ret += `export type ${typeName} = {\n`;
collection.fields.forEach((field) => {
ret += ` ${
field.field.includes("-") ? `"${field.field}"` : field.field
}${field.schema?.is_nullable ? "?" : ""}: ${getType(
field,
useIntersectionTypes
)};\n`;
let fieldName = field.field;
if (fieldName.includes("-"))
fieldName = `"${fieldName}"`;
ret += ` ${field.field.includes("-") ? `"${fieldName}"` : fieldName
}${field.schema?.is_nullable ? "?" : ""}: ${getType(
field,
useIntersectionTypes
)};\n`;
});
ret += "};\n\n";
});
Expand Down Expand Up @@ -52,9 +54,8 @@ function getType(field: Field, useIntersectionTypes = false) {
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 ? "&" : "|"} ${field.relation.collection ? pascalCase(field.relation.collection) : "any"
}${field.relation.type === "many" ? "[]" : ""}`;
}
return type;
}

0 comments on commit 556413c

Please sign in to comment.