Compare commits

...

2 Commits

Author SHA1 Message Date
DrMint c9c8160bfa Added if modified since header 2024-06-15 09:41:24 +02:00
DrMint 6c99de5bfb Added basic caching 2024-06-13 13:22:55 +02:00
20 changed files with 323 additions and 265 deletions

20
package-lock.json generated
View File

@ -13,7 +13,6 @@
"accept-language": "^3.0.18",
"astro": "4.10.2",
"astro-icon": "^1.1.0",
"node-cache": "^5.1.2",
"tippy.js": "^6.3.7",
"ua-parser-js": "^1.0.38"
},
@ -4104,14 +4103,6 @@
"url": "https://github.com/chalk/wrap-ansi?sponsor=1"
}
},
"node_modules/clone": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
"engines": {
"node": ">=0.8"
}
},
"node_modules/clsx": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz",
@ -6545,17 +6536,6 @@
"url": "https://opencollective.com/unified"
}
},
"node_modules/node-cache": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
"dependencies": {
"clone": "2.x"
},
"engines": {
"node": ">= 8.0.0"
}
},
"node_modules/node-releases": {
"version": "2.0.14",
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "v3.accords-library.com",
"version": "3.0.0-beta.3",
"version": "3.0.0-beta.4",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
@ -24,7 +24,6 @@
"accept-language": "^3.0.18",
"astro": "4.10.2",
"astro-icon": "^1.1.0",
"node-cache": "^5.1.2",
"tippy.js": "^6.3.7",
"ua-parser-js": "^1.0.38"
},

View File

@ -151,6 +151,15 @@ const addContentLanguageResponseHeader = defineMiddleware(async ({ url }, next)
return response;
});
const addCacheControlHeaders = defineMiddleware(async (_, next) => {
const response = await next();
if (response.ok) {
response.headers.set("Cache-Control", "max-age=60, stale-while-revalidate=60");
response.headers.set("Vary", "Cookie")
}
return response;
});
const provideLocalsToRequest = defineMiddleware(async ({ url, locals, cookies }, next) => {
locals.currentLocale = getCurrentLocale(url.pathname) ?? "en";
locals.currentCurrency = getCookieCurrency(cookies) ?? "USD";
@ -170,6 +179,7 @@ export const onRequest = sequence(
handleActionsSearchParams,
refreshCookiesMaxAge,
localeNegotiator,
addCacheControlHeaders,
provideLocalsToRequest,
analytics
);

View File

@ -25,7 +25,7 @@ const reqUrl = new URL(Astro.request.url);
const lang = Astro.props.lang ?? reqUrl.searchParams.get("lang")!;
const slug = Astro.props.slug ?? reqUrl.searchParams.get("slug")!;
const { translations, thumbnail, createdAt, updatedAt, updatedBy, attributes } =
Astro.props.page ?? (await payload.getPage(slug));
Astro.props.page ?? (await payload.getPage(slug)).data;
const { getLocalizedUrl, t, formatDate } = await getI18n(Astro.locals.currentLocale);
const { getLocalizedMatch } = await getI18n(lang);

View File

@ -12,7 +12,7 @@ import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
const id = Astro.params.id!;
const audio = await fetchOr404(() => payload.getAudioByID(id));
const audio = await fetchOr404(Astro, () => payload.getAudioByID(id));
if (audio instanceof Response) {
return audio;
}

View File

@ -12,7 +12,7 @@ const { getLocalizedUrl, getLocalizedMatch, t, formatDate } = await getI18n(
Astro.locals.currentLocale
);
const galleryImage = await fetchOr404(() => payload.getCollectibleGalleryImage(slug, index));
const galleryImage = await fetchOr404(Astro, () => payload.getCollectibleGalleryImage(slug, index));
if (galleryImage instanceof Response) {
return galleryImage;
}

View File

@ -11,7 +11,7 @@ import RichText from "components/RichText/RichText.astro";
const slug = Astro.params.slug!;
const { getLocalizedMatch, getLocalizedUrl, t } = await getI18n(Astro.locals.currentLocale);
const gallery = await fetchOr404(() => payload.getCollectibleGallery(slug));
const gallery = await fetchOr404(Astro, () => payload.getCollectibleGallery(slug));
if (gallery instanceof Response) {
return gallery;
}

View File

@ -25,7 +25,7 @@ const { getLocalizedMatch, getLocalizedUrl, t, formatDate, formatPrice } = await
Astro.locals.currentLocale
);
const collectible = await fetchOr404(() => payload.getCollectible(slug));
const collectible = await fetchOr404(Astro, () => payload.getCollectible(slug));
if (collectible instanceof Response) {
return collectible;
}

View File

@ -12,7 +12,7 @@ const { formatScanIndexShort, getLocalizedMatch, getLocalizedUrl } = await getI1
Astro.locals.currentLocale
);
const scanPage = await fetchOr404(() => payload.getCollectibleScanPage(slug, index));
const scanPage = await fetchOr404(Astro, () => payload.getCollectibleScanPage(slug, index));
if (scanPage instanceof Response) {
return scanPage;
}

