Refactor json/xml code
This commit is contained in:
parent
4d904517a5
commit
bdfc122beb
@ -1,11 +1,7 @@
|
|||||||
import { getPhotosCached } from '@/photo/cache';
|
import { getPhotosCached } from '@/photo/cache';
|
||||||
import {
|
import { SITE_FEEDS_ENABLED } from '@/app/config';
|
||||||
BASE_URL,
|
|
||||||
SITE_FEEDS_ENABLED,
|
|
||||||
META_TITLE,
|
|
||||||
} from '@/app/config';
|
|
||||||
import { FEED_PHOTO_REQUEST_LIMIT } from '@/feed';
|
import { FEED_PHOTO_REQUEST_LIMIT } from '@/feed';
|
||||||
import { formatPhotoForFeedJson } from '@/feed/json';
|
import { formatFeedJson } from '@/feed/json';
|
||||||
|
|
||||||
// Cache for 24 hours
|
// Cache for 24 hours
|
||||||
export const revalidate = 86_400;
|
export const revalidate = 86_400;
|
||||||
@ -16,13 +12,7 @@ export async function GET() {
|
|||||||
limit: FEED_PHOTO_REQUEST_LIMIT,
|
limit: FEED_PHOTO_REQUEST_LIMIT,
|
||||||
sortBy: 'createdAt',
|
sortBy: 'createdAt',
|
||||||
});
|
});
|
||||||
return Response.json({
|
return Response.json(formatFeedJson(photos));
|
||||||
meta: {
|
|
||||||
title: META_TITLE,
|
|
||||||
url: BASE_URL,
|
|
||||||
},
|
|
||||||
photos: photos.map(formatPhotoForFeedJson),
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
return new Response('Feed disabled', { status: 404 });
|
return new Response('Feed disabled', { status: 404 });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,7 @@
|
|||||||
import { getPhotosCached } from '@/photo/cache';
|
import { getPhotosCached } from '@/photo/cache';
|
||||||
import {
|
import { SITE_FEEDS_ENABLED } from '@/app/config';
|
||||||
BASE_URL,
|
|
||||||
META_DESCRIPTION,
|
|
||||||
META_TITLE,
|
|
||||||
SITE_FEEDS_ENABLED,
|
|
||||||
} from '@/app/config';
|
|
||||||
import { FEED_PHOTO_REQUEST_LIMIT } from '@/feed';
|
import { FEED_PHOTO_REQUEST_LIMIT } from '@/feed';
|
||||||
import { createRssItems } from '@/feed/rss';
|
import { formatFeedRss } from '@/feed/rss';
|
||||||
import { ABSOLUTE_PATH_FOR_RSS_XML } from '@/app/paths';
|
|
||||||
|
|
||||||
// Cache for 24 hours
|
// Cache for 24 hours
|
||||||
export const revalidate = 86_400;
|
export const revalidate = 86_400;
|
||||||
@ -19,27 +13,8 @@ export async function GET() {
|
|||||||
sortBy: 'createdAt',
|
sortBy: 'createdAt',
|
||||||
});
|
});
|
||||||
|
|
||||||
const items = createRssItems(photos);
|
return new Response(
|
||||||
|
formatFeedRss(photos),
|
||||||
return new Response(`<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<rss version="2.0"
|
|
||||||
xmlns:content="http://purl.org/rss/1.0/modules/content/"
|
|
||||||
xmlns:atom="http://www.w3.org/2005/Atom"
|
|
||||||
xmlns:media="http://search.yahoo.com/mrss/"
|
|
||||||
>
|
|
||||||
<channel>
|
|
||||||
<title>${META_TITLE}</title>
|
|
||||||
<atom:link
|
|
||||||
href="${ABSOLUTE_PATH_FOR_RSS_XML}"
|
|
||||||
rel="self"
|
|
||||||
type="application/rss+xml"
|
|
||||||
/>
|
|
||||||
<link>${BASE_URL}</link>
|
|
||||||
<description>${META_DESCRIPTION}</description>
|
|
||||||
${items.join('\n')}
|
|
||||||
</channel>
|
|
||||||
</rss>
|
|
||||||
`,
|
|
||||||
{ headers: { 'Content-Type': 'text/xml' } },
|
{ headers: { 'Content-Type': 'text/xml' } },
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -9,6 +9,8 @@ import {
|
|||||||
} from '.';
|
} from '.';
|
||||||
import { formatDateFromPostgresString } from '@/utility/date';
|
import { formatDateFromPostgresString } from '@/utility/date';
|
||||||
import { Photo } from '@/photo';
|
import { Photo } from '@/photo';
|
||||||
|
import { BASE_URL } from '@/app/config';
|
||||||
|
import { META_TITLE } from '@/app/config';
|
||||||
|
|
||||||
interface FeedPhotoJson {
|
interface FeedPhotoJson {
|
||||||
id: string
|
id: string
|
||||||
@ -21,7 +23,7 @@ interface FeedPhotoJson {
|
|||||||
src: Record<'small' | 'medium' | 'large', FeedMedia>
|
src: Record<'small' | 'medium' | 'large', FeedMedia>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const formatPhotoForFeedJson = (photo: Photo): FeedPhotoJson => ({
|
const formatPhotoForFeedJson = (photo: Photo): FeedPhotoJson => ({
|
||||||
...getCoreFeedFields(photo),
|
...getCoreFeedFields(photo),
|
||||||
url: absolutePathForPhoto({ photo }),
|
url: absolutePathForPhoto({ photo }),
|
||||||
...photo.make && { make: photo.make },
|
...photo.make && { make: photo.make },
|
||||||
@ -34,3 +36,11 @@ export const formatPhotoForFeedJson = (photo: Photo): FeedPhotoJson => ({
|
|||||||
large: generateFeedMedia(photo, FEED_PHOTO_WIDTH_LARGE),
|
large: generateFeedMedia(photo, FEED_PHOTO_WIDTH_LARGE),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const formatFeedJson = (photos: Photo[]) => ({
|
||||||
|
meta: {
|
||||||
|
title: META_TITLE,
|
||||||
|
url: BASE_URL,
|
||||||
|
},
|
||||||
|
photos: photos.map(formatPhotoForFeedJson),
|
||||||
|
});
|
||||||
|
|||||||
@ -6,9 +6,10 @@ import {
|
|||||||
generateFeedMedia,
|
generateFeedMedia,
|
||||||
getCoreFeedFields,
|
getCoreFeedFields,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { absolutePathForPhoto } from '@/app/paths';
|
import { ABSOLUTE_PATH_FOR_RSS_XML, absolutePathForPhoto } from '@/app/paths';
|
||||||
import { formatDate } from '@/utility/date';
|
import { formatDate } from '@/utility/date';
|
||||||
import { formatStringForXml } from '@/utility/string';
|
import { formatStringForXml } from '@/utility/string';
|
||||||
|
import { BASE_URL, META_DESCRIPTION, META_TITLE } from '@/app/config';
|
||||||
|
|
||||||
interface FeedPhotoRss {
|
interface FeedPhotoRss {
|
||||||
id: string
|
id: string
|
||||||
@ -30,11 +31,12 @@ const formatPhotoForFeedRss = (photo: Photo): FeedPhotoRss => ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const feedPhotoToXml = (photo: FeedPhotoRss): string => {
|
const feedPhotoToXml = (photo: FeedPhotoRss): string => {
|
||||||
const formattedDate = formatDate({ date: photo.pubDate, length: 'rss' });
|
|
||||||
return `<item>
|
return `<item>
|
||||||
<title>${photo.title}</title>
|
<title>${photo.title}</title>
|
||||||
<link>${photo.link}</link>
|
<link>${photo.link}</link>
|
||||||
<pubDate>${formattedDate}</pubDate>
|
<pubDate>
|
||||||
|
${formatDate({ date: photo.pubDate, length: 'rss' })}
|
||||||
|
</pubDate>
|
||||||
<guid isPermaLink="true">${photo.link}</guid>
|
<guid isPermaLink="true">${photo.link}</guid>
|
||||||
${photo.description
|
${photo.description
|
||||||
? `<description><![CDATA[${photo.description}]]></description>`
|
? `<description><![CDATA[${photo.description}]]></description>`
|
||||||
@ -55,5 +57,22 @@ const feedPhotoToXml = (photo: FeedPhotoRss): string => {
|
|||||||
</item>`;
|
</item>`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createRssItems = (photos: Photo[]) =>
|
export const formatFeedRss = (photos: Photo[]) =>
|
||||||
photos.map(formatPhotoForFeedRss).map(feedPhotoToXml);
|
`<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<rss version="2.0"
|
||||||
|
xmlns:content="http://purl.org/rss/1.0/modules/content/"
|
||||||
|
xmlns:atom="http://www.w3.org/2005/Atom"
|
||||||
|
xmlns:media="http://search.yahoo.com/mrss/"
|
||||||
|
>
|
||||||
|
<channel>
|
||||||
|
<title>${META_TITLE}</title>
|
||||||
|
<atom:link
|
||||||
|
href="${ABSOLUTE_PATH_FOR_RSS_XML}"
|
||||||
|
rel="self"
|
||||||
|
type="application/rss+xml"
|
||||||
|
/>
|
||||||
|
<link>${BASE_URL}</link>
|
||||||
|
<description>${META_DESCRIPTION}</description>
|
||||||
|
${photos.map(formatPhotoForFeedRss).map(feedPhotoToXml).join('\n')}
|
||||||
|
</channel>
|
||||||
|
</rss>`;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user