diff --git a/examples/examples.js b/examples/examples.js index b47753c..756e1bf 100644 --- a/examples/examples.js +++ b/examples/examples.js @@ -105,7 +105,7 @@ let apiKey = process.env.TONICPOW_API_KEY || '' // Example: Update a user // user.first_name = 'Jack' - user.payout_address = 'mrz@moneybutton.com' + // user.payout_address = 'mrz@moneybutton.com' user = await TonicPow.updateUser(user) console.log(user.first_name) @@ -405,11 +405,19 @@ let apiKey = process.env.TONICPOW_API_KEY || '' conversion = await TonicPow.cancelConversion(conversion.id, 'not needed anymore') console.log('conversion status', conversion.status) + // + // Example: Set a custom header + // + TonicPow.session.customHeaders = { + "x-custom-header": "custom-value", + "x-another-custom-header": "another-value", + } + // // Example: Get a current rate // let rate = await TonicPow.getCurrentRate('usd',0.00) - console.log('rate found ', rate.currency_name, 'price in satoshis', rate.price_in_satoshis) + console.log('price in satoshis', rate.price_in_satoshis) // // Example: Logout user diff --git a/lib/api.js b/lib/api.js index 5c08dba..f2aa393 100644 --- a/lib/api.js +++ b/lib/api.js @@ -20,15 +20,27 @@ const internalHeaderKey = 'x-user-session-token' const visitorSessionKey = 'tncpw_session' // Current version for requests from the API -const pkgVersion = 'v0.1.74' +const pkgVersion = 'v0.1.75' const apiVersion = 'v1' // getOptions is a factory for axios default options -function getOptions (useCustomSessionToken = '') { +function getOptions (t, useCustomSessionToken = '') { + + // Set the default options and headers let defaultOptions = { jar: cookieJar, withCredentials: true, headers: { 'User-Agent': 'tonicpow-js ' + pkgVersion } } + + // Detect custom headers + if (t.session.customHeaders) { + for (const [key, value] of Object.entries(t.session.customHeaders)) { + defaultOptions.headers[key] = value + } + } + + // Set the user session token if set if (useCustomSessionToken && useCustomSessionToken.length > 0) { defaultOptions.headers[internalHeaderKey] = useCustomSessionToken } + return defaultOptions } @@ -333,6 +345,19 @@ let session = { this._apiToken = null } } + }, + + // The current custom headers + get customHeaders () { + return this._customHeaders + }, + + set customHeaders (value) { + if (value && typeof value === 'object') { + this._customHeaders = value + } else { + this._customHeaders = null + } } } @@ -370,6 +395,9 @@ function wrapAxios (t) { // Modify the response after sending (cookie management) tonicAxios.interceptors.response.use(function (response) { + // Clear custom headers + t.session.customHeaders = null + // Save the cookie for api or user response.config.jar.getCookies(response.config.url, { allPaths: true }, function (err, cookies) { if (err) { @@ -440,28 +468,28 @@ function setUserToken (t, token) { // // For more information: https://docs.tonicpow.com/#632ed94a-3afd-4323-af91-bdf307a399d2 async function createSession (t) { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/auth/session', { api_key: t.config.apiKey }, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/auth/session', { api_key: t.config.apiKey }, getOptions(t)) } // prolongSession will attempt to prolong a session (either the user or api) // // For more information: https://docs.tonicpow.com/#632ed94a-3afd-4323-af91-bdf307a399d2 async function prolongSession (t, customSessionToken = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/auth/session', getOptions(customSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/auth/session', getOptions(t, customSessionToken)) } // endSession will end the given session token or the api token if none is given // // For more information: https://docs.tonicpow.com/#1dfeff1e-6c8d-4b32-904e-a19261b1f89e async function endSession (t, customSessionToken = '') { - return tonicAxios.delete(t.config.apiUrl + apiVersion + '/auth/session', getOptions(customSessionToken)) + return tonicAxios.delete(t.config.apiUrl + apiVersion + '/auth/session', getOptions(t, customSessionToken)) } // loginUser will attempt to login a user // // For more information: https://docs.tonicpow.com/#5cad3e9a-5931-44bf-b110-4c4b74c7a070 async function loginUser (t, email, password) { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/login', { email: email, password: password }, getOptions(t.session.apiToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/login', { email: email, password: password }, getOptions(t, t.session.apiToken)) } // logoutUser will end the user session, if no session token set it will use the session.userToken @@ -475,7 +503,7 @@ async function logoutUser (t, userSessionToken = '') { } userSessionToken = t.session.userToken } - return tonicAxios.delete(t.config.apiUrl + apiVersion + '/users/logout', getOptions(userSessionToken)) + return tonicAxios.delete(t.config.apiUrl + apiVersion + '/users/logout', getOptions(t, userSessionToken)) } // currentUser will attempt get the profile for the current user or userSessionToken @@ -490,14 +518,14 @@ async function currentUser (t, userSessionToken = '') { userSessionToken = t.session.userToken } - return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/account', getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/account', getOptions(t, userSessionToken)) } // getUserBalance will first update a given user's balance from the chain and then return the user info // // For more information: https://docs.tonicpow.com/#8478765b-95b8-47ad-8b86-2db5bce54924 async function getUserBalance (t, userId, lastBalance = 0) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/balance/' + userId + '?last_balance=' + lastBalance, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/balance/' + userId + '?last_balance=' + lastBalance, getOptions(t)) } // getUser will get a user by ID or email address @@ -513,7 +541,7 @@ async function getUser (t, userId = 0, email = '') { url += 'email=' + email } - return tonicAxios.get(url, getOptions()) + return tonicAxios.get(url, getOptions(t)) } // createUser will create a new user given the attributes @@ -525,28 +553,28 @@ async function createUser (t, user) { throw Error('email is required') } - return tonicAxios.post(t.config.apiUrl + apiVersion + '/users', user, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/users', user, getOptions(t)) } // updateUser will update an existing user // // For more information: https://docs.tonicpow.com/#7c3c3c3a-f636-469f-a884-449cf6fb35fe async function updateUser (t, user, userSessionToken = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users', user, getOptions(userSessionToken)) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users', user, getOptions(t, userSessionToken)) } // forgotPassword will fire a forgot password request // // For more information: https://docs.tonicpow.com/#2c33dae4-d6b1-4949-9e84-fb02157ab7cd async function forgotPassword (t, email) { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/password/forgot', { email: email }, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/password/forgot', { email: email }, getOptions(t)) } // resetPassword will reset a password from a forgotPassword() request // // For more information: https://docs.tonicpow.com/#370fbeec-adb2-4ed3-82dc-2dffa840e490 async function resetPassword (t, resetToken, password, passwordConfirm) { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/password/reset', { token: resetToken, password: password, password_confirm: passwordConfirm }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/password/reset', { token: resetToken, password: password, password_confirm: passwordConfirm }, getOptions(t)) } // resendEmailVerification will resend an email to the user @@ -554,14 +582,14 @@ async function resetPassword (t, resetToken, password, passwordConfirm) { // // For more information: https://docs.tonicpow.com/#a12a3eff-491b-4079-99f6-07497b9e4efe async function resendEmailVerification (t, userId, userSessionToken = '') { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/verify/email/send', { id: userId }, getOptions(userSessionToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/verify/email/send', { id: userId }, getOptions(t, userSessionToken)) } // completeEmailVerification will complete an email verification with a given token // // For more information: https://docs.tonicpow.com/#f5081800-a224-4f36-8014-94981f0bd55d async function completeEmailVerification (t, emailToken) { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/verify/email', { token: emailToken }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/verify/email', { token: emailToken }, getOptions(t)) } // resendPhoneVerification will resend a phone verification code to the user @@ -569,19 +597,19 @@ async function completeEmailVerification (t, emailToken) { // // For more information: https://docs.tonicpow.com/#fcc4fe4d-f298-45bd-b51e-a5c107834528 async function resendPhoneVerification (t, userId, userSessionToken = '') { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/verify/phone/send', { id: userId }, getOptions(userSessionToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/users/verify/phone/send', { id: userId }, getOptions(t, userSessionToken)) } // completePhoneVerification will complete a phone verification with a given code and number // // For more information: https://docs.tonicpow.com/#573403c4-b872-475d-ac04-de32a88ecd19 async function completePhoneVerification (t, phone, phoneCode) { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/verify/phone', { phone: phone, phone_code: phoneCode }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/verify/phone', { phone: phone, phone_code: phoneCode }, getOptions(t)) } // requestActivation will send a request for activation // -// For more information: todo: add documentation +// For more information: https://docs.tonicpow.com/#c3d2f569-dc5e-4885-9701-a58522cb92cf async function requestActivation (t, userSessionToken = '') { // Missing token or empty token (fall back to the current user session token) if (!userSessionToken || userSessionToken.length < 1) { @@ -591,7 +619,7 @@ async function requestActivation (t, userSessionToken = '') { userSessionToken = t.session.userToken } - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/request', '', getOptions(userSessionToken)) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/request', '', getOptions(t, userSessionToken)) } // acceptUser will accept a user (if approval is required for new users) @@ -600,7 +628,7 @@ async function requestActivation (t, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#65c3962d-c309-4ef4-b85f-7ec1f08f031b async function acceptUser (t, userId = 0, email = '', reason = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/accept', { id: userId, email: email, reason: reason }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/accept', { id: userId, email: email, reason: reason }, getOptions(t)) } // activateUser will activate a user (if all application criteria is met) @@ -608,7 +636,7 @@ async function acceptUser (t, userId = 0, email = '', reason = '') { // // For more information: https://docs.tonicpow.com/#aa499fdf-2492-43ee-99d4-fc9735676431 async function activateUser (t, userId = 0, email = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/activate', { id: userId, email: email }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/activate', { id: userId, email: email }, getOptions(t)) } // pauseUser will pause a user account (all payouts go to internal address) @@ -616,7 +644,7 @@ async function activateUser (t, userId = 0, email = '') { // // For more information: https://docs.tonicpow.com/#3307310d-86a9-4a5c-84ff-c38c581c77e5 async function pauseUser (t, userId = 0, reason = '', email = 0) { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/pause', { id: userId, reason: reason, email: email }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/status/pause', { id: userId, reason: reason, email: email }, getOptions(t)) } // userExists will check if a user exists by email address @@ -624,14 +652,14 @@ async function pauseUser (t, userId = 0, reason = '', email = 0) { // // For more information: https://docs.tonicpow.com/#2d8c37d4-c88b-4cec-83ad-fa72b0f41f17 async function userExists (t, email) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/exists?email=' + email, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/exists?email=' + email, getOptions(t)) } // releaseUserBalance will send the internal balance to the user's payout_address // // For more information: https://docs.tonicpow.com/#be82b6cb-7fe8-4f03-9b0c-dbade8f2d40f async function releaseUserBalance (t, userId, reason = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/wallet/release', { id: userId, reason: reason }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/wallet/release', { id: userId, reason: reason }, getOptions(t)) } // refundUserBalance will send the internal balance back to the corresponding campaigns @@ -639,7 +667,7 @@ async function releaseUserBalance (t, userId, reason = '') { // // For more information: https://docs.tonicpow.com/#c373c7ed-189d-4aa6-88da-c4a58955fd28 async function refundUserBalance (t, userId, reason) { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/wallet/refund', { id: userId, reason: reason }, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/users/wallet/refund', { id: userId, reason: reason }, getOptions(t)) } // getUserReferrals will return all the related referrals to the given user @@ -656,7 +684,7 @@ async function getUserReferrals (t, userId = 0, email = '') { url += 'email=' + email } - return tonicAxios.get(url, getOptions()) + return tonicAxios.get(url, getOptions(t)) } // listUserReferrals will return a list of active users that have referrals @@ -664,7 +692,7 @@ async function getUserReferrals (t, userId = 0, email = '') { // // For more information: https://docs.tonicpow.com/#3fd8e647-abfa-422f-90af-952cccd3be7c async function listUserReferrals (t, page = 1, resultsPerPage = 20, sortBy = '', sortOrder = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/referrals?current_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/users/referrals?current_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions(t)) } // @@ -677,7 +705,7 @@ async function listUserReferrals (t, page = 1, resultsPerPage = 20, sortBy = '', // // For more information: https://docs.tonicpow.com/#153c0b65-2d4c-4972-9aab-f791db05b37b async function createAdvertiserProfile (t, profile, userSessionToken = '') { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/advertisers', profile, getOptions(userSessionToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/advertisers', profile, getOptions(t, userSessionToken)) } // getAdvertiserProfile will get an existing advertiser profile @@ -686,7 +714,7 @@ async function createAdvertiserProfile (t, profile, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#b3a62d35-7778-4314-9321-01f5266c3b51 async function getAdvertiserProfile (t, profileId, userSessionToken = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/advertisers/details/' + profileId, getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/advertisers/details/' + profileId, getOptions(t, userSessionToken)) } // updateAdvertiserProfile will update an existing profile @@ -694,7 +722,7 @@ async function getAdvertiserProfile (t, profileId, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#0cebd1ff-b1ce-4111-aff6-9d586f632a84 async function updateAdvertiserProfile (t, profile, userSessionToken = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/advertisers', profile, getOptions(userSessionToken)) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/advertisers', profile, getOptions(t, userSessionToken)) } // listCampaignsByAdvertiserProfile will return a list of campaigns @@ -702,7 +730,7 @@ async function updateAdvertiserProfile (t, profile, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#98017e9a-37dd-4810-9483-b6c400572e0c async function listCampaignsByAdvertiserProfile (t, profileId, page = 1, resultsPerPage = 20, sortBy = '', sortOrder = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/advertisers/campaigns/' + profileId + '?current_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/advertisers/campaigns/' + profileId + '?current_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions(t)) } // @@ -715,7 +743,7 @@ async function listCampaignsByAdvertiserProfile (t, profileId, page = 1, results // // For more information: https://docs.tonicpow.com/#b67e92bf-a481-44f6-a31d-26e6e0c521b1 async function createCampaign (t, campaign, userSessionToken = '') { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/campaigns', campaign, getOptions(userSessionToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/campaigns', campaign, getOptions(t, userSessionToken)) } // getCampaign will get an existing campaign @@ -724,7 +752,7 @@ async function createCampaign (t, campaign, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#b827446b-be34-4678-b347-33c4f63dbf9e async function getCampaign (t, campaignId, userSessionToken = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/details/' + campaignId, getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/details/' + campaignId, getOptions(t, userSessionToken)) } // getCampaignByShortCode will get an existing campaign via a short code from a link @@ -733,7 +761,7 @@ async function getCampaign (t, campaignId, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#8451b92f-ea74-47aa-8ac1-c96647e2dbfd async function getCampaignByShortCode (t, shortCode, userSessionToken = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/link/' + shortCode, getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/link/' + shortCode, getOptions(t, userSessionToken)) } // getCampaignBalance will update the models's balance from the chain @@ -741,7 +769,7 @@ async function getCampaignByShortCode (t, shortCode, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#b6c60c63-8ac5-4c74-a4a2-cf3e858e5a8d async function getCampaignBalance (t, campaignId, lastBalance = 0) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/balance/' + campaignId + '?last_balance=' + lastBalance, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/balance/' + campaignId + '?last_balance=' + lastBalance, getOptions(t)) } // updateCampaign will update an existing campaign @@ -749,7 +777,7 @@ async function getCampaignBalance (t, campaignId, lastBalance = 0) { // // For more information: https://docs.tonicpow.com/#665eefd6-da42-4ca9-853c-fd8ca1bf66b2 async function updateCampaign (t, campaign, userSessionToken = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/campaigns', campaign, getOptions(userSessionToken)) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/campaigns', campaign, getOptions(t, userSessionToken)) } // listCampaigns will return a list of active campaigns @@ -757,7 +785,7 @@ async function updateCampaign (t, campaign, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#c1b17be6-cb10-48b3-a519-4686961ff41c async function listCampaigns (t, customSessionToken = '', page = 1, resultsPerPage = 20, sortBy = '', sortOrder = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/list?current_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions(customSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/list?current_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions(t, customSessionToken)) } // listCampaignsByUrl will return a list of active campaigns @@ -765,7 +793,7 @@ async function listCampaigns (t, customSessionToken = '', page = 1, resultsPerPa // // For more information: https://docs.tonicpow.com/#30a15b69-7912-4e25-ba41-212529fba5ff async function listCampaignsByUrl (t, url, page = 1, resultsPerPage = 20, sortBy = '', sortOrder = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/find?target_url=' + url + '¤t_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/find?target_url=' + url + '¤t_page=' + page + '&results_per_page=' + resultsPerPage + '&sort_by=' + sortBy + '&sort_order=' + sortOrder, getOptions(t)) } // campaignsFeed will return a feed of active campaigns @@ -774,14 +802,14 @@ async function listCampaignsByUrl (t, url, page = 1, resultsPerPage = 20, sortBy // // For more information: https://docs.tonicpow.com/#b3fe69d3-24ba-4c2a-a485-affbb0a738de async function campaignsFeed (t, feedType = 'rss') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/feed?feed_type=' + feedType, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/feed?feed_type=' + feedType, getOptions(t)) } // campaignStatistics will get basic statistics on all campaigns // // For more information: https://docs.tonicpow.com/#d3108b14-486e-4e27-8176-57ec63cd49f2 async function campaignStatistics (t) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/statistics', getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/campaigns/statistics', getOptions(t)) } // @@ -794,7 +822,7 @@ async function campaignStatistics (t) { // // For more information: https://docs.tonicpow.com/#29a93e9b-9726-474c-b25e-92586200a803 async function createGoal (t, goal, userSessionToken = '') { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/goals', goal, getOptions(userSessionToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/goals', goal, getOptions(t, userSessionToken)) } // getGoal will get an existing goal @@ -803,7 +831,7 @@ async function createGoal (t, goal, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#48d7bbc8-5d7b-4078-87b7-25f545c3deaf async function getGoal (t, goalId, userSessionToken = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/goals/details/' + goalId, getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/goals/details/' + goalId, getOptions(t, userSessionToken)) } // updateGoal will update an existing goal @@ -811,7 +839,7 @@ async function getGoal (t, goalId, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#395f5b7d-6a5d-49c8-b1ae-abf7f90b42a2 async function updateGoal (t, goal, userSessionToken = '') { - return tonicAxios.put(t.config.apiUrl + apiVersion + '/goals', goal, getOptions(userSessionToken)) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/goals', goal, getOptions(t, userSessionToken)) } // @@ -824,7 +852,7 @@ async function updateGoal (t, goal, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#fce465a1-d8d5-442d-be22-95169170167e async function getConversion (t, conversionId) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/conversions/details/' + conversionId, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/conversions/details/' + conversionId, getOptions(t)) } // createConversionByGoalID will fire a conversion for a given goal id, if successful it will make a new Conversion @@ -832,7 +860,7 @@ async function getConversion (t, conversionId) { // For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27 async function createConversionByGoalID (t, goalId, tncpwSession, customDimensions = '', optionalPurchaseAmount = 0.00, delayInMinutes = 0) { let data = { goal_id: goalId, tncpw_session: tncpwSession, custom_dimensions: customDimensions, delay_in_minutes: delayInMinutes, amount: optionalPurchaseAmount } - return tonicAxios.post(t.config.apiUrl + apiVersion + '/conversions', data, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/conversions', data, getOptions(t)) } // createConversionByGoalName will fire a conversion for a given goal name, if successful it will make a new Conversion @@ -840,7 +868,7 @@ async function createConversionByGoalID (t, goalId, tncpwSession, customDimensio // For more information: https://docs.tonicpow.com/#d19c9850-3832-45b2-b880-3ef2f3b7dc37 async function createConversionByGoalName (t, goalName, tncpwSession, customDimensions = '', optionalPurchaseAmount = 0.00, delayInMinutes = 0) { let data = { name: goalName, tncpw_session: tncpwSession, custom_dimensions: customDimensions, delay_in_minutes: delayInMinutes, amount: optionalPurchaseAmount } - return tonicAxios.post(t.config.apiUrl + apiVersion + '/conversions', data, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/conversions', data, getOptions(t)) } // createConversionByUserID will fire a conversion for a given goal and user id, if successful it will make a new Conversion @@ -848,7 +876,7 @@ async function createConversionByGoalName (t, goalName, tncpwSession, customDime // For more information: https://docs.tonicpow.com/#d724f762-329e-473d-bdc4-aebc19dd9ea8 async function createConversionByUserID (t, goalId, userId, customDimensions = '', optionalPurchaseAmount = 0.00, delayInMinutes = 0) { let data = { goal_id: goalId, user_id: userId, custom_dimensions: customDimensions, delay_in_minutes: delayInMinutes, amount: optionalPurchaseAmount } - return tonicAxios.post(t.config.apiUrl + apiVersion + '/conversions', data, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/conversions', data, getOptions(t)) } // cancelConversion will cancel an existing conversion (if delay was set and > 1 minute remaining) @@ -856,7 +884,7 @@ async function createConversionByUserID (t, goalId, userId, customDimensions = ' // For more information: https://docs.tonicpow.com/#e650b083-bbb4-4ff7-9879-c14b1ab3f753 async function cancelConversion (t, conversionId, reason) { let data = { id: conversionId, reason: reason } - return tonicAxios.put(t.config.apiUrl + apiVersion + '/conversions/cancel', data, getOptions()) + return tonicAxios.put(t.config.apiUrl + apiVersion + '/conversions/cancel', data, getOptions(t)) } // @@ -869,7 +897,7 @@ async function cancelConversion (t, conversionId, reason) { // // For more information: https://docs.tonicpow.com/#154bf9e1-6047-452f-a289-d21f507b0f1d async function createLink (t, link, userSessionToken = '') { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/links', link, getOptions(userSessionToken)) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/links', link, getOptions(t, userSessionToken)) } // getLink will get an existing link @@ -878,7 +906,7 @@ async function createLink (t, link, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#c53add03-303e-4f72-8847-2adfdb992eb3 async function getLink (t, linkId, userSessionToken = '') { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/links/details/' + linkId, getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/links/details/' + linkId, getOptions(t, userSessionToken)) } // listLinksByUserID will get links associated to the user id @@ -887,7 +915,7 @@ async function getLink (t, linkId, userSessionToken = '') { // // For more information: https://docs.tonicpow.com/#23d068f1-4f0e-476a-a802-50b7edccd0b2 async function listLinksByUserID (t, userId, userSessionToken = '', page = 1, resultsPerPage = 20) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/links/user/' + userId + '?current_page=' + page + '&results_per_page=' + resultsPerPage, getOptions(userSessionToken)) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/links/user/' + userId + '?current_page=' + page + '&results_per_page=' + resultsPerPage, getOptions(t, userSessionToken)) } // checkLink will check for an existing link with a short_code @@ -895,7 +923,7 @@ async function listLinksByUserID (t, userId, userSessionToken = '', page = 1, re // // For more information: https://docs.tonicpow.com/#cc9780b7-0d84-4a60-a28f-664b2ecb209b async function checkLink (t, shortCode) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/links/check/' + shortCode, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/links/check/' + shortCode, getOptions(t)) } // @@ -907,7 +935,7 @@ async function checkLink (t, shortCode) { // // For more information: https://docs.tonicpow.com/#29a93e9b-9726-474c-b25e-92586200a803 async function createVisitorSession (t, visitorSession) { - return tonicAxios.post(t.config.apiUrl + apiVersion + '/visitors/sessions', visitorSession, getOptions()) + return tonicAxios.post(t.config.apiUrl + apiVersion + '/visitors/sessions', visitorSession, getOptions(t)) } // getVisitorSession will get a visitor session @@ -915,7 +943,7 @@ async function createVisitorSession (t, visitorSession) { // // For more information: https://docs.tonicpow.com/#cf560448-6dda-42a6-9051-136afabe78e6 async function getVisitorSession (t, tncpwSession) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/visitors/sessions/details/' + tncpwSession, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/visitors/sessions/details/' + tncpwSession, getOptions(t)) } // @@ -927,7 +955,7 @@ async function getVisitorSession (t, tncpwSession) { // // For more information: https://docs.tonicpow.com/#71b8b7fc-317a-4e68-bd2a-5b0da012361c async function getCurrentRate (t, currency, customAmount = 0.00) { - return tonicAxios.get(t.config.apiUrl + apiVersion + '/rates/' + currency + '?amount=' + customAmount, getOptions()) + return tonicAxios.get(t.config.apiUrl + apiVersion + '/rates/' + currency + '?amount=' + customAmount, getOptions(t)) } // @@ -1210,7 +1238,7 @@ module.exports = { return new Promise(async (resolve, reject) => { try { initCheck(this.loaded) - let response = await requestActivation(this, setUserToken(this, userSessionToken)) + await requestActivation(this, setUserToken(this, userSessionToken)) resolve({ success: 'activation requested' }) } catch (e) { reject(checkError(e))