Reorganization. Split remote/result/request. Split picsum specific api.
This commit is contained in:
parent
5f9bdd5511
commit
c3c629943c
6 changed files with 550 additions and 532 deletions
29
src/api/picsum.ts
Normal file
29
src/api/picsum.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { z } from "zod"
|
||||
import { fetchJsonSafeWith } from "../request"
|
||||
import type { RequestError } from "../request"
|
||||
import type { Result } from "../result"
|
||||
|
||||
export const picsumApiImageSchema = z.object({
|
||||
id: z.string(),
|
||||
author: z.string(),
|
||||
width: z.number(),
|
||||
height: z.number(),
|
||||
url: z.string(),
|
||||
download_url: z.string(), // WARNING: the api-endpoint returns "url" and "download_url". You definitely want "download_url" for image sources.
|
||||
})
|
||||
export type PicsumApiImage = z.infer<typeof picsumApiImageSchema>
|
||||
|
||||
export type GetPicsumImagesParams = {
|
||||
page: number
|
||||
limit: number
|
||||
}
|
||||
|
||||
export async function getPicsumImages({
|
||||
page,
|
||||
limit,
|
||||
}: GetPicsumImagesParams): Promise<Result<PicsumApiImage[], RequestError>> {
|
||||
return fetchJsonSafeWith(
|
||||
`https://picsum.photos/v2/list?limit=${String(limit)}&page=${String(page)}`,
|
||||
(json) => z.array(picsumApiImageSchema).parse(json),
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue