-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement relational fields * use intersection type instead of union type for relational fields
- Loading branch information
Showing
6 changed files
with
475 additions
and
915 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function log(str: string) { | ||
console.log( | ||
`%c[directus-extension-generate-types]%c\n${str}`, | ||
"font-weight: bold;" | ||
); | ||
} | ||
|
||
export function warn(str: string) { | ||
console.warn( | ||
`%c[directus-extension-generate-types]%c\n${str}`, | ||
"font-weight: bold;" | ||
); | ||
} | ||
|
||
export function error(str: string) { | ||
console.error( | ||
`%c[directus-extension-generate-types]%c\n${str}`, | ||
"font-weight: bold;" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,53 @@ | ||
import { Field } from "lib/types"; | ||
import { getCollections } from "../api"; | ||
|
||
export default async function generateTsTypes(api) { | ||
const collections = await getCollections(api); | ||
let ret = ""; | ||
const types = []; | ||
|
||
ret += Object.values(collections) | ||
.map((collection) => { | ||
const collectionName = collection.collection; | ||
const typeName = collectionName | ||
.split("_") | ||
.map((x) => x.charAt(0).toUpperCase() + x.slice(1)) | ||
.join(""); | ||
types.push(`${collectionName}: ${typeName}`); | ||
return `export type ${typeName} = { | ||
${Object.values(collection.fields) | ||
.map( | ||
(x) => | ||
` ${x.field}${x.schema?.is_nullable ? "?" : ""}: ${getType(x.type)};` | ||
) | ||
.join("\n")} | ||
};`; | ||
}) | ||
.join("\n\n"); | ||
Object.values(collections).forEach((collection) => { | ||
const collectionName = collection.collection; | ||
const typeName = pascalCase(collectionName); | ||
types.push(`${collectionName}: ${typeName}`); | ||
ret += `export type ${typeName} = {\n`; | ||
collection.fields.forEach((field) => { | ||
ret += ` ${field.field}${ | ||
field.schema?.is_nullable ? "?" : "" | ||
}: ${getType(field)};\n`; | ||
}); | ||
ret += "};\n\n"; | ||
}); | ||
|
||
ret += "\n\n"; | ||
|
||
ret += `export type CustomDirectusTypes = { | ||
${types.map((x) => ` ${x};`).join("\n")} | ||
};`; | ||
ret += | ||
"export type CustomDirectusTypes = {\n" + | ||
types.map((x) => ` ${x};`).join("\n") + | ||
"\n};"; | ||
|
||
ret += "\n"; | ||
|
||
return ret; | ||
} | ||
|
||
function getType(directusType: string) { | ||
if (["integer", "bigInteger", "float", "decimal"].includes(directusType)) | ||
return "number"; | ||
if (["boolean"].includes(directusType)) return "boolean"; | ||
if (["json", "csv"].includes(directusType)) return "unknown"; | ||
return "string"; | ||
function pascalCase(str: string) { | ||
return str | ||
.split(" ") | ||
.flatMap((x) => x.split("_")) | ||
.map((x) => x.charAt(0).toUpperCase() + x.slice(1)) | ||
.join(""); | ||
} | ||
|
||
function getType(field: Field) { | ||
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) { | ||
type += ` & ${pascalCase(field.relation.collection)}${ | ||
field.relation.type === "many" ? "[]" : "" | ||
}`; | ||
} | ||
return type; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import type { | ||
Collection as DirectusCollection, | ||
Field, | ||
Field as DirectusField, | ||
} from "@directus/shared/types"; | ||
|
||
export type Field = DirectusField & { | ||
relation?: { | ||
type: "many" | "one"; | ||
collection: string; | ||
}; | ||
}; | ||
export type Collection = DirectusCollection & { fields: Field[] }; | ||
export type Collections = { [collection: string]: Collection }; |
Oops, something went wrong.