Skip to content

Commit

Permalink
fixes some typing issues with system collections
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLfulus committed Dec 7, 2023
1 parent 42962f6 commit 1af029c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
9 changes: 6 additions & 3 deletions packages/cli/default/extensions/filters/directus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,20 @@ export function to_ts_type(context: TemplateContext, field: Field) {
} else {
if (field.type.relationship?.type == "o2m") {
types.push(
to_collection_name(context, field.type.relationship.ref.collection),
"Collections." +
to_collection_name(context, field.type.relationship.ref.collection),
);
}
if (field.type.relationship?.type == "m2o") {
types.push(
to_collection_name(context, field.type.relationship.ref.collection),
"Collections." +
to_collection_name(context, field.type.relationship.ref.collection),
);
}
if (field.type.relationship?.type == "a2o") {
field.type.relationship.refs.forEach((ref) => {
types.push(to_collection_name(context, ref.collection));
"Collections." +
types.push(to_collection_name(context, ref.collection));
});
}
}
Expand Down
63 changes: 23 additions & 40 deletions packages/cli/default/templates/default/client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,21 @@ export namespace Types {
}

/**
* System collections.
* All collection types.
*/

export namespace Collections {

{% for collection in registry.collections -%}
{% if collection.is_system %}

{%- set field_count = collection.fields | only_custom_fields | length -%}
{%- if field_count > 0 -%}
{% filter comment -%}
The extra fields added to the {{ collection.name | to_collection_text }} collection.
{% endfilter %}
export interface Extra{{ collection.name | to_collection_name }} {
{%- for field in collection.fields | only_custom_fields %}
{%- set type = field | to_ts_type -%}
{%- if type != 'never' %}
{{ field.name | to_ts_identifier }}: {{ type }};
{%- endif -%}
{%- endfor %}
};
{% endif %}

{% filter comment -%}
The resolved {{ collection.name | to_collection_text }} collection type.
{% endfilter %}
export type {{ collection.name | to_collection_name }} = Directus.{{ collection.name | to_collection_name }}<Schema>;
export type {{ collection.name | to_collection_name }} = Directus.{{ collection.name | to_collection_name }}<System>;
{% endif %}
{% endfor %}
}



/**
* User collections.
*/
export namespace Collections {
{% for collection in registry.collections -%}
{% if not collection.is_system %}
/**
Expand All @@ -112,31 +91,36 @@ export namespace Collections {
{% endfor %}
}


/**
* System collections.
* System schema extensions.
*/
export interface Schema {

{% for collection in registry.collections | only_with_custom_fields -%}
{% if collection.is_system %}
{% set suffix = "" if collection.is_singleton else "[]" %}
export interface System {

{%- set field_count = collection.fields | only_custom_fields | length -%}
/**
* The extended {{ collection.name | to_collection_text }} collection.
*/
{{ collection.name | to_ts_identifier }}: Collections.Extra{{ collection.name | to_collection_name }}{{ suffix }};
{% endif %}
{% for collection in registry.collections -%}
{% if collection.is_system %}

{% filter comment -%}
The definition for the {{ collection.name | to_collection_text }} system collection.
{% endfilter %}
{{ collection.name }}: {
{%- for field in collection.fields | only_custom_fields %}
{%- set type = field | to_ts_type -%}
{%- if type != 'never' %}
{{ field.name | to_ts_identifier }}: {{ type }};
{%- endif -%}
{%- endfor %}
}{{ "" if collection.is_singleton else "[]" }};

{% endif %}
{% endfor %}

}


/**
* User collections.
* Schema definition.
*/
export interface Schema {
export interface Schema extends System {
{% for collection in registry.collections -%}
{% if not collection.is_system %}
{% set suffix = "" if collection.is_singleton else "[]" %}
Expand All @@ -146,7 +130,6 @@ export interface Schema {
{{ collection.name | to_ts_identifier }}: Collections.{{ collection.name | pascal_case }}{{ suffix }};
{% endif %}
{% endfor %}

}


Expand Down

0 comments on commit 1af029c

Please sign in to comment.