directus-typescript-gen icon indicating copy to clipboard operation
directus-typescript-gen copied to clipboard

Include Directus' internal Collections in output file

Open Anoesj opened this issue 3 years ago • 9 comments

Hey @elierotenberg, thanks for your work on this package, it's very useful!

I'm working on a PR for the https://github.com/Intevel/nuxt-directus package and for this, I needed to get the types of Directus' internal collections.

This PR adds a --directusTypeName and --allTypeName flag. --directusTypeName will be the name of the exported type that includes Directus' internal collection schemas and defaults to DirectusCollections. --allTypeName will be the name of the type that returns all collection schemas (app + Directus internal collections) and defaults to Collections.

I also renamed the --typeName flag to --appTypeName and added an alias so --typeName is backwards compatible. I made the flag optional and let it default to AppCollections.

Furthermore, I changed some of the internals of the package. By reading the JSON parsed spec directly, we can read the x-collection property Directus adds to schema objects to get the ID of the collection, instead of using Regex and camel-casing tricks. This x-collection property is ignored by openApiTs(spec) which turns the generated OpenAPI3 spec into a large types file string, that's why we need to read from the JSON parsed spec instead.

Example of new output:

// omitted for brevity

export type AppCollections = {
  article: components["schemas"]["ItemsArticle"];
  article_author: components["schemas"]["ItemsArticleAuthor"];
  author: components["schemas"]["ItemsAuthor"];
};

export type DirectusCollections = {
  directus_activity: components["schemas"]["Activity"];
  directus_collections: components["schemas"]["Collections"];
  directus_fields: components["schemas"]["Fields"];
  directus_files: components["schemas"]["Files"];
  directus_files: components["schemas"]["Folders"];
  directus_permissions: components["schemas"]["Permissions"];
  directus_presets: components["schemas"]["Presets"];
  directus_relations: components["schemas"]["Relations"];
  directus_revisions: components["schemas"]["Revisions"];
  directus_roles: components["schemas"]["Roles"];
  directus_settings: components["schemas"]["Settings"];
  directus_users: components["schemas"]["Users"];
  directus_webhooks: components["schemas"]["Webhooks"];
  directus_flows: components["schemas"]["Flows"];
  directus_operations: components["schemas"]["Operations"];
};

export type Collections = AppCollections & DirectusCollections;

The tests probably need some changes. I couldn't run them to see if this is breaking some features, as Docker is currently broken on my machine 🙃. Finally, I think it's probably time to update the dependencies of this project sometime too, as pretty much every dependency is rather outdated.

Anoesj avatar Oct 10 '22 10:10 Anoesj

@Anoesj Have you integrated this to the nuxt-directus library ?

LilaRest avatar Apr 01 '23 09:04 LilaRest

@LilaRest Not integrated, I made a PR where you can pass all Directus Collection types into nuxt-directus at once, instead of one by one. I needed Directus' internal Collections for that as well.

Anoesj avatar Apr 01 '23 13:04 Anoesj

@LilaRest Not integrated, I made a PR where you can pass all Directus Collection types into nuxt-directus at once, instead of one by one. I needed Directus' internal Collections for that as well.

@Anoesj Oh ok, so it's not merged yet. I'll consider using your fork until it's merged, that's an awesome feature ! Thanks for you work :)

LilaRest avatar Apr 01 '23 13:04 LilaRest

@LilaRest thanks! I didn't find the time yet to work on the feedback I received (which includes not introducing breaking changes while also improving the typings).

Anoesj avatar Apr 01 '23 13:04 Anoesj

In case you're intested in helping: https://github.com/directus-community/nuxt-directus/pull/79 😄

Anoesj avatar Apr 01 '23 13:04 Anoesj

@Anoesj Yeah I can understand, I don't think I'll have time to work on this too, maybe in the next months. Is the breaking change the only problem of your PR ? I mean do you consider it as stable ?

LilaRest avatar Apr 01 '23 13:04 LilaRest

@LilaRest Mainly that yes, I use it in production and I'm very happy with it!

Anoesj avatar Apr 01 '23 14:04 Anoesj

@Anoesj Great, thanks for your answers ^^

LilaRest avatar Apr 01 '23 14:04 LilaRest