Skip to content

Commit

Permalink
[initial-value-templates] Allow templates to specify parameters schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 24, 2019
1 parent c37b3ac commit 6d5d4af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/@sanity/initial-value-templates/src/Template.ts
@@ -1,10 +1,13 @@
import {TemplateParameter} from './TemplateParameters'

export interface Template {
id: string
title: string
description?: string
schemaType: string
icon?: Function
value: {[key: string]: any}
parameters?: TemplateParameter[]
}

export class TemplateBuilder {
Expand Down Expand Up @@ -63,7 +66,7 @@ export class TemplateBuilder {
}

serialize(): Template {
const {id, title, description, schemaType, value, icon} = this.spec
const {id, title, description, schemaType, value, icon, parameters} = this.spec
if (!id) {
throw new Error('Template is missing required "id"')
}
Expand All @@ -80,7 +83,7 @@ export class TemplateBuilder {
throw new Error(`Template with ID "${id}" is missing required "value"`)
}

return {id, title, description, schemaType, value, icon}
return {id, title, description, schemaType, value, icon, parameters}
}

clone(withSpec?: Partial<Template>) {
Expand Down
25 changes: 25 additions & 0 deletions packages/@sanity/initial-value-templates/src/TemplateParameters.ts
@@ -0,0 +1,25 @@
export type TemplateParameter = FieldDefinition | ArrayFieldDefinition

export interface TypeTarget {
type: string
}

export interface ReferenceTarget {
type: 'reference'
to: TypeTarget | TypeTarget[]
}

export interface FieldDefinition {
name: string
type: string
title?: string
description?: string
options?: {[key: string]: any}
}

export type ReferenceFieldDefinition = FieldDefinition & ReferenceTarget

export type ArrayFieldDefinition = FieldDefinition & {
type: 'array'
of: (ReferenceTarget | TypeTarget)[]
}

0 comments on commit 6d5d4af

Please sign in to comment.