Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add optional system collections support #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

StephenGunn
Copy link

@StephenGunn StephenGunn commented Dec 13, 2024

Summary

This pull request introduces an optional --includeSystemCollections CLI flag to include Directus system collections in the generated TypeScript definitions. By default, the tool only generates types for user-defined collections discovered through spec.paths. With this new flag enabled, we also check spec.components.schemas for x-collection fields, adding those collections to the final output.

Changes

  • Added a new --includeSystemCollections boolean flag (default false).
  • When enabled, system collections defined in spec.components.schemas that have an x-collection property are included in the generated types.
  • To prevent duplicates (where the same collection might appear both in paths and components.schemas), we now gather all collections first in a collections object and only write them out once at the end. This avoids repetitive entries without heavily modifying the existing if (spec.paths) logic, keeping the original structure intact.

Reasoning Behind the Logic Change

Initially, collections were appended directly to the output source as they were discovered. With the addition of system collections (and thus a second method of discovering them), it became possible to generate the same collection twice. To fix this without diverging too much from the original approach, I switched to a two-step process:

  1. Collect all user-defined and optional system collections into a single data structure.
  2. Write them out in one pass, ensuring each collection is listed only once.

This preserves most of the original logic while simply deferring when we write to source.

Testing

  • Verified locally by generating types with and without the --includeSystemCollections flag.
  • Confirmed that enabling the flag adds system collections and doesn’t produce duplicates.
  • Confirmed that disabling the flag results in the original behavior, maintaining backward compatibility.
  • Checked against a running Directus server and a directus.oas.json with identical results
  • Builds with no errors or warnings.

Documentation

  • Updated the README to describe the new --includeSystemCollections option and its usage.

Impact

  • Minor version bump (1.1.0) since this is a backward-compatible enhancement.
  • No breaking changes introduced.

Thank you for this project, hope you understand why this will help!

add --includeSystemCollections or -s to include the directus system
collections to the output. This allows custom fields on collections like
directus_users from causing type errors in the SDK.
Made it easier to read the system collections info in readme

changed the readme to follow original flow and added my flag to the end

reverted small readme changes for clarity

reverted small style changes my lsp setup decided to do on its own

more formatting changes to match original

fixed formatting to match original
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant