jsaari97 / netlify-ts Goto Github PK
View Code? Open in Web Editor NEWTurn your Netlify CMS collections into TypeScript types!
License: MIT License
Turn your Netlify CMS collections into TypeScript types!
License: MIT License
Hi having problems with the file output location of the types file
When I'm running on the CLI, the output argument will always use the default value of netlify-types.ts
netlify-ts public/admin/config.yml src/my-types.ts
outputs a file to src/my-types.ts
netlify-ts public/admin/config.yml src/my-types.ts
outputs a file to netlify-types.ts
WSL2 Ubuntu on windows 11
npm 6.13.15
node 14.17.6
Hi, first of all - nice project!
If I have a collection like this:
name: "cars"
label: "Cars"
label_singular: "Car"
the generated interface is called cars
. That is a good default because it is the unique identifier. It would, however, be more idiomatic to name the interface Car
.
What do you think about adding an opt-in configuration for using the label_singular
as interface name? Or maybe a way to define a custom mapping for interface names?
Best regards
Hey, first of all, this is a very nice library!
If I define a field with widget: "number"
, the generated type for this field is a string
. Is there a reasoning behind this or could this be changed to number
?
Hi ๐
Merry Christmas and Happy New Year!
Minor tweak here. The default value_type
for widget type number seems to be int
. So if you don't specify it, it will type fields as strings
netlify-ts/src/widget/resolve.ts
Lines 37 to 38 in 3d45884
Makes sense since the docs describe value_type
as it defaults to string
:
value_type: accepts int or float; any other value results in saving as a
string
But that is simply not true :P
https://github.com/netlify/netlify-cms/blob/88a5a8098e16f36254d96ec0b814d27e4415f100/packages/netlify-cms-widget-number/src/NumberControl.js#L71-L81
If an image
or file
widget has property media_library.config.multiple
set to true
, and is using an external filesystem (eg. Cloudinary or Uploadcare) โ then the widget type should resolve to string[]
, as the widget data type is saved as an array of strings.
In other cases the data type should be string
.
also it fails if field name is something like 'large_video'
in transform.js
const pattern = new RegExp(`(${typeNames.map((w) => `${name}_${w}`).join("|")}) {`);
here's my regexp
/(Post_Slice_Hero|Post_Slice_editor|Post_Slice_large_video) {/
and here's what pattern.test runs against (for example)
'interface Page_Slice_LargeVideo { title: string; description?: string; media: any; poster?: string; }',
so 'rest' and 'interfaces' arrays get messed up and types eventually are broken
UPD: some more info
I believe it happens only to "typed lists" when label is used to set widgetName here
I'll have to disable labels anyways as they are less permanent and robust.
Thanks for a great module BTW. I wonder why it's not more popular. It would be great to have a way to parse a js object instead of config.yml to use with manual init .
Hello! It seems the body field is missing from my types. I haven't managed to track down the cause (don't have time now), but definitely happening in the transformType()
function.
This console.log()
could help in tracking down the cause.
if (widget.name === "body") {
console.log('body types', types);
// outputs: body types [ [ 'title: string;', 'date: string;' ], [] ]
return types;
}
My config and generated type:
Config.yml
...
collections:
- label: "Blog"
name: "blog"
folder: "content/post"
create: true
slug: "{{slug}}"
fields:
- { label: "Title", name: "title", widget: "string", required: true }
- {
label: "Publish date",
name: "publishDate",
widget: "datetime",
required: true,
}
- {
label: "Featured Image",
name: "thumbnail",
widget: "image",
required: false,
}
- { label: "Body", name: "body", widget: "markdown", required: true }
- {
label: "Created date",
name: "createdDate",
widget: "datetime",
required: true,
}
...
Generated type:
export interface Blog {
title: string;
publishDate: string;
thumbnail?: string;
createdDate: string;
}
If you translate anything to accomodate the end user, it's the label. The names, however, are more of "code" than anything else, so the types should be named from the names of the fields and collections rather than the labels.
When generating fields with the widget datetime
I'd like them to be typed as Date because gray-matter automatically parses these fields to Date.
Example Netlify config.yml:
collections:
- label: "Blog"
name: "blog"
fields:
- {
label: "Published date",
name: "publishedDate",
widget: "datetime",
required: true,
}
Generated types:
export interface Blog {
publishedDate: string;
}
Desired generated type:
export interface Blog {
publishedDate: Date;
}
My setup:
"gray-matter": "^4.0.3",
"netlify-ts": "^2.0.0",
For situations when promises aren't supported (for example, inside an eslint rule), would you be open to a netlifyTs.sync(...)
function? It seems like it's only the filesystem-reading that relies on promises, and those APIs have sync equivalents too. I'd be happy to put up a PR if so.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.