Skip to content

Commit

Permalink
feat: update saveComponentsToFiles function and related tests
Browse files Browse the repository at this point in the history
- Updated actions to use customFetch instead of ofetch
- Modified the `saveComponentsToFiles` function to accept a structured `spaceData` object containing components, groups, and presets, instead of separate parameters.
- Updated the default filename behavior to save as `components.json` instead of including the space ID in the filename.
- Enhanced test cases to reflect changes in the function signature and filename generation logic.
- Removed unused imports and interfaces to streamline the codebase.
  • Loading branch information
alvarosabu committed Jan 16, 2025
1 parent 20bca5a commit 8472a25
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 128 deletions.
29 changes: 22 additions & 7 deletions src/commands/components/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ describe('pull components actions', () => {
interntal_tags_ids: [1],
}]

await saveComponentsToFiles('12345', components, { path: '/path/to/components' })
await saveComponentsToFiles('12345', { components, groups: [], presets: [] }, {
path: '/path/to/components',
verbose: false,
})

const files = vol.readdirSync('/path/to/components')
expect(files).toEqual(['components.12345.json'])
expect(files).toEqual(['components.json'])
})

it('should save components to files with custom filename', async () => {
Expand All @@ -121,10 +124,14 @@ describe('pull components actions', () => {
interntal_tags_ids: [1],
}]

await saveComponentsToFiles('12345', components, { path: '/path/to/components2', filename: 'custom' })
await saveComponentsToFiles('12345', { components, groups: [], presets: [] }, {
path: '/path/to/components2',
filename: 'custom',
verbose: false,
})

const files = vol.readdirSync('/path/to/components2')
expect(files).toEqual(['custom.12345.json'])
expect(files).toEqual(['custom.json'])
})

it('should save components to files with custom suffix', async () => {
Expand All @@ -144,7 +151,11 @@ describe('pull components actions', () => {
interntal_tags_ids: [1],
}]

await saveComponentsToFiles('12345', components, { path: '/path/to/components3', suffix: 'custom' })
await saveComponentsToFiles('12345', { components, groups: [], presets: [] }, {
path: '/path/to/components3',
suffix: 'custom',
verbose: false,
})

const files = vol.readdirSync('/path/to/components3')
expect(files).toEqual(['components.custom.json'])
Expand Down Expand Up @@ -177,10 +188,14 @@ describe('pull components actions', () => {
interntal_tags_ids: [1],
}]

await saveComponentsToFiles('12345', components, { path: '/path/to/components4', separateFiles: true })
await saveComponentsToFiles('12345', { components, groups: [], presets: [] }, {
path: '/path/to/components4',
separateFiles: true,
verbose: false,
})

const files = vol.readdirSync('/path/to/components4')
expect(files).toEqual(['component-name-2.12345.json', 'component-name.12345.json'])
expect(files).toEqual(['component-name-2.json', 'component-name.json'])
})
})
})
101 changes: 21 additions & 80 deletions src/commands/components/actions.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,17 @@
import { ofetch } from 'ofetch'
import { handleAPIError, handleFileSystemError, slugify } from '../../utils'
import { regionsDomain } from '../../constants'
import { handleAPIError, handleFileSystemError } from '../../utils'
import type { RegionCode } from '../../constants'
import { join } from 'node:path'
import { resolvePath, saveToFile } from '../../utils/filesystem'
import type { PullComponentsOptions } from './constants'
import type { SaveComponentsOptions, SpaceComponent, SpaceComponentGroup, SpaceComponentPreset, SpaceData } from './constants'
import { getStoryblokUrl } from '../../utils/api-routes'
import { customFetch } from '../../utils/fetch'

export interface SpaceComponent {
name: string
display_name: string
created_at: string
updated_at: string
id: number
schema: Record<string, unknown>
image?: string
preview_field?: string
is_root?: boolean
is_nestable?: boolean
preview_tmpl?: string
all_presets?: Record<string, unknown>
preset_id?: number
real_name?: string
component_group_uuid?: string
color: null
internal_tags_list: string[]
interntal_tags_ids: number[]
content_type_asset_preview?: string
}

export interface SpaceComponentGroup {
name: string
id: number
uuid: string
parent_id: number
parent_uuid: string
}

export interface ComponentsSaveOptions {
path?: string
filename?: string
separateFiles?: boolean
suffix?: string
}

export interface SpaceComponentPreset {
id: number
name: string
preset: Record<string, unknown>
component_id: number
space_id: number
created_at: string
updated_at: string
image: string
color: string
icon: string
description: string
}

