Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
"rules": {
// Correctness (in addition to the default rules)
"typescript/no-base-to-string": "off",
"typescript/restrict-template-expressions": "off",
"typescript/unbound-method": "off",
"promise/no-new-statics": "error",
"promise/valid-params": "error",
// Perf
Expand Down Expand Up @@ -115,7 +118,6 @@
"eslint/no-redeclare": "error",
"eslint/no-self-compare": "error",
"eslint/no-throw-literal": "error",
"eslint/require-await": "warn",
"eslint/symbol-description": "error",
"typescript/no-unsafe-argument": "error",
"typescript/no-unsafe-assignment": "error",
Expand Down Expand Up @@ -219,6 +221,13 @@
"eslint/max-lines": "off",
"eslint/max-lines-per-function": "off",
"eslint/max-nested-callbacks": "off",
"typescript/no-unsafe-argument": "off",
"typescript/no-unsafe-assignment": "off",
"typescript/no-unsafe-call": "off",
"typescript/no-unsafe-return": "off",
"typescript/no-unsafe-member-access": "off",
"typescript/no-unsafe-type-assertion": "off",
"typescript/no-explicit-any": "off",
// Vitest specific rules
"vitest/no-import-node-test": "warn",
"vitest/prefer-to-be-falsy": "warn",
Expand Down
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"oxc.oxc-vscode",
"esbenp.prettier-vscode",
"vitest.explorer"
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"oxc.typeAware": true,
"oxc.unusedDisableDirectives": "warn",
"oxc.lint.run": "onType"
}
20 changes: 20 additions & 0 deletions .zed/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Folder-specific settings
//
// For a full list of overridable settings, and general information on folder-specific settings,
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
{
"lsp": {
"oxc": {
"initialization_options": {
"options": {
"run": "onType",
"configPath": null,
"tsConfigPath": null,
"unusedDisableDirectives": "warn",
"typeAware": true,
"flags": {}
}
}
}
}
}
104 changes: 104 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"scripts": {
"format": "npx prettier . --write --cache",
"format:check": "npx prettier . --check --cache",
"lint": "oxlint",
"lint:fix": "oxlint --fix",
"lint": "oxlint --type-aware",
"lint:fix": "oxlint --type-aware --fix",
"prepare": "husky",
"test": "vitest run",
"test:e2e": "vitest run $(find test -name '*.e2e-spec.ts')",
Expand All @@ -52,6 +52,7 @@
"husky": "9.1.7",
"lint-staged": "16.2.4",
"oxlint": "1.22.0",
"oxlint-tsgolint": "^0.2.0",
"prettier": "3.6.2",
"prettier-plugin-organize-imports": "4.3.0",
"prettier-plugin-packagejson": "2.5.19",
Expand Down
33 changes: 27 additions & 6 deletions src/loader/cleanup-entries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { LoaderContext } from "astro/loaders";
import { z } from "astro/zod";
import {
pocketBaseErrorResponse,
pocketBaseListResponse
} from "../types/pocketbase-api-response.type";
import type { PocketBaseEntry } from "../types/pocketbase-entry.type";
import { pocketBaseBaseEntry } from "../types/pocketbase-entry.type";
import type { PocketBaseLoaderBaseOptions } from "../types/pocketbase-loader-options.type";

/**
Expand Down Expand Up @@ -67,10 +74,10 @@ export async function cleanupEntries(
);
}
} else {
const reason = await collectionRequest
.json()
.then((data) => data.message);
const errorMessage = `Fetching ids failed with status code ${collectionRequest.status}.\nReason: ${reason}`;
const errorResponse = pocketBaseErrorResponse.parse(
await collectionRequest.json()
);
const errorMessage = `Fetching ids failed with status code ${collectionRequest.status}.\nReason: ${errorResponse.message}`;
context.logger.error(errorMessage);
}

Expand All @@ -81,7 +88,9 @@ export async function cleanupEntries(
}

// Get the data from the response
const response = await collectionRequest.json();
const response = cleanUpEntriesResponse.parse(
await collectionRequest.json()
);

// Add the ids to the set
for (const item of response.items) {
Expand All @@ -97,7 +106,10 @@ export async function cleanupEntries(

// Create a mapping from PocketBase IDs to store keys for proper cleanup
const storedIds = new Map<string, string>(
context.store.values().map((entry) => [entry.data.id as string, entry.id])
context.store
.values()
// oxlint-disable-next-line no-unsafe-type-assertion
.map((entry) => [(entry.data as PocketBaseEntry).id, entry.id])
);

// Check which PocketBase IDs are missing from the server response
Expand All @@ -114,3 +126,12 @@ export async function cleanupEntries(
context.logger.info(`Cleaned up ${cleanedUp} old entries.`);
}
}

/**
* The response schema for cleaning up entries.
*/
const cleanUpEntriesResponse = pocketBaseListResponse
.omit({ items: true })
.extend({
items: z.array(pocketBaseBaseEntry.pick({ id: true }))
});
19 changes: 13 additions & 6 deletions src/loader/fetch-collection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
pocketBaseErrorResponse,
pocketBaseListResponse
} from "../types/pocketbase-api-response.type";
import type { PocketBaseEntry } from "../types/pocketbase-entry.type";
import type { ExperimentalPocketBaseLiveLoaderCollectionFilter } from "../types/pocketbase-live-loader-filter.type";
import type { PocketBaseLoaderBaseOptions } from "../types/pocketbase-loader-options.type";
Expand Down Expand Up @@ -77,18 +81,21 @@ export async function fetchCollection<TEntry extends PocketBaseEntry>(
}

