astro-loader-pocketbase 2.3.0 → 2.3.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,41 +1,60 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "A content loader for Astro that uses the PocketBase API",
5
+ "keywords": [
6
+ "astro",
7
+ "astro-content-loader",
8
+ "astro-loader",
9
+ "pocketbase",
10
+ "withastro"
11
+ ],
12
+ "homepage": "https://github.com/pawcoding/astro-loader-pocketbase",
5
13
  "license": "MIT",
6
14
  "author": "Luis Wolf <development@pawcode.de> (https://pawcode.de)",
7
- "homepage": "https://github.com/pawcoding/astro-loader-pocketbase",
8
15
  "type": "module",
9
16
  "exports": {
10
- ".": "./index.ts"
17
+ ".": "./src/index.ts"
11
18
  },
12
19
  "files": [
13
- "index.ts",
14
20
  "src"
15
21
  ],
16
22
  "scripts": {
17
- "lint": "npx eslint",
18
- "prepare": "husky"
19
- },
20
- "peerDependencies": {
21
- "astro": "^5.0.0"
23
+ "format": "npx prettier . --write --cache",
24
+ "format:check": "npx prettier . --check --cache",
25
+ "lint": "npx eslint --cache",
26
+ "lint:fix": "npx eslint --fix --cache",
27
+ "prepare": "husky",
28
+ "test": "vitest run",
29
+ "test:e2e": "vitest run $(find test -name '*.e2e-spec.ts')",
30
+ "test:e2e:pocketbase": "npm run test:e2e:setup && ./.pocketbase/pocketbase serve",
31
+ "test:e2e:setup": "./scripts/setup-pocketbase.sh",
32
+ "test:e2e:watch": "vitest watch $(find test -name '*.e2e-spec.ts')",
33
+ "test:unit": "vitest run $(find test -name '*.spec.ts')",
34
+ "test:unit:watch": "vitest watch $(find test -name '*.spec.ts')",
35
+ "test:watch": "vitest watch"
22
36
  },
23
37
  "devDependencies": {
38
+ "@commitlint/cli": "^19.6.1",
39
+ "@commitlint/config-conventional": "^19.6.0",
24
40
  "@eslint/js": "^9.19.0",
25
41
  "@stylistic/eslint-plugin": "^3.0.1",
26
42
  "@types/node": "^22.13.0",
43
+ "@vitest/coverage-v8": "^3.0.4",
27
44
  "astro": "^5.2.3",
28
45
  "eslint": "^9.19.0",
46
+ "eslint-config-prettier": "^10.0.1",
29
47
  "globals": "^15.14.0",
30
48
  "husky": "^9.1.7",
49
+ "lint-staged": "^15.4.3",
50
+ "prettier": "^3.4.2",
51
+ "prettier-plugin-organize-imports": "^4.1.0",
52
+ "prettier-plugin-packagejson": "^2.5.8",
31
53
  "typescript": "^5.7.3",
32
- "typescript-eslint": "^8.22.0"
54
+ "typescript-eslint": "^8.22.0",
55
+ "vitest": "^3.0.4"
33
56
  },
34
- "keywords": [
35
- "astro",
36
- "astro-content-loader",
37
- "astro-loader",
38
- "pocketbase",
39
- "withastro"
40
- ]
57
+ "peerDependencies": {
58
+ "astro": "^5.0.0"
59
+ }
41
60
  }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { pocketbaseLoader } from "./pocketbase-loader";
2
+ import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
3
+
4
+ export { pocketbaseLoader };
5
+ export type { PocketBaseLoaderOptions };
@@ -1,5 +1,5 @@
1
1
  import type { LoaderContext } from "astro/loaders";
2
- import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
2
+ import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
3
3
 
