Erste kleine Anpassungen am Tibi-Svelte-Starter um später mehr Zeit in neuen Projekten zu sparen. Hier werden noch weitere Anpassungen folgen, die grundlegend in den meisten Projekten benötigt werden.

This commit is contained in:
2022-03-17 11:12:06 +01:00
parent 75a8906d4a
commit 5caa62eb7e
9 changed files with 585 additions and 26 deletions

View File

@@ -43,9 +43,7 @@ const _f = function (url, options): Promise<Response> {
.replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push((key = key.toLowerCase()))
all.push([key, value])
headers[key] = headers[key]
? `${headers[key]},${value}`
: value
headers[key] = headers[key] ? `${headers[key]},${value}` : value
})
resolve(response())
}
@@ -62,12 +60,7 @@ const _f = function (url, options): Promise<Response> {
})
}
const _fetch =
typeof fetch === "undefined"
? typeof window === "undefined"
? _f
: window.fetch || _f
: fetch
const _fetch = typeof fetch === "undefined" ? (typeof window === "undefined" ? _f : window.fetch || _f) : fetch
export const api = async <T>(
endpoint: string,
@@ -107,8 +100,7 @@ export const api = async <T>(
let method = "GET"
let query = "&count=1"
if (options?.filter)
query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
if (options?.filter) query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
if (options?.sort) query += "&sort=" + options.sort + "&sort=_id"
if (options?.limit) query += "&limit=" + options.limit
if (options?.offset) query += "&offset=" + options.offset
@@ -149,8 +141,7 @@ export const api = async <T>(
// @ts-ignore
let data = (await response?.json()) || null
if (response?.status < 200 || response?.status >= 400)
throw { response, data }
if (response?.status < 200 || response?.status >= 400) throw { response, data }
// @ts-ignore
return { data, count: response?.headers?.get("x-results-count") || 0 }
@@ -163,3 +154,34 @@ export const getContent = async (path: string): Promise<Content> => {
}
return null
}
export const getGeneralInformation = async (): Promise<GeneralInformation[]> => {
try {
let response = await api<GeneralInformation[]>("general", {
method: "get",
offset: 0,
limit: 1,
filter: {
active: true,
},
})
return response.data
} catch (e) {
return null
}
}
export const getArticles = async (): Promise<TibiArticle[]> => {
try {
let response = await api<TibiArticle[]>("articles", {
method: "get",
offset: 0,
filter: {
active: true,
},
})
return response.data
} catch (e) {
return null
}
}