View File

@ -12,7 +12,7 @@ import RichText from "components/RichText/RichText.astro";
const slug = Astro.params.slug!;
const { getLocalizedMatch, t } = await getI18n(Astro.locals.currentLocale);
const scans = await fetchOr404(() => payload.getCollectibleScans(slug));
const scans = await fetchOr404(Astro, () => payload.getCollectibleScans(slug));
if (scans instanceof Response) {
return scans;
}

View File

@ -17,7 +17,7 @@ import RichText from "components/RichText/RichText.astro";
const slug = Astro.params.slug!;
const folder = await fetchOr404(() => payload.getFolder(slug));
const folder = await fetchOr404(Astro, () => payload.getFolder(slug));
if (folder instanceof Response) {
return folder;
}

View File

@ -7,7 +7,7 @@ import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
const id = Astro.params.id!;
const image = await fetchOr404(() => payload.getImageByID(id));
const image = await fetchOr404(Astro, () => payload.getImageByID(id));
if (image instanceof Response) {
return image;
}

View File

@ -8,7 +8,7 @@ import { fetchOr404 } from "src/utils/responses";
const slug = Astro.params.slug!;
const page = await fetchOr404(() => payload.getPage(slug));
const page = await fetchOr404(Astro, () => payload.getPage(slug));
if (page instanceof Response) {
return page;
}

View File

@ -12,7 +12,7 @@ import { fetchOr404 } from "src/utils/responses";
import { sizesToSrcset } from "src/utils/img";
const id = Astro.params.id!;
const recorder = await fetchOr404(() => payload.getRecorderByID(id));
const recorder = await fetchOr404(Astro, () => payload.getRecorderByID(id));
if (recorder instanceof Response) {
return recorder;
}

View File

@ -8,8 +8,13 @@ import Card from "components/Card.astro";
import { getI18n } from "src/i18n/i18n";
import { cache } from "src/utils/payload";
import type { WordingKey } from "src/i18n/wordings-keys";
import { fetchOr404 } from "src/utils/responses";
const events = await fetchOr404(Astro, payload.getChronologyEvents);
if (events instanceof Response) {
return events;
}
const events = await payload.getChronologyEvents();
const groupedEvents = groupBy(events, (event) => event.date.year);
const { getLocalizedUrl, t, formatTimelineDate } = await getI18n(Astro.locals.currentLocale);
---

View File

@ -12,7 +12,7 @@ import { formatInlineTitle, formatRichTextToString } from "src/utils/format";
import { fetchOr404 } from "src/utils/responses";
const id = Astro.params.id!;
const video = await fetchOr404(() => payload.getVideoByID(id));
const video = await fetchOr404(Astro, () => payload.getVideoByID(id));
if (video instanceof Response) {
return video;
}

View File

