Last active
June 29, 2020 13:41
-
-
Save nabilfreeman/5ea3ab66075a0caa8ecb1347459b8f08 to your computer and use it in GitHub Desktop.
Pizzly Node.js integration using Axios and environment variables (2 examples: Xero and Trello)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Axios = require('axios'); | |
const { | |
PIZZLY_URL, | |
PIZZLY_SECRET_KEY, | |
PIZZLY_TRELLO_ID, | |
PIZZLY_XERO_ID, | |
} = process.env; | |
const integrations = { | |
trello: PIZZLY_TRELLO_ID, | |
xero: PIZZLY_XERO_ID, | |
}; | |
const methods = ['get', 'post', 'put', 'delete']; | |
const makePizzlyInstance = (integration) => { | |
const instance = Axios.create({ | |
baseURL: `${PIZZLY_URL}/proxy/${integration}`, | |
timeout: 0, // no timeouts! | |
}); | |
instance.defaults.auth = { | |
username: PIZZLY_SECRET_KEY, | |
}; | |
instance.defaults.headers = { | |
...instance.defaults.headers, | |
'Pizzly-Auth-Id': integrations[integration], | |
}; | |
return instance; | |
}; | |
module.exports = async function ( | |
integration, method, endpoint, body = {}, headers = {}, | |
) { | |
if (!integrations[integration]) { | |
throw new Error(`You specified an unknown integration (${integration}). Available integrations: ${Object.keys(integrations).join('|')}`); | |
} | |
if (!methods.includes(method)) { | |
throw new Error(`You specified an unknown method (${method}). Available methods: ${methods.join('|')}`); | |
} | |
const instance = makePizzlyInstance(integration); | |
const response = await instance.request({ | |
method, | |
url: endpoint, | |
data: body, | |
headers, | |
}); | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment