Class DiscourseApi

Hierarchy

  • EventEmitter
    • DiscourseApi

Constructors

  • Create a api client

    Parameters

    • url: string

      The URL of your site. Don't add / at the end

    • Optional options: ApiOptions

      options

    Returns DiscourseApi

Properties

_chat: ChatApi
_webhook: WebhookReceptor
url: string

The URL of discourse site

prefixed: string | boolean

Accessors

  • get _defaultHeaders(): Record<string, string>
  • Get default header

    Returns Record<string, string>

Methods

  • send request easily

    Parameters

    • endpoint: string

      the endpoint. if the endpoint not starts with '/', we consider it as a url

    • method: "GET" | "POST" | "DELETE" | "PUT" = "GET"

      method. defaltly 'GET'

    • Optional data: unknown

      for "POST" and "PUT": request payload. For "GET" and "DELETE": URLSearchParams

    • Optional options: {
          doNotAddJSON?: boolean;
          headers?: Record<string, string>;
          overrideHeaders?: boolean;
          params?: unknown;
      }
      • Optional doNotAddJSON?: boolean

        When true, we will not automatically add the end of .json when fetching

      • Optional headers?: Record<string, string>

        custom header

      • Optional overrideHeaders?: boolean

        When true, the options.header will override the default headers. Otherwise, the request will use a header that is a combination of the two

      • Optional params?: unknown

        URL Params

    Returns Promise<any>

    Promise

  • Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns DiscourseApi

  • Archive a topic

    Parameters

    • id: number

      Topic id

    • Optional until: string | Date

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Close a topic

    Parameters

    • id: number

      Topic id

    • Optional until: string | Date

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Creates a new topic, a new post, or a private message

    Parameters

    • payloads: {
          archetype?: string;
          category?: number;
          created_at?: string;
          embed_url?: string;
          external_id?: string;
          raw: string;
          reply_to_post_number?: number;
          target_recipients?: string;
          title?: string;
          topic_id?: number;
      }

      payloads

      • Optional archetype?: string

        Required for new private message.

      • Optional category?: number

        Optional if creating a new topic, and ignored if creating a new post.

      • Optional created_at?: string

        Datestring for created at

      • Optional embed_url?: string

        Provide a URL from a remote system to associate a forum topic with that URL, typically for using Discourse as a comments system for an external blog.

      • Optional external_id?: string

        Provide an external_id from a remote system to associate a forum topic with that id.

      • raw: string

        Raw post

      • Optional reply_to_post_number?: number

        Optional, the post number to reply to inside a topic.

      • Optional target_recipients?: string

        Required for private message, comma separated.

      • Optional title?: string

        Required if creating a new topic or new private message.

      • Optional topic_id?: number

        Required if creating a new post.

    Returns Promise<Post>

  • Create a upload

    Parameters

    • file: PathLike

      File path or file buffer

    • Optional options: FormData | {
          encoding?: BufferEncoding;
          filename?: string;
          synchronous?: boolean;
          type?: "avatar" | "profile_background" | "card_background" | "custom_emoji" | "composer";
          user_id?: number;
      }

    Returns Promise<Uploads>

  • Decrypt the encrypted user_api_key from discourse

    Parameters

    • private_key: string

      your private_key

    • encrypted: string

      The encrypted user_api_key

    Returns {
        api: string | number;
        key: string;
        nonce: string;
        push: boolean;
    }

    • api: string | number

      Api version

    • key: string

      The user api key

    • nonce: string

      The nonce provided when generating user_api_key

    • push: boolean
  • Parameters

    • id: string | number

      Post id

    • permanently: boolean = false

      The SiteSetting.can_permanently_delete needs to be enabled first before this param can be used. Also this endpoint needs to be called first without permanently and then followed up with a second call 5 minutes later with permanently to permanently delete.

    Returns Promise<any>

  • Calls each of the listeners registered for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Rest ...args: any[]

    Returns boolean

  • Return an array listing the events for which the emitter has registered listeners.

    Returns (string | symbol)[]

  • Get the latest topics. It is an alias of listLatest

    Parameters

    • Rest ...args: never[]

    Returns Promise<{
        flair_groups?: {
            flair_bg_color: string;
            flair_color: string;
            flair_url: string;
            id: number;
            name?: string;
        };
        primary_groups: unknown[];
        topic_list: {
            can_create_topic: boolean;
            more_topics_url?: string;
            per_page: number;
            topics: SuggestedTopic[];
        };
        users: BasicUser[];
    }>

  • 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.

    https://docs.discourse.org/#tag/Posts/operation/getPost

    Parameters

    • id: string | number

    Returns Promise<Post>

  • Get only the cooked of the specified post

    Parameters

    • post_id: string | number

    Returns Promise<{
        cooked: string;
    }>

  • Get the raw of the specified post

    Parameters

    • post_id: string | number

    Returns Promise<string>

  • Get information on a specified topic. It is a alias of getTopic

    Parameters

    • topic_id: string | number
    • Rest ...args: never[]

    Returns Promise<Topic>

  • Get posts by post_id on a specified topic

    Parameters

    • topic_id: string | number
    • post_ids: (string | number)[]

    Returns Promise<{
        id: number;
        post_stream: {
            posts: Post[];
        };
    }>

  • Like a post

    Parameters

    • post_id: string | number

    Returns Promise<Post>

    The updated post

  • Get the latest topics

    Parameters

    • Optional options: {
          ascending?: boolean;
          custom_url?: string;
          order?: "views" | "posters" | "default" | "created" | "activity" | "posts" | "category" | "likes" | "op_likes";
          status?: "closed" | "archived" | "open" | "deleted" | "listed" | "public" | "unlisted";
      }
      • Optional ascending?: boolean
      • Optional custom_url?: string

        You can usually provide more_topics_url If you provide this value, you cannot provide other options

      • Optional order?: "views" | "posters" | "default" | "created" | "activity" | "posts" | "category" | "likes" | "op_likes"
      • Optional status?: "closed" | "archived" | "open" | "deleted" | "listed" | "public" | "unlisted"

    Returns Promise<{
        flair_groups?: {
            flair_bg_color: string;
            flair_color: string;
            flair_url: string;
            id: number;
            name?: string;
        };
        primary_groups: unknown[];
        topic_list: {
            can_create_topic: boolean;
            more_topics_url?: string;
            per_page: number;
            topics: SuggestedTopic[];
        };
        users: BasicUser[];
    }>

  • List a topic. (Make the topic visible again)

    Parameters

    • id: number

      Topic id

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Return the number of listeners listening to a given event.

    Parameters

    • event: string | symbol

    Returns number

  • Return the listeners registered for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T

    Returns ((...args) => void)[]

  • Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any
    • Optional once: boolean

    Returns DiscourseApi

  • Add a listener for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns DiscourseApi

  • Add a one-time listener for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns DiscourseApi

  • Open a topic

    Parameters

    • id: number

      Topic id

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Pin a topic

    Parameters

    • id: number

      Topic id

    • globally: boolean
    • until: string | Date

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Remove all listeners, or those of the specified event.

    Parameters

    • Optional event: string | symbol

    Returns DiscourseApi

  • Remove the listeners of a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any
    • Optional once: boolean

    Returns DiscourseApi

  • Revoke a user api key.

    Parameters

    • Optional user_api_key: string

      The user api key to revoke. If not specified, options.user_api_key is used

    Returns Promise<any>

  • Unarchive a topic

    Parameters

    • id: number

      Topic id

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Unlike a post

    Parameters

    • post_id: string | number

    Returns Promise<Post>

    The updated post

  • Unlist a topic

    Parameters

    • id: number

      Topic id

    • Optional until: string | Date

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Unpin a topic

    Parameters

    • id: number

      Topic id

    • globally: boolean

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

  • Parameters

    • id: number

      Topic id

    • status: "pinned" | "visible" | "closed" | "archived" | "pinned_globally"

      Status

    • enabled: boolean

      Enabled

    • Optional until: string | Date

      Until. Only required for pinned and pinned_globally

    Returns Promise<{
        success: string;
        topic_status_update?: null | string;
    }>

Generated using TypeDoc