4
4
  /**
5
5
  * Cleanup entries that are no longer in the collection.
@@ -1,7 +1,7 @@
1
1
  import type { LoaderContext } from "astro/loaders";
2
- import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
3
- import { isRealtimeData } from "./utils/is-realtime-data";
4
- import { parseEntry } from "./utils/parse-entry";
2
+ import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
3
+ import { isRealtimeData } from "../utils/is-realtime-data";
4
+ import { parseEntry } from "./parse-entry";
5
5
 
6
6
  /**
7
7
  * Handles realtime updates for the loader without making any new network requests.
@@ -1,6 +1,6 @@
1
1
  import type { LoaderContext } from "astro/loaders";
2
- import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
3
- import { parseEntry } from "./utils/parse-entry";
2
+ import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
3
+ import { parseEntry } from "./parse-entry";
4
4
 
5
5
  /**
6
6
  * Load (modified) entries from a PocketBase collection.
@@ -0,0 +1,78 @@
1
+ import type { LoaderContext } from "astro/loaders";
2
+ import packageJson from "../../package.json";
3
+ import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
4
+ import { getSuperuserToken } from "../utils/get-superuser-token";
5
+ import { shouldRefresh } from "../utils/should-refresh";
6
+ import { cleanupEntries } from "./cleanup-entries";
7
+ import { handleRealtimeUpdates } from "./handle-realtime-updates";
8
+ import { loadEntries } from "./load-entries";
9
+
10
+ export async function loader(
11
+ context: LoaderContext,
12
+ options: PocketBaseLoaderOptions
13
+ ): Promise<void> {
14
+ context.logger.label = `pocketbase-loader:${options.collectionName}`;
15
+
16
+ // Check if the collection should be refreshed.
17
+ const refresh = shouldRefresh(
18
+ context.refreshContextData,
19
+ options.collectionName
20
+ );
21
+ if (!refresh) {
22
+ return;
23
+ }
24
+
25
+ // Handle realtime updates
26
+ const handled = await handleRealtimeUpdates(context, options);
27
+ if (handled) {
28
+ return;
29
+ }
30
+
31
+ // Get the date of the last fetch to only update changed entries.
32
+ let lastModified = context.meta.get("last-modified");
33
+
34
+ // Check if the version has changed to force an update
35
+ const lastVersion = context.meta.get("version");
36
+ if (lastVersion !== packageJson.version) {
37
+ if (lastVersion) {
38
+ context.logger.info(
39
+ `PocketBase loader was updated from ${lastVersion} to ${packageJson.version}. All entries will be loaded again.`
40
+ );
41
+ }
42
+
43
+ // Disable incremental builds and clear the store
44
+ lastModified = undefined;
45
+ context.store.clear();
46
+ }
47
+
48
+ // Disable incremental builds if no updated field is provided
49
+ if (!options.updatedField) {
50
+ context.logger.info(
51
+ `No "updatedField" was provided. Incremental builds are disabled.`
52
+ );
53
+ lastModified = undefined;
54
+ }
55
+
56
+ // Try to get a superuser token to access all resources.
57
+ let token: string | undefined;
58
+ if (options.superuserCredentials) {
59
+ token = await getSuperuserToken(
60
+ options.url,
61
+ options.superuserCredentials,
62
+ context.logger
63
+ );
64
+ }
65
+
66
+ if (context.store.keys().length > 0) {
67
+ // Cleanup entries that are no longer in the collection
68
+ await cleanupEntries(options, context, token);
69
+ }
70
+
71
+ // Load the (modified) entries
72
+ await loadEntries(options, context, token, lastModified);
73
+
74
+ // Set the last modified date to the current date
75
+ context.meta.set("last-modified", new Date().toISOString().replace("T", " "));
76
+
77
+ context.meta.set("version", packageJson.version);
78
+ }
@@ -1,7 +1,7 @@
1
1
  import type { LoaderContext } from "astro/loaders";
2
2
  import type { PocketBaseEntry } from "../types/pocketbase-entry.type";
3
3
  import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
4
- import { slugify } from "./slugify";
4
+ import { slugify } from "../utils/slugify";
5
5
 
6
6
  /**
7
7
  * Parse an entry from PocketBase to match the schema and store it in the store.
@@ -1,13 +1,7 @@
1
- import type { Loader, LoaderContext } from "astro/loaders";
2
- import type { ZodSchema } from "astro/zod";
3
- import packageJson from "./../package.json";
4
- import { cleanupEntries } from "./cleanup-entries";
5
- import { generateSchema } from "./generate-schema";
6
- import { handleRealtimeUpdates } from "./handle-realtime-updates";
7
- import { loadEntries } from "./load-entries";
1
+ import type { Loader } from "astro/loaders";
2
+ import { loader } from "./loader/loader";
3
+ import { generateSchema } from "./schema/generate-schema";
8
4
  import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
9
- import { getSuperuserToken } from "./utils/get-superuser-token";
10
- import { shouldRefresh } from "./utils/should-refresh";
11
5
 
12
6
  /**
13
7
  * Loader for collections stored in PocketBase.
@@ -17,78 +11,9 @@ import { shouldRefresh } from "./utils/should-refresh";
17
11
  export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
18
12
  return {
19
13
  name: "pocketbase-loader",
20
- load: async (context: LoaderContext): Promise<void> => {
21
- context.logger.label = `pocketbase-loader:${options.collectionName}`;
22
-
23
- // Check if the collection should be refreshed.
24
- const refresh = shouldRefresh(
25
- context.refreshContextData,
26
- options.collectionName
27
- );
28
- if (!refresh) {
29
- return;
30
- }
31
-
32
- // Handle realtime updates
33
- const handled = await handleRealtimeUpdates(context, options);
34
- if (handled) {
35
- return;
36
- }
37
-
38
- // Get the date of the last fetch to only update changed entries.
39
- let lastModified = context.meta.get("last-modified");
40
-
41
- // Check if the version has changed to force an update
42
- const lastVersion = context.meta.get("version");
43
- if (lastVersion !== packageJson.version) {
44
- if (lastVersion) {
45
- context.logger.info(
46
- `PocketBase loader was updated from ${lastVersion} to ${packageJson.version}. All entries will be loaded again.`
47
- );
48
- }
49
-
50
- // Disable incremental builds and clear the store
51
- lastModified = undefined;
52
- context.store.clear();
53
- }
54
-
55
- // Disable incremental builds if no updated field is provided
56
- if (!options.updatedField) {
57
- context.logger.info(
58
- `No "updatedField" was provided. Incremental builds are disabled.`
59
- );
60
- lastModified = undefined;
61
- }
62
-
63
- // Try to get a superuser token to access all resources.
64
- let token: string | undefined;
65
- if (options.superuserCredentials) {
66
- token = await getSuperuserToken(
67
- options.url,
68
- options.superuserCredentials,
69
- context.logger
70
- );
71
- }
72
-
73
- if (context.store.keys().length > 0) {
74
- // Cleanup entries that are no longer in the collection
75
- await cleanupEntries(options, context, token);
76
- }
77
-
78
- // Load the (modified) entries
79
- await loadEntries(options, context, token, lastModified);
80
-
81
- // Set the last modified date to the current date
82
- context.meta.set(
83
- "last-modified",
84
- new Date().toISOString().replace("T", " ")
85
- );
86
-
87
- context.meta.set("version", packageJson.version);
88
- },
89
- schema: async (): Promise<ZodSchema> => {
90
- // Generate the schema for the collection according to the API
91
- return await generateSchema(options);
92
- }
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)
93
18
  };
94
19
  }
@@ -1,11 +1,11 @@
1
1
  import type { ZodSchema } from "astro/zod";
2
2
  import { z } from "astro/zod";
3
- import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
4
- import type { PocketBaseCollection } from "./types/pocketbase-schema.type";
5
- import { getRemoteSchema } from "./utils/get-remote-schema";
6
- import { parseSchema } from "./utils/parse-schema";
7
- import { readLocalSchema } from "./utils/read-local-schema";
8
- import { transformFiles } from "./utils/transform-files";
3
+ import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type";
4
+ import type { PocketBaseCollection } from "../types/pocketbase-schema.type";
5
+ import { getRemoteSchema } from "./get-remote-schema";
6
+ import { parseSchema } from "./parse-schema";
7
+ import { readLocalSchema } from "./read-local-schema";
8
+ import { transformFiles } from "./transform-files";
9
9
 
10
10
  /**
11
11
  * Basic schema for every PocketBase collection.
@@ -1,6 +1,6 @@
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 "./get-superuser-token";
3
+ import { getSuperuserToken } from "../utils/get-superuser-token";
4
4
 
5
5
  /**
6
6
  * Fetches the schema for the specified collection from the PocketBase instance.
@@ -56,7 +56,7 @@ export function transformFiles(
56
56
  * @param entryId ID of the entry.
57
57
  * @param file Name of the file.
58
58
  */
59
- function transformFileUrl(
59
+ export function transformFileUrl(
60
60
  base: string,
61
61
  collectionName: string,
62
62
  entryId: string,
package/index.ts DELETED
@@ -1,5 +0,0 @@
1
- import { pocketbaseLoader } from "./src/pocketbase-loader";
2
- import type { PocketBaseLoaderOptions } from "./src/types/pocketbase-loader-options.type";
3
-
4
- export { pocketbaseLoader };
5
- export type { PocketBaseLoaderOptions };
File without changes
File without changes