Skip to content

Commit

Permalink
Throw when using deprecated getEntryByX functions with content layer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Aug 7, 2024
1 parent fb3efef commit ede3610
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
40 changes: 13 additions & 27 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function createGetCollection({
data,
collection,
};
if(hasFilter && !filter(entry)) {
if (hasFilter && !filter(entry)) {
continue;
}
result.push(entry);
Expand Down Expand Up @@ -170,20 +170,13 @@ export function createGetEntryBySlug({
return async function getEntryBySlug(collection: string, slug: string) {
const store = await globalDataStore.get();

if (store.hasCollection(collection)) {
const data = store.get(collection, slug);
if (!data) {
throw new Error(`Entry ${collection}${slug} was not found.`);
}

const entry = store.get<DataEntry>(collection, slug);

return {
...entry,
collection,
};
}
if (!collectionNames.has(collection)) {
if (store.hasCollection(collection)) {
throw new AstroError({
...AstroErrorData.GetEntryDeprecationError,
message: AstroErrorData.GetEntryDeprecationError.message(collection, 'getEntryBySlug'),
});
}
// eslint-disable-next-line no-console
console.warn(`The collection ${JSON.stringify(collection)} does not exist.`);
return undefined;
Expand Down Expand Up @@ -221,20 +214,13 @@ export function createGetDataEntryById({
return async function getDataEntryById(collection: string, id: string) {
const store = await globalDataStore.get();

if (store.hasCollection(collection)) {
const data = store.get(collection, id);
if (!data) {
throw new Error(`Entry ${collection}${id} was not found.`);
}

const entry = store.get<DataEntry>(collection, id);

return {
...entry,
collection,
};
}
if (!collectionNames.has(collection)) {
if (store.hasCollection(collection)) {
throw new AstroError({
...AstroErrorData.GetEntryDeprecationError,
message: AstroErrorData.GetEntryDeprecationError.message(collection, 'getDataEntryById'),
});
}
// eslint-disable-next-line no-console
console.warn(`The collection ${JSON.stringify(collection)} does not exist.`);
return undefined;
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,18 @@ export const ContentLayerWriteError = {
title: 'Could not write content layer data.',
} satisfies ErrorData;

/**
* @docs
* @description
* The `getDataEntryById` and `getEntryBySlug` functions are deprecated and cannot be used with content layer collections. Use the `getEntry` function instead.
*/
export const GetEntryDeprecationError = {
name: 'GetEntryDeprecationError',
title: 'Invalid use of `getDataEntryById` or `getEntryBySlug` function.',
message: (collection: string, method: string) => `The \`${method}\` function is deprecated and cannot be used to query the "${collection}" collection. Use \`getEntry\` instead.`,
hint: 'Use the `getEntry` or `getCollection` functions to query content layer collections.',
} satisfies ErrorData;

/**
* @docs
* @message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
import { getCollection, getDataEntryById } from 'astro:content';
import { getCollection, getEntry } from 'astro:content';
const blog = await getCollection('blog');
const first = await getDataEntryById('blog', 1);
const first = await getEntry('blog', 1);
const dogs = await getCollection('dogs');
---
<html>
Expand Down

0 comments on commit ede3610

Please sign in to comment.