@ -1,177 +1,177 @@
{
"disclaimer": "Usage subject to terms: https://openexchangerates.org/terms",
"license": "https://openexchangerates.org/license",
"timestamp": 1717740000,
"timestamp": 1718308800,
"base": "USD",
"rates": {
"AED": 3.673,
"AFN": 70.453638,
"ALL": 91.912021,
"AMD": 387.678915,
"ANG": 1.79986,
"AOA": 854.407667,
"ARS": 898.5413,
"AUD": 1.498617,
"AWG": 1.8,
"AED": 3.67305,
"AFN": 70.500002,
"ALL": 92.875,
"AMD": 388.38664,
"ANG": 1.806289,
"AOA": 855,
"ARS": 902.25,
"AUD": 1.506865,
"AWG": 1.8025,
"AZN": 1.7,
"BAM": 1.795869,
"BAM": 1.815928,
"BBD": 2,
"BDT": 117.294372,
"BGN": 1.79491,
"BHD": 0.376872,
"BIF": 2869.308172,
"BDT": 117.778138,
"BGN": 1.821445,
"BHD": 0.376882,
"BIF": 2879,
"BMD": 1,
"BND": 1.345362,
"BOB": 6.901365,
"BRL": 5.258701,
"BND": 1.352522,
"BOB": 6.925559,
"BRL": 5.3671,
"BSD": 1,
"BTC": 0.000014026029,
"BTN": 83.367902,
"BWP": 13.755584,
"BYN": 3.268278,
"BZD": 2.013085,
"CAD": 1.366955,
"CDF": 2822.733026,
"CHF": 0.89004,
"CLF": 0.032927,
"CLP": 908.55,
"CNH": 7.255147,
"CNY": 7.2447,
"COP": 3927.880338,
"CRC": 527.874933,
"BTC": 0.000015006774,
"BTN": 83.726029,
"BWP": 13.635836,
"BYN": 3.279777,
"BZD": 2.020138,
"CAD": 1.373867,
"CDF": 2845,
"CHF": 0.893783,
"CLF": 0.033281,
"CLP": 918.33,
"CNH": 7.270265,
"CNY": 7.2526,
"COP": 4064.333563,
"CRC": 528.390438,
"CUC": 1,
"CUP": 25.75,
"CVE": 101.249236,
"CZK": 22.552099,
"DJF": 177.811501,
"DKK": 6.849959,
"DOP": 59.242392,
"DZD": 134.471532,
"EGP": 47.5129,
"CVE": 103.1,
"CZK": 23.0107,
"DJF": 177,
"DKK": 6.9455,
"DOP": 59.4,
"DZD": 134.851507,
"EGP": 47.708,
"ERN": 15,
"ETB": 57.42215,
"EUR": 0.918217,
"FJD": 2.25895,
"FKP": 0.782189,
"GBP": 0.782189,
"GEL": 2.785,
"GGP": 0.782189,
"GHS": 14.880927,
"GIP": 0.782189,
"ETB": 57.5,
"EUR": 0.931055,
"FJD": 2.2337,
"FKP": 0.783387,
"GBP": 0.783387,
"GEL": 2.86,
"GGP": 0.783387,
"GHS": 15.04,
"GIP": 0.783387,
"GMD": 67.775,
"GNF": 8600.013522,
"GTQ": 7.76001,
"GYD": 209.061081,
"HKD": 7.809645,
"HNL": 24.673005,
"HRK": 6.918151,
"HTG": 132.552876,
"HUF": 358.085123,
"IDR": 16216.760923,
"ILS": 3.720825,
"IMP": 0.782189,
"INR": 83.458757,
"IQD": 1308.290714,
"GNF": 8605,
"GTQ": 7.785371,
"GYD": 209.587622,
"HKD": 7.81073,
"HNL": 24.799999,
"HRK": 7.01418,
"HTG": 133.038665,
"HUF": 369.590851,
"IDR": 16286.77334,
"ILS": 3.715655,
"IMP": 0.783387,
"INR": 83.540593,
"IQD": 1310,
"IRR": 42100,
"ISK": 137.46,
"JEP": 0.782189,
"JMD": 155.295453,
"JOD": 0.7088,
"JPY": 155.5174,
"KES": 130,
"KGS": 87.3013,
"KHR": 4101.618815,
"KMF": 452.450119,
"ISK": 139.01,
"JEP": 0.783387,
"JMD": 156.002299,
"JOD": 0.7089,
"JPY": 157.063125,
"KES": 128.5,
"KGS": 87.1182,
"KHR": 4119,
"KMF": 455.500241,
"KPW": 900,
"KRW": 1367.937913,
"KWD": 0.306384,
"KYD": 0.832281,
"KZT": 446.221602,
"LAK": 21498.895118,
"LBP": 89418.618549,
"LKR": 302.314319,
"LRD": 193.950039,
"LSL": 18.953322,
"LYD": 4.829787,
"MAD": 9.877784,
"MDL": 17.619678,
"MGA": 4467.052458,
"MKD": 56.57657,
"MMK": 2101.212378,
"KRW": 1376.539507,
"KWD": 0.306419,
"KYD": 0.835233,
"KZT": 451.97877,
"LAK": 21780,
"LBP": 89550,
"LKR": 304.586904,
"LRD": 193.999927,
"LSL": 18.4,
"LYD": 4.85,
"MAD": 9.9465,
"MDL": 17.699209,
"MGA": 4470,
"MKD": 57.263542,
"MMK": 2201.379322,
"MNT": 3450,
"MOP": 8.033479,
"MRU": 39.126423,
"MUR": 46.32,
"MVR": 15.4,
"MWK": 1731.539932,
"MXN": 17.987572,
"MYR": 4.6955,
"MZN": 63.92499,
"NAD": 18.953497,
"NGN": 1485.31,
"NIO": 36.764809,
"NOK": 10.558218,
"NPR": 133.383523,
"NZD": 1.613954,
"OMR": 0.384963,
"MOP": 8.063005,
"MRU": 39.45,
"MUR": 46.637189,
"MVR": 15.41,
"MWK": 1733.5,
"MXN": 18.468878,
"MYR": 4.71875,
"MZN": 63.850001,
"NAD": 18.4,
"NGN": 1505,
"NIO": 36.8,
"NOK": 10.64482,
"NPR": 133.959461,
"NZD": 1.62071,
"OMR": 0.384946,
"PAB": 1,
"PEN": 3.738103,
"PGK": 3.887667,
"PHP": 58.519502,
"PKR": 278.529362,
"PLN": 3.932408,
"PYG": 7514.698541,
"QAR": 3.642917,
"RON": 4.569,
"RSD": 107.495,
"RUB": 89.173363,
"RWF": 1298.427641,
"SAR": 3.750551,
"SBD": 8.482503,
"SCR": 13.845723,
"PEN": 3.771,
"PGK": 3.904201,
"PHP": 58.6695,
"PKR": 278.575,
"PLN": 4.046857,
"PYG": 7533.486837,
"QAR": 3.641,
"RON": 4.6339,
"RSD": 108.997,
"RUB": 88.381576,
"RWF": 1320,
"SAR": 3.751693,
"SBD": 8.457605,
"SCR": 13.855402,
"SDG": 586,
"SEK": 10.38479,
"SGD": 1.34481,
"SHP": 0.782189,
"SEK": 10.472668,
"SGD": 1.3506,
"SHP": 0.783387,
"SLL": 20969.5,
"SOS": 570.753969,
"SRD": 31.7645,
"SOS": 571,
"SRD": 31.627,
"SSP": 130.26,
"STD": 22281.8,
"STN": 22.496155,
"SVC": 8.738769,
"STN": 22.9,
"SVC": 8.769202,
"SYP": 2512.53,
"SZL": 18.948934,
"THB": 36.351667,
"TJS": 10.696062,
"TMT": 3.51,
"TND": 3.10175,
"TOP": 2.352606,
"TRY": 32.310647,
"TTD": 6.757172,
"TWD": 32.282,
"TZS": 2615,
"UAH": 40.094667,
"UGX": 3788.628608,
"SZL": 18.4,
"THB": 36.78,
"TJS": 10.798706,
"TMT": 3.5,
"TND": 3.113,
"TOP": 2.354259,
"TRY": 32.291698,
"TTD": 6.811102,
"TWD": 32.359801,
"TZS": 2621.947791,
"UAH": 40.510836,
"UGX": 3738.089071,
"USD": 1,
"UYU": 38.896466,
"UZS": 12659.755435,
"VES": 36.442787,
"VND": 25422.651993,
"UYU": 39.198992,
"UZS": 12635,
"VES": 36.383822,
"VND": 25442.5,
"VUV": 118.722,
"WST": 2.8,
"XAF": 602.310759,
"XAG": 0.03205488,
"XAU": 0.0004209,
"XAF": 610.732275,
"XAG": 0.03455545,
"XAU": 0.00043413,
"XCD": 2.70255,
"XDR": 0.75467,
"XOF": 602.310759,
"XPD": 0.00108737,
"XPF": 109.572414,
"XPT": 0.00099467,
"YER": 250.425029,
"ZAR": 18.932716,
"ZMW": 26.240618,
"XDR": 0.760342,
"XOF": 610.732275,
"XPD": 0.00113637,
"XPF": 111.104457,
"XPT": 0.00105291,
"YER": 250.350066,
"ZAR": 18.430643,
"ZMW": 26.433191,
"ZWL": 322
}
}

