Skip to content

Commit

Permalink
[types] Add typings for slug
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 5becf1c commit a4d11dd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@sanity/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './slug'
export * from './paths'
export * from './schema'
export * from './documents'
Expand Down
7 changes: 7 additions & 0 deletions packages/@sanity/types/src/schema/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Note: INCOMPLETE, but it's a start

import {SlugOptions} from '../slug'

export interface Schema {
name: string
get: (name: string) => SchemaType
Expand Down Expand Up @@ -49,6 +51,11 @@ export interface BlockSchemaType extends ObjectSchemaType {
of?: SchemaType[]
}

export interface SlugSchemaType extends ObjectSchemaType {
jsonType: 'object'
options?: SlugOptions
}

export interface ObjectField<T extends SchemaType = SchemaType> {
name: string
fieldset?: string
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/types/src/slug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types'
42 changes: 42 additions & 0 deletions packages/@sanity/types/src/slug/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {SlugSchemaType} from '../schema'
import {SanityDocument} from '../documents'
import {Path} from '../paths'

export interface Slug {
_type: 'slug'
current: string
}

export type SlugParent = Record<string, unknown> | Record<string, unknown>[]

export interface SlugSourceOptions {
parentPath: Path
parent: SlugParent
}

export type SlugSourceFn = (
document: SanityDocument,
options: SlugSourceOptions
) => string | Promise<string>

export type SlugifierFn = (source: string, schemaType: SlugSchemaType) => string | Promise<string>

export interface SlugUniqueOptions {
parent: SlugParent
type: SlugSchemaType
document: SanityDocument
defaultIsUnique: UniqueCheckerFn
path: Path
}

export type UniqueCheckerFn = (
slug: string,
options: SlugUniqueOptions
) => boolean | Promise<boolean>

export interface SlugOptions {
source?: string | Path | SlugSourceFn
maxLength?: number
slugify?: SlugifierFn
isUnique?: UniqueCheckerFn
}

0 comments on commit a4d11dd

Please sign in to comment.