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:
48
src/api.ts
48
src/api.ts
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
35
src/store.ts
35
src/store.ts
@@ -1,4 +1,5 @@
|
||||
import { writable, get } from "svelte/store"
|
||||
import { getGeneralInformation, getArticles } from "./api"
|
||||
|
||||
const initLoc = {
|
||||
path: (typeof window !== "undefined" && window.location?.pathname) || "/",
|
||||
@@ -10,3 +11,37 @@ const initLoc = {
|
||||
initLoc.categoryPath = initLoc.path.replace(/\/\d{4,99}[^\/]+$/, "")
|
||||
|
||||
export const location = writable(initLoc)
|
||||
|
||||
// General Information
|
||||
|
||||
export const generalInformation = writable<GeneralInformation>()
|
||||
const getGeneralProjectInformation = async () => {
|
||||
const infos = await getGeneralInformation()
|
||||
if (infos && infos.length) {
|
||||
generalInformation.set(infos[0])
|
||||
}
|
||||
}
|
||||
getGeneralProjectInformation()
|
||||
|
||||
// Articles
|
||||
|
||||
export const articles = writable<TibiArticle[]>()
|
||||
const getAllArticles = async () => {
|
||||
const list = await getArticles()
|
||||
articles.set(list)
|
||||
}
|
||||
getAllArticles()
|
||||
|
||||
// Cookies - Webmakers Cookie Bar
|
||||
|
||||
export const ccTags = writable<string[]>([])
|
||||
|
||||
const updateCcTtags = () => {
|
||||
// @ts-ignore
|
||||
ccTags.set(window.ccLoadedTags || [])
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("ccInit", updateCcTtags)
|
||||
window.addEventListener("ccAccept", updateCcTtags)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user