View File

@ -1953,17 +1953,30 @@ type GetPayloadSDKParams = {
apiURL: string;
email: string;
password: string;
cache: Cache;
tokenCache?: {
set: (token: string, expirationTimestamp: number) => void;
get: () => string | undefined;
};
responseCache?: {
set: (url: string, response: any) => void;
get: (url: string) => any | undefined;
};
};
type Cache = {
set: (token: string, expirationTimestamp: number) => void;
get: () => string | undefined;
export type PayloadResponse<T> = {
data: T;
timestamp: Date;
};
const logResponse = (res: Response) => console.log(res.status, res.statusText, res.url);
export const getPayloadSDK = ({ apiURL, email, password, cache }: GetPayloadSDKParams) => {
export const getPayloadSDK = ({
apiURL,
email,
password,
tokenCache,
responseCache,
}: GetPayloadSDKParams) => {
const refreshToken = async () => {
const loginUrl = payloadApiUrl(Collections.Recorders, "login");
const loginResult = await fetch(loginUrl, {
@ -1981,72 +1994,82 @@ export const getPayloadSDK = ({ apiURL, email, password, cache }: GetPayloadSDKP
token: string;
exp: number;
};
cache.set(token, exp);
tokenCache?.set(token, exp);
return token;
};
const payloadApiUrl = (collection: Collections, endpoint?: string, isGlobal?: boolean): string =>
`${apiURL}/${isGlobal === undefined ? "" : "globals/"}${collection}${endpoint === undefined ? "" : `/${endpoint}`}`;
const request = async (url: string): Promise<Response> => {
const request = async (url: string): Promise<PayloadResponse<any>> => {
const cachedResponse = responseCache?.get(url);
if (cachedResponse) {
return cachedResponse;
}
const result = await fetch(url, {
headers: {
Authorization: `JWT ${cache.get() ?? (await refreshToken())}`,
Authorization: `JWT ${tokenCache?.get() ?? (await refreshToken())}`,
},
});
logResponse(result);
if (result.status !== 200) {
if (!result.ok) {
throw new Error("Unhandled fetch error");
}
return result;
const data = { data: await result.json(), timestamp: new Date() };
responseCache?.set(url, data);
return data;
};
return {
getConfig: async (): Promise<EndpointWebsiteConfig> =>
await (await request(payloadApiUrl(Collections.WebsiteConfig, `config`, true))).json(),
getFolder: async (slug: string): Promise<EndpointFolder> =>
await (await request(payloadApiUrl(Collections.Folders, `slug/${slug}`))).json(),
getLanguages: async (): Promise<Language[]> =>
await (await request(payloadApiUrl(Collections.Languages, `all`))).json(),
getCurrencies: async (): Promise<Currency[]> =>
await (await request(payloadApiUrl(Collections.Currencies, `all`))).json(),
getWordings: async (): Promise<EndpointWording[]> =>
await (await request(payloadApiUrl(Collections.Wordings, `all`))).json(),
getPage: async (slug: string): Promise<EndpointPage> =>
await (await request(payloadApiUrl(Collections.Pages, `slug/${slug}`))).json(),
getCollectible: async (slug: string): Promise<EndpointCollectible> =>
await (await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}`))).json(),
getCollectibleScans: async (slug: string): Promise<EndpointCollectibleScans> =>
await (await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/scans`))).json(),
getConfig: async (): Promise<PayloadResponse<EndpointWebsiteConfig>> =>
await request(payloadApiUrl(Collections.WebsiteConfig, `config`, true)),
getFolder: async (slug: string): Promise<PayloadResponse<EndpointFolder>> =>
await request(payloadApiUrl(Collections.Folders, `slug/${slug}`)),
getLanguages: async (): Promise<PayloadResponse<Language[]>> =>
await request(payloadApiUrl(Collections.Languages, `all`)),
getCurrencies: async (): Promise<PayloadResponse<Currency[]>> =>
await request(payloadApiUrl(Collections.Currencies, `all`)),
getWordings: async (): Promise<PayloadResponse<EndpointWording[]>> =>
await request(payloadApiUrl(Collections.Wordings, `all`)),
getPage: async (slug: string): Promise<PayloadResponse<EndpointPage>> =>
await request(payloadApiUrl(Collections.Pages, `slug/${slug}`)),
getCollectible: async (slug: string): Promise<PayloadResponse<EndpointCollectible>> =>
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}`)),
getCollectibleScans: async (
slug: string
): Promise<PayloadResponse<EndpointCollectibleScans>> =>
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/scans`)),
getCollectibleScanPage: async (
slug: string,
index: string
): Promise<EndpointCollectibleScanPage> =>
await (
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/scans/${index}`))
).json(),
getCollectibleGallery: async (slug: string): Promise<EndpointCollectibleGallery> =>
await (await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/gallery`))).json(),
): Promise<PayloadResponse<EndpointCollectibleScanPage>> =>
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/scans/${index}`)),
getCollectibleGallery: async (
slug: string
): Promise<PayloadResponse<EndpointCollectibleGallery>> =>
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/gallery`)),
getCollectibleGalleryImage: async (
slug: string,
index: string
): Promise<EndpointCollectibleGalleryImage> =>
await (
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/gallery/${index}`))
).json(),
getChronologyEvents: async (): Promise<EndpointChronologyEvent[]> =>
await (await request(payloadApiUrl(Collections.ChronologyEvents, `all`))).json(),
getChronologyEventByID: async (id: string): Promise<EndpointChronologyEvent> =>
await (await request(payloadApiUrl(Collections.ChronologyEvents, `id/${id}`))).json(),
getImageByID: async (id: string): Promise<EndpointImage> =>
await (await request(payloadApiUrl(Collections.Images, `id/${id}`))).json(),
getAudioByID: async (id: string): Promise<EndpointAudio> =>
await (await request(payloadApiUrl(Collections.Audios, `id/${id}`))).json(),
getVideoByID: async (id: string): Promise<EndpointVideo> =>
await (await request(payloadApiUrl(Collections.Videos, `id/${id}`))).json(),
getRecorderByID: async (id: string): Promise<EndpointRecorder> =>
await (await request(payloadApiUrl(Collections.Recorders, `id/${id}`))).json(),
): Promise<PayloadResponse<EndpointCollectibleGalleryImage>> =>
await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/gallery/${index}`)),
getChronologyEvents: async (): Promise<PayloadResponse<EndpointChronologyEvent[]>> =>
await request(payloadApiUrl(Collections.ChronologyEvents, `all`)),
getChronologyEventByID: async (
id: string
): Promise<PayloadResponse<EndpointChronologyEvent>> =>
await request(payloadApiUrl(Collections.ChronologyEvents, `id/${id}`)),
getImageByID: async (id: string): Promise<PayloadResponse<EndpointImage>> =>
await request(payloadApiUrl(Collections.Images, `id/${id}`)),
getAudioByID: async (id: string): Promise<PayloadResponse<EndpointAudio>> =>
await request(payloadApiUrl(Collections.Audios, `id/${id}`)),
getVideoByID: async (id: string): Promise<PayloadResponse<EndpointVideo>> =>
await request(payloadApiUrl(Collections.Videos, `id/${id}`)),
getRecorderByID: async (id: string): Promise<PayloadResponse<EndpointRecorder>> =>
await request(payloadApiUrl(Collections.Recorders, `id/${id}`)),
};
};

