astro-loader-pocketbase 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "A content loader for Astro that uses the PocketBase API",
5
5
  "license": "MIT",
6
6
  "author": "Luis Wolf <development@pawcode.de> (https://pawcode.de)",
@@ -5,6 +5,7 @@ import { generateSchema } from "./generate-schema";
5
5
  import { loadEntries } from "./load-entries";
6
6
  import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
7
7
  import { getSuperuserToken } from "./utils/get-superuser-token";
8
+ import { shouldRefresh } from "./utils/should-refresh";
8
9
 
9
10
  /**
10
11
  * Loader for collections stored in PocketBase.
@@ -15,13 +16,12 @@ export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
15
16
  return {
16
17
  name: "pocketbase-loader",
17
18
  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) {
25
25
  return;
26
26
  }
27
27
 
@@ -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
+ }