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

Feature request: Customize file name #164

Closed
tatemz opened this issue Nov 10, 2022 · 6 comments
Closed

Feature request: Customize file name #164

tatemz opened this issue Nov 10, 2022 · 6 comments

Comments

@tatemz
Copy link
Contributor

tatemz commented Nov 10, 2022

Currently, it is not possible to override the file name

Can the moduleName function used here be from a context instead?

@marcolink
Copy link
Collaborator

marcolink commented Nov 18, 2022

Hi @tatemz
What input would be relevant for you to compute a custom file name?
Would the ContentType name be enough?

@marcolink
Copy link
Collaborator

marcolink commented Nov 18, 2022

@tatemz
Ok, I looked a bit deeper into this, what you can do:

Create your own Renderer by extending The DefaultContentTypeRenderer and override the createContext method.

Instead of returning the Context created by createDefaultContext, you could return the same context, but use a different implementation for the moduleName function.

Finally you can set your new renderer when instantiating a new CFDefinitionBuilder here.

This should do the trick :)

@marcolink
Copy link
Collaborator

closing this for now

@tatemz
Copy link
Contributor Author

tatemz commented Jan 27, 2023

Hey so I came back to this and my team and I ended up doing exactly what you suggested by extending the renderer. We actually made two versions, one generated the TS, and another generated runtypes (link) configuration to ensure runtime validity for some of our models.

Here's the sample output. We had to use Runtypes.Lazy() to support possible recursive types in some of our Contentful models.

import * as Contentful from 'contentful';
import * as Runtypes from 'runtypes';
import { Asset } from '../Asset';

export interface Author {
    contentType: 'author';
    authorName?: Contentful.EntryFields.Symbol;
    authorImage?: Asset;
}

export const Author: Runtypes.Runtype<Author> = Runtypes.Lazy(() =>
    Runtypes.Record({
        authorName: Runtypes.Optional(Runtypes.String),
        authorImage: Runtypes.Optional(Asset),
        contentType: Runtypes.Literal('author'),
    })
);

Thanks for a great tool!

@marcolink
Copy link
Collaborator

@tatemz This seems to be a pretty cool use case 😍

Would you be interested to open a PR and add it to the project?

@marcolink marcolink reopened this Jan 28, 2023
@marcolink
Copy link
Collaborator

@tatemz added a simple type-guard generator: #226

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

No branches or pull requests

2 participants