import sendError, { API } from "./sendError"

async function getPinFromBody<T>(context: API<T>) {
  const body = JSON.parse(context.req.body)
  if (
    typeof body !== "object" ||
    !body ||
    !("pin" in body) ||
    typeof body.pin !== "string"
  )
    throw sendError(context, {
      rejected: true,
      message: "No pin in request body!",
      statusCode: 401,
      solved: true,
      type: "warn",
    })
  const { pin } = body

  return pin
}

export default getPinFromBody
