Skip to content

Commit

Permalink
feat(resolveLinks): add array of options
Browse files Browse the repository at this point in the history
  • Loading branch information
RKcode committed Apr 8, 2024
1 parent 7153926 commit e0f7d69
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -14,6 +14,9 @@ const parseValue = (value: string) => {
if (value.match(/^[\d.]+$/)) {
return Number(value)
}
if (value.includes(',')) {
return value.split(',')
}
return value
}

Expand Down Expand Up @@ -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'
}
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/genericTypes.ts
Original file line number Diff line number Diff line change
@@ -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<string | undefined>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) } }
: {}),
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
8 changes: 6 additions & 2 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ export type BasicType = 'asset' | 'multiasset' | 'multilink' | 'table' | 'richte

export type CompilerOptions = Partial<Options>;

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
}

export interface CliOptions {
source: string
target?: string
resolveLinks?: string | string[]
titleSuffix?: string
titlePrefix?: string
customTypeParser?: string
Expand Down

0 comments on commit e0f7d69

Please sign in to comment.