messaging-api-messenger
Advanced tools
Comparing version 1.0.0-beta.27 to 1.0.0-beta.29
import FormData from 'form-data'; | ||
import { AxiosInstance } from 'axios'; | ||
import { OnRequestFunction } from 'messaging-api-common'; | ||
import * as Types from './MessengerTypes'; | ||
@@ -10,13 +9,27 @@ export default class MessengerClient { | ||
static connect(accessTokenOrConfig: string | Types.ClientConfig, version?: string): MessengerClient; | ||
_onRequest: OnRequestFunction | undefined; | ||
_axios: AxiosInstance; | ||
_accessToken: string; | ||
_appId?: string; | ||
_appSecret?: string; | ||
_version: string; | ||
/** | ||
* The underlying axios instance. | ||
*/ | ||
readonly axios: AxiosInstance; | ||
/** | ||
* The version of the Facebook Graph API. | ||
*/ | ||
readonly version: string; | ||
/** | ||
* The access token used by the client. | ||
*/ | ||
readonly accessToken: string; | ||
/** | ||
* The app secret used by the client. | ||
*/ | ||
readonly appSecret?: string; | ||
/** | ||
* The app ID used by the client. | ||
*/ | ||
private appId?; | ||
/** | ||
* The callback to be called when receiving requests. | ||
*/ | ||
private onRequest?; | ||
constructor(accessTokenOrConfig: string | Types.ClientConfig, version?: string); | ||
get version(): string; | ||
get axios(): AxiosInstance; | ||
get accessToken(): string; | ||
get appSecret(): string | undefined; | ||
/** | ||
@@ -23,0 +36,0 @@ * Get Page Info |
@@ -35,3 +35,3 @@ "use strict"; | ||
const get_1 = __importDefault(require("lodash/get")); | ||
const invariant_1 = __importDefault(require("invariant")); | ||
const ts_invariant_1 = __importDefault(require("ts-invariant")); | ||
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject")); | ||
@@ -62,8 +62,8 @@ const omit_1 = __importDefault(require("lodash/omit")); | ||
const config = accessTokenOrConfig; | ||
this._accessToken = config.accessToken; | ||
invariant_1.default(!config.version || typeof config.version === 'string', 'Type of `version` must be string.'); | ||
this._appId = config.appId; | ||
this._appSecret = config.appSecret; | ||
this._version = extractVersion(config.version || '6.0'); | ||
this._onRequest = config.onRequest; | ||
this.accessToken = config.accessToken; | ||
ts_invariant_1.default(!config.version || typeof config.version === 'string', 'Type of `version` must be string.'); | ||
this.appId = config.appId; | ||
this.appSecret = config.appSecret; | ||
this.version = extractVersion(config.version || '6.0'); | ||
this.onRequest = config.onRequest; | ||
origin = config.origin; | ||
@@ -74,13 +74,13 @@ if (typeof config.skipAppSecretProof === 'boolean') { | ||
else { | ||
skipAppSecretProof = this._appSecret == null; | ||
skipAppSecretProof = this.appSecret == null; | ||
} | ||
} | ||
else { | ||
this._accessToken = accessTokenOrConfig; | ||
invariant_1.default(typeof version === 'string', 'Type of `version` must be string.'); | ||
this._version = extractVersion(version); | ||
this.accessToken = accessTokenOrConfig; | ||
ts_invariant_1.default(typeof version === 'string', 'Type of `version` must be string.'); | ||
this.version = extractVersion(version); | ||
skipAppSecretProof = true; | ||
} | ||
this._axios = axios_1.default.create({ | ||
baseURL: `${origin || 'https://graph.facebook.com'}/v${this._version}/`, | ||
this.axios = axios_1.default.create({ | ||
baseURL: `${origin || 'https://graph.facebook.com'}/v${this.version}/`, | ||
headers: { 'Content-Type': 'application/json' }, | ||
@@ -102,8 +102,8 @@ transformRequest: [ | ||
}); | ||
this._axios.interceptors.request.use(messaging_api_common_1.createRequestInterceptor({ onRequest: this._onRequest })); | ||
this.axios.interceptors.request.use(messaging_api_common_1.createRequestInterceptor({ onRequest: this.onRequest })); | ||
// add appsecret_proof to request | ||
if (!skipAppSecretProof) { | ||
invariant_1.default(this._appSecret, 'Must provide appSecret when skipAppSecretProof is false'); | ||
const appSecret = this._appSecret; | ||
this._axios.interceptors.request.use((config) => { | ||
ts_invariant_1.default(this.appSecret, 'Must provide appSecret when skipAppSecretProof is false'); | ||
const appSecret = this.appSecret; | ||
this.axios.interceptors.request.use((config) => { | ||
const isBatch = config.url === '/' && Array.isArray(config.data.batch); | ||
@@ -137,3 +137,3 @@ if (isBatch) { | ||
const urlParts = url_1.default.parse(config.url || '', true); | ||
const accessToken = get_1.default(urlParts, 'query.access_token', this._accessToken); | ||
const accessToken = get_1.default(urlParts, 'query.access_token', this.accessToken); | ||
const appSecretProof = crypto_1.default | ||
@@ -158,14 +158,2 @@ .createHmac('sha256', appSecret) | ||
} | ||
get version() { | ||
return this._version; | ||
} | ||
get axios() { | ||
return this._axios; | ||
} | ||
get accessToken() { | ||
return this._accessToken; | ||
} | ||
get appSecret() { | ||
return this._appSecret; | ||
} | ||
/** | ||
@@ -178,4 +166,4 @@ * Get Page Info | ||
getPageInfo({ accessToken: customAccessToken, } = {}) { | ||
return this._axios | ||
.get(`/me?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -189,9 +177,9 @@ } | ||
debugToken({ accessToken: customAccessToken, } = {}) { | ||
invariant_1.default(this._appId, 'App ID is required to debug token'); | ||
invariant_1.default(this._appSecret, 'App Secret is required to debug token'); | ||
const accessToken = `${this._appId}|${this._appSecret}`; | ||
return this._axios | ||
ts_invariant_1.default(this.appId, 'App ID is required to debug token'); | ||
ts_invariant_1.default(this.appSecret, 'App Secret is required to debug token'); | ||
const accessToken = `${this.appId}|${this.appSecret}`; | ||
return this.axios | ||
.get(`/debug_token`, { | ||
params: { | ||
input_token: customAccessToken || this._accessToken, | ||
input_token: customAccessToken || this.accessToken, | ||
access_token: accessToken, | ||
@@ -215,7 +203,7 @@ }, | ||
], includeValues, verifyToken, accessToken: appAccessToken, }) { | ||
const appId = this._appId; | ||
invariant_1.default(appId, 'App ID is required to create subscription'); | ||
invariant_1.default(this._appSecret || appAccessToken, 'App Secret or App Token is required to create subscription'); | ||
const accessToken = appAccessToken || `${appId}|${this._appSecret}`; | ||
return this._axios | ||
const { appId } = this; | ||
ts_invariant_1.default(appId, 'App ID is required to create subscription'); | ||
ts_invariant_1.default(this.appSecret || appAccessToken, 'App Secret or App Token is required to create subscription'); | ||
const accessToken = appAccessToken || `${appId}|${this.appSecret}`; | ||
return this.axios | ||
.post(`/${appId}/subscriptions?access_token=${accessToken}`, { | ||
@@ -236,7 +224,7 @@ object, | ||
getSubscriptions({ accessToken: appAccessToken, } = {}) { | ||
const appId = this._appId; | ||
invariant_1.default(appId, 'App ID is required to get subscriptions'); | ||
invariant_1.default(this._appSecret || appAccessToken, 'App Secret or App Token is required to get subscriptions'); | ||
const accessToken = appAccessToken || `${appId}|${this._appSecret}`; | ||
return this._axios | ||
const { appId } = this; | ||
ts_invariant_1.default(appId, 'App ID is required to get subscriptions'); | ||
ts_invariant_1.default(this.appSecret || appAccessToken, 'App Secret or App Token is required to get subscriptions'); | ||
const accessToken = appAccessToken || `${appId}|${this.appSecret}`; | ||
return this.axios | ||
.get(`/${appId}/subscriptions?access_token=${accessToken}`) | ||
@@ -251,6 +239,6 @@ .then((res) => res.data.data, handleError); | ||
getPageSubscription({ accessToken: appAccessToken, } = {}) { | ||
const appId = this._appId; | ||
invariant_1.default(appId, 'App ID is required to get subscription'); | ||
invariant_1.default(this._appSecret || appAccessToken, 'App Secret or App Token is required to get subscription'); | ||
const accessToken = appAccessToken || `${appId}|${this._appSecret}`; | ||
const { appId } = this; | ||
ts_invariant_1.default(appId, 'App ID is required to get subscription'); | ||
ts_invariant_1.default(this.appSecret || appAccessToken, 'App Secret or App Token is required to get subscription'); | ||
const accessToken = appAccessToken || `${appId}|${this.appSecret}`; | ||
return this.getSubscriptions({ | ||
@@ -266,4 +254,4 @@ accessToken, | ||
getMessagingFeatureReview({ accessToken: customAccessToken, } = {}) { | ||
return this._axios | ||
.get(`/me/messaging_feature_review?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me/messaging_feature_review?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data.data, handleError); | ||
@@ -278,4 +266,4 @@ } | ||
getUserProfile(userId, { fields = ['id', 'name', 'first_name', 'last_name', 'profile_pic'], accessToken: customAccessToken, } = {}) { | ||
return this._axios | ||
.get(`/${userId}?fields=${fields.join(',')}&access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/${userId}?fields=${fields.join(',')}&access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -289,14 +277,14 @@ } | ||
getMessengerProfile(fields, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.get(`/me/messenger_profile?fields=${fields.join(',')}&access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me/messenger_profile?fields=${fields.join(',')}&access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data.data, handleError); | ||
} | ||
setMessengerProfile(profile, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/me/messenger_profile?access_token=${customAccessToken || this._accessToken}`, profile) | ||
return this.axios | ||
.post(`/me/messenger_profile?access_token=${customAccessToken || this.accessToken}`, profile) | ||
.then((res) => res.data, handleError); | ||
} | ||
deleteMessengerProfile(fields, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.delete(`/me/messenger_profile?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.delete(`/me/messenger_profile?access_token=${customAccessToken || this.accessToken}`, { | ||
data: { | ||
@@ -364,4 +352,4 @@ fields, | ||
getUserPersistentMenu(userId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.get(`/me/custom_user_settings?psid=${userId}&access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me/custom_user_settings?psid=${userId}&access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data.data[0] | ||
@@ -374,4 +362,4 @@ ? res.data.data[0].userLevelPersistentMenu | ||
if (menuItems.some((item) => 'locale' in item && item.locale === 'default')) { | ||
return this._axios | ||
.post(`/me/custom_user_settings?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/me/custom_user_settings?access_token=${customAccessToken || this.accessToken}`, { | ||
psid: userId, | ||
@@ -383,4 +371,4 @@ persistentMenu: menuItems, | ||
// menuItems is in type MenuItem[] | ||
return this._axios | ||
.post(`/me/custom_user_settings?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/me/custom_user_settings?access_token=${customAccessToken || this.accessToken}`, { | ||
psid: userId, | ||
@@ -398,4 +386,4 @@ persistentMenu: [ | ||
deleteUserPersistentMenu(userId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.delete(`/me/custom_user_settings?psid=${userId}¶ms=[%22persistent_menu%22]&access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.delete(`/me/custom_user_settings?psid=${userId}¶ms=[%22persistent_menu%22]&access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -506,4 +494,4 @@ } | ||
getMessageTags({ accessToken: customAccessToken, } = {}) { | ||
return this._axios | ||
.get(`/page_message_tags?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/page_message_tags?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data.data, handleError); | ||
@@ -518,4 +506,4 @@ } | ||
const { accessToken: customAccessToken } = body; | ||
return this._axios | ||
.post(`/me/messages?access_token=${customAccessToken || this._accessToken}`, body) | ||
return this.axios | ||
.post(`/me/messages?access_token=${customAccessToken || this.accessToken}`, body) | ||
.then((res) => res.data, handleError); | ||
@@ -554,4 +542,4 @@ } | ||
formdata.append('recipient', JSON.stringify(messaging_api_common_1.snakecaseKeysDeep(recipient))); | ||
return this._axios | ||
.post(`/me/messages?access_token=${options.accessToken || this._accessToken}`, formdata, { | ||
return this.axios | ||
.post(`/me/messages?access_token=${options.accessToken || this.accessToken}`, formdata, { | ||
headers: formdata.getHeaders(), | ||
@@ -680,3 +668,3 @@ }) | ||
sendBatch(batch, { accessToken: customAccessToken } = {}) { | ||
invariant_1.default(batch.length <= 50, 'limit the number of requests which can be in a batch to 50'); | ||
ts_invariant_1.default(batch.length <= 50, 'limit the number of requests which can be in a batch to 50'); | ||
const responseAccessPaths = batch.map((item) => item.responseAccessPath); | ||
@@ -697,5 +685,5 @@ const bodyEncodedbatch = batch | ||
}); | ||
return this._axios | ||
return this.axios | ||
.post('/', { | ||
accessToken: customAccessToken || this._accessToken, | ||
accessToken: customAccessToken || this.accessToken, | ||
batch: bodyEncodedbatch, | ||
@@ -726,4 +714,4 @@ }) | ||
createLabel(name, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/me/custom_labels?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/me/custom_labels?access_token=${customAccessToken || this.accessToken}`, { | ||
name, | ||
@@ -739,4 +727,4 @@ }) | ||
associateLabel(userId, labelId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/${labelId}/label?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/${labelId}/label?access_token=${customAccessToken || this.accessToken}`, { | ||
user: userId, | ||
@@ -752,4 +740,4 @@ }) | ||
dissociateLabel(userId, labelId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.delete(`/${labelId}/label?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.delete(`/${labelId}/label?access_token=${customAccessToken || this.accessToken}`, { | ||
data: { user: userId }, | ||
@@ -766,4 +754,4 @@ }) | ||
const fields = options.fields ? options.fields.join(',') : 'name'; | ||
return this._axios | ||
.get(`/${userId}/custom_labels?fields=${fields}&access_token=${options.accessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/${userId}/custom_labels?fields=${fields}&access_token=${options.accessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -778,4 +766,4 @@ } | ||
const fields = options.fields ? options.fields.join(',') : 'name'; | ||
return this._axios | ||
.get(`/${labelId}?fields=${fields}&access_token=${options.accessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/${labelId}?fields=${fields}&access_token=${options.accessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -790,4 +778,4 @@ } | ||
const fields = options.fields ? options.fields.join(',') : 'name'; | ||
return this._axios | ||
.get(`/me/custom_labels?fields=${fields}&access_token=${options.accessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me/custom_labels?fields=${fields}&access_token=${options.accessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -801,4 +789,4 @@ } | ||
deleteLabel(labelId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.delete(`/${labelId}?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.delete(`/${labelId}?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -844,4 +832,4 @@ } | ||
} | ||
return this._axios | ||
.post(`/me/message_attachments?access_token=${options.accessToken || this._accessToken}`, ...args) | ||
return this.axios | ||
.post(`/me/message_attachments?access_token=${options.accessToken || this.accessToken}`, ...args) | ||
.then((res) => res.data, handleError); | ||
@@ -876,4 +864,4 @@ } | ||
passThreadControl(recipientId, targetAppId, metadata, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/me/pass_thread_control?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/me/pass_thread_control?access_token=${customAccessToken || this.accessToken}`, { | ||
recipient: { id: recipientId }, | ||
@@ -894,4 +882,4 @@ targetAppId, | ||
takeThreadControl(recipientId, metadata, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/me/take_thread_control?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/me/take_thread_control?access_token=${customAccessToken || this.accessToken}`, { | ||
recipient: { id: recipientId }, | ||
@@ -908,4 +896,4 @@ metadata, | ||
requestThreadControl(recipientId, metadata, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/me/request_thread_control?access_token=${customAccessToken || this._accessToken}`, { | ||
return this.axios | ||
.post(`/me/request_thread_control?access_token=${customAccessToken || this.accessToken}`, { | ||
recipient: { id: recipientId }, | ||
@@ -922,4 +910,4 @@ metadata, | ||
getSecondaryReceivers({ accessToken: customAccessToken, } = {}) { | ||
return this._axios | ||
.get(`/me/secondary_receivers?fields=id,name&access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me/secondary_receivers?fields=id,name&access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data.data, handleError); | ||
@@ -933,4 +921,4 @@ } | ||
getThreadOwner(recipientId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.get(`/me/thread_owner?recipient=${recipientId}&access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me/thread_owner?recipient=${recipientId}&access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data.data[0].threadOwner, handleError); | ||
@@ -944,4 +932,4 @@ } | ||
getInsights(metrics, options = {}) { | ||
return this._axios | ||
.get(`/me/insights/?${querystring_1.default.stringify(Object.assign({ metric: metrics.join(','), access_token: options.accessToken || this._accessToken }, options))}`) | ||
return this.axios | ||
.get(`/me/insights/?${querystring_1.default.stringify(Object.assign({ metric: metrics.join(','), access_token: options.accessToken || this.accessToken }, options))}`) | ||
.then((res) => res.data.data, handleError); | ||
@@ -970,5 +958,5 @@ } | ||
setNLPConfigs(config = {}, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
return this.axios | ||
.post(`/me/nlp_configs?${querystring_1.default.stringify(messaging_api_common_1.snakecaseKeysDeep(config))}`, { | ||
accessToken: customAccessToken || this._accessToken, | ||
accessToken: customAccessToken || this.accessToken, | ||
}) | ||
@@ -992,3 +980,3 @@ .then((res) => res.data, handleError); | ||
logCustomEvents({ appId, pageId, pageScopedUserId, events, }) { | ||
return this._axios | ||
return this.axios | ||
.post(`/${appId}/activities`, { | ||
@@ -1010,3 +998,3 @@ event: 'CUSTOM_APP_EVENTS', | ||
getUserField({ field, userId, appSecret, app, page, accessToken: customAccessToken, }) { | ||
const accessToken = customAccessToken || this._accessToken; | ||
const accessToken = customAccessToken || this.accessToken; | ||
// $appsecret_proof= hash_hmac('sha256', $access_token, $app_secret); | ||
@@ -1019,3 +1007,3 @@ const appsecretProof = crypto_1.default | ||
const pageQueryString = page ? `&page=${page}` : ''; | ||
return this._axios | ||
return this.axios | ||
.get(`/${userId}/${field}?access_token=${accessToken}&appsecret_proof=${appsecretProof}${appQueryString}${pageQueryString}`) | ||
@@ -1063,4 +1051,4 @@ .then((res) => res.data, handleError); | ||
createPersona(persona, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.post(`/me/personas?access_token=${customAccessToken || this._accessToken}`, persona) | ||
return this.axios | ||
.post(`/me/personas?access_token=${customAccessToken || this.accessToken}`, persona) | ||
.then((res) => res.data, handleError); | ||
@@ -1074,4 +1062,4 @@ } | ||
getPersona(personaId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.get(`/${personaId}?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/${personaId}?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -1085,4 +1073,4 @@ } | ||
getPersonas(cursor, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.get(`/me/personas?access_token=${customAccessToken || this._accessToken}${cursor ? `&after=${cursor}` : ''}`) | ||
return this.axios | ||
.get(`/me/personas?access_token=${customAccessToken || this.accessToken}${cursor ? `&after=${cursor}` : ''}`) | ||
.then((res) => res.data, handleError); | ||
@@ -1095,3 +1083,2 @@ } | ||
do { | ||
// eslint-disable-next-line no-await-in-loop | ||
const { data, paging, } = yield this.getPersonas(cursor, { | ||
@@ -1112,4 +1099,4 @@ accessToken: customAccessToken, | ||
deletePersona(personaId, { accessToken: customAccessToken } = {}) { | ||
return this._axios | ||
.delete(`/${personaId}?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.delete(`/${personaId}?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -1116,0 +1103,0 @@ } |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "1.0.0-beta.27", | ||
"version": "1.0.0-beta.29", | ||
"main": "dist/index.js", | ||
@@ -15,2 +15,5 @@ "browser": "lib/browser.js", | ||
"dependencies": { | ||
"@types/append-query": "^2.0.0", | ||
"@types/lodash": "^4.14.156", | ||
"@types/warning": "^3.0.0", | ||
"append-query": "^2.1.0", | ||
@@ -20,5 +23,5 @@ "axios": "^0.19.2", | ||
"form-data": "^3.0.0", | ||
"invariant": "^2.2.4", | ||
"lodash": "^4.17.15", | ||
"messaging-api-common": "^1.0.0-beta.26", | ||
"messaging-api-common": "^1.0.0-beta.29", | ||
"ts-invariant": "^0.4.4", | ||
"warning": "^4.0.3" | ||
@@ -35,3 +38,3 @@ }, | ||
}, | ||
"gitHead": "38107f34a63c32cf0a1c7994c4c39a2d6034daa4" | ||
"gitHead": "3389b14efbdcaf968ed7476820edaaaa95da0772" | ||
} |
@@ -12,3 +12,3 @@ import MockAdapter from 'axios-mock-adapter'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -15,0 +15,0 @@ }); |
@@ -11,3 +11,3 @@ import MockAdapter from 'axios-mock-adapter'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -14,0 +14,0 @@ }); |
@@ -10,3 +10,3 @@ import MockAdapter from 'axios-mock-adapter'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -13,0 +13,0 @@ }); |
@@ -10,3 +10,3 @@ import MockAdapter from 'axios-mock-adapter'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -13,0 +13,0 @@ }); |
@@ -11,3 +11,3 @@ import MockAdapter from 'axios-mock-adapter'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -14,0 +14,0 @@ }); |
@@ -16,3 +16,3 @@ import fs from 'fs'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -19,0 +19,0 @@ }); |
@@ -11,3 +11,3 @@ import MockAdapter from 'axios-mock-adapter'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -14,0 +14,0 @@ }); |
@@ -17,3 +17,3 @@ import fs from 'fs'; | ||
beforeEach(() => { | ||
axios = require('axios'); // eslint-disable-line global-require | ||
axios = require('axios'); | ||
_create = axios.create; | ||
@@ -20,0 +20,0 @@ }); |
@@ -15,3 +15,3 @@ import crypto from 'crypto'; | ||
import get from 'lodash/get'; | ||
import invariant from 'invariant'; | ||
import invariant from 'ts-invariant'; | ||
import isPlainObject from 'lodash/isPlainObject'; | ||
@@ -69,13 +69,31 @@ import omit from 'lodash/omit'; | ||
_onRequest: OnRequestFunction | undefined; | ||
/** | ||
* The underlying axios instance. | ||
*/ | ||
readonly axios: AxiosInstance; | ||
_axios: AxiosInstance; | ||
/** | ||
* The version of the Facebook Graph API. | ||
*/ | ||
readonly version: string; | ||
_accessToken: string; | ||
/** | ||
* The access token used by the client. | ||
*/ | ||
readonly accessToken: string; | ||
_appId?: string; | ||
/** | ||
* The app secret used by the client. | ||
*/ | ||
readonly appSecret?: string; | ||
_appSecret?: string; | ||
/** | ||
* The app ID used by the client. | ||
*/ | ||
private appId?: string; | ||
_version: string; | ||
/** | ||
* The callback to be called when receiving requests. | ||
*/ | ||
private onRequest?: OnRequestFunction; | ||
@@ -91,3 +109,3 @@ constructor( | ||
this._accessToken = config.accessToken; | ||
this.accessToken = config.accessToken; | ||
invariant( | ||
@@ -98,6 +116,6 @@ !config.version || typeof config.version === 'string', | ||
this._appId = config.appId; | ||
this._appSecret = config.appSecret; | ||
this._version = extractVersion(config.version || '6.0'); | ||
this._onRequest = config.onRequest; | ||
this.appId = config.appId; | ||
this.appSecret = config.appSecret; | ||
this.version = extractVersion(config.version || '6.0'); | ||
this.onRequest = config.onRequest; | ||
origin = config.origin; | ||
@@ -108,6 +126,6 @@ | ||
} else { | ||
skipAppSecretProof = this._appSecret == null; | ||
skipAppSecretProof = this.appSecret == null; | ||
} | ||
} else { | ||
this._accessToken = accessTokenOrConfig; | ||
this.accessToken = accessTokenOrConfig; | ||
invariant( | ||
@@ -118,3 +136,3 @@ typeof version === 'string', | ||
this._version = extractVersion(version); | ||
this.version = extractVersion(version); | ||
@@ -124,4 +142,4 @@ skipAppSecretProof = true; | ||
this._axios = axios.create({ | ||
baseURL: `${origin || 'https://graph.facebook.com'}/v${this._version}/`, | ||
this.axios = axios.create({ | ||
baseURL: `${origin || 'https://graph.facebook.com'}/v${this.version}/`, | ||
headers: { 'Content-Type': 'application/json' }, | ||
@@ -147,4 +165,4 @@ transformRequest: [ | ||
this._axios.interceptors.request.use( | ||
createRequestInterceptor({ onRequest: this._onRequest }) | ||
this.axios.interceptors.request.use( | ||
createRequestInterceptor({ onRequest: this.onRequest }) | ||
); | ||
@@ -155,9 +173,9 @@ | ||
invariant( | ||
this._appSecret, | ||
this.appSecret, | ||
'Must provide appSecret when skipAppSecretProof is false' | ||
); | ||
const appSecret = this._appSecret as string; | ||
const appSecret = this.appSecret as string; | ||
this._axios.interceptors.request.use((config) => { | ||
this.axios.interceptors.request.use((config) => { | ||
const isBatch = config.url === '/' && Array.isArray(config.data.batch); | ||
@@ -204,3 +222,3 @@ | ||
'query.access_token', | ||
this._accessToken | ||
this.accessToken | ||
); | ||
@@ -223,18 +241,2 @@ | ||
get version(): string { | ||
return this._version; | ||
} | ||
get axios(): AxiosInstance { | ||
return this._axios; | ||
} | ||
get accessToken(): string { | ||
return this._accessToken; | ||
} | ||
get appSecret(): string | undefined { | ||
return this._appSecret; | ||
} | ||
/** | ||
@@ -249,4 +251,4 @@ * Get Page Info | ||
}: Types.AccessTokenOptions = {}): Promise<Types.PageInfo> { | ||
return this._axios | ||
.get(`/me?access_token=${customAccessToken || this._accessToken}`) | ||
return this.axios | ||
.get(`/me?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -263,11 +265,11 @@ } | ||
}: Types.AccessTokenOptions = {}): Promise<Types.TokenInfo> { | ||
invariant(this._appId, 'App ID is required to debug token'); | ||
invariant(this._appSecret, 'App Secret is required to debug token'); | ||
invariant(this.appId, 'App ID is required to debug token'); | ||
invariant(this.appSecret, 'App Secret is required to debug token'); | ||
const accessToken = `${this._appId}|${this._appSecret}`; | ||
const accessToken = `${this.appId}|${this.appSecret}`; | ||
return this._axios | ||
return this.axios | ||
.get(`/debug_token`, { | ||
params: { | ||
input_token: customAccessToken || this._accessToken, | ||
input_token: customAccessToken || this.accessToken, | ||
access_token: accessToken, | ||
@@ -306,13 +308,13 @@ }, | ||
}): Promise<{ success: boolean }> { | ||
const appId = this._appId; | ||
const { appId } = this; | ||
invariant(appId, 'App ID is required to create subscription'); | ||
invariant( | ||
this._appSecret || appAccessToken, | ||
this.appSecret || appAccessToken, | ||
'App Secret or App Token is required to create subscription' | ||
); | ||
const accessToken = appAccessToken || `${appId}|${this._appSecret}`; | ||
const accessToken = appAccessToken || `${appId}|${this.appSecret}`; | ||
return this._axios | ||
return this.axios | ||
.post(`/${appId}/subscriptions?access_token=${accessToken}`, { | ||
@@ -338,12 +340,12 @@ object, | ||
} = {}): Promise<Types.MessengerSubscription[]> { | ||
const appId = this._appId; | ||
const { appId } = this; | ||
invariant(appId, 'App ID is required to get subscriptions'); | ||
invariant( | ||
this._appSecret || appAccessToken, | ||
this.appSecret || appAccessToken, | ||
'App Secret or App Token is required to get subscriptions' | ||
); | ||
const accessToken = appAccessToken || `${appId}|${this._appSecret}`; | ||
const accessToken = appAccessToken || `${appId}|${this.appSecret}`; | ||
return this._axios | ||
return this.axios | ||
.get(`/${appId}/subscriptions?access_token=${accessToken}`) | ||
@@ -363,10 +365,10 @@ .then((res) => res.data.data, handleError); | ||
} = {}): Promise<Types.MessengerSubscription> { | ||
const appId = this._appId; | ||
const { appId } = this; | ||
invariant(appId, 'App ID is required to get subscription'); | ||
invariant( | ||
this._appSecret || appAccessToken, | ||
this.appSecret || appAccessToken, | ||
'App Secret or App Token is required to get subscription' | ||
); | ||
const accessToken = appAccessToken || `${appId}|${this._appSecret}`; | ||
const accessToken = appAccessToken || `${appId}|${this.appSecret}`; | ||
@@ -391,6 +393,6 @@ return this.getSubscriptions({ | ||
}: Types.AccessTokenOptions = {}): Promise<Types.MessagingFeatureReview[]> { | ||
return this._axios | ||
return this.axios | ||
.get( | ||
`/me/messaging_feature_review?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -414,6 +416,6 @@ ) | ||
): Promise<Types.User> { | ||
return this._axios | ||
return this.axios | ||
.get<Types.User>( | ||
`/${userId}?fields=${fields.join(',')}&access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -433,6 +435,6 @@ ) | ||
): Promise<Types.MessengerProfile[]> { | ||
return this._axios | ||
return this.axios | ||
.get<{ data: Types.MessengerProfile[] }>( | ||
`/me/messenger_profile?fields=${fields.join(',')}&access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -447,6 +449,6 @@ ) | ||
): Promise<Types.MutationSuccessResponse> { | ||
return this._axios | ||
return this.axios | ||
.post<Types.MutationSuccessResponse>( | ||
`/me/messenger_profile?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -462,6 +464,6 @@ profile | ||
): Promise<Types.MutationSuccessResponse> { | ||
return this._axios | ||
return this.axios | ||
.delete<Types.MutationSuccessResponse>( | ||
`/me/messenger_profile?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -584,6 +586,6 @@ { | ||
): Promise<Types.PersistentMenu | null> { | ||
return this._axios | ||
return this.axios | ||
.get( | ||
`/me/custom_user_settings?psid=${userId}&access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -618,6 +620,6 @@ ) | ||
) { | ||
return this._axios | ||
return this.axios | ||
.post<Types.MutationSuccessResponse>( | ||
`/me/custom_user_settings?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -633,6 +635,6 @@ { | ||
// menuItems is in type MenuItem[] | ||
return this._axios | ||
return this.axios | ||
.post( | ||
`/me/custom_user_settings?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -657,6 +659,6 @@ { | ||
): Promise<Types.MutationSuccessResponse> { | ||
return this._axios | ||
return this.axios | ||
.delete( | ||
`/me/custom_user_settings?psid=${userId}¶ms=[%22persistent_menu%22]&access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -872,6 +874,6 @@ ) | ||
}: Types.AccessTokenOptions = {}): Promise<Types.MessageTagResponse> { | ||
return this._axios | ||
return this.axios | ||
.get<{ data: Types.MessageTagResponse }>( | ||
`/page_message_tags?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -892,5 +894,5 @@ ) | ||
return this._axios | ||
return this.axios | ||
.post<Types.SendMessageSuccessResponse>( | ||
`/me/messages?access_token=${customAccessToken || this._accessToken}`, | ||
`/me/messages?access_token=${customAccessToken || this.accessToken}`, | ||
body | ||
@@ -951,5 +953,5 @@ ) | ||
return this._axios | ||
return this.axios | ||
.post<Types.SendMessageSuccessResponse>( | ||
`/me/messages?access_token=${options.accessToken || this._accessToken}`, | ||
`/me/messages?access_token=${options.accessToken || this.accessToken}`, | ||
formdata, | ||
@@ -1271,5 +1273,5 @@ { | ||
return this._axios | ||
return this.axios | ||
.post('/', { | ||
accessToken: customAccessToken || this._accessToken, | ||
accessToken: customAccessToken || this.accessToken, | ||
batch: bodyEncodedbatch, | ||
@@ -1314,6 +1316,6 @@ }) | ||
): Promise<{ id: string }> { | ||
return this._axios | ||
return this.axios | ||
.post<{ id: string }>( | ||
`/me/custom_labels?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -1337,6 +1339,6 @@ { | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.post<{ success: true }>( | ||
`/${labelId}/label?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -1360,6 +1362,6 @@ { | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.delete<{ success: true }>( | ||
`/${labelId}/label?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -1391,3 +1393,3 @@ { | ||
const fields = options.fields ? options.fields.join(',') : 'name'; | ||
return this._axios | ||
return this.axios | ||
.get<{ | ||
@@ -1403,3 +1405,3 @@ data: { name: string; id: string }[]; | ||
`/${userId}/custom_labels?fields=${fields}&access_token=${ | ||
options.accessToken || this._accessToken | ||
options.accessToken || this.accessToken | ||
}` | ||
@@ -1420,6 +1422,6 @@ ) | ||
const fields = options.fields ? options.fields.join(',') : 'name'; | ||
return this._axios | ||
return this.axios | ||
.get<{ name: string; id: string }>( | ||
`/${labelId}?fields=${fields}&access_token=${ | ||
options.accessToken || this._accessToken | ||
options.accessToken || this.accessToken | ||
}` | ||
@@ -1447,3 +1449,3 @@ ) | ||
const fields = options.fields ? options.fields.join(',') : 'name'; | ||
return this._axios | ||
return this.axios | ||
.get<{ | ||
@@ -1459,3 +1461,3 @@ data: { name: string; id: string }[]; | ||
`/me/custom_labels?fields=${fields}&access_token=${ | ||
options.accessToken || this._accessToken | ||
options.accessToken || this.accessToken | ||
}` | ||
@@ -1475,5 +1477,5 @@ ) | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.delete<{ success: true }>( | ||
`/${labelId}?access_token=${customAccessToken || this._accessToken}` | ||
`/${labelId}?access_token=${customAccessToken || this.accessToken}` | ||
) | ||
@@ -1533,6 +1535,6 @@ .then((res) => res.data, handleError); | ||
return this._axios | ||
return this.axios | ||
.post( | ||
`/me/message_attachments?access_token=${ | ||
options.accessToken || this._accessToken | ||
options.accessToken || this.accessToken | ||
}`, | ||
@@ -1593,6 +1595,6 @@ ...args | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.post<{ success: true }>( | ||
`/me/pass_thread_control?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -1631,6 +1633,6 @@ { | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.post<{ success: true }>( | ||
`/me/take_thread_control?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -1655,6 +1657,6 @@ { | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.post<{ success: true }>( | ||
`/me/request_thread_control?access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}`, | ||
@@ -1682,3 +1684,3 @@ { | ||
> { | ||
return this._axios | ||
return this.axios | ||
.get<{ | ||
@@ -1691,3 +1693,3 @@ data: { | ||
`/me/secondary_receivers?fields=id,name&access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -1709,3 +1711,3 @@ ) | ||
}> { | ||
return this._axios | ||
return this.axios | ||
.get<{ | ||
@@ -1721,3 +1723,3 @@ data: [ | ||
`/me/thread_owner?recipient=${recipientId}&access_token=${ | ||
customAccessToken || this._accessToken | ||
customAccessToken || this.accessToken | ||
}` | ||
@@ -1737,7 +1739,7 @@ ) | ||
) { | ||
return this._axios | ||
return this.axios | ||
.get( | ||
`/me/insights/?${querystring.stringify({ | ||
metric: metrics.join(','), | ||
access_token: options.accessToken || this._accessToken, | ||
access_token: options.accessToken || this.accessToken, | ||
...options, | ||
@@ -1825,3 +1827,3 @@ })}` | ||
): Promise<any> { | ||
return this._axios | ||
return this.axios | ||
.post( | ||
@@ -1832,3 +1834,3 @@ `/me/nlp_configs?${querystring.stringify( | ||
{ | ||
accessToken: customAccessToken || this._accessToken, | ||
accessToken: customAccessToken || this.accessToken, | ||
} | ||
@@ -1866,3 +1868,3 @@ ) | ||
}) { | ||
return this._axios | ||
return this.axios | ||
.post(`/${appId}/activities`, { | ||
@@ -1899,3 +1901,3 @@ event: 'CUSTOM_APP_EVENTS', | ||
}) { | ||
const accessToken = customAccessToken || this._accessToken; | ||
const accessToken = customAccessToken || this.accessToken; | ||
@@ -1911,3 +1913,3 @@ // $appsecret_proof= hash_hmac('sha256', $access_token, $app_secret); | ||
return this._axios | ||
return this.axios | ||
.get( | ||
@@ -1988,5 +1990,5 @@ `/${userId}/${field}?access_token=${accessToken}&appsecret_proof=${appsecretProof}${appQueryString}${pageQueryString}` | ||
): Promise<{ id: string }> { | ||
return this._axios | ||
return this.axios | ||
.post<{ id: string }>( | ||
`/me/personas?access_token=${customAccessToken || this._accessToken}`, | ||
`/me/personas?access_token=${customAccessToken || this.accessToken}`, | ||
persona | ||
@@ -2010,3 +2012,3 @@ ) | ||
}> { | ||
return this._axios | ||
return this.axios | ||
.get<{ | ||
@@ -2016,3 +2018,3 @@ id: string; | ||
profilePictureUrl: string; | ||
}>(`/${personaId}?access_token=${customAccessToken || this._accessToken}`) | ||
}>(`/${personaId}?access_token=${customAccessToken || this.accessToken}`) | ||
.then((res) => res.data, handleError); | ||
@@ -2037,3 +2039,3 @@ } | ||
}> { | ||
return this._axios | ||
return this.axios | ||
.get<{ | ||
@@ -2047,3 +2049,3 @@ data: { | ||
}>( | ||
`/me/personas?access_token=${customAccessToken || this._accessToken}${ | ||
`/me/personas?access_token=${customAccessToken || this.accessToken}${ | ||
cursor ? `&after=${cursor}` : '' | ||
@@ -2072,3 +2074,2 @@ }` | ||
do { | ||
// eslint-disable-next-line no-await-in-loop | ||
const { | ||
@@ -2105,5 +2106,5 @@ data, | ||
): Promise<{ success: true }> { | ||
return this._axios | ||
return this.axios | ||
.delete<{ success: true }>( | ||
`/${personaId}?access_token=${customAccessToken || this._accessToken}` | ||
`/${personaId}?access_token=${customAccessToken || this.accessToken}` | ||
) | ||
@@ -2110,0 +2111,0 @@ .then((res) => res.data, handleError); |
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
754226
13486
11
+ Added@types/append-query@^2.0.0
+ Added@types/lodash@^4.14.156
+ Added@types/warning@^3.0.0
+ Addedts-invariant@^0.4.4
+ Added@types/append-query@2.0.3(transitive)
+ Added@types/warning@3.0.3(transitive)
+ Addedts-invariant@0.4.4(transitive)
+ Addedtslib@1.14.1(transitive)
- Removedinvariant@^2.2.4
- Removedinvariant@2.2.4(transitive)