//native
export type DynSelect_queue_id = string
// Dropdown of the account's queues, by name.
export async function queue_id(auth: RT.Cloudflare) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/queues`,
{
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { result } = (await response.json()) as {
result: { queue_id: string; queue_name: string }[]
}
return result.map((q) => ({ value: q.queue_id, label: q.queue_name }))
}
/**
* Send Queue Message
* Send a message to a Cloudflare Queue: set json_body for a JSON message or text_body for a plain-text one.
*/
export async function main(
auth: RT.Cloudflare,
queue_id: DynSelect_queue_id,
json_body: { [key: string]: any } | undefined,
text_body: string | undefined,
delay_seconds: number | undefined
) {
if ((json_body != null) === !!text_body) {
throw new Error("Set exactly one of json_body or text_body")
}
const message =
json_body != null
? { body: json_body, content_type: "json" }
: { body: text_body, content_type: "text" }
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/queues/${queue_id}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
...message,
delay_seconds: delay_seconds ?? undefined,
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 4 hours ago