Files
yogastic/yoga-app/src/api/strapi/strapi-client.ts
2025-08-27 09:21:20 +02:00

62 lines
1.7 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) {
let result = undefined;
try {
const absoluteUrl = this.strapiUrl + path;
console.info("httpGet", {path,absoluteUrl});
result = await httpClient.httpGet(this.strapiUrl + path);
} catch (e) {
console.log("httpGet error", e);
throw e;
}
if (!result.ok) {
console.info("httpGet not ok", result);
throw new Error(result.statusText);
}
return result;
}
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;