forked from cms/tibi-svelte-starter
Starter Projekt angefangen, etwas aufzubohren und ein paar grundlegend benötigte Collections, Teheming-Styles und Komponenten hinzugefügt. (WIP)
This commit is contained in:
116
src/api.ts
116
src/api.ts
@@ -1,6 +1,7 @@
|
||||
import { apiBaseURL } from "./config"
|
||||
import * as sentry from "./sentry"
|
||||
import * as SSR from "../api/hooks/lib/ssr.js"
|
||||
import config from "../api/hooks/config"
|
||||
|
||||
// [MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)
|
||||
const _f = function (url, options): Promise<Response> {
|
||||
@@ -74,9 +75,12 @@ export const api = async <T>(
|
||||
headers?: {
|
||||
[key: string]: string
|
||||
}
|
||||
params?: {
|
||||
[key: string]: string
|
||||
}
|
||||
params?: APIParams
|
||||
body?: any
|
||||
noToken?: boolean
|
||||
signal?: AbortSignal
|
||||
_groupBy?: string
|
||||
showErrors?: boolean
|
||||
}
|
||||
): Promise<{ data: T; count: number }> => {
|
||||
if (typeof window === "undefined") {
|
||||
@@ -98,6 +102,7 @@ export const api = async <T>(
|
||||
}
|
||||
|
||||
let method = "GET"
|
||||
if (options?.method) method = options.method.toUpperCase()
|
||||
|
||||
let query = "&count=1"
|
||||
if (options?.filter) query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
|
||||
@@ -105,6 +110,7 @@ export const api = async <T>(
|
||||
if (options?.limit) query += "&limit=" + options.limit
|
||||
if (options?.offset) query += "&offset=" + options.offset
|
||||
if (options?.projection) query += "&projection=" + options.projection
|
||||
if (options?._groupBy) query += "&_groupBy=" + options._groupBy
|
||||
|
||||
if (options?.params) {
|
||||
Object.keys(options.params).forEach((p) => {
|
||||
@@ -116,9 +122,17 @@ export const api = async <T>(
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
// send jwt
|
||||
if (!options?.noToken) {
|
||||
// try {
|
||||
// headers["Authorization"] =
|
||||
// "Bearer " + (await getLogin()).tokenString
|
||||
// } catch (e) { }
|
||||
}
|
||||
|
||||
if (options?.headers) headers = { ...headers, ...options.headers }
|
||||
|
||||
let url = apiBaseURL + endpoint + (query ? "?" + query : "")
|
||||
let url = apiBaseURL + endpoint + (query ? (endpoint.includes("?") ? query : "?" + query) : "")
|
||||
|
||||
const span = sentry.currentTransaction()?.startChild({
|
||||
op: "fetch",
|
||||
@@ -130,21 +144,46 @@ export const api = async <T>(
|
||||
headers["sentry-trace"] = trace_id
|
||||
}
|
||||
|
||||
const response = await _fetch(url, {
|
||||
method,
|
||||
mode: "cors",
|
||||
headers,
|
||||
try {
|
||||
const response = await _fetch(url, {
|
||||
method,
|
||||
mode: "cors",
|
||||
headers,
|
||||
body: options?.body ? JSON.stringify(options.body) : null,
|
||||
signal: options?.signal,
|
||||
})
|
||||
|
||||
span?.finish()
|
||||
|
||||
// @ts-ignore
|
||||
let data = (await response?.json()) || null
|
||||
|
||||
if (response?.status < 200 || response?.status >= 400) throw { response, data }
|
||||
|
||||
// @ts-ignore
|
||||
return { data, count: response?.headers?.get("x-results-count") || 0 }
|
||||
} catch (e) {
|
||||
if (options?.showErrors && !(e instanceof DOMException)) {
|
||||
// newNotification({
|
||||
// class: "error",
|
||||
// html: "Es ist ein Fehler aufgetreten! Bitte laden Sie die Seite neu und versuchen es später nocheinmal.",
|
||||
// timeout: 6000,
|
||||
// })
|
||||
}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
export const sendEmail = async (type: string = "contactForm", data: any, noToken?: boolean) => {
|
||||
await api("email?type=" + type, {
|
||||
method: "post",
|
||||
body: data,
|
||||
noToken,
|
||||
showErrors: true,
|
||||
headers: {
|
||||
token: config.publicToken,
|
||||
},
|
||||
})
|
||||
|
||||
span?.finish()
|
||||
|
||||
// @ts-ignore
|
||||
let data = (await response?.json()) || null
|
||||
|
||||
if (response?.status < 200 || response?.status >= 400) throw { response, data }
|
||||
|
||||
// @ts-ignore
|
||||
return { data, count: response?.headers?.get("x-results-count") || 0 }
|
||||
}
|
||||
|
||||
export const getContent = async (path: string): Promise<Content> => {
|
||||
@@ -155,14 +194,14 @@ export const getContent = async (path: string): Promise<Content> => {
|
||||
return null
|
||||
}
|
||||
|
||||
export const getGeneralInformation = async (): Promise<GeneralInformation[]> => {
|
||||
export const getGeneralInformation = async (): Promise<GeneralInfo[]> => {
|
||||
try {
|
||||
let response = await api<GeneralInformation[]>("general", {
|
||||
let response = await api<GeneralInfo[]>("general", {
|
||||
method: "get",
|
||||
offset: 0,
|
||||
limit: 1,
|
||||
filter: {
|
||||
active: true,
|
||||
public: true,
|
||||
},
|
||||
})
|
||||
return response.data
|
||||
@@ -185,3 +224,38 @@ export const getArticles = async (): Promise<TibiArticle[]> => {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const getNavigations = async (l: Locale): Promise<Navigation[]> => {
|
||||
try {
|
||||
let response = await api<Navigation[]>("navigation", {
|
||||
method: "get",
|
||||
offset: 0,
|
||||
filter: {
|
||||
locale: l.key,
|
||||
},
|
||||
})
|
||||
return response.data
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const getGalleries = async (galIds: number[], params?: APIParams): Promise<Gallery[]> => {
|
||||
try {
|
||||
let response = await api<Gallery[]>("galleries", {
|
||||
method: "get",
|
||||
offset: 0,
|
||||
params: {
|
||||
sort: "title",
|
||||
...params,
|
||||
},
|
||||
filter: {
|
||||
_id: { $in: galIds },
|
||||
},
|
||||
})
|
||||
|
||||
return response.data
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user