-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Export defineCollection from astro/config #13814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Parent:
feat: live content collections
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,168 @@ | ||
| import type { Loader } from 'esbuild'; | ||
| import type { ZodLiteral, ZodNumber, ZodObject, ZodString, ZodType, ZodUnion } from 'zod'; | ||
| import { CONTENT_LAYER_TYPE, LIVE_CONTENT_TYPE } from '../content/consts.js'; | ||
| import type { LiveLoader } from '../content/loaders/types.js'; | ||
| import { AstroError, AstroErrorData, AstroUserError } from '../core/errors/index.js'; | ||
|
|
||
| export function getImporterFilename() { | ||
| // Find the first line in the stack trace that doesn't include 'defineCollection' or 'getImporterFilename' | ||
| const stackLine = new Error().stack | ||
| ?.split('\n') | ||
| .find( | ||
| (line) => | ||
| !line.includes('defineCollection') && | ||
| !line.includes('getImporterFilename') && | ||
| line !== 'Error', | ||
| ); | ||
| if (!stackLine) { | ||
| return undefined; | ||
| } | ||
| // Extract the relative path from the stack line | ||
| const match = /\/((?:src|chunks)\/.*?):\d+:\d+/.exec(stackLine); | ||
|
|
||
| return match?.[1] ?? undefined; | ||
| } | ||
|
|
||
| // This needs to be in sync with ImageMetadata | ||
| export type ImageFunction = () => ZodObject<{ | ||
| src: ZodString; | ||
| width: ZodNumber; | ||
| height: ZodNumber; | ||
| format: ZodUnion< | ||
| [ | ||
| ZodLiteral<'png'>, | ||
| ZodLiteral<'jpg'>, | ||
| ZodLiteral<'jpeg'>, | ||
| ZodLiteral<'tiff'>, | ||
| ZodLiteral<'webp'>, | ||
| ZodLiteral<'gif'>, | ||
| ZodLiteral<'svg'>, | ||
| ZodLiteral<'avif'>, | ||
| ] | ||
| >; | ||
| }>; | ||
|
|
||
| export interface DataEntry { | ||
| id: string; | ||
| data: Record<string, unknown>; | ||
| filePath?: string; | ||
| body?: string; | ||
| } | ||
|
|
||
| export interface DataStore { | ||
| get: (key: string) => DataEntry; | ||
| entries: () => Array<[id: string, DataEntry]>; | ||
| set: (key: string, data: Record<string, unknown>, body?: string, filePath?: string) => void; | ||
| values: () => Array<DataEntry>; | ||
| keys: () => Array<string>; | ||
| delete: (key: string) => void; | ||
| clear: () => void; | ||
| has: (key: string) => boolean; | ||
| } | ||
|
|
||
| export interface MetaStore { | ||
| get: (key: string) => string | undefined; | ||
| set: (key: string, value: string) => void; | ||
| delete: (key: string) => void; | ||
| has: (key: string) => boolean; | ||
| } | ||
|
|
||
| export type BaseSchema = ZodType; | ||
|
|
||
| export type SchemaContext = { image: ImageFunction }; | ||
|
|
||
| type ContentLayerConfig<S extends BaseSchema, TData extends { id: string } = { id: string }> = { | ||
| type?: 'content_layer'; | ||
| schema?: S | ((context: SchemaContext) => S); | ||
| loader: | ||
| | Loader | ||
| | (() => | ||
| | Array<TData> | ||
| | Promise<Array<TData>> | ||
| | Record<string, Omit<TData, 'id'> & { id?: string }> | ||
| | Promise<Record<string, Omit<TData, 'id'> & { id?: string }>>); | ||
| }; | ||
|
|
||
| type DataCollectionConfig<S extends BaseSchema> = { | ||
| type: 'data'; | ||
| schema?: S | ((context: SchemaContext) => S); | ||
| }; | ||
|
|
||
| type ContentCollectionConfig<S extends BaseSchema> = { | ||
| type?: 'content'; | ||
| schema?: S | ((context: SchemaContext) => S); | ||
| loader?: never; | ||
| }; | ||
|
|
||
| type LiveDataCollectionConfig<S extends BaseSchema, L extends LiveLoader> = { | ||
| type: 'live'; | ||
| schema?: S; | ||
| loader: L; | ||
| }; | ||
|
|
||
| export type CollectionConfig< | ||
| S extends BaseSchema, | ||
| TLiveLoader = never, | ||
| > = TLiveLoader extends LiveLoader | ||
| ? LiveDataCollectionConfig<S, TLiveLoader> | ||
| : ContentCollectionConfig<S> | DataCollectionConfig<S> | ContentLayerConfig<S>; | ||
|
|
||
| export function defineCollection<S extends BaseSchema, TLiveLoader = undefined>( | ||
| config: CollectionConfig<S, TLiveLoader>, | ||
| ): CollectionConfig<S, TLiveLoader> { | ||
| const importerFilename = getImporterFilename(); | ||
| const isInLiveConfig = importerFilename?.includes('live.config'); | ||
|
|
||
| if (config.type === LIVE_CONTENT_TYPE) { | ||
| if (!isInLiveConfig) { | ||
| throw new AstroError({ | ||
| ...AstroErrorData.LiveContentConfigError, | ||
| message: AstroErrorData.LiveContentConfigError.message( | ||
| 'Collections with type `live` must be defined in a `src/live.config.ts` file.', | ||
| importerFilename ?? 'your content config file', | ||
| ), | ||
| }); | ||
| } | ||
| if (!config.loader) { | ||
| throw new AstroError({ | ||
| ...AstroErrorData.LiveContentConfigError, | ||
| message: AstroErrorData.LiveContentConfigError.message( | ||
| 'Collections with type `live` must have a `loader` defined.', | ||
| importerFilename, | ||
| ), | ||
| }); | ||
| } | ||
| if (config.schema) { | ||
| if (typeof config.schema === 'function') { | ||
| throw new AstroError({ | ||
| ...AstroErrorData.LiveContentConfigError, | ||
| message: AstroErrorData.LiveContentConfigError.message( | ||
| 'The schema cannot be a function for live collections. Please use a schema object instead.', | ||
| importerFilename, | ||
| ), | ||
| }); | ||
| } | ||
| } | ||
| return config; | ||
| } | ||
| if (isInLiveConfig) { | ||
| throw new AstroError({ | ||
| ...AstroErrorData.LiveContentConfigError, | ||
| message: AstroErrorData.LiveContentConfigError.message( | ||
| 'Collections in a `live.config.ts` file must have a type of `live`.', | ||
| getImporterFilename(), | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| if ('loader' in config) { | ||
| if (config.type && config.type !== CONTENT_LAYER_TYPE) { | ||
| throw new AstroUserError( | ||
| `Collections that use the Content Layer API must have a \`loader\` defined and no \`type\` set. Check your collection definitions in ${getImporterFilename() ?? 'your content config file'}.`, | ||
| ); | ||
| } | ||
| config.type = CONTENT_LAYER_TYPE; | ||
| } | ||
| if (!config.type) config.type = 'content'; | ||
| return config; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
packages/astro/test/fixtures/live-loaders/src/pages/api.ts
This file contains hidden or 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 type { APIRoute } from 'astro'; | ||
| import { getCollection, getEntry } from 'astro:content'; | ||
|
|
||
| export const prerender = false; | ||
|
|
||
| export const GET: APIRoute = async ({ request }) => { | ||
| const addToAge = new URL(request.url).searchParams.get('addToAge'); | ||
| const filter = addToAge ? { addToAge: parseInt(addToAge) } : undefined; | ||
| const collection = await getCollection('liveStuff', filter); | ||
| const entryByString = await getEntry('liveStuff', '123'); | ||
| const entryByObject = await getEntry( | ||
| 'liveStuff', | ||
| { id: '456', ...filter }, | ||
| ); | ||
| return Response.json({ collection, entryByObject, entryByString }); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything below this line has been moved from
runtime.tsandcontent.d.ts. The only changes are those needed to move types from a.d.tsto a.tsfile