From cca73eb0d8c9f5cb9cf9c70d9d1395aa64462ac6 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sat, 7 Oct 2023 14:29:05 -0500 Subject: [PATCH] Add explicit types to public /api --- src/app/api/route.ts | 8 +++----- src/photo/index.ts | 21 +-------------------- src/site/api.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 src/site/api.ts diff --git a/src/app/api/route.ts b/src/app/api/route.ts index 5573b5e5..becd8833 100644 --- a/src/app/api/route.ts +++ b/src/app/api/route.ts @@ -1,22 +1,20 @@ import { getPhotosCached } from '@/cache'; -import { parsePhotoForApi } from '@/photo'; +import { API_PHOTO_REQUEST_LIMIT, formatPhotoForApi } from '@/site/api'; import { BASE_URL, PUBLIC_API_ENABLED, SITE_TITLE, } from '@/site/config'; -const API_PHOTO_LIMIT = 20; - export async function GET() { if (PUBLIC_API_ENABLED) { - const photos = await getPhotosCached({ limit: API_PHOTO_LIMIT }); + const photos = await getPhotosCached({ limit: API_PHOTO_REQUEST_LIMIT }); return Response.json({ meta: { title: SITE_TITLE, url: BASE_URL, }, - photos: photos.map(parsePhotoForApi), + photos: photos.map(formatPhotoForApi), }); } else { return Response.json({ message: 'API is disabled' }); diff --git a/src/photo/index.ts b/src/photo/index.ts index d22cfbfd..a6ee8910 100644 --- a/src/photo/index.ts +++ b/src/photo/index.ts @@ -1,7 +1,4 @@ -import { - ABSOLUTE_PATH_FOR_HOME_IMAGE, - absolutePathForPhoto, -} from '@/site/paths'; +import { ABSOLUTE_PATH_FOR_HOME_IMAGE } from '@/site/paths'; import { formatDateFromPostgresString } from '@/utility/date'; import { formatAperture, @@ -10,7 +7,6 @@ import { formatExposureTime, formatFocalLength, } from '@/utility/exif'; -import { getNextImageUrlForRequest } from '@/utility/image'; import camelcaseKeys from 'camelcase-keys'; import type { Metadata } from 'next'; @@ -90,21 +86,6 @@ export const parsePhotoFromDb = (photoDbRaw: PhotoDb): Photo => { }; }; -export const parsePhotoForApi = (photo: Photo) => ({ - id: photo.id, - title: photo.title, - url: absolutePathForPhoto(photo), - ...photo.make && { make: photo.make }, - ...photo.model && { model: photo.model }, - ...photo.tags.length > 0 && { tags: photo.tags }, - takenAtNaive: formatDateFromPostgresString(photo.takenAtNaive), - src: { - small: getNextImageUrlForRequest(photo.url, 200), - medium: getNextImageUrlForRequest(photo.url, 640), - large: getNextImageUrlForRequest(photo.url, 1200), - }, -}); - export const parseCachedPhotoDates = (photo: Photo) => ({ ...photo, takenAt: new Date(photo.takenAt), diff --git a/src/site/api.ts b/src/site/api.ts new file mode 100644 index 00000000..8255f72e --- /dev/null +++ b/src/site/api.ts @@ -0,0 +1,43 @@ +import { Photo } from '@/photo'; +import { absolutePathForPhoto } from './paths'; +import { formatDateFromPostgresString } from '@/utility/date'; +import { getNextImageUrlForRequest } from '@/utility/image'; + +export const API_PHOTO_REQUEST_LIMIT = 20; + +export interface PublicApi { + meta: { + title: string + url: string + } + photos: PublicApiPhoto[] +} + +interface PublicApiPhoto { + id: string + title?: string + url: string + make?: string + model?: string + tags?: string[] + takenAtNaive: string + src: Record< + 'small' | 'medium' | 'large', + string + > +} + +export const formatPhotoForApi = (photo: Photo): PublicApiPhoto => ({ + id: photo.id, + title: photo.title, + url: absolutePathForPhoto(photo), + ...photo.make && { make: photo.make }, + ...photo.model && { model: photo.model }, + ...photo.tags.length > 0 && { tags: photo.tags }, + takenAtNaive: formatDateFromPostgresString(photo.takenAtNaive), + src: { + small: getNextImageUrlForRequest(photo.url, 200), + medium: getNextImageUrlForRequest(photo.url, 640), + large: getNextImageUrlForRequest(photo.url, 1200), + }, +});