Skip to main content

Parsing URL Params

worker/routers/users.ts
export const usersRouter = new RequestRouter({
displayName: 'Users Router',
DocumentComponent: AppHTMLDocument,
session: true,
})

interface GetUserParams {
username: string
}

// Declaring a route that returns a React element...
users.get<GetUserParams>('/profile/:username', async ({ session, params }) => {
const user = database.fetchBy({ username })
let isFriends: boolean

if (!user) {
return new KeyworkResourceError(`${username} not found`, 404)
}

if (session) {
const currentUser = database.fetchBy({ sessionID: session.sessionID })
isFriends = currentUser && user.friendsList.includes(currentUser.id)
}

return <UserProfile user={user} isFriends={isFriends} />
})