messaging-api-slack
Advanced tools
Comparing version 0.6.10 to 0.6.13
@@ -62,2 +62,5 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;}; | ||
class SlackOAuthClient { | ||
@@ -71,7 +74,19 @@ | ||
constructor(token) {_initialiseProps.call(this); | ||
constructor(accessTokenOrConfig) {_initialiseProps.call(this); | ||
let origin; | ||
if (accessTokenOrConfig && typeof accessTokenOrConfig === 'object') { | ||
const config = accessTokenOrConfig; | ||
this._token = config.accessToken; | ||
origin = config.origin; | ||
} else { | ||
// Bot User OAuth Access Token | ||
this._token = accessTokenOrConfig; | ||
} | ||
// Web API | ||
// https://api.slack.com/web | ||
this._axios = _axios2.default.create({ | ||
baseURL: 'https://slack.com/api/', | ||
baseURL: `${origin || 'https://slack.com'}/api/`, | ||
headers: { | ||
@@ -81,4 +96,2 @@ 'Content-Type': 'application/x-www-form-urlencoded' } }); | ||
// Bot User OAuth Access Token | ||
this._token = token; | ||
} | ||
@@ -331,3 +344,3 @@ | ||
* FIXME: [breaking] support include_locale, limit, presence | ||
*/}exports.default = SlackOAuthClient;SlackOAuthClient.connect = token => new SlackOAuthClient(token);var _initialiseProps = function _initialiseProps() {var _this = this;this.callMethod = (() => {var _ref = _asyncToGenerator(function* (method, body = {}) {body.token = body.token || _this._token; // eslint-disable-line no-param-reassign | ||
*/}exports.default = SlackOAuthClient;SlackOAuthClient.connect = accessTokenOrConfig => new SlackOAuthClient(accessTokenOrConfig);var _initialiseProps = function _initialiseProps() {var _this = this;this.callMethod = (() => {var _ref = _asyncToGenerator(function* (method, body = {}) {body.token = body.token || _this._token; // eslint-disable-line no-param-reassign | ||
const response = yield _this._axios.post(method, _querystring2.default.stringify(body));const data = response.data,config = response.config,request = response.request;if (!data.ok) {throw new _axiosError2.default(`Slack API - ${data.error}`, { config, request, response });}return data;});return function (_x) {return _ref.apply(this, arguments);};})();this.getChannelInfo = (channelId, options = {}) => this.callMethod('channels.info', _extends({ channel: channelId }, options)).then(data => data.channel);this.getChannelList = () => this.callMethod('channels.list').then(data => data.channels);this.getConversationInfo = (channelId, options = {}) => this.callMethod('conversations.info', _extends({ channel: channelId }, options)).then(data => data.channel);this.getConversationMembers = (channelId, options = {}) => this.callMethod('conversations.members', _extends({ channel: channelId }, options)).then(data => ({ members: data.members, next: data.response_metadata && data.response_metadata.next_cursor }));this.getAllConversationMembers = (() => {var _ref2 = _asyncToGenerator(function* (channelId) {let allMembers = [];let continuationCursor;do {var _ref3 = yield _this.getConversationMembers(channelId, { cursor: continuationCursor });const members = _ref3.members,next = _ref3.next;allMembers = allMembers.concat(members);continuationCursor = next;} while (continuationCursor);return allMembers;});return function (_x2) {return _ref2.apply(this, arguments);};})();this.getConversationList = (options = {}) => this.callMethod('conversations.list', options).then(data => ({ channels: data.channels, next: data.response_metadata && data.response_metadata.next_cursor }));this.getAllConversationList = (() => {var _ref4 = _asyncToGenerator(function* (options = {}) {let allChannels = [];let continuationCursor;do {var _ref5 = yield _this.getConversationList(_extends({}, options, { cursor: continuationCursor }));const channels = _ref5.channels,next = _ref5.next;allChannels = allChannels.concat(channels);continuationCursor = next;} while (continuationCursor);return allChannels;});return function () {return _ref4.apply(this, arguments);};})();this.postMessage = (channel, message, options = {}) => {if (options.attachments && typeof options.attachments !== 'string') {// A JSON-based array of structured attachments, presented as a URL-encoded string. | ||
@@ -334,0 +347,0 @@ // eslint-disable-next-line no-param-reassign |
{ | ||
"name": "messaging-api-slack", | ||
"description": "Messaging API client for Slack", | ||
"version": "0.6.10", | ||
"version": "0.6.13", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=6" |
@@ -13,2 +13,3 @@ # messaging-api-slack | ||
- [API Reference](#api-reference) | ||
- [Test](#test) | ||
- [Webhook Client](#webhook-client) | ||
@@ -378,2 +379,21 @@ - [Usage](#usage-1) | ||
## Test | ||
### Point requests to your dummy server | ||
To avoid sending requests to real Slack server, specify `origin` option when constructing your client: | ||
```js | ||
const { SlackOAuthClient } = require('messaging-api-slack'); | ||
const client = SlackOAuthClient.connect({ | ||
accessToken: ACCESS_TOKEN, | ||
origin: 'https://mydummytestserver.com', | ||
}); | ||
``` | ||
> Warning: Don't do this on production server. | ||
<br /> | ||
## Webhook Client | ||
@@ -380,0 +400,0 @@ |
@@ -17,66 +17,2 @@ import querystring from 'querystring'; | ||
describe('connect', () => { | ||
let axios; | ||
let _create; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
_create = axios.create; | ||
}); | ||
afterEach(() => { | ||
axios.create = _create; | ||
}); | ||
it('create axios with slack api url', () => { | ||
axios.create = jest.fn(); | ||
SlackOAuthClient.connect(TOKEN); | ||
expect(axios.create).toBeCalledWith({ | ||
baseURL: 'https://slack.com/api/', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
}); | ||
}); | ||
describe('constructor', () => { | ||
let axios; | ||
let _create; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
_create = axios.create; | ||
}); | ||
afterEach(() => { | ||
axios.create = _create; | ||
}); | ||
it('create axios with with slack api url', () => { | ||
axios.create = jest.fn(); | ||
new SlackOAuthClient(TOKEN); // eslint-disable-line no-new | ||
expect(axios.create).toBeCalledWith({ | ||
baseURL: 'https://slack.com/api/', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
}); | ||
}); | ||
describe('#axios', () => { | ||
it('should return underlying http client', () => { | ||
const client = new SlackOAuthClient(TOKEN); | ||
const http = client.axios; | ||
expect(http.get).toBeDefined(); | ||
expect(http.post).toBeDefined(); | ||
expect(http.put).toBeDefined(); | ||
expect(http.delete).toBeDefined(); | ||
}); | ||
}); | ||
describe('#accessToken', () => { | ||
it('should return underlying access token', () => { | ||
const client = new SlackOAuthClient(TOKEN); | ||
expect(client.accessToken).toBe(TOKEN); | ||
}); | ||
}); | ||
describe('#callMethod', () => { | ||
@@ -83,0 +19,0 @@ it('should call slack api', async () => { |
@@ -13,58 +13,2 @@ import MockAdapter from 'axios-mock-adapter'; | ||
describe('connect', () => { | ||
let axios; | ||
let _create; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
_create = axios.create; | ||
}); | ||
afterEach(() => { | ||
axios.create = _create; | ||
}); | ||
it('create axios with webhook url', () => { | ||
axios.create = jest.fn(); | ||
SlackWebhookClient.connect(URL); | ||
expect(axios.create).toBeCalledWith({ | ||
baseURL: 'https://hooks.slack.com/services/XXXXXXXX/YYYYYYYY/zzzzzZZZZZ', | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
}); | ||
}); | ||
describe('constructor', () => { | ||
let axios; | ||
let _create; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
_create = axios.create; | ||
}); | ||
afterEach(() => { | ||
axios.create = _create; | ||
}); | ||
it('create axios with with webhook url', () => { | ||
axios.create = jest.fn(); | ||
new SlackWebhookClient(URL); // eslint-disable-line no-new | ||
expect(axios.create).toBeCalledWith({ | ||
baseURL: 'https://hooks.slack.com/services/XXXXXXXX/YYYYYYYY/zzzzzZZZZZ', | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
}); | ||
}); | ||
describe('#axios', () => { | ||
it('should return underlying http client', () => { | ||
const client = new SlackWebhookClient(URL); | ||
expect(client.axios.get).toBeDefined(); | ||
expect(client.axios.post).toBeDefined(); | ||
expect(client.axios.put).toBeDefined(); | ||
expect(client.axios.delete).toBeDefined(); | ||
}); | ||
}); | ||
describe('sendRawBody', () => { | ||
@@ -71,0 +15,0 @@ it('should call messages api', async () => { |
@@ -24,4 +24,2 @@ /* @flow */ | ||
type Token = string; | ||
type PostMessageOptions = { | ||
@@ -63,15 +61,32 @@ as_user?: boolean, | ||
type ClientConfig = { | ||
accessToken: string, | ||
origin?: string, | ||
}; | ||
export default class SlackOAuthClient { | ||
static connect = (token: Token): SlackOAuthClient => | ||
new SlackOAuthClient(token); | ||
static connect = ( | ||
accessTokenOrConfig: string | ClientConfig | ||
): SlackOAuthClient => new SlackOAuthClient(accessTokenOrConfig); | ||
_axios: Axios; | ||
_token: Token; | ||
_token: string; | ||
constructor(token: Token) { | ||
constructor(accessTokenOrConfig: string | ClientConfig) { | ||
let origin; | ||
if (accessTokenOrConfig && typeof accessTokenOrConfig === 'object') { | ||
const config = accessTokenOrConfig; | ||
this._token = config.accessToken; | ||
origin = config.origin; | ||
} else { | ||
// Bot User OAuth Access Token | ||
this._token = accessTokenOrConfig; | ||
} | ||
// Web API | ||
// https://api.slack.com/web | ||
this._axios = axios.create({ | ||
baseURL: 'https://slack.com/api/', | ||
baseURL: `${origin || 'https://slack.com'}/api/`, | ||
headers: { | ||
@@ -81,4 +96,2 @@ 'Content-Type': 'application/x-www-form-urlencoded', | ||
}); | ||
// Bot User OAuth Access Token | ||
this._token = token; | ||
} | ||
@@ -90,3 +103,3 @@ | ||
get accessToken(): Token { | ||
get accessToken(): string { | ||
return this._token; | ||
@@ -93,0 +106,0 @@ } |
92082
15
2522
512