Skip to content

Commit

Permalink
Revert "feat(typegen): add all schema types exported union"
Browse files Browse the repository at this point in the history
This reverts commit 5d97a0e.
  • Loading branch information
sgulseth committed Jun 17, 2024
1 parent 5d97a0e commit e65adee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ export type SanityImagePaletteSwatch = {
population?: number;
title?: string;
_type: \\"sanity.imagePaletteSwatch\\";
};
export type allSchemaTypes = Author | Post | Ghost | BlockContent | SanityAssetSourceData | Slug | Geopoint | SanityImageAsset | SanityFileAsset | SanityImageCrop | SanityImageHotspot | SanityImageMetadata | SanityImageDimensions | SanityImagePalette | SanityImagePaletteSwatch;"
};"
`;

exports[`generateSchemaTypes should generate correct types for document schema with inline fields 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ describe('generateSchemaTypes', () => {
"export type Author = {
_id: string;
name?: string;
};
export type allSchemaTypes = Author;"
};"
`)
})

Expand All @@ -82,9 +80,7 @@ export type allSchemaTypes = Author;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type Product = {
price: number;
};
export type allSchemaTypes = Product;"
};"
`)
})

Expand All @@ -107,9 +103,7 @@ export type allSchemaTypes = Product;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type Task = {
completed: boolean;
};
export type allSchemaTypes = Task;"
};"
`)
})

Expand Down Expand Up @@ -148,9 +142,7 @@ export type allSchemaTypes = Task;"
street: string;
city: string;
};
};
export type allSchemaTypes = User;"
};"
`)
})

Expand All @@ -177,9 +169,7 @@ export type allSchemaTypes = User;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type BlogPost = {
tags: Array<string>;
};
export type allSchemaTypes = BlogPost;"
};"
`)
})

Expand All @@ -203,9 +193,7 @@ export type allSchemaTypes = BlogPost;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type DynamicData = {
metadata: unknown;
};
export type allSchemaTypes = DynamicData;"
};"
`)
})

Expand All @@ -229,9 +217,7 @@ export type allSchemaTypes = DynamicData;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type Impossible = {
willNotHappen: null;
};
export type allSchemaTypes = Impossible;"
};"
`)
})

Expand Down Expand Up @@ -273,9 +259,7 @@ export type allSchemaTypes = Impossible;"
export type Author = {
name: string;
};
export type allSchemaTypes = BlogPost | Author;"
};"
`)
})

Expand All @@ -302,9 +286,7 @@ export type allSchemaTypes = BlogPost | Author;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type MixedContent = {
content: string | number;
};
export type allSchemaTypes = MixedContent;"
};"
`)
})

Expand All @@ -328,9 +310,7 @@ export type allSchemaTypes = MixedContent;"
expect(actualOutput).toMatchInlineSnapshot(`
"export type OptionalData = {
obsoleteField: null;
};
export type allSchemaTypes = OptionalData;"
};"
`)
})

Expand Down
20 changes: 0 additions & 20 deletions packages/@sanity/codegen/src/typescript/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,6 @@ export class TypeGenerator {
typeDeclarations.push(t.exportNamedDeclaration(typeAlias))
})

typeDeclarations.push(
t.exportNamedDeclaration(
t.tsTypeAliasDeclaration(
t.identifier('allSchemaTypes'),
null,
t.tsUnionType(
this.schema.map(({name}) => {
const typeName = this.typeNameMap.get(name)
if (!typeName) {
// this is a type guard since maps return undefined if the key is not found, however this map should
// be set inside the loop above by `this.getTypeName(...)`
throw new Error(`Unexpected error: Could not find type name for schema ${name}`)
}
return t.tsTypeReference(t.identifier(typeName))
}),
),
),
),
)

// Generate TypeScript code from the AST nodes
return typeDeclarations.map((decl) => new CodeGenerator(decl).generate().code).join('\n\n')
}
Expand Down

0 comments on commit e65adee

Please sign in to comment.