Starting basic API client
This commit is contained in:
parent
f7e24657f5
commit
2e54690ee4
@ -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")
|
||||
client.autoCancellation(false)
|
||||
@ -9,4 +16,27 @@ client.files.getRelativeURL = (record: { [key: string]: any; }, filename: string
|
||||
return res.substring(21)
|
||||
}
|
||||
|
||||
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()
|
@ -36,5 +36,10 @@ export interface Recipe extends BaseRecord {
|
||||
tags?: Tag[]
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const Collection = {
|
||||
RECIPES: 'recipes',
|
||||
STEPS: 'steps',
|
||||
INGREDIENTS: 'ingredients',
|
||||
TAGS: 'tags',
|
||||
IMAGES: 'images'
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
---
|
||||
import client from "@/data/pocketbase";
|
||||
import client, { client2 } from "@/data/pocketbase";
|
||||
|
||||
import SiteLayout from "@/layouts/base";
|
||||
import ImageCarousel from "@/components/Detail/ImageCarousel";
|
||||
import IngredientTableView from "@/components/Detail/IngredientTableView";
|
||||
@ -8,6 +9,9 @@ import InfoView from "@/components/Detail/InfoView";
|
||||
|
||||
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 stepIds = re.steps
|
||||
|
Loading…
x
Reference in New Issue
Block a user