Skip to content

Commit

Permalink
feat(config): define SingleWorkspace type, allow omitting `basePath…
Browse files Browse the repository at this point in the history
…`, `name`
  • Loading branch information
rexxars committed Aug 10, 2022
1 parent 82df86a commit e9da1a2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/sanity/src/config/createConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Config} from './types'
import type {Config} from './types'

export function createConfig<T extends Config>(config: T): T {
return config
Expand Down
20 changes: 15 additions & 5 deletions packages/sanity/src/config/prepareConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import {AuthStore, createAuthStore} from '../datastores'
import {InitialValueTemplateItem, Template, TemplateResponse} from '../templates'
import {isNonNullable} from '../util'
import {defaultFileAssetSources, defaultImageAssetSources} from '../form/defaults'
import {Source, SourceOptions, Config, WorkspaceSummary, PreparedConfig} from './types'
import {
Source,
SourceOptions,
Config,
WorkspaceSummary,
PreparedConfig,
SingleWorkspace,
WorkspaceOptions,
} from './types'
import {
schemaTypesReducer,
resolveProductionUrlReducer,
Expand Down Expand Up @@ -58,7 +66,9 @@ function normalizeLogo(
* of the Studio or for testing, use `resolveConfig`.
*/
export function prepareConfig(config: Config): PreparedConfig {
const workspaceOptions = Array.isArray(config) ? config : [config]
const workspaceOptions: WorkspaceOptions[] | [SingleWorkspace] = Array.isArray(config)
? config
: [config]

// TODO: throw if there is more than one workspace with the same name
// TODO: call `validateWorkspaceBasePaths`
Expand Down Expand Up @@ -96,8 +106,8 @@ export function prepareConfig(config: Config): PreparedConfig {
})

const schemaValidationProblemGroups = schema._validation
const schemaErrors = schemaValidationProblemGroups?.filter(
(msg) => !!msg.problems.find((p) => p.severity === 'error')
const schemaErrors = schemaValidationProblemGroups?.filter((msg) =>
msg.problems.some((p) => p.severity === 'error')
)

if (schemaValidationProblemGroups && schemaErrors?.length) {
Expand Down Expand Up @@ -142,7 +152,7 @@ export function prepareConfig(config: Config): PreparedConfig {
title,
`${rootSource.projectId} ${rootSource.dataset}`
),
name: rootSource.name,
name: rootSource.name || 'default',
projectId: rootSource.projectId,
theme: rootSource.theme || studioTheme,
title,
Expand Down
6 changes: 3 additions & 3 deletions packages/sanity/src/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {first, map} from 'rxjs/operators'
import {CurrentUser} from '@sanity/types'
import {SanityClient} from '@sanity/client'
import {createMockAuthStore} from '../datastores/authStore/createMockAuthStore'
import {Config, Source, Workspace, WorkspaceOptions} from './types'
import {Config, SingleWorkspace, Source, Workspace, WorkspaceOptions} from './types'
import {prepareConfig} from './prepareConfig'

/**
Expand Down Expand Up @@ -34,8 +34,8 @@ export function resolveConfig(config: Config): Observable<Workspace[]> {
}

type CreateWorkspaceFromConfigOptions =
| WorkspaceOptions
| (WorkspaceOptions & {currentUser: CurrentUser; client: SanityClient})
| SingleWorkspace
| (SingleWorkspace & {currentUser: CurrentUser; client: SanityClient})

/**
* PRIMARILY FOR TESTING PURPOSES.
Expand Down
12 changes: 10 additions & 2 deletions packages/sanity/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export type AsyncConfigPropertyReducer<TValue, TContext> = (
export type Plugin<TOptions = void> = (options: TOptions) => PluginOptions

export interface WorkspaceOptions extends SourceOptions {
basePath?: string
basePath: string
subtitle?: string
logo?: React.ComponentType
icon?: React.ComponentType
Expand Down Expand Up @@ -346,7 +346,15 @@ export interface Workspace extends Omit<Source, 'type'> {
unstable_sources: Source[]
}

export type Config = WorkspaceOptions | WorkspaceOptions[]
/**
* If a single workspace is used, not specifying a name or basePath is acceptable
*/
export type SingleWorkspace = Omit<WorkspaceOptions, 'name' | 'basePath'> & {
name?: string
basePath?: string
}

export type Config = SingleWorkspace | WorkspaceOptions[]

export interface PreparedConfig {
type: 'prepared-config'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,19 @@ describe('validateBasePaths', () => {

describe('validateNames', () => {
it('allows missing name on single workspace', () => {
// @ts-expect-error Types should encourage a name (shouldn't they?)
validateNames([{basePath: '/'}])
// @ts-expect-error Types should encourage a name (shouldn't they?)
validateNames([{name: undefined, basePath: '/'}])
})

it('throws if more than one workspace is defined, but one or more workspace does not have a name', () => {
expect(() => {
// @ts-expect-error Types should encourage a name
validateNames([{basePath: '/first'}, {name: 'second', basePath: '/2nd'}])
}).toThrowErrorMatchingInlineSnapshot(
`"All workspaces must have a \`name\`, unless only a single workspace is defined. Workspace at index 0 did not define a \`name\`."`
)

expect(() => {
validateNames([
// @ts-expect-error Types should encourage a name
{basePath: '/first', title: 'First'},
{name: 'second', basePath: '/2nd'},
])
Expand All @@ -113,7 +109,6 @@ describe('validateNames', () => {
)

expect(() => {
// @ts-expect-error Types should encourage a name
validateNames([{name: 'first', basePath: '/1st'}, {basePath: '/second'}])
}).toThrowErrorMatchingInlineSnapshot(
`"All workspaces must have a \`name\`, unless only a single workspace is defined. Workspace at index 1 did not define a \`name\`."`
Expand All @@ -122,7 +117,6 @@ describe('validateNames', () => {
expect(() => {
validateNames([
{name: 'first', basePath: '/1st'},
// @ts-expect-error Types should encourage a name
{basePath: '/second', title: 'Second'},
])
}).toThrowErrorMatchingInlineSnapshot(
Expand Down
2 changes: 1 addition & 1 deletion packages/sanity/src/studio/workspaces/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface WorkspaceLike {
name: string
name?: string
title?: string
basePath?: string
}
4 changes: 2 additions & 2 deletions packages/sanity/test/form/TestProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import {SanityClient} from '@sanity/client'
import {LayerProvider, studioTheme, ThemeProvider, ToastProvider} from '@sanity/ui'
import {createWorkspaceFromConfig, WorkspaceOptions} from '../../src/config'
import {createWorkspaceFromConfig, SingleWorkspace} from '../../src/config'
import {SourceProvider, WorkspaceProvider} from '../../src/studio'

interface TestProviderOptions {
config: WorkspaceOptions
config: SingleWorkspace
client: SanityClient
}

Expand Down

0 comments on commit e9da1a2

Please sign in to comment.