View File

@ -4,39 +4,47 @@ import {
type EndpointWebsiteConfig,
} from "src/shared/payload/payload-sdk";
import { getPayloadSDK } from "src/shared/payload/payload-sdk";
import NodeCache from "node-cache";
const REFRESH_FREQUENCY_IN_SEC = 60;
const nodeCache = new NodeCache({
checkperiod: REFRESH_FREQUENCY_IN_SEC,
deleteOnExpire: true,
forceString: true,
maxKeys: 1,
});
const TOKEN_KEY = "token";
let token: string | undefined = undefined;
let expiration: number | undefined = undefined;
const responseCache = new Map<string, any>();
export const payload = getPayloadSDK({
apiURL: import.meta.env.PAYLOAD_API_URL,
email: import.meta.env.PAYLOAD_USER,
password: import.meta.env.PAYLOAD_PASSWORD,
cache: {
tokenCache: {
get: () => {
const cachedToken = nodeCache.get<string>(TOKEN_KEY);
if (cachedToken !== undefined) {
const cachedTokenTtl = nodeCache.getTtl(TOKEN_KEY) as number;
const diffInMinutes = Math.floor((cachedTokenTtl - Date.now()) / 1000 / 60);
console.log("Retrieved token from cache. TTL is", diffInMinutes, "minutes.");
return cachedToken;
if (!token) return undefined;
if (!expiration || expiration < Date.now()) {
console.log("[PayloadSDK] No token to be retrieved or the token expired");
return undefined;
}
console.log("No token to be retrieved or the token expired");
return undefined;
const diffInMinutes = Math.floor((expiration - Date.now()) / 1000 / 60);
console.log("[PayloadSDK] Retrieved token from cache. TTL is", diffInMinutes, "minutes.");
return token;
},
set: (token, exp) => {
const now = Math.floor(Date.now() / 1000);
const ttl = Math.floor(exp - now - REFRESH_FREQUENCY_IN_SEC * 2);
const ttlInMinutes = Math.floor(ttl / 60);
console.log("Token was refreshed. TTL is", ttlInMinutes, "minutes.");
nodeCache.set(TOKEN_KEY, token, ttl);
set: (newToken, newExpiration) => {
token = newToken;
expiration = newExpiration * 1000;
const diffInMinutes = Math.floor((expiration - Date.now()) / 1000 / 60);
console.log("[PayloadSDK] New token set. TTL is", diffInMinutes, "minutes.");
},
},
responseCache: {
get: (url) => {
const cachedResponse = responseCache.get(url);
if (!cachedResponse) {
console.log("[ResponseCaching] No cached response found for", url);
return undefined;
}
console.log("[ResponseCaching] Retrieved cache response for", url);
return cachedResponse;
},
set: (url, response) => {
console.log("[ResponseCaching] Caching response for", url);
responseCache.set(url, response);
},
},
});
@ -49,26 +57,26 @@ type Cache = {
};
const fetchNewData = async (): Promise<Cache> => ({
locales: await payload.getLanguages(),
currencies: (await payload.getCurrencies()).map(({ id }) => id),
wordings: await payload.getWordings(),
config: await payload.getConfig(),
locales: (await payload.getLanguages()).data,
currencies: (await payload.getCurrencies()).data.map(({ id }) => id),
wordings: (await payload.getWordings()).data,
config: (await payload.getConfig()).data,
});
export let cache = await fetchNewData();
export const refreshWordings = async () => {
cache.wordings = await payload.getWordings();
cache.wordings = (await payload.getWordings()).data;
};
export const refreshCurrencies = async () => {
cache.currencies = (await payload.getCurrencies()).map(({ id }) => id);
cache.currencies = (await payload.getCurrencies()).data.map(({ id }) => id);
};
export const refreshLocales = async () => {
cache.locales = await payload.getLanguages();
cache.locales = (await payload.getLanguages()).data;
};
export const refreshWebsiteConfig = async () => {
cache.config = await payload.getConfig();
cache.config = (await payload.getConfig()).data;
};

View File

@ -1,6 +1,39 @@
export const fetchOr404 = async <T>(promise: () => Promise<T>): Promise<T | Response> => {
import type { PayloadResponse } from "src/shared/payload/payload-sdk";
export const fetchOr404 = async <T>(
{
request,
response,
}: {
request: Request;
response: {
headers: Headers;
};
},
promise: () => Promise<PayloadResponse<T>>
): Promise<T | Response> => {
try {
return await promise();
const { data, timestamp } = await promise();
const lastModified = timestamp.toUTCString();
if (request.headers.get("If-Modified-Since") === lastModified) {
return new Response(null, {
status: 304,
statusText: "Not Modified",
});
}
// const userEtag = Astro.request.headers.get("If-None-Match");
// if (userEtag === collectible.etag) {
// return new Response(null, {
// status: 304,
// statusText: "Not Modified",
// });
// }
response.headers.set("Last-Modified", lastModified);
return data;
} catch {
return new Response(null, {
status: 404,