Edits history of script submission #12862 for ' Create an Inquiry (persona)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Persona = {
      apiKey: string;
    };
    /**
     * Create an Inquiry
     * Creates a new inquiry with optional pre-filled attributes.
    
    See [Sessions](https://docs.withpersona.com/docs/inquiry-sessions) for how to continue the inquiry in [Embedded Flow](https://docs.withpersona.com/docs/embedded-flow) or [Hosted Flow](https://docs.withpersona.com/docs/hosted-flow).
     */
    export async function main(
      auth: Persona,
      body: {
        data: {
          attributes: {
            "template-id"?: string;
            "inquiry-template-id"?: string;
            "inquiry-template-version-id"?: string;
            "reference-id"?: string;
            "account-id"?: string;
            "creator-email-address"?: string;
            "theme-id"?: string;
            "theme-set-id"?: string;
            "redirect-uri"?: string;
            note?: string;
            fields?:
              | {}
              | ({
                  birthdate?: string;
                  "name-first"?: string;
                  "name-middle"?: string;
                  "name-last"?: string;
                  "phone-number"?: string;
                  "email-address"?: string;
                } & {
                  "address-street-1"?: string;
                  "address-street-2"?: string;
                  "address-city"?: string;
                  "address-subdivision"?: string;
                  "address-postal-code"?: string;
                } & { "address-country-code"?: string });
            tags?: string[];
            "initial-step-name"?: string;
          };
        };
        meta?: {
          "auto-create-account"?: false | true;
          "auto-create-account-type-id"?: string;
          "auto-create-account-reference-id"?: string;
          "expiration-after-create-interval-seconds"?: number;
          "expiration-after-start-interval-seconds"?: number;
          "expiration-after-resume-interval-seconds"?: number;
          "one-time-link-expiration-seconds"?: number;
        };
      },
      include?: string,
      fields?: string,
      Key_Inflection?: string,
      Idempotency_Key?: string,
      Persona_Version?: string,
    ) {
      const url = new URL(`https://api.withpersona.com/api/v1/inquiries`);
      for (const [k, v] of [
        ["include", include],
        ["fields", fields],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const headers: Record<string, string> = {
        Authorization: `Bearer ${auth.apiKey}`,
        "Content-Type": "application/json",
      };
      if (Key_Inflection) {
        headers["Key-Inflection"] = Key_Inflection;
      }
      if (Idempotency_Key) {
        headers["Idempotency-Key"] = Idempotency_Key;
      }
      if (Persona_Version) {
        headers["Persona-Version"] = Persona_Version;
      }
      const response = await fetch(url, {
        method: "POST",
        headers,
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago