Skip to content

Commit

Permalink
Added optional expressions using ES6 format
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Dec 31, 2019
1 parent 1dd633c commit 85fb0ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
80 changes: 40 additions & 40 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const internalHeaderKey = 'x-user-session-token'
const version = 'v1'

// getOptions is a factory for axios default options
function getOptions (useCustomSessionToken) {
function getOptions (useCustomSessionToken='') {
let defaultOptions = { jar: cookieJar, withCredentials: true, headers: {} }
if (useCustomSessionToken && useCustomSessionToken.length > 0) {
defaultOptions.headers[internalHeaderKey] = useCustomSessionToken
Expand All @@ -33,7 +33,7 @@ function initCheck (loaded) {
// extractSessionTokenFromHeader will extract a session token from a cookie header value
//
// Example `cookieHeader` Value: 'Cookie: session_token=this-is-the-session-token-value-getting-extracted; another_cookie=value; third_cookie=value'
function extractSessionTokenFromHeader (cookieHeader) {
function extractSessionTokenFromHeader (cookieHeader='') {
if (!cookieHeader || cookieHeader.length < 10) {
return cookieHeader
}
Expand Down Expand Up @@ -246,14 +246,14 @@ async function createSession (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, userSessionToken) {
async function prolongSession (t, userSessionToken='') {
return axios.get(t.config.apiUrl + version + '/auth/session', getOptions(userSessionToken))
}

// 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, userSessionToken) {
async function endSession (t, userSessionToken='') {
return axios.delete(t.config.apiUrl + version + '/auth/session', getOptions(userSessionToken))
}

Expand All @@ -267,7 +267,7 @@ async function loginUser (t, email, password) {
// logoutUser will end the user session, if no session token set it will use the session.userToken
//
// For more information: https://docs.tonicpow.com/#39d65294-376a-4366-8f71-a02b08f9abdf
async function logoutUser (t, userSessionToken) {
async function logoutUser (t, userSessionToken='') {
// Missing token or empty token
if (!userSessionToken || userSessionToken.length < 1) {
throw Error('user session must be set')
Expand All @@ -279,7 +279,7 @@ async function logoutUser (t, userSessionToken) {
// currentUser will attempt get the profile for the current user or userSessionToken
//
// For more information: https://docs.tonicpow.com/#7f6e9b5d-8c7f-4afc-8e07-7aafdd891521
async function currentUser (t, userSessionToken) {
async function currentUser (t, userSessionToken='') {
// Missing token or empty token
if (!userSessionToken || userSessionToken.length < 1) {
throw Error('user session must be set')
Expand Down Expand Up @@ -325,7 +325,7 @@ async function createUser (t, user) {
// updateUser will update an existing user
//
// For more information: https://docs.tonicpow.com/#7c3c3c3a-f636-469f-a884-449cf6fb35fe
async function updateUser (t, user, userSessionToken) {
async function updateUser (t, user, userSessionToken='') {
return axios.put(t.config.apiUrl + version + '/users', user, getOptions(userSessionToken))
}

Expand Down Expand Up @@ -366,7 +366,7 @@ async function completePhoneVerification (t, phone, phoneCode) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#153c0b65-2d4c-4972-9aab-f791db05b37b
async function createAdvertiserProfile (t, profile, userSessionToken) {
async function createAdvertiserProfile (t, profile, userSessionToken='') {
return axios.post(t.config.apiUrl + version + '/advertisers', profile, getOptions(userSessionToken))
}

Expand All @@ -375,15 +375,15 @@ async function createAdvertiserProfile (t, profile, userSessionToken) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b3a62d35-7778-4314-9321-01f5266c3b51
async function getAdvertiserProfile (t, profileId, userSessionToken) {
async function getAdvertiserProfile (t, profileId, userSessionToken='') {
return axios.get(t.config.apiUrl + version + '/advertisers/details/' + profileId, getOptions(userSessionToken))
}

// updateAdvertiserProfile will update an existing profile
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#0cebd1ff-b1ce-4111-aff6-9d586f632a84
async function updateAdvertiserProfile (t, profile, userSessionToken) {
async function updateAdvertiserProfile (t, profile, userSessionToken='') {
return axios.put(t.config.apiUrl + version + '/advertisers', profile, getOptions(userSessionToken))
}

Expand All @@ -396,7 +396,7 @@ async function updateAdvertiserProfile (t, profile, userSessionToken) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b67e92bf-a481-44f6-a31d-26e6e0c521b1
async function createCampaign (t, campaign, userSessionToken) {
async function createCampaign (t, campaign, userSessionToken='') {
return axios.post(t.config.apiUrl + version + '/campaigns', campaign, getOptions(userSessionToken))
}

Expand All @@ -405,7 +405,7 @@ async function createCampaign (t, campaign, userSessionToken) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b827446b-be34-4678-b347-33c4f63dbf9e
async function getCampaign (t, campaignId, userSessionToken) {
async function getCampaign (t, campaignId, userSessionToken='') {
return axios.get(t.config.apiUrl + version + '/campaigns/details/' + campaignId, getOptions(userSessionToken))
}

Expand All @@ -420,14 +420,14 @@ async function getCampaignBalance (t, campaignId) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#665eefd6-da42-4ca9-853c-fd8ca1bf66b2
async function updateCampaign (t, campaign, userSessionToken) {
async function updateCampaign (t, campaign, userSessionToken='') {
return axios.put(t.config.apiUrl + version + '/campaigns', campaign, getOptions(userSessionToken))
}

// listCampaigns will return a list of active campaigns
//
// For more information: https://docs.tonicpow.com/#c1b17be6-cb10-48b3-a519-4686961ff41c
async function listCampaigns (t, customSessionToken) {
async function listCampaigns (t, customSessionToken='') {
return axios.get(t.config.apiUrl + version + '/campaigns/list', getOptions(customSessionToken))
}

Expand All @@ -440,7 +440,7 @@ async function listCampaigns (t, customSessionToken) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#29a93e9b-9726-474c-b25e-92586200a803
async function createGoal (t, goal, userSessionToken) {
async function createGoal (t, goal, userSessionToken='') {
return axios.post(t.config.apiUrl + version + '/goals', goal, getOptions(userSessionToken))
}

Expand All @@ -449,22 +449,22 @@ async function createGoal (t, goal, userSessionToken) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#48d7bbc8-5d7b-4078-87b7-25f545c3deaf
async function getGoal (t, goalId, userSessionToken) {
async function getGoal (t, goalId, userSessionToken='') {
return axios.get(t.config.apiUrl + version + '/goals/details/' + goalId, getOptions(userSessionToken))
}

// updateGoal will update an existing goal
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#395f5b7d-6a5d-49c8-b1ae-abf7f90b42a2
async function updateGoal (t, goal, userSessionToken) {
async function updateGoal (t, goal, userSessionToken='') {
return axios.put(t.config.apiUrl + version + '/goals', goal, getOptions(userSessionToken))
}

// convertGoal will fire a conversion for a given goal, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27
async function convertGoal (t, goalName, visitorSessionId, additionalData, customUserId) {
async function convertGoal (t, goalName, visitorSessionId, additionalData='', customUserId='') {
let data = { name: goalName, visitor_session_id: visitorSessionId, additional_data: additionalData, user_id: customUserId }
return axios.post(t.config.apiUrl + version + '/goals/convert', data, getOptions())
}
Expand All @@ -478,7 +478,7 @@ async function convertGoal (t, goalName, visitorSessionId, additionalData, custo
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#154bf9e1-6047-452f-a289-d21f507b0f1d
async function createLink (t, link, userSessionToken) {
async function createLink (t, link, userSessionToken='') {
return axios.post(t.config.apiUrl + version + '/links', link, getOptions(userSessionToken))
}

Expand All @@ -487,7 +487,7 @@ async function createLink (t, link, userSessionToken) {
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#c53add03-303e-4f72-8847-2adfdb992eb3
async function getLink (t, linkId, userSessionToken) {
async function getLink (t, linkId, userSessionToken='') {
return axios.get(t.config.apiUrl + version + '/links/details/' + linkId, getOptions(userSessionToken))
}

Expand All @@ -509,7 +509,7 @@ module.exports = {
config: config,
loaded: false,
session: session,
init: async function (apiKey, environment, customSessionToken) {
init: async function (apiKey, environment, customSessionToken='') {
config.apiKey = apiKey
config.environment = environment
wrapAxios(this)
Expand All @@ -528,7 +528,7 @@ module.exports = {
}
})
},
prolongSession: async function (customSessionToken) {
prolongSession: async function (customSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -539,7 +539,7 @@ module.exports = {
}
})
},
endSession: async function (customSessionToken) {
endSession: async function (customSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -562,7 +562,7 @@ module.exports = {
}
})
},
logoutUser: async function (userSessionToken) {
logoutUser: async function (userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -574,7 +574,7 @@ module.exports = {
}
})
},
currentUser: async function (userSessionToken) {
currentUser: async function (userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -586,7 +586,7 @@ module.exports = {
}
})
},
getUser: async function (userId, email) {
getUser: async function (userId=0, email='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand Down Expand Up @@ -619,7 +619,7 @@ module.exports = {
}
})
},
updateUser: async function (user, userSessionToken) {
updateUser: async function (user, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand Down Expand Up @@ -674,7 +674,7 @@ module.exports = {
}
})
},
createAdvertiserProfile: async function (profile, userSessionToken) {
createAdvertiserProfile: async function (profile, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -685,7 +685,7 @@ module.exports = {
}
})
},
getAdvertiserProfile: async function (profileId, userSessionToken) {
getAdvertiserProfile: async function (profileId, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -696,7 +696,7 @@ module.exports = {
}
})
},
updateAdvertiserProfile: async function (profile, userSessionToken) {
updateAdvertiserProfile: async function (profile, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -707,7 +707,7 @@ module.exports = {
}
})
},
createCampaign: async function (campaign, userSessionToken) {
createCampaign: async function (campaign, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -718,7 +718,7 @@ module.exports = {
}
})
},
getCampaign: async function (campaignId, userSessionToken) {
getCampaign: async function (campaignId, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -740,7 +740,7 @@ module.exports = {
}
})
},
updateCampaign: async function (campaign, userSessionToken) {
updateCampaign: async function (campaign, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -751,7 +751,7 @@ module.exports = {
}
})
},
listCampaigns: async function (customSessionToken) {
listCampaigns: async function (customSessionToken = '') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -762,7 +762,7 @@ module.exports = {
}
})
},
createGoal: async function (goal, userSessionToken) {
createGoal: async function (goal, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -773,7 +773,7 @@ module.exports = {
}
})
},
getGoal: async function (goalId, userSessionToken) {
getGoal: async function (goalId, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -784,7 +784,7 @@ module.exports = {
}
})
},
updateGoal: async function (goal, userSessionToken) {
updateGoal: async function (goal, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -795,7 +795,7 @@ module.exports = {
}
})
},
convertGoal: async function (goalName, visitorSessionId, additionalData, customUserId) {
convertGoal: async function (goalName, visitorSessionId, additionalData='', customUserId='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -806,7 +806,7 @@ module.exports = {
}
})
},
createLink: async function (link, userSessionToken) {
createLink: async function (link, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand All @@ -817,7 +817,7 @@ module.exports = {
}
})
},
getLink: async function (linkId, userSessionToken) {
getLink: async function (linkId, userSessionToken='') {
return new Promise(async (resolve, reject) => {
try {
initCheck(this.loaded)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tonicpow-js",
"version": "0.1.16",
"version": "0.1.17",
"description": "TonicPow API Library in JS - https://docs.tonicpow.com",
"main": "lib/api.js",
"repository": {
Expand Down

0 comments on commit 85fb0ee

Please sign in to comment.