Skip to content

Instantly share code, notes, and snippets.

@nabilfreeman
Last active June 29, 2020 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabilfreeman/5ea3ab66075a0caa8ecb1347459b8f08 to your computer and use it in GitHub Desktop.
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)
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