Skip to content

Commit

Permalink
Merge pull request #56 from cjpearson/component-typing
Browse files Browse the repository at this point in the history
Better typing when there are no component restrictions
  • Loading branch information
dohomi authored Nov 1, 2023
2 parents 06c373d + a400b9a commit 13d666c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ describe("storyblokToTypescript", () => {
});
const mainType = prepareString(types[2]);
const expectation = makeExpectString(`
myField?:any[];
myRequiredField:any[];
myField?:ResourceNameStoryblok[];
myRequiredField:ResourceNameStoryblok[];
`);

expect(mainType).toBe(expectation);
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ export default async function storyblokToTypescript({
const getStoryTypeTitle = (t: string) => `StoryblokStory<${camelcase(getTitle(t), {pascalCase: true})}>`

const groupUuids: { [k: string]: JSONSchema4 } = {}
const allComponents: string[] = []

componentsJson.components.forEach(value => {
const componentName = camelcase(getTitle(value.name), {
pascalCase: true
})
if (value.component_group_uuid) {
if (!groupUuids[value.component_group_uuid]) {
groupUuids[value.component_group_uuid] = []
}
groupUuids[value.component_group_uuid].push(camelcase(getTitle(value.name), {
pascalCase: true
}))
groupUuids[value.component_group_uuid].push(componentName)
}

allComponents.push(componentName)
})

async function genTsSchema() {
Expand Down Expand Up @@ -167,6 +171,7 @@ export default async function storyblokToTypescript({
}
} else {
console.log('Type: bloks array but not whitelisted (will result in all elements):', title)
obj[key].tsType = `(${allComponents.join(' | ')})[]`
}
}
Object.assign(parseObj, obj)
Expand Down

0 comments on commit 13d666c

Please sign in to comment.