-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): improve
astro:content
types (#9906)
* feat: improve �stro:content types * fix: do not change tsconfig * "astro/zod" -> "zod" * `CollectionConfig` parameter extends `BaseSchema` * fix: update import to zod * feat: add fallbacks for every export * Update .changeset/young-bulldogs-tickle.md --------- Co-authored-by: lilnasy <[email protected]> Co-authored-by: Nate Moore <[email protected]>
- Loading branch information
1 parent
5eb0597
commit 3c0876c
Showing
4 changed files
with
84 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Improves the types for the `astro:content` module by making low fidelity types available before running `astro sync` |
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
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,75 @@ | ||
import { defineCollection as _defineCollection } from '../content/runtime.js'; | ||
import { z } from 'zod'; | ||
|
||
export { z }; | ||
|
||
// This needs to be in sync with ImageMetadata | ||
export type ImageFunction = () => z.ZodObject<{ | ||
src: z.ZodString; | ||
width: z.ZodNumber; | ||
height: z.ZodNumber; | ||
format: z.ZodUnion< | ||
[ | ||
z.ZodLiteral<'png'>, | ||
z.ZodLiteral<'jpg'>, | ||
z.ZodLiteral<'jpeg'>, | ||
z.ZodLiteral<'tiff'>, | ||
z.ZodLiteral<'webp'>, | ||
z.ZodLiteral<'gif'>, | ||
z.ZodLiteral<'svg'>, | ||
z.ZodLiteral<'avif'>, | ||
] | ||
>; | ||
}>; | ||
|
||
type BaseSchemaWithoutEffects = | ||
| z.AnyZodObject | ||
| z.ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> | ||
| z.ZodDiscriminatedUnion<string, z.AnyZodObject[]> | ||
| z.ZodIntersection<BaseSchemaWithoutEffects, BaseSchemaWithoutEffects>; | ||
|
||
type BaseSchema = BaseSchemaWithoutEffects | z.ZodEffects<BaseSchemaWithoutEffects>; | ||
|
||
export type SchemaContext = { image: ImageFunction }; | ||
|
||
type DataCollectionConfig<S extends BaseSchema> = { | ||
type: 'data'; | ||
schema?: S | ((context: SchemaContext) => S); | ||
}; | ||
|
||
type ContentCollectionConfig<S extends BaseSchema> = { | ||
type?: 'content'; | ||
schema?: S | ((context: SchemaContext) => S); | ||
}; | ||
|
||
type CollectionConfig<S extends BaseSchema> = ContentCollectionConfig<S> | DataCollectionConfig<S>; | ||
|
||
export function defineCollection<S extends BaseSchema>( | ||
input: CollectionConfig<S> | ||
): CollectionConfig<S> { | ||
return _defineCollection(input); | ||
} | ||
|
||
const noop: (...args: any[]) => any = () => {}; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export const getEntryBySlug = noop; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export const getDataEntryById = noop; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export const getCollection = noop; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export const getEntry = noop; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export const getEntries = noop; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export const reference = noop; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export type CollectionKey = any; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export type CollectionEntry<C> = any; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export type ContentCollectionKey = any; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export type DataCollectionKey = any; | ||
/** Run `astro sync` to generate high fidelity types */ | ||
export type ContentConfig = any; |