Skip to content

Commit f5b26a1

Browse files
committed
support v3
1 parent a98c3a7 commit f5b26a1

30 files changed

+3117
-249
lines changed

Diff for: .babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": ["es2015", "stage-2", "es2015-node"],
3+
"plugins": [],
4+
"sourceMaps": "both"
5+
}

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ jspm_packages
3636
# Optional REPL history
3737
.node_repl_history
3838
/.idea/
39+
/dist/

Diff for: README.md

+100-21
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,120 @@
11
# sendbird-nodejs
2-
Thin wrapper around SendBird Server REST API
3-
4-
# PLEASE NOTE THIS WORKS ON TOP OF `SENDBIRD V2.0` API - V3.0 WILL COME SOON
2+
Thin wrapper around SendBird's Platform API
53

64
## Installation
7-
`npm install sendbird-nodejs --save`
5+
npm install sendbird-nodejs --save
6+
7+
or
88

9+
yarn add sendbird-nodejs
910

1011
## Usage
11-
See [SendBird Documentation](https://docs.sendbird.com/v2/platform) for payload and response details.
12-
You don't need to pass the `auth` field on the payload, you provide it when creating a new instance of `SendBird` and it is attached to all requests.
12+
See [SendBird Documentation](https://docs.sendbird.com/platform) for payload and response details.
13+
You must provide your `API Token` when creating a new instance of `SendBird` and it will be attached to all requests.
1314

1415
```javascript
1516
var SendBird = require('sendbird-nodejs');
16-
var sb = new SendBird(config.sendBird.appToken);
17+
var sb = new SendBird(/* your sendbird api token here */);
18+
```
19+
20+
The `sb` instance we just created has an entry for each endpoint in the [SendBird Platform API](https://docs.sendbird.com/platform), it is an object with the endpoints methods.
21+
Note that all methods return a `Promise` as returned by [request-promise](https://github.com/request/request-promise).
22+
23+
24+
[sb.users](https://docs.sendbird.com/platform#user)
25+
26+
[sb.openChannels](https://docs.sendbird.com/platform#open_channel)
27+
[sb.openChannels.messages](https://docs.sendbird.com/platform#messages)
28+
[sb.openChannels.metadata](https://docs.sendbird.com/platform#meta)
29+
[sb.openChannels.metacounter](https://docs.sendbird.com/platform#meta)
30+
31+
[sb.groupChannels](https://docs.sendbird.com/platform#group_channel)
32+
[sb.groupChannels.messages](https://docs.sendbird.com/platform#messages)
33+
[sb.groupChannels.metadata](https://docs.sendbird.com/platform#meta)
34+
[sb.groupChannels.metacounter](https://docs.sendbird.com/platform#meta)
35+
36+
##### Missing endpoints
37+
38+
[sb.applications](https://docs.sendbird.com/platform#application)
39+
[sb.migration](https://docs.sendbird.com/platform#migration)
40+
[sb.bots](https://docs.sendbird.com/platform#bot_interface)
41+
42+
### Example
43+
44+
To create a user you would simply need to have something like this:
45+
```javascript
46+
const SendBird = require('sendbird-nodejs');
47+
const sb = SendBird('<SOME API TOKEN>');
48+
49+
const payload = {
50+
"user_id": string,
51+
"nickname": string,
52+
"profile_url": string,
53+
"issue_access_token": boolean // (Optional)
54+
};
55+
sb.users.create(payload)
56+
.then(function (response) {
57+
// do something with SendBird response
58+
// {
59+
// "user_id": string,
60+
// "nickname": string,
61+
// "profile_url": string,
62+
// "access_token": string,
63+
// "last_seen_at": long,
64+
// "is_online": boolean
65+
// }
66+
});
1767
```
1868

19-
The `sb` instance we just created has a field for each endpoint in the [SendBird Server API](https://docs.sendbird.com/v2/platform#overview) that is an object with the endpoints methods.
20-
[sb.user](https://docs.sendbird.com/v2/platform#user)
21-
[sb.channel](https://docs.sendbird.com/v2/platform#open_chat)
22-
[sb.messaging](https://docs.sendbird.com/v2/platform#messaging)
23-
[sb.admin](https://docs.sendbird.com/v2/platform#admin)
24-
[sb.migration](https://docs.sendbird.com/v2/platform#migration)
69+
There are two different types of parameters the Platform API requires
70+
71+
1. `url params` - you can see them in sendbird docs as `{some_param}` in the `URL`
72+
2. `payload/querystring params` - you can see them in the sendbird docs under the `request` section
2573

26-
So, to create a user you would simply need to have something like this:
74+
We will treat the two types differently when calling the API.
75+
`url params` will be used as arguments for the API method you are calling.
76+
`payload/querystring params` will always a plain object and the last argument of the API method.
77+
78+
This is how we would `send` a `message` to a `group channel` on behalf of some user using the API: [Send Message Docs](https://docs.sendbird.com/platform#messages_3_send)
2779
```javascript
28-
var payload = {
29-
"id": string, // User ID
30-
"nickname": string, // User nickname
31-
"image_url": string, // User profile image URL
32-
"issue_access_token": boolean // Default is false. Use only if you want to create an access token for this user
80+
const SendBird = require('sendbird-nodejs');
81+
const sb = SendBird('<SOME API TOKEN>');
82+
83+
const channelUrl = 'channel-url-from-sendbird';
84+
const payload = {
85+
"message_type": "MESG", // Text message
86+
"user_id": string, // Sender user_id
87+
"message": string, // Empty string is not allowed.
88+
"data": string, // (Optional) Custom data
89+
"custom_type": string, // (Optional) default: ''
90+
"mark_as_read": boolean // (Optional) default: true
3391
};
34-
sb.user.create(payload)
92+
sb.groupChannels.messages.send(channelUrl, payload)
3593
.then(function (response) {
36-
// do something with SendBird response
94+
// do something with SendBird response
95+
// {
96+
// "message_id": long,
97+
// "type": "MESG",
98+
// "message": string,
99+
// "file": {
100+
// "name": string,
101+
// "url": string,
102+
// "type": string,
103+
// "size": int
104+
// },
105+
// "data": string,
106+
// "channel_url": string,
107+
// "created_at": long,
108+
// "user": {
109+
// "nickname": string,
110+
// "user_id": string,
111+
// "profile_url": string
112+
// },
113+
// "custom_type": string
114+
// }
37115
});
38116
```
117+
In the [docs](https://docs.sendbird.com/platform#messages_3_send) we see the `URL` is `https://api.sendbird.com/v3/{channel_type}/{channel_url}/messages` so the `channel_type` param will be set by using `sb.groupChannels` and the `channel_url` is the first argument we of `sb.groupChannels.message.send`
39118

40119

41120
## Contributing

Diff for: package.json

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
{
22
"name": "sendbird-nodejs",
3-
"version": "0.0.8",
4-
"description": "Thin wrapper around SendBird Server REST API",
5-
"main": "index.js",
3+
"version": "1.0.0",
4+
"description": "Thin wrapper around SendBird Server REST API v3",
5+
"main": "dist/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"build": "babel src -d dist",
8+
"test": "mocha --compilers js:babel-register"
89
},
910
"repository": {
1011
"type": "git",
1112
"url": "git+https://github.com/kfiroo/sendbird-nodejs.git"
1213
},
1314
"keywords": [
1415
"sendbird",
16+
"sendbird sdk",
17+
"sdk",
1518
"node",
1619
"nodejs"
1720
],
18-
"author": "kfiroo",
21+
"author": "kfiroo <[email protected]>",
1922
"license": "MIT",
2023
"bugs": {
2124
"url": "https://github.com/kfiroo/sendbird-nodejs/issues"
2225
},
2326
"homepage": "https://github.com/kfiroo/sendbird-nodejs#readme",
2427
"dependencies": {
25-
"bluebird": "3.4.1",
26-
"lodash": "4.13.1",
27-
"request": "2.73.0"
28+
"babel-cli": "6.18.0",
29+
"babel-preset-es2015": "6.18.0",
30+
"babel-preset-es2015-node": "6.1.1",
31+
"babel-preset-stage-2": "6.18.0",
32+
"bluebird": "3.4.6",
33+
"expect": "1.20.2",
34+
"lodash": "4.17.2",
35+
"mocha": "3.2.0",
36+
"nock": "9.0.2",
37+
"nodemon": "1.11.0",
38+
"request": "2.79.0",
39+
"request-promise": "4.1.1"
2840
},
29-
"browserDependencies": {}
41+
"devDependencies": {
42+
43+
}
3044
}

Diff for: src/SendBird.js

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
'use strict';
22

3-
var createService = require('./utils/createService');
3+
const rp = require('request-promise');
44

5-
var adminService = require('./services/adminService');
6-
var channelService = require('./services/channelService');
7-
var messagingService = require('./services/messagingService');
8-
var migrationService = require('./services/migrationService');
9-
var userService = require('./services/userService');
5+
const DEFAULT_BASE_URL = 'https://api.sendbird.com/v3/';
106

11-
function SendBird(apiToken) {
12-
if (!(this instanceof SendBird)) {
13-
return new SendBird(apiToken);
14-
}
7+
// users
8+
const usersService = require('./services/users');
9+
// open channel
10+
const openChannelsService = require('./services/openChannels');
11+
// group channel
12+
const groupChannelsService = require('./services/groupChannels');
13+
14+
function SendBird(apiToken, baseUrl = DEFAULT_BASE_URL) {
1515
if (!apiToken) {
1616
throw new Error('Invalid api token');
1717
}
18-
this.admin = createService(apiToken, adminService);
19-
this.channel = createService(apiToken, channelService);
20-
this.messaging = createService(apiToken, messagingService);
21-
this.migration = createService(apiToken, migrationService);
22-
this.user = createService(apiToken, userService);
18+
const request = rp.defaults({
19+
baseUrl,
20+
headers: {
21+
'Api-Token': apiToken
22+
},
23+
json: true
24+
});
25+
26+
const api = {};
27+
28+
api.users = usersService(request);
29+
api.openChannels = openChannelsService(request);
30+
api.groupChannels = groupChannelsService(request);
31+
32+
return api;
2333
}
2434

25-
module.exports = SendBird;
35+
module.exports = SendBird;

Diff for: src/constants/requestTypes.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
GET: 'GET',
5+
POST: 'POST',
6+
PUT: 'PUT',
7+
DELETE: 'DELETE'
8+
};

Diff for: index.js renamed to src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
22

3-
var SendBird = require('./src/SendBird');
3+
const SendBird = require('./SendBird');
44

55
module.exports = SendBird;

Diff for: src/services/UserService.js

-28
This file was deleted.

Diff for: src/services/adminService.js

-30
This file was deleted.

Diff for: src/services/channelService.js

-35
This file was deleted.

0 commit comments

Comments
 (0)