Last active
May 7, 2024 16:54
-
-
Save karlhorky/46785c6f90924738fdb44bf2e1931f17 to your computer and use it in GitHub Desktop.
Expo API Routes version of Express Naïve Guest List API https://github.com/upleveled/express-guest-list-api-memory-data-store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// guests+api.ts | |
import { ExpoRequest, ExpoResponse } from 'expo-router/server'; | |
import { guestList, Guest } from './index+api.ts'; | |
export const guestList: Guest[] = []; | |
export function GET(request: ExpoRequest): ExpoResponse { | |
return ExpoResponse.json(guestList); | |
} | |
export function POST(request: ExpoRequest): Promise<ExpoResponse> { | |
return request.json().then((body: Record<string, unknown>) => { | |
if (!body.firstName || !body.lastName) { | |
return ExpoResponse.status(400).json({ | |
errors: [ | |
{ message: 'Request body missing a firstName or lastName property' }, | |
], | |
}); | |
} | |
if (Object.keys(body).length > 3) { | |
return ExpoResponse.status(400).json({ | |
errors: [ | |
{ | |
message: | |
'Request body contains more than firstName, lastName and deadline properties', | |
}, | |
], | |
}); | |
} | |
const guest: Guest = { | |
id: String(id++), | |
firstName: body.firstName as string, | |
lastName: body.lastName as string, | |
...(body.deadline ? { deadline: body.deadline as string } : {}), | |
attending: false, | |
}; | |
guestList.push(guest); | |
return ExpoResponse.json(guest); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// guests/[id]+api.ts | |
import { ExpoRequest, ExpoResponse } from 'expo-router/server'; | |
import { guestList } from '../index+api.ts'; | |
export function GET(request: ExpoRequest): ExpoResponse { | |
const guest = guestList.find( | |
(currentGuest) => | |
currentGuest.id === request.expoUrl.searchParams.get('id'), | |
); | |
if (!guest) { | |
return ExpoResponse.status(404).json({ | |
errors: [ | |
{ message: `Guest ${request.expoUrl.searchParams.get('id')} not found` }, | |
], | |
}); | |
} | |
return ExpoResponse.json(guest); | |
} | |
export function PUT(request: ExpoRequest): Promise<ExpoResponse> { | |
return request.json().then((body: Record<string, unknown>) => { | |
const allowedKeys: Record<keyof Guest, boolean> = { | |
id: false, | |
firstName: true, | |
lastName: true, | |
deadline: true, | |
attending: true, | |
}; | |
const difference = Object.keys(body).filter( | |
(key) => !allowedKeys[key as keyof Guest], | |
); | |
if (difference.length > 0) { | |
return ExpoResponse.status(400).json({ | |
errors: [ | |
{ | |
message: `Request body contains more than allowed properties (${Object.keys( | |
allowedKeys, | |
).join(', ')}). The request also contains these extra keys that are not allowed: ${difference.join( | |
', ', | |
)}`, | |
}, | |
], | |
}); | |
} | |
const guest = guestList.find( | |
(currentGuest) => | |
currentGuest.id === request.expoUrl.searchParams.get('id'), | |
); | |
if (!guest) { | |
return ExpoResponse.status(404).json({ | |
errors: [ | |
{ | |
message: `Guest ${request.expoUrl.searchParams.get('id')} not found`, | |
}, | |
], | |
}); | |
} | |
if (body.firstName) guest.firstName = body.firstName as string; | |
if (body.lastName) guest.lastName = body.lastName as string; | |
if (body.deadline) guest.deadline = body.deadline as string; | |
if ('attending' in body) guest.attending = body.attending as boolean; | |
return ExpoResponse.json(guest); | |
}); | |
} | |
export function DELETE(request: ExpoRequest): ExpoResponse { | |
const guest = guestList.find( | |
(currentGuest) => | |
currentGuest.id === request.expoUrl.searchParams.get('id'), | |
); | |
if (!guest) { | |
return ExpoResponse.status(404).json({ | |
errors: [ | |
{ message: `Guest ${request.expoUrl.searchParams.get('id')} not found` }, | |
], | |
}); | |
} | |
guestList.splice(guestList.indexOf(guest), 1); | |
return ExpoResponse.json(guest); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// index+api.ts | |
import { ExpoRequest, ExpoResponse } from 'expo-router/server'; | |
type Guest = { | |
id: string; | |
firstName: string; | |
lastName: string; | |
deadline?: string; | |
attending: boolean; | |
}; | |
let id = 1; | |
export const guestList: Guest[] = []; | |
export function GET(request: ExpoRequest): ExpoResponse { | |
return ExpoResponse.json({ | |
guests: `${request.expoUrl.origin}/guests/`, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment