Skip to content

Commit 5a51b97

Browse files
committed
fix(refresh): handle array in refresh context data
This is needed for pawcoding/astro-integration-pocketbase#10.
1 parent ee12df6 commit 5a51b97

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/pocketbase-loader.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateSchema } from "./generate-schema";
55
import { loadEntries } from "./load-entries";
66
import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
77
import { getSuperuserToken } from "./utils/get-superuser-token";
8+
import { shouldRefresh } from "./utils/should-refresh";
89

910
/**
1011
* Loader for collections stored in PocketBase.
@@ -15,13 +16,12 @@ export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
1516
return {
1617
name: "pocketbase-loader",
1718
load: async (context: LoaderContext): Promise<void> => {
18-
if (
19-
context.refreshContextData?.source === "astro-integration-pocketbase" &&
20-
context.refreshContextData.collection &&
21-
context.refreshContextData.collection !== options.collectionName
22-
) {
23-
// Skip the refresh if the reload was triggered by the `astro-integration-pocketbase`
24-
// and the collection name does not match the current collection.
19+
// Check if the collection should be refreshed.
20+
const refresh = shouldRefresh(
21+
context.refreshContextData,
22+
options.collectionName
23+
);
24+
if (!refresh) {
2525
return;
2626
}
2727

src/utils/should-refresh.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { LoaderContext } from "astro/loaders";
2+
3+
/**
4+
* Checks if the collection should be refreshed.
5+
*/
6+
export function shouldRefresh(
7+
context: LoaderContext["refreshContextData"],
8+
collectionName: string
9+
): boolean {
10+
// Check if the refresh was triggered by the `astro-integration-pocketbase`
11+
// and the correct metadata is provided.
12+
if (
13+
!context ||
14+
context.source !== "astro-integration-pocketbase" ||
15+
!context.collection
16+
) {
17+
return true;
18+
}
19+
20+
// Check if the collection name matches the current collection.
21+
if (typeof context.collection === "string") {
22+
return context.collection === collectionName;
23+
}
24+
25+
// Check if the collection is included in the list of collections.
26+
if (Array.isArray(context.collection)) {
27+
return context.collection.includes(collectionName);
28+
}
29+
30+
// Should not happen but return true to be safe.
31+
return true;
32+
}

0 commit comments

Comments
 (0)