astro-loader-pocketbase
Version:
A content loader for Astro that uses the PocketBase API
275 lines • 9.4 kB
text/typescript
import { LiveCollectionError } from "astro/content/runtime";
import { ZodType, z } from "astro/zod";
import { LiveLoader, LoaderContext } from "astro/loaders";
//#region src/types/pocketbase-entry.type.d.ts
/**
* Schema for a PocketBase entry.
*/
declare const pocketBaseEntry: z.ZodObject<{
id: z.ZodString;
collectionId: z.ZodString;
collectionName: z.ZodString;
}, z.core.$loose>;
/**
* Type for a PocketBase entry.
*/
type PocketBaseEntry = z.infer<typeof pocketBaseEntry>;
//#endregion
//#region src/types/pocketbase-live-loader-filter.type.d.ts
/**
* Filter for a single entry
*/
interface PocketBaseLiveLoaderEntryFilter {
/**
* Id of the entry.
*/
id: string;
}
/**
* Filter for a collection of entries.
*/
interface PocketBaseLiveLoaderCollectionFilter {
/**
* Additional filter to apply to the collection.
* This will be added to the filter supplied in the {@link PocketBaseLiveLoaderOptions}.
*
* Example:
* ```ts
* // config:
* filter: 'release >= @now && deleted = false'
*
* // request
* `?filter=(${loaderFilter})&&(release >= @now && deleted = false)`
* ```
*
* @see {@link https://pocketbase.io/docs/api-records/#listsearch-records PocketBase documentation} for valid syntax
*/
filter?: string;
/**
* Page number to load (1-indexed).
*/
page?: number;
/**
* Number of entries to load per page.
* If not provided all entries will be loaded in chunks of 100.
*/
perPage?: number;
/**
* Sort order in which the entries should be returned.
*
* Example:
* ```ts
* // config:
* sort: '-created,id'
*
* // request
* `?sort=-created,id`
* ```
*
* @see {@link https://pocketbase.io/docs/api-records/#listsearch-records PocketBase documentation} for valid syntax
*/
sort?: string;
}
//#endregion
//#region src/types/pocketbase-loader-options.type.d.ts
/**
* Options for both build time and live collection loader
*/
interface PocketBaseLoaderBaseOptions {
/**
* URL of the PocketBase instance.
*/
url: string;
/**
* Name of the collection in PocketBase.
*/
collectionName: string;
/**
* Name of the field(s) containing the content of an entry.
* This must be the name of a field in the PocketBase collection that contains the content.
* The content will be parsed as HTML and rendered to the page.
*
* If you want to render multiple fields as main content, you can pass an array of field names.
* The loader will concatenate the content of all fields in the order they are defined in the array.
* Each block will be contained in a `<section>` element.
*/
contentFields?: string | Array<string>;
/**
* Name of the field containing the last update date of an entry.
* Ideally, this field should be of type `autodate` and have the value "Update" or "Create/Update".
* For the build time loader, this field is used to only fetch entries that have been modified since the last build.
* For the live collection loader, this field is used to set the `lastModified` cache hint.
*/
updatedField?: string;
/**
* Custom filter that is applied when loading data from PocketBase.
*
* The loader will also add it's own filters for incremental builds.
* These will be added to your custom filter query.
*
* Example:
* ```ts
* // config:
* filter: 'release >= @now && deleted = false'
*
* // request
* `?filter=(${loaderFilter})&&(release >= @now && deleted = false)`
* ```
*
* @see {@link https://pocketbase.io/docs/api-records/#listsearch-records PocketBase documentation} for valid syntax
*/
filter?: string;
/**
* Specify which fields to return for each record.
* This can be either a comma-separated string of field names or an array of field names.
* Only the specified fields will be included in the response and schema.
*
* Use "*" to include all fields (same as not specifying the fields option).
*
* Note: The basic fields (`id`, `collectionId`, `collectionName`) are automatically included
* in API requests when using field filtering. Additionally, any custom fields specified in the
* loader options (`idField`, `updatedField`, `contentFields`) are also automatically included.
*
* Warning: Expand fields are not currently supported by this loader.
*
* Example:
* ```ts
* // Using string format:
* fields: 'title,content,author'
*
* // Using array format:
* fields: ['title', 'content', 'author']
*
* // Include all fields:
* fields: '*'
* ```
*
* @see {@link https://pocketbase.io/docs/api-records/#listsearch-records PocketBase documentation} for valid syntax
*/
fields?: string | Array<string>;
/**
* Credentials of a superuser to get full access to the PocketBase instance.
* 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.
*/
superuserCredentials?: {
/**
* Email of the superuser.
*/
email: string;
/**
* Password of the superuser.
*/
password: string;
} | {
/**
* Impersonate auth token of the superuser.
* This token will take precedence over the email and password.
*/
impersonateToken: string;
};
}
/**
* Options for the PocketBase loader.
*/
type PocketBaseLoaderOptions = PocketBaseLoaderBaseOptions & {
/**
* Field that should be used as the unique identifier for the collection.
* This must be the name of a field in the collection that contains unique values.
* If not provided, the `id` field will be used.
* The value of this field will be used in `getEntry` and `getEntries` to load the entry or entries.
*
* If the field is a string, it will be slugified to be used in the URL.
*/
idField?: string;
/**
* File path to the local schema file.
* This file will be used to generate the schema for the collection.
* If `superuserCredentials` are provided, this option will be ignored.
*/
localSchema?: string;
/**
* Record of zod schemas for all JSON fields in the collection.
* The key must be the name of a field in the collection.
* If no schema is provided for a field, the value will be treated as `unknown`.
*
* Note that this will only be used for fields of type `json`.
*/
jsonSchemas?: Record<string, ZodType>;
/**
* Experimental options for the loader.
*
* @experimental All of these options are experimental and may change in the future.
*/
experimental?: {
/**
* Whether to only create types for the live loader.
* This will not load any data, but only generate types that can be used with the live loader.
*/
liveTypesOnly?: boolean;
};
};
/**
* Options for the PocketBase live loader.
*/
type PocketBaseLiveLoaderOptions = PocketBaseLoaderBaseOptions;
//#endregion
//#region src/pocketbase-loader.d.ts
/**
* Loader for collections stored in PocketBase.
*
* @param options Options for the loader. See {@link PocketBaseLoaderOptions} for more details.
*/
declare function pocketbaseLoader(options: PocketBaseLoaderOptions): {
name: string;
load: (context: LoaderContext) => Promise<void>;
createSchema: () => Promise<{
schema: import("zod/v4").ZodObject<{
id: import("zod/v4").ZodString;
collectionId: import("zod/v4").ZodString;
collectionName: import("zod/v4").ZodString;
}, import("zod/v4/core").$strip> | import("zod/v4").ZodPipe<import("zod/v4").ZodObject<{
id: import("zod/v4").ZodString;
collectionId: import("zod/v4").ZodString;
collectionName: import("zod/v4").ZodString;
}, import("zod/v4/core").$strip>, import("zod/v4").ZodTransform<{
[x: string]: unknown;
id: string;
collectionId: string;
collectionName: string;
}, {
id: string;
collectionId: string;
collectionName: string;
}>>;
types: string;
}>;
};
/**
* Live loader for collections stored in PocketBase.
*
* @param options Options for the live loader. See {@link PocketBaseLiveLoaderOptions} for more details.
*/
declare function pocketbaseLiveLoader<TEntry extends PocketBaseEntry>(options: PocketBaseLiveLoaderOptions): LiveLoader<TEntry, PocketBaseLiveLoaderEntryFilter, PocketBaseLiveLoaderCollectionFilter, LiveCollectionError>;
//#endregion
//#region src/schema/transform-files.d.ts
/**
* Transforms a file name to a PocketBase file URL.
*
* @param base Base URL of the PocketBase instance.
* @param collectionName Name of the collection.
* @param entryId ID of the entry.
* @param file Name of the file.
*/
declare function transformFileUrl(base: string, collectionName: string, entryId: string, file: string): string;
//#endregion
//#region src/types/errors.d.ts
/**
* Error thrown when there is an authentication issue with PocketBase.
*/
declare class PocketBaseAuthenticationError extends LiveCollectionError {
constructor(collection: string, message: string);
static is(error: unknown): error is PocketBaseAuthenticationError;
}
//#endregion
export { PocketBaseAuthenticationError, type PocketBaseLiveLoaderCollectionFilter, type PocketBaseLiveLoaderEntryFilter, type PocketBaseLiveLoaderOptions, type PocketBaseLoaderOptions, pocketbaseLiveLoader, pocketbaseLoader, transformFileUrl };
//# sourceMappingURL=index.d.mts.map