Edits history of script submission #15828 for ' Updates a category (discourse)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Discourse = {
      apiKey: string;
      defaultHost: string;
      apiUsername: string;
    };
    /**
     * Updates a category
     *
     */
    export async function main(
      auth: Discourse,
      id: string,
      body: {
        name: string;
        color?: string;
        text_color?: string;
        parent_category_id?: number;
        allow_badges?: false | true;
        slug?: string;
        topic_featured_links_allowed?: false | true;
        permissions?: { everyone?: number; staff?: number };
        search_priority?: number;
        form_template_ids?: unknown[];
      },
    ) {
      const url = new URL(`https://${auth.defaultHost}/categories/${id}.json`);
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "API-KEY": auth.apiKey,
          "API-USERNAME": auth.apiUsername,
        },
        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