astro-loader-pocketbase 2.6.3-next.1 → 2.7.0-next.2

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/README.md CHANGED
@@ -143,12 +143,16 @@ const blog = defineCollection({
143
143
  ...options,
144
144
  superuserCredentials: {
145
145
  email: "<superuser-email>",
146
- password: "<superuser-password>"
146
+ password: "<superuser-password>",
147
+ // or
148
+ impersonateToken: "<superuser-impersonate-token>"
147
149
  }
148
150
  })
149
151
  });
150
152
  ```
151
153
 
154
+ _It's recommended to use an [impersonate token (API token)](https://pocketbase.io/docs/authentication/#api-keys) instead of the email and password, as this is more secure and can be easily revoked._
155
+
152
156
  Under the hood, the loader will use the [PocketBase API](https://pocketbase.io/docs/api-collections/#view-collection) to fetch the schema of your collection and generate types with Zod based on that schema.
153
157
 
154
158
  ### Local schema
@@ -193,18 +197,18 @@ This will remove `undefined` from the type of these fields and mark them as requ
193
197
 
194
198
  ## All options
195
199
 
196
- | Option | Type | Required | Description |
197
- | ---------------------- | ------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
198
- | `url` | `string` | x | The URL of your PocketBase instance. |
199
- | `collectionName` | `string` | x | The name of the collection in your PocketBase instance. |
200
- | `idField` | `string` | | The field in the collection to use as unique id. Defaults to `id`. |
201
- | `contentFields` | `string \| Array<string>` | | The field in the collection to use as content. This can also be an array of fields. |
202
- | `updatedField` | `string` | | The field in the collection that stores the last update date of an entry. This is used for incremental builds. |
203
- | `filter` | `string` | | Custom filter to use when fetching entries. Used to filter the entries by specific conditions. |
204
- | `superuserCredentials` | `{ email: string, password: string }` | | The email and password of the superuser of the PocketBase instance. This is used for automatic type generation. |
205
- | `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
206
- | `jsonSchemas` | `Record<string, z.ZodSchema>` | | A record of Zod schemas to use for type generation of `json` fields. |
207
- | `improveTypes` | `boolean` | | Whether to improve the types of `number` and `boolean` fields, removing `undefined` from them. |
200
+ | Option | Type | Required | Description |
201
+ | ---------------------- | --------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------- |
202
+ | `url` | `string` | x | The URL of your PocketBase instance. |
203
+ | `collectionName` | `string` | x | The name of the collection in your PocketBase instance. |
204
+ | `idField` | `string` | | The field in the collection to use as unique id. Defaults to `id`. |
205
+ | `contentFields` | `string \| Array<string>` | | The field in the collection to use as content. This can also be an array of fields. |
206
+ | `updatedField` | `string` | | The field in the collection that stores the last update date of an entry. This is used for incremental builds. |
207
+ | `filter` | `string` | | Custom filter to use when fetching entries. Used to filter the entries by specific conditions. |
208
+ | `superuserCredentials` | `{ email: string, password: string } \| { impersonateToken: string }` | | The email and password or impersonate token of a superuser of the PocketBase instance. This is used for automatic type generation. |
209
+ | `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
210
+ | `jsonSchemas` | `Record<string, z.ZodSchema>` | | A record of Zod schemas to use for type generation of `json` fields. |
211
+ | `improveTypes` | `boolean` | | Whether to improve the types of `number` and `boolean` fields, removing `undefined` from them. |
208
212
 
209
213
  ## Special cases
210
214
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "2.6.3-next.1",
3
+ "version": "2.7.0-next.2",
4
4
  "description": "A content loader for Astro that uses the PocketBase API",
5
5
  "keywords": [
6
6
  "astro",
@@ -39,26 +39,27 @@
39
39
  "test:e2e:watch": "vitest watch $(find test -name '*.e2e-spec.ts')",
40
40
  "test:unit": "vitest run $(find test -name '*.spec.ts')",
41
41
  "test:unit:watch": "vitest watch $(find test -name '*.spec.ts')",
42
- "test:watch": "vitest watch"
42
+ "test:watch": "vitest watch",
43
+ "typecheck": "npx tsc --noEmit"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@commitlint/cli": "^19.8.1",
46
47
  "@commitlint/config-conventional": "^19.8.1",
47
- "@eslint/js": "^9.30.0",
48
- "@stylistic/eslint-plugin": "^5.0.0",
48
+ "@eslint/js": "^9.30.1",
49
+ "@stylistic/eslint-plugin": "^5.1.0",
49
50
  "@types/node": "^22.14.1",
50
51
  "@vitest/coverage-v8": "^3.2.4",
51
- "astro": "^5.10.1",
52
- "eslint": "^9.30.0",
52
+ "astro": "^5.11.0",
53
+ "eslint": "^9.30.1",
53
54
  "eslint-config-prettier": "^10.1.5",
54
- "globals": "^16.2.0",
55
+ "globals": "^16.3.0",
55
56
  "husky": "^9.1.7",
56
57
  "lint-staged": "^16.1.2",
57
58
  "prettier": "^3.6.2",
58
59
  "prettier-plugin-organize-imports": "^4.1.0",
59
- "prettier-plugin-packagejson": "^2.5.17",
60
+ "prettier-plugin-packagejson": "^2.5.18",
60
61
  "typescript": "^5.8.3",
61
- "typescript-eslint": "^8.35.0",
62
+ "typescript-eslint": "^8.35.1",
62
63
  "vitest": "^3.2.4"
63
64
  },
64
65
  "peerDependencies": {
@@ -56,9 +56,16 @@ export async function cleanupEntries(
56
56
  if (!collectionRequest.ok) {
57
57
  // If the collection is locked, an superuser token is required
58
58
  if (collectionRequest.status === 403) {
59
- context.logger.error(
60
- `The collection is not accessible without superuser rights. Please provide superuser credentials in the config.`
61
- );
59
+ if (
60
+ options.superuserCredentials &&
61
+ "impersonateToken" in options.superuserCredentials
62
+ ) {
63
+ context.logger.error("The given impersonate token is not valid.");
64
+ } else {
65
+ context.logger.error(
66
+ "The collection is not accessible without superuser rights. Please provide superuser credentials in the config."
67
+ );
68
+ }
62
69
  } else {
63
70
  const reason = await collectionRequest
64
71
  .json()
@@ -9,8 +9,6 @@ import { parseEntry } from "./parse-entry";
9
9
  * @param context Context of the loader.
10
10
  * @param superuserToken Superuser token to access all resources.
11
11
  * @param lastModified Date of the last fetch to only update changed entries.
12
- *
13
- * @returns `true` if the collection has an updated column, `false` otherwise.
14
12
  */
15
13
  export async function loadEntries(
16
14
  options: PocketBaseLoaderOptions,
@@ -78,9 +76,16 @@ export async function loadEntries(
78
76
  if (!collectionRequest.ok) {
79
77
  // If the collection is locked, an superuser token is required
80
78
  if (collectionRequest.status === 403) {
81
- throw new Error(
82
- `The collection is not accessible without superuser rights. Please provide superuser credentials in the config.`
83
- );
79
+ if (
80
+ options.superuserCredentials &&
81
+ "impersonateToken" in options.superuserCredentials
82
+ ) {
83
+ throw new Error("The given impersonate token is not valid.");
84
+ } else {
85
+ throw new Error(
86
+ "The collection is not accessible without superuser rights. Please provide superuser credentials in the config."
87
+ );
88
+ }
84
89
  }
85
90
 
86
91
  // Get the reason for the error
@@ -11,11 +11,22 @@ import { getSuperuserToken } from "./utils/get-superuser-token";
11
11
  * @param options Options for the loader. See {@link PocketBaseLoaderOptions} for more details.
12
12
  */
13
13
  export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
14
- // Get a superuser token if credentials are provided
15
14
  let tokenPromise: Promise<string | undefined>;
16
15
  if (options.superuserCredentials) {
17
- tokenPromise = getSuperuserToken(options.url, options.superuserCredentials);
16
+ if ("impersonateToken" in options.superuserCredentials) {
17
+ // Impersonate token provided, so use it directly.
18
+ tokenPromise = Promise.resolve(
19
+ options.superuserCredentials.impersonateToken
20
+ );
21
+ } else {
22
+ // Email and password provided, so get a temporary superuser token.
23
+ tokenPromise = getSuperuserToken(
24
+ options.url,
25
+ options.superuserCredentials
26
+ );
27
+ }
18
28
  } else {
29
+ // No credentials provided, so no token can be used.
19
30
  tokenPromise = Promise.resolve(undefined);
20
31
  }
21
32
 
@@ -58,16 +58,24 @@ export interface PocketBaseLoaderOptions {
58
58
  * Credentials of a superuser to get full access to the PocketBase instance.
59
59
  * This is required to get automatic type generation without a local schema, to access all resources even if they are not public and to fetch content of hidden fields.
60
60
  */
61
- superuserCredentials?: {
62
- /**
63
- * Email of the superuser.
64
- */
65
- email: string;
66
- /**
67
- * Password of the superuser.
68
- */
69
- password: string;
70
- };
61
+ superuserCredentials?:
62
+ | {
63
+ /**
64
+ * Email of the superuser.
65
+ */
66
+ email: string;
67
+ /**
68
+ * Password of the superuser.
69
+ */
70
+ password: string;
71
+ }
72
+ | {
73
+ /**
74
+ * Impersonate auth token of the superuser.
75
+ * This token will take precedence over the email and password.
76
+ */
77
+ impersonateToken: string;
78
+ };
71
79
  /**
72
80
  * File path to the local schema file.
73
81
  * This file will be used to generate the schema for the collection.