Add explicit types to public /api

This commit is contained in:
Sam Becker 2023-10-07 14:29:05 -05:00
parent bc7c4a35c8
commit cca73eb0d8
3 changed files with 47 additions and 25 deletions

View File

@ -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' });

View File

@ -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),

43
src/site/api.ts Normal file
View File

@ -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),
},
});