Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): add typegen methods to codegen package
co-authored-by: Tonina Zhelyazkova <zhelyazkova.tonina@gmail.com>
- Loading branch information
1 parent
e133519
commit 64e2e8d
Showing
12 changed files
with
2,051 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export {readSchema} from '../readSchema' | ||
export {findQueriesInPath} from '../typescript/findQueriesInPath' | ||
export {findQueriesInSource} from '../typescript/findQueriesInSource' | ||
export {getResolver} from '../typescript/moduleResolver' | ||
export {registerBabel} from '../typescript/registerBabel' | ||
export {TypeGenerator} from '../typescript/typeGenerator' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {readFile} from 'fs/promises' | ||
import {type SchemaType} from 'groq-js' | ||
|
||
export async function readSchema(path: string): Promise<SchemaType> { | ||
const content = await readFile(path, 'utf-8') | ||
return JSON.parse(content) // todo: ZOD validation? | ||
} |
197 changes: 197 additions & 0 deletions
197
packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`generateTypesFromSchema should generate TypeScript type declarations for a schema 1`] = ` | ||
"export type Author = { | ||
_id: string; | ||
_type: \\"author\\"; | ||
_createdAt: string; | ||
_updatedAt: string; | ||
_rev: string; | ||
name?: string; | ||
}; | ||
export type Post = { | ||
_id: string; | ||
_type: \\"post\\"; | ||
_createdAt: string; | ||
_updatedAt: string; | ||
_rev: string; | ||
title?: string; | ||
author?: { | ||
_ref: string; | ||
_weak?: boolean; | ||
} | { | ||
_ref: string; | ||
_weak?: boolean; | ||
}; | ||
slug?: Slug; | ||
excerpt?: string; | ||
mainImage?: { | ||
_type: \\"image\\"; | ||
asset: { | ||
_ref: string; | ||
_weak?: boolean; | ||
}; | ||
caption?: string; | ||
attribution?: string; | ||
hotspot?: { | ||
_type: \\"sanity.imageHotspot\\"; | ||
x: number; | ||
y: number; | ||
height: number; | ||
width: number; | ||
}; | ||
crop?: { | ||
_type: \\"sanity.imageCrop\\"; | ||
top: number; | ||
bottom: number; | ||
left: number; | ||
right: number; | ||
}; | ||
}; | ||
body?: BlockContent; | ||
}; | ||
export type Ghost = { | ||
_id: string; | ||
_type: \\"ghost\\"; | ||
_createdAt: string; | ||
_updatedAt: string; | ||
_rev: string; | ||
name?: string; | ||
}; | ||
export type BlockContent = Array<{ | ||
_key: string; | ||
level?: number; | ||
style?: \\"normal\\" | \\"h1\\" | \\"h2\\" | \\"h3\\" | \\"h4\\" | \\"blockquote\\"; | ||
listItem?: \\"bullet\\"; | ||
children: Array<{ | ||
_key: string; | ||
text: string; | ||
marks: Array<string | \\"strong\\" | \\"em\\">; | ||
}>; | ||
markDefs: Array<{ | ||
href?: string; | ||
}>; | ||
_key: string; | ||
}>; | ||
export type SanityAssetSourceData = { | ||
name?: string; | ||
id?: string; | ||
url?: string; | ||
_type: \\"sanity.assetSourceData\\"; | ||
}; | ||
export type Slug = { | ||
current?: string; | ||
source?: string; | ||
_type: \\"slug\\"; | ||
}; | ||
export type Geopoint = { | ||
lat?: number; | ||
lng?: number; | ||
alt?: number; | ||
_type: \\"geopoint\\"; | ||
}; | ||
export type SanityImageAsset = { | ||
_id: string; | ||
_type: \\"sanity.imageAsset\\"; | ||
_createdAt: string; | ||
_updatedAt: string; | ||
_rev: string; | ||
originalFilename?: string; | ||
label?: string; | ||
title?: string; | ||
description?: string; | ||
altText?: string; | ||
sha1hash?: string; | ||
extension?: string; | ||
mimeType?: string; | ||
size?: number; | ||
assetId?: string; | ||
uploadId?: string; | ||
path?: string; | ||
url?: string; | ||
metadata?: SanityImageMetadata; | ||
source?: SanityAssetSourceData; | ||
}; | ||
export type SanityFileAsset = { | ||
_id: string; | ||
_type: \\"sanity.fileAsset\\"; | ||
_createdAt: string; | ||
_updatedAt: string; | ||
_rev: string; | ||
originalFilename?: string; | ||
label?: string; | ||
title?: string; | ||
description?: string; | ||
altText?: string; | ||
sha1hash?: string; | ||
extension?: string; | ||
mimeType?: string; | ||
size?: number; | ||
assetId?: string; | ||
uploadId?: string; | ||
path?: string; | ||
url?: string; | ||
source?: SanityAssetSourceData; | ||
}; | ||
export type SanityImageCrop = { | ||
top?: number; | ||
bottom?: number; | ||
left?: number; | ||
right?: number; | ||
_type: \\"sanity.imageCrop\\"; | ||
}; | ||
export type SanityImageHotspot = { | ||
x?: number; | ||
y?: number; | ||
height?: number; | ||
width?: number; | ||
_type: \\"sanity.imageHotspot\\"; | ||
}; | ||
export type SanityImageMetadata = { | ||
location?: Geopoint; | ||
dimensions?: SanityImageDimensions; | ||
palette?: SanityImagePalette; | ||
lqip?: string; | ||
blurHash?: string; | ||
hasAlpha?: boolean; | ||
isOpaque?: boolean; | ||
_type: \\"sanity.imageMetadata\\"; | ||
}; | ||
export type SanityImageDimensions = { | ||
height?: number; | ||
width?: number; | ||
aspectRatio?: number; | ||
_type: \\"sanity.imageDimensions\\"; | ||
}; | ||
export type SanityImagePalette = { | ||
darkMuted?: SanityImagePaletteSwatch; | ||
lightVibrant?: SanityImagePaletteSwatch; | ||
darkVibrant?: SanityImagePaletteSwatch; | ||
vibrant?: SanityImagePaletteSwatch; | ||
dominant?: SanityImagePaletteSwatch; | ||
lightMuted?: SanityImagePaletteSwatch; | ||
muted?: SanityImagePaletteSwatch; | ||
_type: \\"sanity.imagePalette\\"; | ||
}; | ||
export type SanityImagePaletteSwatch = { | ||
background?: string; | ||
foreground?: string; | ||
population?: number; | ||
title?: string; | ||
_type: \\"sanity.imagePaletteSwatch\\"; | ||
};" | ||
`; |
16 changes: 16 additions & 0 deletions
16
packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import path from 'node:path' | ||
|
||
import {describe, expect, test} from '@jest/globals' | ||
import {lastValueFrom} from 'rxjs' | ||
|
||
import {findQueriesInPath} from '../findQueriesInPath' | ||
|
||
describe('findQueriesInPath', () => { | ||
test('should throw an error if the query name already exists', async () => { | ||
const observable = findQueriesInPath({ | ||
path: path.join(__dirname, 'fixtures', '{source1,source2}.ts'), | ||
}) | ||
const result = await lastValueFrom(observable) | ||
expect(result.error?.message).toMatch(/Duplicate query name found:/) | ||
}) | ||
}) |
Oops, something went wrong.