messaging-api-slack
Advanced tools
Comparing version 1.0.0-beta.29 to 1.0.0-beta.33
@@ -7,3 +7,3 @@ import { AxiosInstance } from 'axios'; | ||
*/ | ||
static connect(accessTokenOrConfig: string | Types.ClientConfig): SlackOAuthClient; | ||
static connect(config: Types.ClientConfig): SlackOAuthClient; | ||
/** | ||
@@ -47,3 +47,3 @@ * The underlying axios instance. | ||
private onRequest?; | ||
constructor(accessTokenOrConfig: string | Types.ClientConfig); | ||
constructor(config: Types.ClientConfig); | ||
callMethod(method: Types.AvailableMethod, inputBody?: Record<string, any>): Promise<Types.OAuthAPIResponse>; | ||
@@ -50,0 +50,0 @@ /** |
@@ -18,2 +18,3 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const ts_invariant_1 = __importDefault(require("ts-invariant")); | ||
const omit_1 = __importDefault(require("lodash/omit")); | ||
@@ -33,18 +34,10 @@ const warning_1 = __importDefault(require("warning")); | ||
class SlackOAuthClient { | ||
constructor(accessTokenOrConfig) { | ||
let origin; | ||
if (typeof accessTokenOrConfig === 'string') { | ||
// Bot User OAuth Access Token | ||
this.accessToken = accessTokenOrConfig; | ||
} | ||
else { | ||
const config = accessTokenOrConfig; | ||
this.accessToken = config.accessToken; | ||
this.onRequest = config.onRequest; | ||
origin = config.origin; | ||
} | ||
constructor(config) { | ||
ts_invariant_1.default(typeof config !== 'string', `SlackOAuthClient: do not allow constructing client with ${config} string. Use object instead.`); | ||
this.accessToken = config.accessToken; | ||
this.onRequest = config.onRequest; | ||
// Web API | ||
// https://api.slack.com/web | ||
this.axios = axios_1.default.create({ | ||
baseURL: `${origin || 'https://slack.com'}/api/`, | ||
baseURL: `${config.origin || 'https://slack.com'}/api/`, | ||
headers: { | ||
@@ -79,5 +72,5 @@ 'Content-Type': 'application/x-www-form-urlencoded', | ||
*/ | ||
static connect(accessTokenOrConfig) { | ||
static connect(config) { | ||
warning_1.default(false, '`SlackOAuthClient.connect(...)` is deprecated. Use `new SlackOAuthClient(...)` instead.'); | ||
return new SlackOAuthClient(accessTokenOrConfig); | ||
return new SlackOAuthClient(config); | ||
} | ||
@@ -84,0 +77,0 @@ callMethod(method, inputBody = {}) { |
@@ -12,3 +12,3 @@ import { AxiosInstance } from 'axios'; | ||
*/ | ||
static connect(urlOrConfig: string | ClientConfig): SlackWebhookClient; | ||
static connect(config: ClientConfig): SlackWebhookClient; | ||
/** | ||
@@ -22,3 +22,3 @@ * The underlying axios instance. | ||
private onRequest?; | ||
constructor(urlOrConfig: string | ClientConfig); | ||
constructor(config: ClientConfig); | ||
sendRawBody(body: Record<string, any>): Promise<Types.SendMessageSuccessResponse>; | ||
@@ -25,0 +25,0 @@ sendText(text: string): Promise<Types.SendMessageSuccessResponse>; |
@@ -7,19 +7,13 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const ts_invariant_1 = __importDefault(require("ts-invariant")); | ||
const warning_1 = __importDefault(require("warning")); | ||
const messaging_api_common_1 = require("messaging-api-common"); | ||
class SlackWebhookClient { | ||
constructor(urlOrConfig) { | ||
let url; | ||
if (urlOrConfig && typeof urlOrConfig === 'object') { | ||
const config = urlOrConfig; | ||
url = config.url; | ||
this.onRequest = config.onRequest; | ||
} | ||
else { | ||
url = urlOrConfig; | ||
} | ||
constructor(config) { | ||
ts_invariant_1.default(typeof config !== 'string', `SlackWebhookClient: do not allow constructing client with ${config} string. Use object instead.`); | ||
this.onRequest = config.onRequest; | ||
// incoming webhooks | ||
// https://api.slack.com/incoming-webhooks | ||
this.axios = axios_1.default.create({ | ||
baseURL: url, | ||
baseURL: config.url, | ||
headers: { 'Content-Type': 'application/json' }, | ||
@@ -32,5 +26,5 @@ }); | ||
*/ | ||
static connect(urlOrConfig) { | ||
static connect(config) { | ||
warning_1.default(false, '`SlackWebhookClient.connect(...)` is deprecated. Use `new SlackWebhookClient(...)` instead.'); | ||
return new SlackWebhookClient(urlOrConfig); | ||
return new SlackWebhookClient(config); | ||
} | ||
@@ -37,0 +31,0 @@ sendRawBody(body) { |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "1.0.0-beta.29", | ||
"version": "1.0.0-beta.33", | ||
"main": "dist/index.js", | ||
@@ -19,3 +19,4 @@ "types": "dist/index.d.ts", | ||
"lodash": "^4.17.15", | ||
"messaging-api-common": "^1.0.0-beta.29" | ||
"messaging-api-common": "^1.0.0-beta.29", | ||
"ts-invariant": "^0.4.4" | ||
}, | ||
@@ -31,3 +32,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "3389b14efbdcaf968ed7476820edaaaa95da0772" | ||
"gitHead": "a3f7afb4cef3784c3459f6be0810e0518b1bb384" | ||
} |
@@ -20,18 +20,2 @@ import MockAdapter from 'axios-mock-adapter'; | ||
describe('create axios with slack api url', () => { | ||
it('with args', () => { | ||
axios.create = jest.fn().mockReturnValue({ | ||
interceptors: { | ||
request: { | ||
use: jest.fn(), | ||
}, | ||
}, | ||
}); | ||
SlackOAuthClient.connect(TOKEN); | ||
expect(axios.create).toBeCalledWith({ | ||
baseURL: 'https://slack.com/api/', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
}); | ||
it('with config', () => { | ||
@@ -87,18 +71,2 @@ axios.create = jest.fn().mockReturnValue({ | ||
describe('create axios with with slack api url', () => { | ||
it('with args', () => { | ||
axios.create = jest.fn().mockReturnValue({ | ||
interceptors: { | ||
request: { | ||
use: 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' }, | ||
}); | ||
}); | ||
it('with config', () => { | ||
@@ -144,9 +112,4 @@ axios.create = jest.fn().mockReturnValue({ | ||
it('should return underlying http client', () => { | ||
let client = new SlackOAuthClient(TOKEN); | ||
expect(client.axios.get).toBeDefined(); | ||
expect(client.axios.post).toBeDefined(); | ||
expect(client.axios.put).toBeDefined(); | ||
expect(client.axios.delete).toBeDefined(); | ||
const client = new SlackOAuthClient({ accessToken: TOKEN }); | ||
client = new SlackOAuthClient({ accessToken: TOKEN }); | ||
expect(client.axios.get).toBeDefined(); | ||
@@ -161,6 +124,4 @@ expect(client.axios.post).toBeDefined(); | ||
it('should return underlying access token', () => { | ||
let client = new SlackOAuthClient(TOKEN); | ||
expect(client.accessToken).toBe(TOKEN); | ||
const client = new SlackOAuthClient({ accessToken: TOKEN }); | ||
client = new SlackOAuthClient({ accessToken: TOKEN }); | ||
expect(client.accessToken).toBe(TOKEN); | ||
@@ -167,0 +128,0 @@ }); |
@@ -20,21 +20,2 @@ import MockAdapter from 'axios-mock-adapter'; | ||
describe('create axios with webhook url', () => { | ||
it('with args', () => { | ||
axios.create = jest.fn().mockReturnValue({ | ||
interceptors: { | ||
request: { | ||
use: jest.fn(), | ||
}, | ||
}, | ||
}); | ||
SlackWebhookClient.connect({ | ||
url: URL, | ||
}); | ||
expect(axios.create).toBeCalledWith({ | ||
baseURL: | ||
'https://hooks.slack.com/services/XXXXXXXX/YYYYYYYY/zzzzzZZZZZ', | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
}); | ||
it('with config', () => { | ||
@@ -48,3 +29,3 @@ axios.create = jest.fn().mockReturnValue({ | ||
}); | ||
SlackWebhookClient.connect(URL); | ||
SlackWebhookClient.connect({ url: URL }); | ||
@@ -73,19 +54,2 @@ expect(axios.create).toBeCalledWith({ | ||
describe('create axios with with webhook url', () => { | ||
it('with args', () => { | ||
axios.create = jest.fn().mockReturnValue({ | ||
interceptors: { | ||
request: { | ||
use: 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' }, | ||
}); | ||
}); | ||
it('with config', () => { | ||
@@ -115,3 +79,4 @@ axios.create = jest.fn().mockReturnValue({ | ||
it('should return underlying http client', () => { | ||
const client = new SlackWebhookClient(URL); | ||
const client = new SlackWebhookClient({ url: URL }); | ||
expect(client.axios.get).toBeDefined(); | ||
@@ -118,0 +83,0 @@ expect(client.axios.post).toBeDefined(); |
@@ -8,3 +8,5 @@ import MockAdapter from 'axios-mock-adapter'; | ||
const createMock = (): { client: SlackWebhookClient; mock: MockAdapter } => { | ||
const client = new SlackWebhookClient(URL); | ||
const client = new SlackWebhookClient({ | ||
url: URL, | ||
}); | ||
const mock = new MockAdapter(client.axios); | ||
@@ -11,0 +13,0 @@ return { client, mock }; |
@@ -5,2 +5,3 @@ import querystring from 'querystring'; | ||
import axios, { AxiosInstance } from 'axios'; | ||
import invariant from 'ts-invariant'; | ||
import omit from 'lodash/omit'; | ||
@@ -37,5 +38,3 @@ import warning from 'warning'; | ||
*/ | ||
static connect( | ||
accessTokenOrConfig: string | Types.ClientConfig | ||
): SlackOAuthClient { | ||
static connect(config: Types.ClientConfig): SlackOAuthClient { | ||
warning( | ||
@@ -45,3 +44,3 @@ false, | ||
); | ||
return new SlackOAuthClient(accessTokenOrConfig); | ||
return new SlackOAuthClient(config); | ||
} | ||
@@ -114,20 +113,15 @@ | ||
constructor(accessTokenOrConfig: string | Types.ClientConfig) { | ||
let origin; | ||
constructor(config: Types.ClientConfig) { | ||
invariant( | ||
typeof config !== 'string', | ||
`SlackOAuthClient: do not allow constructing client with ${config} string. Use object instead.` | ||
); | ||
if (typeof accessTokenOrConfig === 'string') { | ||
// Bot User OAuth Access Token | ||
this.accessToken = accessTokenOrConfig; | ||
} else { | ||
const config = accessTokenOrConfig; | ||
this.accessToken = config.accessToken; | ||
this.onRequest = config.onRequest; | ||
this.accessToken = config.accessToken; | ||
this.onRequest = config.onRequest; | ||
origin = config.origin; | ||
} | ||
// Web API | ||
// https://api.slack.com/web | ||
this.axios = axios.create({ | ||
baseURL: `${origin || 'https://slack.com'}/api/`, | ||
baseURL: `${config.origin || 'https://slack.com'}/api/`, | ||
headers: { | ||
@@ -134,0 +128,0 @@ 'Content-Type': 'application/x-www-form-urlencoded', |
import axios, { AxiosInstance } from 'axios'; | ||
import invariant from 'ts-invariant'; | ||
import warning from 'warning'; | ||
@@ -20,3 +21,3 @@ import { | ||
*/ | ||
static connect(urlOrConfig: string | ClientConfig): SlackWebhookClient { | ||
static connect(config: ClientConfig): SlackWebhookClient { | ||
warning( | ||
@@ -26,3 +27,3 @@ false, | ||
); | ||
return new SlackWebhookClient(urlOrConfig); | ||
return new SlackWebhookClient(config); | ||
} | ||
@@ -40,18 +41,14 @@ | ||
constructor(urlOrConfig: string | ClientConfig) { | ||
let url; | ||
constructor(config: ClientConfig) { | ||
invariant( | ||
typeof config !== 'string', | ||
`SlackWebhookClient: do not allow constructing client with ${config} string. Use object instead.` | ||
); | ||
if (urlOrConfig && typeof urlOrConfig === 'object') { | ||
const config = urlOrConfig; | ||
this.onRequest = config.onRequest; | ||
url = config.url; | ||
this.onRequest = config.onRequest; | ||
} else { | ||
url = urlOrConfig; | ||
} | ||
// incoming webhooks | ||
// https://api.slack.com/incoming-webhooks | ||
this.axios = axios.create({ | ||
baseURL: url, | ||
baseURL: config.url, | ||
headers: { 'Content-Type': 'application/json' }, | ||
@@ -58,0 +55,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
324708
7
5270
+ Addedts-invariant@^0.4.4
+ Addedts-invariant@0.4.4(transitive)
+ Addedtslib@1.14.1(transitive)