Refactoring API types and Dates now working.

This commit is contained in:
Jesse Brault 2024-07-30 21:23:50 -05:00
parent 86a7471656
commit 509f5292fc
4 changed files with 78 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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