From 509f5292fc926448a2db550ab7c3d0f7c7a6e11c Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Tue, 30 Jul 2024 21:23:50 -0500 Subject: [PATCH] Refactoring API types and Dates now working. --- src/api/getRecipeInfos.ts | 42 ++++++++++++++++++++++++++++++-- src/api/types/ImageView.ts | 14 ++++++++++- src/api/types/RecipeInfoView.ts | 17 +++++++++++-- src/api/types/RecipeInfosView.ts | 12 +++++++-- 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/api/getRecipeInfos.ts b/src/api/getRecipeInfos.ts index 3a936be..c620cac 100644 --- a/src/api/getRecipeInfos.ts +++ b/src/api/getRecipeInfos.ts @@ -1,5 +1,5 @@ import { ApiError } from './ApiError' -import { RecipeInfosView } from './types/RecipeInfosView' +import RecipeInfosView, { RawRecipeInfosView } from './types/RecipeInfosView' const getRecipeInfos = async ( token: string | null, @@ -19,7 +19,45 @@ const getRecipeInfos = async ( } ) if (response.ok) { - return (await response.json()) as RecipeInfosView + const { pageNumber, pageSize, content } = + (await response.json()) as RawRecipeInfosView + return { + pageNumber, + pageSize, + content: content.map( + ({ + id, + updated: rawUpdated, + title, + ownerId, + ownerUsername, + isPublic, + starCount, + mainImage: rawMainImage + }) => ({ + id, + updated: new Date(rawUpdated), + title, + ownerId, + ownerUsername, + isPublic, + starCount, + mainImage: { + url: rawMainImage.url, + created: new Date(rawMainImage.created), + modified: rawMainImage.modified + ? new Date(rawMainImage.modified) + : null, + filename: rawMainImage.fileName, + mimeType: rawMainImage.mimeType, + alt: rawMainImage.alt, + caption: rawMainImage.caption, + owner: rawMainImage.owner, + isPublic: rawMainImage.isPublic + } + }) + ) + } } else { throw new ApiError(response.status, response.statusText) } diff --git a/src/api/types/ImageView.ts b/src/api/types/ImageView.ts index ee34b91..38b6d97 100644 --- a/src/api/types/ImageView.ts +++ b/src/api/types/ImageView.ts @@ -1,9 +1,21 @@ import UserInfoView from './UserInfoView' -interface ImageView { +export interface RawImageView { url: string created: string modified: string | null + fileName: string + mimeType: string + alt: string | null + caption: string | null + owner: UserInfoView + isPublic: boolean +} + +interface ImageView { + url: string + created: Date + modified: Date | null filename: string mimeType: string alt: string | null diff --git a/src/api/types/RecipeInfoView.ts b/src/api/types/RecipeInfoView.ts index 8a08929..e6e3c31 100644 --- a/src/api/types/RecipeInfoView.ts +++ b/src/api/types/RecipeInfoView.ts @@ -1,6 +1,17 @@ -import ImageView from './ImageView' +import ImageView, { RawImageView } from './ImageView' -export interface RecipeInfoView { +export interface RawRecipeInfoView { + id: number + updated: string + title: string + ownerId: number + ownerUsername: string + isPublic: boolean + starCount: number + mainImage: RawImageView +} + +interface RecipeInfoView { id: number updated: Date title: string @@ -10,3 +21,5 @@ export interface RecipeInfoView { starCount: number mainImage: ImageView } + +export default RecipeInfoView diff --git a/src/api/types/RecipeInfosView.ts b/src/api/types/RecipeInfosView.ts index 8bebb79..e9a0d8b 100644 --- a/src/api/types/RecipeInfosView.ts +++ b/src/api/types/RecipeInfosView.ts @@ -1,7 +1,15 @@ -import { RecipeInfoView } from './types/RecipeInfoView' +import RecipeInfoView, { RawRecipeInfoView } from './RecipeInfoView' -export interface RecipeInfosView { +export interface RawRecipeInfosView { + pageNumber: number + pageSize: number + content: RawRecipeInfoView[] +} + +interface RecipeInfosView { pageNumber: number pageSize: number content: RecipeInfoView[] } + +export default RecipeInfosView