From e0f7d69641beedfe6d309baff47ca32479b161d6 Mon Sep 17 00:00:00 2001 From: Kevin Rousse Date: Mon, 8 Apr 2024 17:40:41 +0200 Subject: [PATCH] feat(resolveLinks): add array of options --- src/cli.ts | 24 +++++++++++++++++++++++- src/genericTypes.ts | 8 +++++--- src/index.ts | 2 +- src/typings.ts | 8 ++++++-- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index f4013f5..12888cb 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import storyblokToTypescript from './index' import {resolve} from 'path' -import {CliOptions, StoryblokTsOptions} from "./typings"; +import {CliOptions, ResolveLinkOption, StoryblokTsOptions} from "./typings"; import * as fs from 'fs'; import {JSONSchema4} from "json-schema"; @@ -14,6 +14,9 @@ const parseValue = (value: string) => { if (value.match(/^[\d.]+$/)) { return Number(value) } + if (value.includes(',')) { + return value.split(',') + } return value } @@ -70,6 +73,21 @@ if (!props.source) { process.exit() } +const isResolveLinkOption = (param: string): param is ResolveLinkOption => + ["url", "link", "story"].includes(param) + +const isValidResolveLinkOption = ( + param?: string | string[] +): param is ResolveLinkOption | ResolveLinkOption[] => + (typeof param === "string" && isResolveLinkOption(param)) || + (Array.isArray(param) && param.every(isResolveLinkOption)) + +const resolveLinks = props.resolveLinks +if (resolveLinks !== undefined && !isValidResolveLinkOption(resolveLinks)) { + console.log('resolveLinks must be a string with values "url", "link" or "story" separated by commas') + process.exit() +} + if (props.target && !props.target.endsWith('.ts')) { props.target += '.d.ts' } @@ -83,6 +101,10 @@ getDataFromPath(props.source).then((rawComponents) => { compilerOptions: props.compilerOptions || {}, path: resolve(props.target || './storyblok-component-types.d.ts') } + + if (resolveLinks) { + options.resolveLinks = Array.isArray(resolveLinks) ? resolveLinks : [resolveLinks] + } if (props.titlePrefix !== undefined) { options.titlePrefix = props.titlePrefix diff --git a/src/genericTypes.ts b/src/genericTypes.ts index 3494f3b..d246919 100644 --- a/src/genericTypes.ts +++ b/src/genericTypes.ts @@ -1,7 +1,7 @@ import { JSONSchema4 } from 'json-schema'; import { compile } from 'json-schema-to-typescript'; -import { BasicType, CompilerOptions, StoryblokResolveOptions } from './typings'; +import { BasicType, CompilerOptions, ResolveLinkOption, StoryblokResolveOptions } from './typings'; const typeFuncs: { [k in BasicType]: (name: string, options: CompilerOptions, storyblokResolve: StoryblokResolveOptions) => Promise @@ -154,7 +154,7 @@ async function generateMultiAssetTypeIfNotYetGenerated(title: string, compilerOp } } -function getStoryLinkTypeByResolveLink(resolveLinkOption?: "url" | "link" | "story"): JSONSchema4 { +function getStoryLinkTypeByResolveLink(resolveLinkOption: ResolveLinkOption): JSONSchema4 { switch (resolveLinkOption) { case "url": return { @@ -334,7 +334,9 @@ async function generateMultiLinkTypeIfNotYetGenerated(title: string, compilerOpt type: 'string', enum: ['_self', '_blank'], }, - story: getStoryLinkTypeByResolveLink(storyblokResolve.resolveLinks) + ...(storyblokResolve.resolveLinks.length + ? { story: { oneOf: storyblokResolve.resolveLinks.map(getStoryLinkTypeByResolveLink) } } + : {}), } }, { diff --git a/src/index.ts b/src/index.ts index 2f8644c..f1ec364 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ export default async function storyblokToTypescript({ path = 'src/typings/generated/components-schema.ts', titleSuffix = '_storyblok', titlePrefix = '', - resolveLinks = "story" + resolveLinks = [] }: StoryblokTsOptions) { const storyblokResolveOptions = { resolveLinks }; diff --git a/src/typings.ts b/src/typings.ts index 667fa99..7f63365 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -20,17 +20,20 @@ export type BasicType = 'asset' | 'multiasset' | 'multilink' | 'table' | 'richte export type CompilerOptions = Partial; +export type ResolveLinkOption = "url" | "link" | "story" + export interface StoryblokResolveOptions { - resolveLinks?: "url" | "link" | "story" + resolveLinks: ResolveLinkOption[] } -export interface StoryblokTsOptions extends StoryblokResolveOptions { +export interface StoryblokTsOptions { componentsJson: { components: JSONSchema4[] }, customTypeParser?: (key: string, options: JSONSchema4) => void compilerOptions?: CompilerOptions path?: string + resolveLinks?: ResolveLinkOption[] titleSuffix?: string titlePrefix?: string } @@ -38,6 +41,7 @@ export interface StoryblokTsOptions extends StoryblokResolveOptions { export interface CliOptions { source: string target?: string + resolveLinks?: string | string[] titleSuffix?: string titlePrefix?: string customTypeParser?: string