Skip to content

Commit

Permalink
feat(codegen): add typegen methods to codegen package
Browse files Browse the repository at this point in the history
co-authored-by: Tonina Zhelyazkova <zhelyazkova.tonina@gmail.com>
  • Loading branch information
sgulseth and tzhelyazkova committed Mar 12, 2024
1 parent e133519 commit 64e2e8d
Show file tree
Hide file tree
Showing 12 changed files with 2,051 additions and 8 deletions.
7 changes: 6 additions & 1 deletion packages/@sanity/codegen/package.json
Expand Up @@ -56,17 +56,22 @@
},
"dependencies": {
"@babel/core": "^7.23.9",
"@babel/generator": "^7.23.6",
"@babel/preset-env": "^7.23.8",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@babel/register": "^7.23.7",
"@babel/traverse": "^7.23.5",
"@babel/types": "^7.23.9",
"debug": "^4.3.4"
"debug": "^4.3.4",
"fast-glob": "^3.3.2",
"groq-js": "1.5.0-canary.1",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.8",
"@types/babel__register": "^7.17.3",
"@types/babel__traverse": "^7.18.1",
"@types/debug": "^4.1.12",
Expand Down
3 changes: 3 additions & 0 deletions packages/@sanity/codegen/src/_exports/index.ts
@@ -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'
7 changes: 7 additions & 0 deletions packages/@sanity/codegen/src/readSchema.ts
@@ -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?
}
@@ -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\\";
};"
`;
@@ -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:/)
})
})

0 comments on commit 64e2e8d

Please sign in to comment.