astro-loader-pocketbase 2.6.1 → 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.1",
|
|
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",
|
|
@@ -44,21 +44,21 @@
|
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@commitlint/cli": "^19.8.1",
|
|
46
46
|
"@commitlint/config-conventional": "^19.8.1",
|
|
47
|
-
"@eslint/js": "^9.
|
|
48
|
-
"@stylistic/eslint-plugin": "^
|
|
47
|
+
"@eslint/js": "^9.30.0",
|
|
48
|
+
"@stylistic/eslint-plugin": "^5.0.0",
|
|
49
49
|
"@types/node": "^22.14.1",
|
|
50
50
|
"@vitest/coverage-v8": "^3.2.4",
|
|
51
|
-
"astro": "^5.
|
|
52
|
-
"eslint": "^9.
|
|
51
|
+
"astro": "^5.10.1",
|
|
52
|
+
"eslint": "^9.30.0",
|
|
53
53
|
"eslint-config-prettier": "^10.1.5",
|
|
54
54
|
"globals": "^16.2.0",
|
|
55
55
|
"husky": "^9.1.7",
|
|
56
56
|
"lint-staged": "^16.1.2",
|
|
57
|
-
"prettier": "^3.
|
|
57
|
+
"prettier": "^3.6.2",
|
|
58
58
|
"prettier-plugin-organize-imports": "^4.1.0",
|
|
59
|
-
"prettier-plugin-packagejson": "^2.5.
|
|
59
|
+
"prettier-plugin-packagejson": "^2.5.17",
|
|
60
60
|
"typescript": "^5.8.3",
|
|
61
|
-
"typescript-eslint": "^8.
|
|
61
|
+
"typescript-eslint": "^8.35.0",
|
|
62
62
|
"vitest": "^3.2.4"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
package/src/loader/loader.ts
CHANGED
|
@@ -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);
|
package/src/pocketbase-loader.ts
CHANGED
|
@@ -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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
38
|
-
|
|
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}`,
|