Edits history of script submission #15774 for ' Get a public list of users (discourse)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Discourse = {
      apiKey: string;
      defaultHost: string;
      apiUsername: string;
    };
    /**
     * Get a public list of users
     *
     */
    export async function main(
      auth: Discourse,
      period:
        | "daily"
        | "weekly"
        | "monthly"
        | "quarterly"
        | "yearly"
        | "all"
        | undefined,
      order:
        | "likes_received"
        | "likes_given"
        | "topic_count"
        | "post_count"
        | "topics_entered"
        | "posts_read"
        | "days_visited"
        | undefined,
      asc: "true" | undefined,
      page: string | undefined,
    ) {
      const url = new URL(`https://${auth.defaultHost}/directory_items.json`);
      for (const [k, v] of [
        ["period", period],
        ["order", order],
        ["asc", asc],
        ["page", page],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          "API-KEY": auth.apiKey,
          "API-USERNAME": auth.apiUsername,
        },
        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