JavaScript

Submit forms from the browser with coss/forms, without a page load.

coss/forms is the SDK's browser entry for Forms. attachForm takes over a form's submit: it posts the fields (files and captcha tokens included) to the form's action, adds a coss/device token so Risk recognizes the browser, and gives you the answer.

const  = .<>("#contact");
if () {
  (, {
    () {
      if (.) {
        // "received", or "pending_confirmation" when the submitter must confirm their email.
        .(.);
      } else if (.) {
        for (const  of .) .(., .);
      } else {
        .(.);
      }
    },
  });
}

The form is reset after a success and its submit buttons are disabled while it is on its way. attachForm returns a function that detaches it.

To post yourself (a framework's submit handler, say), call submitForm with the form element, a FormData or a plain object:

const  = await (
  { : "ada@example.com", : "Hello" },
  { : "https://…/f/form_01k…" },
);

Neither throws: a network failure is { ok: false, error }. Pass device: false to leave the device token out. Requests are sent without cookies (credentials: "omit").

Without JavaScript the same form still works as a plain HTML form, so coss/forms is safe to add as an enhancement.