Edits history of script submission #14537 for ' Starts workflow based on request body (box)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Box = {
      token: string;
    };
    /**
     * Starts workflow based on request body
     * Initiates a flow with a trigger type of `WORKFLOW_MANUAL_START`.
    
    You application must be authorized to use the `Manage Box Relay` application
    scope within the developer console.
     */
    export async function main(
      auth: Box,
      workflow_id: string,
      body: {
        type?: "workflow_parameters";
        flow: { type?: string; id?: string };
        files: { type?: "file"; id?: string }[];
        folder: { type?: "folder"; id?: string };
        outcomes?: {
          id: string;
          collaborators?: {
            type: "variable";
            variable_type: "user_list";
            variable_value: { type: "user"; id: string }[];
          } & {};
          completion_rule?: {
            type: "variable";
            variable_type: "task_completion_rule";
            variable_value: "all_assignees" | "any_assignees";
          } & {};
          file_collaborator_role?: {
            type: "variable";
            variable_type: "collaborator_role";
            variable_value:
              | ("editor" & {})
              | ("viewer" & {})
              | ("previewer" & {})
              | ("uploader" & {})
              | ("previewer uploader" & {})
              | ("viewer uploader" & {})
              | ("co-owner" & {});
          } & {};
          task_collaborators?: {
            type: "variable";
            variable_type: "user_list";
            variable_value: { type: "user"; id: string }[];
          } & {};
          role?: {
            type: "variable";
            variable_type: "collaborator_role";
            variable_value:
              | ("editor" & {})
              | ("viewer" & {})
              | ("previewer" & {})
              | ("uploader" & {})
              | ("previewer uploader" & {})
              | ("viewer uploader" & {})
              | ("co-owner" & {});
          } & {};
        }[];
      },
    ) {
      const url = new URL(`https://api.box.com/2.0/workflows/${workflow_id}/start`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        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 235 days ago