Edits history of script submission #15808 for ' Retrieve a single post (discourse)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Discourse = {
      apiKey: string;
      defaultHost: string;
      apiUsername: string;
    };
    /**
     * Retrieve a single post
     * This endpoint can be used to get the number of likes on a post using the
    `actions_summary` property in the response. `actions_summary` responses
    with the id of `2` signify a `like`. If there are no `actions_summary`
    items with the id of `2`, that means there are 0 likes. Other ids likely
    refer to various different flag types.
    
     */
    export async function main(auth: Discourse, id: string) {
      const url = new URL(`https://${auth.defaultHost}/posts/${id}.json`);
    
      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