Documentation Index Fetch the complete documentation index at: https://mintlify.com/anupom69/bun-hono-frontend-nextjs/llms.txt
Use this file to discover all available pages before exploring further.
Function Signature
async function createMessage (
prevState : MessagePrevState ,
formData : FormData
) : Promise < MessagePrevState >
Source: src/lib/actions/message/createMessage.ts:21
Parameters
Previous state object returned from the last invocation. Used by React’s useActionState hook. Indicates if the previous operation was successful
Error message from the previous operation
Form data containing the message details The message content to send Validation:
Minimum length: 1 character
Error: “Content cannot be empty”
Phone number to send the message to Validation:
Pattern: /^8801\d{9}$/
Must start with “8801” followed by exactly 9 digits
Error: “Invalid phone number format. Must be 8801XXXXXXXXX”
Number of days after which to send the message Validation:
Minimum: 1 day
Error: “Send after is required and minimum after 1 day”
Return Value
Set to true when the message is created successfully
Error message describing what went wrong Possible errors:
Validation errors: Comma-separated list of field validation failures
Backend errors: “Backend Error: ”
Generic errors: “Something went wrong”
Validation Schema
The function uses Zod for validation:
const messageSchema = z . object ({
content: z . string (). min ( 1 , "Content cannot be empty" ),
sendToPhone: z
. string ()
. regex ( / ^ 8801 \d {9} $ / , "Invalid phone number format. Must be 8801XXXXXXXXX" ),
sendAfter: z
. number ()
. min ( 1 , "Send after is required and minimum after 1 day" ),
});
Behavior
Validation : Form data is validated against the Zod schema
API Call : Makes a POST request to {BACKEND_URL}/messages/create-one
Authentication : Uses Basic Auth with environment credentials
Revalidation : Calls revalidatePath("/") on success to refresh the home page
Error Handling : Returns structured error messages for validation, backend, or network errors
Usage Example
import { useActionState } from "react" ;
import createMessage from "@/lib/actions/message/createMessage" ;
function MessageForm () {
const [ state , formAction ] = useActionState ( createMessage , {});
return (
< form action = { formAction } >
< textarea name = "content" placeholder = "Message content" />
< input name = "sendToPhone" placeholder = "8801XXXXXXXXX" />
< input name = "sendAfter" type = "number" min = "1" />
< button type = "submit" > Create Message </ button >
{ state . error && < p >{ state . error }</ p >}
{ state . success && < p > Message created !</ p >}
</ form >
);
}
Backend Endpoint
POST /messages/create-one
Headers:
Authorization: Basic authentication
Content-Type: application/json
Body:
{
"content" : "Hello world" ,
"sendToPhone" : "8801234567890" ,
"sendAfter" : 7
}