3.0.6 • Published 3 years ago

discourse-js v3.0.6

Weekly downloads
90
License
MIT
Repository
github
Last release
3 years ago

NPM Version

Contents

Installation

$ npm i discourse-js

Quick Start

import Discourse from 'discourse-js';

const apiKey = '<api-key-from-discourse>';
const baseUrl = '<your-discourse-url>' || 'http://localhost:3000';

const discourse = new Discourse(apiKey, baseUrl);

discourse.posts
  .create({
    api_username: 'karl',
    topic_id: 11, // optional (required for creating a new post on a topic.)
    raw: 'Hello World',
    imageUri: imageUri, // optional to create a post/topic with an image.
  })
  .then(res => console.log(res))
  .catch(err => console.log(err));

API

Categories

Todo


Groups

Get Group Members

discourse.groups
  .getMembers({ group_name: 'group-name' })
  .then(res => console.log(res))
  .catch(err => console.log(err));

Messages

Todo


Notifications

Todo


Posts

Create a Post

discourse.posts
  .create({
    api_username: 'karl',
    topic_id: 11, // optional (required for creating a new post on a topic.)
    raw: 'Hello World',
    imageUri: imageUri, // optional to create a post/topic with an image.
  })
  .then(res => console.log(res))
  .catch(err => console.log(err));

Like a Post

discourse.posts
  .like({ id: 72 })
  .then(res => console.log(res))
  .catch(err => console.log(err));

Unlike a Post

discourse.posts
  .unlike({ id: 72 })
  .then(res => console.log(res))
  .catch(err => console.log(err));

Note: You can only unlike a post within 5 - 10 minutes after you have liked it. Think of unlike more so like an undo.

See this post here and here for information around the undocumented time limit on unliking a liked post.

Reply to a Post

discourse.posts
  .reply({
    topic_id: 72,
    raw: 'Hello World',
    reply_to_post_number: 14,
  })
  .then(res => console.log(res))
  .catch(err => console.log(err));

Topics

Get a Topic

discourse.topics
  .getTopic({ id })
  .then(res => console.log(res)}
  .catch(err => console.log(err))

Topics have a chunk size of 20, which mean you will only get 20 posts back in one get. This can cause weird problems with nested replies and long threads. You can override this by passing print: true. Note this sets the chunk size to 1000. See API: Getting all posts in a topic

discourse.topics
  .getTopic({
    id,
    print: true
  })
  .then(res => console.log(res)}
  .catch(err => console.log(err))

Delete a Topic

  discourse.topics
    .deleteTopic({ id })
    .then(res => console.log(res)} // Note: delete returns nothing.
    .catch(err => console.log(err))

Users

Get a single User

discourse.users
  .getUser({ username })
  .then(res => console.log(res)}
  .catch(err => console.log(err))

Local Development

This is if you are developing the discourse-js API locally on your machine.

# Clone the repo
$ git clone git@gitlab.com:theworkshop/discourse-js.git
$ cd discourse-js
$ pwd|pbcopy # Copies the current working directory /path/to/discourse-js/
# cd into the directory you want to test locally.
$ npm install /path/to/discourse-js/

Why not just use npm link?:

For speed and productivity. Symlinks do not work with React Native 💩📲.

Read more about npm link and why we do this here.

You will also need a Discourse server running. This can be local or running in the cloud.

License

MIT

3.0.12

3 years ago

3.0.11

3 years ago

3.0.9

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.4

4 years ago

3.0.5

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.4

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.4.0

4 years ago

1.3.2

4 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.2

6 years ago

0.0.1

6 years ago