Giter Club home page Giter Club logo

Comments (6)

tatemz avatar tatemz commented on May 20, 2024 2

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!

from cf-content-types-generator.

marcolink avatar marcolink commented on May 20, 2024

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

from cf-content-types-generator.

marcolink avatar marcolink commented on May 20, 2024

@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 :)

from cf-content-types-generator.

marcolink avatar marcolink commented on May 20, 2024

closing this for now

from cf-content-types-generator.

marcolink avatar marcolink commented on May 20, 2024

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

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

from cf-content-types-generator.

marcolink avatar marcolink commented on May 20, 2024

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

from cf-content-types-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.