export interface SpaceData {
components: SpaceComponent[]
groups: SpaceComponentGroup[]
presets: SpaceComponentPreset[]
}
/**
* Resolves the nested folder structure based on component group hierarchy.
* @param groupUuid - The UUID of the component group.
* @param groups - The list of all component groups.
* @returns The resolved path for the component group.
*/
const resolveGroupPath = (groupUuid: string, groups: SpaceComponentGroup[]): string => {
const group = groups.find(g => g.uuid === groupUuid)
if (!group) { return '' }
const parentPath = group.parent_uuid ? resolveGroupPath(group.parent_uuid, groups) : ''
return join(parentPath, slugify(group.name))
}

export const fetchComponents = async (space: string, token: string, region: string): Promise<SpaceComponent[] | undefined> => {
export const fetchComponents = async (space: string, token: string, region: RegionCode): Promise<SpaceComponent[] | undefined> => {
try {
const response = await ofetch(`https://${regionsDomain[region]}/v1/spaces/${space}/components`, {
const url = getStoryblokUrl(region)
const response = await customFetch<{
components: SpaceComponent[]
}>(`${url}/spaces/${space}/components`, {
headers: {
Authorization: token,
},
Expand All @@ -88,9 +23,12 @@ export const fetchComponents = async (space: string, token: string, region: stri
}
}

export const fetchComponentGroups = async (space: string, token: string, region: string): Promise<SpaceComponentGroup[] | undefined> => {
export const fetchComponentGroups = async (space: string, token: string, region: RegionCode): Promise<SpaceComponentGroup[] | undefined> => {
try {
const response = await ofetch(`https://${regionsDomain[region]}/v1/spaces/${space}/component_groups`, {
const url = getStoryblokUrl(region)
const response = await customFetch<{
component_groups: SpaceComponentGroup[]
}>(`${url}/spaces/${space}/component_groups`, {
headers: {
Authorization: token,
},
Expand All @@ -102,9 +40,12 @@ export const fetchComponentGroups = async (space: string, token: string, region:
}
}

export const fetchComponentPresets = async (space: string, token: string, region: string): Promise<SpaceComponentPreset[] | undefined> => {
export const fetchComponentPresets = async (space: string, token: string, region: RegionCode): Promise<SpaceComponentPreset[] | undefined> => {
try {
const response = await ofetch(`https://${regionsDomain[region]}/v1/spaces/${space}/presets`, {
const url = getStoryblokUrl(region)
const response = await customFetch<{
presets: SpaceComponentPreset[]
}>(`${url}/spaces/${space}/presets`, {
headers: {
Authorization: token,
},
Expand All @@ -119,7 +60,7 @@ export const fetchComponentPresets = async (space: string, token: string, region
export const saveComponentsToFiles = async (
space: string,
spaceData: SpaceData,
options: PullComponentsOptions,
options: SaveComponentsOptions,
) => {
const { components, groups, presets } = spaceData
const { filename = 'components', suffix, path, separateFiles } = options
Expand Down
77 changes: 66 additions & 11 deletions src/commands/components/constants.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
import type { CommandOptions } from '../../types'

export interface SpaceComponent {
name: string
display_name: string
created_at: string
updated_at: string
id: number
schema: Record<string, unknown>
image?: string
preview_field?: string
is_root?: boolean
is_nestable?: boolean
preview_tmpl?: string
all_presets?: Record<string, unknown>
preset_id?: number
real_name?: string
component_group_uuid?: string
color: null
internal_tags_list: string[]
interntal_tags_ids: number[]
content_type_asset_preview?: string
}

export interface SpaceComponentGroup {
name: string
id: number
uuid: string
parent_id: number
parent_uuid: string
}

export interface ComponentsSaveOptions {
path?: string
filename?: string
separateFiles?: boolean
suffix?: string
}

export interface SpaceComponentPreset {
id: number
name: string
preset: Record<string, unknown>
component_id: number
space_id: number
created_at: string
updated_at: string
image: string
color: string
icon: string
description: string
}

export interface SpaceData {
components: SpaceComponent[]
groups: SpaceComponentGroup[]
presets: SpaceComponentPreset[]
}
/**
* Interface representing the options for the `pull-components` command.
*/
export interface PullComponentsOptions extends CommandOptions {
/**
* The path to save the components file to.
* Defaults to `.storyblok/components`.
* @default `.storyblok/components`
*/
path?: string
/**
* The space ID.
* @required true
*/
space: string

/**
* The filename to save the file as.
* Defaults to `components`. The file will be saved as `<filename>.<space>.json`.
Expand All @@ -33,3 +79,12 @@ export interface PullComponentsOptions extends CommandOptions {
*/
separateFiles?: boolean
}

export interface SaveComponentsOptions extends PullComponentsOptions {
/**
* The path to save the components file to.
* Defaults to `.storyblok/components`.
* @default `.storyblok/components`
*/
path?: string
}
Loading

0 comments on commit 8472a25

Please sign in to comment.