// Get the reason for the error
const reason = await collectionRequest
.json()
.then((data) => data.message);
const errorMessage = `Fetching data failed with status code ${collectionRequest.status}.\nReason: ${reason}`;
const errorResponse = pocketBaseErrorResponse.parse(
await collectionRequest.json()
);
const errorMessage = `Fetching data failed with status code ${collectionRequest.status}.\nReason: ${errorResponse.message}`;
throw new Error(errorMessage);
}

// Get the data from the response
const response = await collectionRequest.json();
const response = pocketBaseListResponse.parse(
await collectionRequest.json()
);

// Return current chunk
await chunkLoaded(response.items);
// oxlint-disable-next-line no-unsafe-type-assertion
await chunkLoaded(response.items as Array<TEntry>);

// Update the page and total pages
page = response.page;
Expand Down
14 changes: 9 additions & 5 deletions src/loader/fetch-entry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { pocketBaseErrorResponse } from "../types/pocketbase-api-response.type";
import type { PocketBaseEntry } from "../types/pocketbase-entry.type";
import { pocketBaseEntry } from "../types/pocketbase-entry.type";
import type { ExperimentalPocketBaseLiveLoaderOptions } from "../types/pocketbase-loader-options.type";
import { combineFieldsForRequest } from "../utils/combine-fields-for-request";
import { formatFields } from "../utils/format-fields";
Expand Down Expand Up @@ -52,13 +54,15 @@ export async function fetchEntry<TEntry extends PocketBaseEntry>(
}

// Get the reason for the error
const reason = await entryRequest.json().then((data) => data.message);
const errorMessage = `Fetching entry "${id}" from collection "${options.collectionName}" failed.\nReason: ${reason}`;
const errorResponse = pocketBaseErrorResponse.parse(
await entryRequest.json()
);
const errorMessage = `Fetching entry "${id}" from collection "${options.collectionName}" failed.\nReason: ${errorResponse.message}`;
throw new Error(errorMessage);
}

// Get the data from the response
const response = await entryRequest.json();

return response;
const response = pocketBaseEntry.parse(await entryRequest.json());
// oxlint-disable-next-line no-unsafe-type-assertion
return response as TEntry;
}
3 changes: 1 addition & 2 deletions src/loader/live-collection-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ export async function liveCollectionLoader<TEntry extends PocketBaseEntry>(
try {
await fetchCollection<TEntry>(
options,
// oxlint-disable-next-line require-await
async (chunk) => {
entries.push(...chunk.map((entry) => parseLiveEntry(entry, options)));
},
token,
collectionFilter
);
} catch (error) {
return { error: error as Error };
return { error: error instanceof Error ? error : new Error(String(error)) };
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/loader/live-entry-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export async function liveEntryLoader<TEntry extends PocketBaseEntry>(
const entry = await fetchEntry<TEntry>(id, options, token);
return parseLiveEntry(entry, options);
} catch (error) {
return { error: error as Error };
return { error: error instanceof Error ? error : new Error(String(error)) };
}
}
6 changes: 3 additions & 3 deletions src/pocketbase-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
const token = await tokenPromise;

// Generate the schema for the collection according to the API
return await generateSchema(options, token);
return generateSchema(options, token);
}
};
}
Expand Down Expand Up @@ -78,15 +78,15 @@ export function experimentalPocketbaseLiveLoader<
const token = await tokenPromise;

// Load entries from the collection
return await liveCollectionLoader(filter, options, token);
return liveCollectionLoader<TEntry>(filter, options, token);
},
loadEntry: async ({
filter
}): Promise<LiveDataEntry<TEntry> | { error: Error }> => {
const token = await tokenPromise;

// Load a single entry from the collection
return await liveEntryLoader(filter.id, options, token);
return liveEntryLoader<TEntry>(filter.id, options, token);
}
};
}
Loading