Moved payload and caches to services

This commit is contained in:
DrMint 2024-07-13 14:15:50 +02:00
parent c707733bbc
commit 989854957d
31 changed files with 61 additions and 61 deletions

View File

@ -1,7 +1,7 @@
---
import { UAParser } from "ua-parser-js";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import { PostProcessingTags } from "src/middleware/postProcessing";
import type {
EndpointAudio,

View File

@ -2,7 +2,7 @@
import Button from "components/Button.astro";
import Tooltip from "components/Tooltip.astro";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import { formatCurrency } from "src/utils/currencies";
import {
PostProcessingTags,

View File

@ -2,7 +2,7 @@
import Button from "components/Button.astro";
import Tooltip from "components/Tooltip.astro";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import { formatLocale } from "src/utils/format";
interface Props {

View File

@ -1,5 +1,5 @@
import type { WordingKey } from "src/i18n/wordings-keys";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import { capitalize, formatInlineTitle } from "src/utils/format";
import type { EndpointChronologyEvent, EndpointSource } from "src/shared/payload/endpoint-types";

View File

@ -1,5 +1,5 @@
import { defineMiddleware } from "astro:middleware";
import { pageCache } from "src/utils/payload";
import { pageCache } from "src/services";
export const pageCachingMiddleware = defineMiddleware(async ({ url, request, locals }, next) => {
const pathname = url.pathname;

View File

@ -1,6 +1,6 @@
import type { AstroCookies } from "astro";
import { z } from "astro:content";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import acceptLanguage from "accept-language";
export const getAbsoluteLocaleUrl = (locale: string, url: string) => `/${locale}${url}`;

View File

@ -1,7 +1,7 @@
---
import LibraryCard from "./LibraryCard.astro";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
const { getLocalizedUrl, getLocalizedMatch } = await getI18n(Astro.locals.currentLocale);
---

View File

@ -1,7 +1,7 @@
import type { APIRoute } from "astro";
import { Collections } from "src/shared/payload/constants";
import type { AfterOperationWebHookMessage } from "src/shared/payload/webhooks";
import { contextCache, dataCache } from "src/utils/payload.ts";
import { contextCache, dataCache } from "src/services.ts";
export const POST: APIRoute = async ({ request }) => {
const auth = request.headers.get("Authorization");

View File

@ -1,5 +1,5 @@
import type { APIRoute } from "astro";
import { dataCache, pageCache } from "src/utils/payload";
import { dataCache, pageCache } from "src/services";
export const GET: APIRoute = async () => {
await dataCache.init();

View File

@ -7,7 +7,7 @@ import { getI18n } from "src/i18n/i18n";
import AsideLayout from "components/AppLayout/AsideLayout.astro";
import Attributes from "components/Attributes.astro";
import type { Attribute } from "src/utils/attributes";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { sizesToSrcset } from "src/utils/img";
import PageCredits from "./_components/PageCredits.astro";
import type { EndpointPage } from "src/shared/payload/endpoint-types";

View File

@ -7,7 +7,7 @@ import TimelineNote from "pages/[locale]/timeline/_components/TimelineNote.astro
import TimelineSourcesButton from "pages/[locale]/timeline/_components/TimelineSourcesButton.astro";
import { getI18n } from "src/i18n/i18n";
import type { EndpointChronologyEvent } from "src/shared/payload/endpoint-types";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
export const partial = true;

View File

@ -7,7 +7,7 @@ import Credits from "components/Credits.astro";
import DownloadButton from "components/DownloadButton.astro";
import RichText from "components/RichText/RichText.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
import { getFileIcon } from "src/utils/attributes";

View File

@ -2,7 +2,7 @@
import AppLayout from "components/AppLayout/AppLayout.astro";
import Lightbox from "components/Lightbox.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
import { getFileIcon } from "src/utils/attributes";

View File

@ -2,7 +2,7 @@
import AppLayout from "components/AppLayout/AppLayout.astro";
import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
import { sizesToSrcset } from "src/utils/img";

View File

@ -14,7 +14,7 @@ import { formatInlineTitle, formatLocale, formatRichTextToString } from "src/uti
import AsideLayout from "components/AppLayout/AsideLayout.astro";
import Attributes from "components/Attributes.astro";
import type { Attribute } from "src/utils/attributes";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { sizesToSrcset } from "src/utils/img";
import RichText from "components/RichText/RichText.astro";
import SubFilesSection from "./_components/SubFilesSection.astro";

View File

@ -2,7 +2,7 @@
import AppLayout from "components/AppLayout/AppLayout.astro";
import Lightbox from "components/Lightbox.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";

View File

@ -3,7 +3,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro";
import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro";
import Credits from "components/Credits.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { fetchOr404 } from "src/utils/responses";
import ScanPreview from "./_components/ScanPreview.astro";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";

View File

@ -1,6 +1,6 @@
---
import FiveOFive from "src/pages/500.astro";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
let error;

View File

@ -6,7 +6,7 @@ import Credits from "components/Credits.astro";
import DownloadButton from "components/DownloadButton.astro";
import RichText from "components/RichText/RichText.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
import { getFileIcon } from "src/utils/attributes";

View File

@ -11,7 +11,7 @@ import AudioPreview from "components/Previews/AudioPreview.astro";
import VideoPreview from "components/Previews/VideoPreview.astro";
import AppLayout from "components/AppLayout/AppLayout.astro";
import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import RichText from "components/RichText/RichText.astro";
import FilePreview from "components/Previews/FilePreview.astro";
import { Collections } from "src/shared/payload/constants";

View File

@ -2,7 +2,7 @@
import AppLayout from "components/AppLayout/AppLayout.astro";
import Lightbox from "components/Lightbox.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
import { getFileIcon } from "src/utils/attributes";

View File

@ -3,7 +3,7 @@ import Button from "components/Button.astro";
import LibraryGrid from "./_components/LibraryGrid.astro";
import LinkCard from "./_components/LinkCard.astro";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import AppLayout from "components/AppLayout/AppLayout.astro";
import HomeTitle from "./_components/HomeTitle.astro";

View File

@ -2,7 +2,7 @@
import AppLayout from "components/AppLayout/AppLayout.astro";
import { getI18n } from "src/i18n/i18n";
import Page from "src/pages/[locale]/api/pages/partial.astro";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";

View File

@ -5,7 +5,7 @@ import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro
import Attributes from "components/Attributes.astro";
import RichText from "components/RichText/RichText.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import type { Attribute } from "src/utils/attributes";
import { formatLocale } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";

View File

@ -2,7 +2,7 @@
import AppLayout from "components/AppLayout/AppLayout.astro";
import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import { formatCurrency } from "src/utils/currencies";
import { formatLocale } from "src/utils/format";
import { prepareClassForSelectedCurrencyPostProcessing } from "src/middleware/postProcessing";

View File

@ -2,7 +2,7 @@
import TimelineEvent from "./TimelineEvent.astro";
import { getI18n } from "src/i18n/i18n";
import type { EndpointChronologyEvent } from "src/shared/payload/endpoint-types";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
interface Props {
year: number;

View File

@ -1,12 +1,12 @@
---
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { groupBy } from "src/utils/array";
import TimelineYear from "./_components/TimelineYear.astro";
import AppLayout from "components/AppLayout/AppLayout.astro";
import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro";
import Card from "components/Card.astro";
import { getI18n } from "src/i18n/i18n";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
import type { WordingKey } from "src/i18n/wordings-keys";
import { fetchOr404 } from "src/utils/responses";

View File

@ -7,7 +7,7 @@ import DownloadButton from "components/DownloadButton.astro";
import RichText from "components/RichText/RichText.astro";
import VideoPlayer from "components/VideoPlayer.astro";
import { getI18n } from "src/i18n/i18n";
import { payload } from "src/utils/payload";
import { payload } from "src/services";
import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
import { getFileIcon } from "src/utils/attributes";

View File

@ -1,5 +1,10 @@
import { AnalyticsSDK } from "src/shared/analytics/sdk";
import { MeilisearchSDK } from "src/shared/meilisearch/sdk";
import { ContextCache } from "src/cache/contextCache";
import { DataCache } from "src/cache/dataCache";
import { PageCache } from "src/cache/pageCache";
import { TokenCache } from "src/cache/tokenCache";
import { PayloadSDK } from "src/shared/payload/sdk";
export const meilisearch = new MeilisearchSDK(
import.meta.env.MEILISEARCH_URL,
@ -7,3 +12,29 @@ export const meilisearch = new MeilisearchSDK(
);
export const analytics = new AnalyticsSDK(import.meta.env.ANALYTICS_URL);
const tokenCache = new TokenCache();
export const payload = new PayloadSDK(
import.meta.env.PAYLOAD_API_URL,
import.meta.env.PAYLOAD_USER,
import.meta.env.PAYLOAD_PASSWORD
);
payload.addTokenCache(tokenCache);
const uncachedPayload = new PayloadSDK(
import.meta.env.PAYLOAD_API_URL,
import.meta.env.PAYLOAD_USER,
import.meta.env.PAYLOAD_PASSWORD
);
uncachedPayload.addTokenCache(tokenCache);
export const pageCache = new PageCache(uncachedPayload);
// Loading context cache first so that the server can still serve responses while precaching.
export const contextCache = new ContextCache(payload);
await contextCache.init();
export const dataCache = new DataCache(payload, uncachedPayload, (urls) =>
pageCache.invalidate(urls)
);
payload.addDataCache(dataCache);

View File

@ -7,7 +7,7 @@ import {
type RichTextContent,
type RichTextNode,
} from "src/shared/payload/rich-text";
import { contextCache } from "src/utils/payload";
import { contextCache } from "src/services";
export const formatLocale = (code: string): string =>
contextCache.languages.find(({ id }) => id === code)?.name ?? code;

View File

@ -1,31 +0,0 @@
import { ContextCache } from "src/cache/contextCache";
import { DataCache } from "src/cache/dataCache";
import { PageCache } from "src/cache/pageCache";
import { TokenCache } from "src/cache/tokenCache";
import { PayloadSDK } from "src/shared/payload/sdk";
const tokenCache = new TokenCache();
const payload = new PayloadSDK(
import.meta.env.PAYLOAD_API_URL,
import.meta.env.PAYLOAD_USER,
import.meta.env.PAYLOAD_PASSWORD
);
payload.addTokenCache(tokenCache);
const uncachedPayload = new PayloadSDK(
import.meta.env.PAYLOAD_API_URL,
import.meta.env.PAYLOAD_USER,
import.meta.env.PAYLOAD_PASSWORD
);
uncachedPayload.addTokenCache(tokenCache);
const pageCache = new PageCache(uncachedPayload);
// Loading context cache first so that the server can still serve responses while precaching.
const contextCache = new ContextCache(payload);
await contextCache.init();
const dataCache = new DataCache(payload, uncachedPayload, (urls) => pageCache.invalidate(urls));
payload.addDataCache(dataCache);
export { payload, contextCache, pageCache, dataCache };