yogastic/yoga-app/src/api/strapi/strapi-client.ts
2025-05-23 11:45:09 +02:00

48 lines
1.4 KiB
TypeScript

import httpClient from "@/api/http-client";
import {Payload} from "@/types/generated-strapi-interfaces/common/Payload";
const STRAPI_URL = process.env.STRAPI_URL;
class StrapiClient{
constructor(private strapiUrl: string = "http://localhost:1337") {
}
public getImageUrl(imagePath: string){
if ( !imagePath ){
return "dummy.png"
}
return '/image/'+ imagePath;
}
public async httpGet(path: string){
console.info("httpGet", path);
return await httpClient.httpGet(this.strapiUrl + path);
}
public async httpGetJson<T>(url: string): Promise<Payload<T>>{
const response = await this.httpGet(url);
return await response.json();
}
public async findContentType<T>(contentType: string,options?: FindContentOptions): Promise<Payload<T>>{
const searchParams = new URLSearchParams();
if ( options?.populateAll ){
searchParams.append("populate","*");
}
if (options?.localeAll){
searchParams.append("_locale","all");
}
const response = await this.httpGet("/api/"+contentType+"?"+searchParams.toString());
return await response.json();
}
}
export interface FindContentOptions{
populateAll?: boolean;
localeAll?: boolean;
}
const client = new StrapiClient(STRAPI_URL);
export default client;