Skip to content

Commit

Permalink
Add cache option
Browse files Browse the repository at this point in the history
Close #8
  • Loading branch information
erxclau committed Oct 30, 2023
1 parent 831a2fc commit a39290e
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ function trim(caption: string): string {

function parseCaption(
caption: HTMLElement | null,
{ fullCaption }: { fullCaption: boolean } = { fullCaption: false }
{ getFullCaption }: { getFullCaption: boolean } = { getFullCaption: false }
): string {
if (!caption) {
return "";
}

if (fullCaption) {
if (getFullCaption) {
return caption.innerHTML;
}

Expand All @@ -77,7 +77,7 @@ function parseCaption(

function parseImage(
html: string,
{ fullCaption }: { fullCaption: boolean } = { fullCaption: false }
{ getFullCaption }: { getFullCaption?: boolean } = { getFullCaption: false }
): Image {
const element = parse(html).querySelector("img")!;
return {
Expand All @@ -86,7 +86,7 @@ function parseImage(
parse(element.getAttribute("data-image-caption") ?? "").querySelector(
"p"
),
{ fullCaption }
{ getFullCaption }
),
width: element.getAttribute("width"),
height: element.getAttribute("height"),
Expand All @@ -113,13 +113,17 @@ function joinListOfStrings(strings: string[]) {

export async function fetchImageFromSlug(
slug: string,
{ fullCaption }: { fullCaption: boolean } = { fullCaption: false }
{ getFullCaption, useCache }: { getFullCaption?: boolean; useCache?: boolean; } = { getFullCaption: false, useCache: true }
) {
const url = new URL("https://michigandaily.com/wp-json/wp/v2/media");
url.searchParams.set("media_type", "image");
url.searchParams.set("slug", slug);
url.searchParams.set("_fields", "description");

if (!useCache) {
url.searchParams.set("time", new Date().toISOString());
}

const response = await fetch(url);
if (!response.ok) {
return null;
Expand All @@ -139,16 +143,22 @@ export async function fetchImageFromSlug(
return null;
}

return parseImage(image.description.rendered, { fullCaption });
return parseImage(image.description.rendered, { getFullCaption });
}

export async function fetchPostFromSlug(
slug: string,
{
useTestSite,
useCache,
getFullImageCaption,
}: { useTestSite: boolean; getFullImageCaption: boolean } = {
}: {
useTestSite?: boolean;
useCache?: boolean;
getFullImageCaption?: boolean;
} = {
useTestSite: false,
useCache: true,
getFullImageCaption: false,
}
) {
Expand All @@ -159,6 +169,9 @@ export async function fetchPostFromSlug(
);
url.searchParams.set("slug", slug);
url.searchParams.set("_fields", "coauthors,link,title,_links");
if (!useCache) {
url.searchParams.set("time", new Date().toISOString());
}

const response = await fetch(url);
if (!response.ok) {
Expand All @@ -173,10 +186,17 @@ export async function fetchPostFromSlug(
const story = data.at(0);
const [feature] = story._links["wp:featuredmedia"];

const imageRequest = await fetch(`${feature.href}?_fields=description`);
const params = new URLSearchParams();
params.set("_fields", "description");

if (!useCache) {
params.set("time", new Date().toISOString());
}

const imageRequest = await fetch(`${feature.href}?${params.toString()}`);
const imageData = await imageRequest.json();
const image = parseImage(imageData.description.rendered, {
fullCaption: getFullImageCaption,
getFullCaption: getFullImageCaption,
});

return {
Expand Down

0 comments on commit a39290e

Please sign in to comment.