Edits history of script submission #12016 for ' List all sessions (clerk)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Clerk = {
      apiKey: string;
    };
    /**
     * List all sessions
     * Returns a list of all sessions.
    The sessions are returned sorted by creation date, with the newest sessions appearing first.
    **Deprecation Notice (2024-01-01):** All parameters were initially considered optional, however
    moving forward at least one of `client_id` or `user_id` parameters should be provided.
     */
    export async function main(
      auth: Clerk,
      client_id: string | undefined,
      user_id: string | undefined,
      status:
        | "abandoned"
        | "active"
        | "ended"
        | "expired"
        | "removed"
        | "replaced"
        | "revoked"
        | undefined,
      limit: string | undefined,
      offset: string | undefined,
    ) {
      const url = new URL(`https://api.clerk.com/v1/sessions`);
      for (const [k, v] of [
        ["client_id", client_id],
        ["user_id", user_id],
        ["status", status],
        ["limit", limit],
        ["offset", offset],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago