Edits history of script submission #14494 for ' List user and enterprise events (box)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Box = {
      token: string;
    };
    /**
     * List user and enterprise events
     * Returns up to a year of past events for a given user
    or for the entire enterprise.
    
    By default this returns events for the authenticated user. To retrieve events
    for the entire enterprise, set the `stream_type` to `admin_logs_streaming`
    for live monitoring of new events, or `admin_logs` for querying across
    historical events. The user making the API call will
    need to have admin privileges, and the application will need to have the
    scope `manage enterprise properties` checked.
     */
    export async function main(
      auth: Box,
      stream_type:
        | "all"
        | "changes"
        | "sync"
        | "admin_logs"
        | "admin_logs_streaming"
        | undefined,
      stream_position: string | undefined,
      limit: string | undefined,
      event_type: string | undefined,
      created_after: string | undefined,
      created_before: string | undefined,
    ) {
      const url = new URL(`https://api.box.com/2.0/events`);
      for (const [k, v] of [
        ["stream_type", stream_type],
        ["stream_position", stream_position],
        ["limit", limit],
        ["event_type", event_type],
        ["created_after", created_after],
        ["created_before", created_before],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago