Skip to content

Commit

Permalink
improved optional field types
Browse files Browse the repository at this point in the history
  • Loading branch information
schickling committed Mar 7, 2022
1 parent 6c118c3 commit 8ec7614
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/@contentlayer/core/src/generation/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ const renderRawType = ({ sourcePluginType }: { sourcePluginType: SourcePluginTyp

const renderFieldDef = (field: FieldDef): string => {
const canBeUndefined = field.isRequired === false && field.default === undefined
return `${field.description ? ` /** ${field.description} */\n` : ''} ${field.name}: ${renderFieldType(field)}${
canBeUndefined ? ' | undefined' : ''
}`
return `${field.description ? ` /** ${field.description} */\n` : ''} ${field.name}${
canBeUndefined ? '?' : ''
}: ${renderFieldType(field)}${canBeUndefined ? ' | undefined' : ''}`
}

const renderFieldType = (field: FieldDef): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ test('generate-types: simple schema', async (t) => {
t.snapshot(typeSource)
})

test('generate-types: simple schema with optional fields', async (t) => {
const TestPost = defineDocumentType(() => ({
name: 'TestPost',
filePathPattern: `**/*.md`,
fields: {
title: {
type: 'string',
description: 'The title of the post',
required: true,
},
date: {
type: 'date',
description: 'The date of the post',
},
},
computedFields: {
slug: { type: 'string', resolve: (_) => _._id.replace('.md', '') },
},
}))

const typeSource = await renderTypeSource([TestPost])

t.snapshot(typeSource)
})

test('generate-types: references with embedded schema', async (t) => {
const Post = defineDocumentType(() => ({
name: 'Post',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,74 @@ Generated by [AVA](https://avajs.dev).
`

## generate-types: simple schema with optional fields

> Snapshot 1
`// NOTE This file is auto-generated by the Contentlayer CLI␊
import type { Markdown, MDX } from 'contentlayer/core'␊
import * as Local from 'contentlayer/source-files'␊
export { isType } from 'contentlayer/client'␊
// export type Image = string␊
export type { Markdown, MDX }␊
/** Document types */␊
export type TestPost = {␊
/** File path relative to \`contentDirPath\` */␊
_id: string␊
_raw: Local.RawDocumentData␊
type: 'TestPost'␊
/** The title of the post */␊
title: string␊
/** The date of the post */␊
date?: string | undefined␊
/** Markdown file body */␊
body: Markdown␊
slug: string␊
} ␊
/** Nested types */␊
/** Helper types */␊
export type AllTypes = DocumentTypes | NestedTypes␊
export type AllTypeNames = DocumentTypeNames | NestedTypeNames␊
export type DocumentTypes = TestPost␊
export type DocumentTypeNames = 'TestPost'␊
export type NestedTypes = never␊
export type NestedTypeNames = never␊
export interface ContentlayerGenTypes {␊
documentTypes: DocumentTypes␊
documentTypeMap: DocumentTypeMap␊
documentTypeNames: DocumentTypeNames␊
nestedTypes: NestedTypes␊
nestedTypeMap: NestedTypeMap␊
nestedTypeNames: NestedTypeNames␊
allTypeNames: AllTypeNames␊
}␊
declare global {␊
interface ContentlayerGen extends ContentlayerGenTypes {}␊
}␊
export type DocumentTypeMap = {␊
TestPost: TestPost␊
}␊
export type NestedTypeMap = {␊
}␊
`

## generate-types: references with embedded schema

> Snapshot 1
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/next-contentlayer/src/hooks/useLiveReload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react'

/**
* Needed as a work around for https://github.com/vercel/next.js/issues/19230
* Just needed in casese where you're importing from `contentlayer/generated` and use the data directly in your
* Just needed in cases where you're importing from `contentlayer/generated` and use the data directly in your
* React components without going through `getStaticProps` or `getServerSideProps` first.
*/
export const useLiveReload = () => {
Expand Down

0 comments on commit 8ec7614

Please sign in to comment.