[PIE-28] Switch to type-safe API #12

Merged
breadone merged 9 commits from PIE-28-2 into main 2025-08-16 13:17:10 +12:00
3 changed files with 44 additions and 5 deletions
Showing only changes of commit 2e54690ee4 - Show all commits

View File

@ -1,4 +1,11 @@
import Pocketbase from "pocketbase" import Pocketbase, { type RecordListOptions } from "pocketbase"
import {
type Recipe,
type Ingredient,
type Step,
type Tag,
Collection
} from './schema'
const client = new Pocketbase("http://localhost:4321") const client = new Pocketbase("http://localhost:4321")
client.autoCancellation(false) client.autoCancellation(false)
@ -10,3 +17,26 @@ client.files.getRelativeURL = (record: { [key: string]: any; }, filename: string
} }
export default client; export default client;
class APIClient {
client: Pocketbase
constructor() {
this.client = new Pocketbase("http://localhost:4321")
this.client.autoCancellation(false)
}
async getRecipes(page: number, perPage: number = 20, options: RecordListOptions) {
return await this.client.collection<Recipe>(Collection.RECIPES).getList(page, perPage, options)
}
async getAllRecipes() {
return await this.client.collection<Recipe>(Collection.RECIPES).getFullList({ expand: "steps,ingredients,tags" })
}
async getRecipe(id: string) {
return await this.client.collection<Recipe>(Collection.RECIPES).getOne(id, { expand: 'ingredients,tags,steps', })
}
}
export const client2 = new APIClient()

View File

@ -36,5 +36,10 @@ export interface Recipe extends BaseRecord {
tags?: Tag[] tags?: Tag[]
} }
export const Collection = {
RECIPES: 'recipes',
STEPS: 'steps',
INGREDIENTS: 'ingredients',
TAGS: 'tags',
IMAGES: 'images'
}

View File

@ -1,5 +1,6 @@
--- ---
import client from "@/data/pocketbase"; import client, { client2 } from "@/data/pocketbase";
import SiteLayout from "@/layouts/base"; import SiteLayout from "@/layouts/base";
import ImageCarousel from "@/components/Detail/ImageCarousel"; import ImageCarousel from "@/components/Detail/ImageCarousel";
import IngredientTableView from "@/components/Detail/IngredientTableView"; import IngredientTableView from "@/components/Detail/IngredientTableView";
@ -8,6 +9,9 @@ import InfoView from "@/components/Detail/InfoView";
const { recipeid } = Astro.params; const { recipeid } = Astro.params;
const re2 = await client2.getRecipe(recipeid as string)
console.log(re2)
const re = await client.collection("recipes").getOne(recipeid ?? "0"); const re = await client.collection("recipes").getOne(recipeid ?? "0");
const stepIds = re.steps const stepIds = re.steps