Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
//
// business logic
//
const UserNotFound = 'UserNotFound' as const
const InvalidArguments = 'InvalidArguments' as const
const JSONError = 'JSONError' as const
type UserError = typeof InvalidArguments | typeof UserNotFound | typeof JSONError
/** Parses the `user_id` param */
const getUserId: H.Middleware = pipe(
H.decodeParam('user_id', NonEmptyString.decode),
H.mapLeft(() => InvalidArguments)
)
/** Loads a `User` from a database (fake) */
function loadUser(userId: NonEmptyString): H.Middleware {
return userId === 'ab' ? H.right({ name: 'User name...' }) : H.left(UserNotFound)
}
/** Sends a `User` to the client */
function sendUser(user: User): H.Middleware {
return pipe(
H.status(H.Status.OK),
H.ichain(() => H.json(user, () => JSONError))
)
}
interface User {
name: string
}
//
// business logic
//
const UserNotFound: 'UserNotFound' = 'UserNotFound'
const InvalidArguments: 'InvalidArguments' = 'InvalidArguments'
type UserError = typeof InvalidArguments | typeof UserNotFound | AuthenticationError
/** Parses the `user_id` param */
const getUserId = decodeParam('user_id', NonEmptyString.decode).mapLeft(() => InvalidArguments)
/** Sends a `User` to the client */
const sendUser = (user: User) =>
status(Status.OK)
.closeHeaders()
.send(`Hello ${user.name}!`)
/**
* Loads a `User` from a database. The resulting middleware requires a successful authentication upstream because of the
* `AuthenticatedOpen` constraint
*/
const loadUser = (userId: UserId): Middleware =>
userId === 'ab' ? of({ name: 'User name...' }) : fromLeft(UserNotFound)
// const getUser = getUserId
// .ichain(loadUser) // static error! Property 'AuthenticatedOpen' is missing in type 'StatusOpen' but required in type 'AuthenticatedOpen'