astro-loader-pocketbase 2.6.2 → 2.6.3-next.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.6.2",
3
+ "version": "2.6.3-next.1",
4
4
  "description": "A content loader for Astro that uses the PocketBase API",
5
5
  "keywords": [
6
6
  "astro",
@@ -1,15 +1,18 @@
1
1
  import type { LoaderContext } from "astro/loaders";
2
2
  import packageJson from "../../package.json";
3
3
  import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
4
- import { getSuperuserToken } from "../utils/get-superuser-token";
5
4
  import { shouldRefresh } from "../utils/should-refresh";
6
5
  import { cleanupEntries } from "./cleanup-entries";
7
6
  import { handleRealtimeUpdates } from "./handle-realtime-updates";
8
7
  import { loadEntries } from "./load-entries";
9
8
 
9
+ /**
10
+ * Load entries from a PocketBase collection.
11
+ */
10
12
  export async function loader(
11
13
  context: LoaderContext,
12
- options: PocketBaseLoaderOptions
14
+ options: PocketBaseLoaderOptions,
15
+ token: string | undefined
13
16
  ): Promise<void> {
14
17
  context.logger.label = `pocketbase-loader:${options.collectionName}`;
15
18
 
@@ -59,16 +62,6 @@ export async function loader(
59
62
  lastModified = undefined;
60
63
  }
61
64
 
62
- // Try to get a superuser token to access all resources.
63
- let token: string | undefined;
64
- if (options.superuserCredentials) {
65
- token = await getSuperuserToken(
66
- options.url,
67
- options.superuserCredentials,
68
- context.logger
69
- );
70
- }
71
-
72
65
  if (context.store.keys().length > 0) {
73
66
  // Cleanup entries that are no longer in the collection
74
67
  await cleanupEntries(options, context, token);
@@ -1,7 +1,9 @@
1
1
  import type { Loader } from "astro/loaders";
2
+ import type { ZodSchema } from "astro/zod";
2
3
  import { loader } from "./loader/loader";
3
4
  import { generateSchema } from "./schema/generate-schema";
4
5
  import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
6
+ import { getSuperuserToken } from "./utils/get-superuser-token";
5
7
 
6
8
  /**
7
9
  * Loader for collections stored in PocketBase.
@@ -9,11 +11,27 @@ import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.
9
11
  * @param options Options for the loader. See {@link PocketBaseLoaderOptions} for more details.
10
12
  */
11
13
  export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
14
+ // Get a superuser token if credentials are provided
15
+ let tokenPromise: Promise<string | undefined>;
16
+ if (options.superuserCredentials) {
17
+ tokenPromise = getSuperuserToken(options.url, options.superuserCredentials);
18
+ } else {
19
+ tokenPromise = Promise.resolve(undefined);
20
+ }
21
+
12
22
  return {
13
23
  name: "pocketbase-loader",
14
- // Load the entries from the collection
15
- load: async (context) => loader(context, options),
16
- // Generate the schema for the collection according to the API
17
- schema: async () => await generateSchema(options)
24
+ load: async (context): Promise<void> => {
25
+ const token = await tokenPromise;
26
+
27
+ // Load the entries from the collection
28
+ await loader(context, options, token);
29
+ },
30
+ schema: async (): Promise<ZodSchema> => {
31
+ const token = await tokenPromise;
32
+
33
+ // Generate the schema for the collection according to the API
34
+ return await generateSchema(options, token);
35
+ }
18
36
  };
19
37
  }
@@ -28,14 +28,18 @@ const VALID_ID_TYPES = ["text", "number", "email", "url", "date"];
28
28
  * If a path to a local schema file is provided, the schema is read from the file.
29
29
  *
30
30
  * @param options Options for the loader. See {@link PocketBaseLoaderOptions} for more details.
31
+ * @param token The superuser token to authenticate the request.
31
32
  */
32
33
  export async function generateSchema(
33
- options: PocketBaseLoaderOptions
34
+ options: PocketBaseLoaderOptions,
35
+ token: string | undefined
34
36
  ): Promise<ZodSchema> {
35
37
  let collection: PocketBaseCollection | undefined;
36
38
 
37
- // Try to get the schema directly from the PocketBase instance
38
- collection = await getRemoteSchema(options);
39
+ if (token) {
40
+ // Try to get the schema directly from the PocketBase instance
41
+ collection = await getRemoteSchema(options, token);
42
+ }
39
43
 
40
44
  const hasSuperuserRights = !!collection || !!options.superuserCredentials;
41
45
 
@@ -1,30 +1,16 @@
1
1
  import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
2
2
  import type { PocketBaseCollection } from "../types/pocketbase-schema.type";
3
- import { getSuperuserToken } from "../utils/get-superuser-token";
4
3
 
5
4
  /**
6
5
  * Fetches the schema for the specified collection from the PocketBase instance.
7
6
  *
8
7
  * @param options Options for the loader. See {@link PocketBaseLoaderOptions} for more details.
8
+ * @param token The superuser token to authenticate the request.
9
9
  */
10
10
  export async function getRemoteSchema(
11
- options: PocketBaseLoaderOptions
11
+ options: PocketBaseLoaderOptions,
12
+ token: string
12
13
  ): Promise<PocketBaseCollection | undefined> {
13
- if (!options.superuserCredentials) {
14
- return undefined;
15
- }
16
-
17
- // Get a superuser token
18
- const token = await getSuperuserToken(
19
- options.url,
20
- options.superuserCredentials
21
- );
22
-
23
- // If the token is invalid try another method
24
- if (!token) {
25
- return undefined;
26
- }
27
-
28
14
  // Build URL and headers for the schema request
29
15
  const schemaUrl = new URL(
30
16
  `api/collections/${options.collectionName}`,