|
| 1 | +/// <reference types="vite/client" /> |
| 2 | + |
| 3 | +import { apiRoute, jwtHeaders } from './utils'; |
| 4 | +import { |
| 5 | + AuctionInfo, |
| 6 | + auctionInfoSchema, |
| 7 | + CreateAuctionInfo, |
| 8 | + UpdateAuctionInfo, |
| 9 | +} from '@shared/auctions.ts'; |
| 10 | + |
| 11 | +class AuctionsApi { |
| 12 | + // Retrieve information about all auctions. |
| 13 | + static async getAllAuctions(token: string): Promise<AuctionInfo[]> { |
| 14 | + const res = await fetch(apiRoute('auctions'), { |
| 15 | + method: 'GET', |
| 16 | + headers: { |
| 17 | + 'Content-Type': 'application/json', |
| 18 | + ...jwtHeaders(token), |
| 19 | + }, |
| 20 | + }); |
| 21 | + |
| 22 | + if (!res.ok) throw new Error('failed to fetch auctions'); |
| 23 | + |
| 24 | + // Use the defined schema to validate the response. |
| 25 | + // This also takes care of converting the date strings to Date objects. |
| 26 | + const rawBody: AuctionInfo[] = await res.json(); |
| 27 | + const body = rawBody.map((a) => auctionInfoSchema.validateSync(a)); |
| 28 | + return body; |
| 29 | + } |
| 30 | + |
| 31 | + // Retrieve information about a specific auction. |
| 32 | + static async getAuctionInfo(id: string, token: string): Promise<AuctionInfo> { |
| 33 | + const res = await fetch(apiRoute(`auctions/${id}`), { |
| 34 | + method: 'GET', |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/json', |
| 37 | + ...jwtHeaders(token), |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + if (!res.ok) throw new Error('failed to fetch auction'); |
| 42 | + |
| 43 | + // Use the defined schema to validate the response. |
| 44 | + // This also takes care of converting the date strings to a Date. |
| 45 | + const rawBody = await res.json(); |
| 46 | + const body = auctionInfoSchema.validateSync(rawBody); |
| 47 | + return body; |
| 48 | + } |
| 49 | + |
| 50 | + // Create a new auction. |
| 51 | + // If successful, the information about the new auction will be returned. |
| 52 | + static async createAuction( |
| 53 | + createAuctionInfo: CreateAuctionInfo, |
| 54 | + token: string, |
| 55 | + ): Promise<AuctionInfo> { |
| 56 | + const res = await fetch(apiRoute('auctions'), { |
| 57 | + method: 'POST', |
| 58 | + headers: { 'Content-Type': 'application/json', ...jwtHeaders(token) }, |
| 59 | + body: JSON.stringify(createAuctionInfo), |
| 60 | + }); |
| 61 | + if (!res.ok) throw new Error('failed to create auction'); |
| 62 | + const rawBody = await res.json(); |
| 63 | + const body = auctionInfoSchema.validateSync(rawBody); |
| 64 | + return body; |
| 65 | + } |
| 66 | + |
| 67 | + // Update information about an existing auction. |
| 68 | + // If successful, the auction's updated information will be returned. |
| 69 | + static async updateAuction( |
| 70 | + id: string, |
| 71 | + auctionInfo: Partial<UpdateAuctionInfo>, |
| 72 | + token: string, |
| 73 | + ): Promise<AuctionInfo> { |
| 74 | + const res = await fetch(apiRoute(`auctions/${id}`), { |
| 75 | + method: 'PUT', |
| 76 | + headers: { |
| 77 | + 'Content-Type': 'application/json', |
| 78 | + ...jwtHeaders(token), |
| 79 | + }, |
| 80 | + body: JSON.stringify(auctionInfo), |
| 81 | + }); |
| 82 | + if (!res.ok) throw new Error('failed to update auction'); |
| 83 | + const rawBody = await res.json(); |
| 84 | + const body = auctionInfoSchema.validateSync(rawBody); |
| 85 | + return body; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export default AuctionsApi; |
0 commit comments