pipedrive / client-nodejs

Pipedrive API client for NodeJS
MIT License
205 stars 82 forks source link

Functional approach and Promise support #84

Closed IgorHalfeld closed 4 years ago

IgorHalfeld commented 5 years ago

I work at @NOALVO and here we use a functional approach. I saw the issue #38 and I think curry approach with promise would be nice!

const Pipedrive = require('pipedrive');
const pipedrive = new Pipedrive.Client('API_KEY_GOES_HERE', { strictMode: true });

/**
 * @param {String} name Entity name e.g. Deal
 * @param {Object} payload Payload data
 * @returns {Promise}
 */
export const createEntity = name => payload => new Promise((resolve, reject) => {
  pipedrive[name].add(payload, (error, id) => error ? reject(error) : resolve(id));
});

/**
 * @param {String} name Entity name e.g. Deal
 * @returns {Promise}
 */
export const listEntity = name => new Promise((resolve, reject) => {
  pipedrive[name].getAll({}, (error, entities) => error ? reject(error) : resolve(entities));
});

/**
 * @param {String} name Entity name e.g. Deal
 * @param {String} id Entity id
 * @returns {Promise}
 */
export const getEntity = name => id => new Promise((resolve, reject) => {
  pipedrive[name].get(id, (error, entity) => error ? reject(error) : resolve(entity));
});

export const createDeal = createEntity('Deals');
export const listDeal = listEntity('Deals');
// createDeal({ name: 'Example' ... });
// listDeal();

of course, this is only a wrapper, but would be awesome be native implementation 💃 I created a issue before send PR, only for see whats you think

this badass implementation is from @FelipeMonobe 🚀

IgorHalfeld commented 5 years ago

Anyone from @pipedrive can say something about it? xD

koundinyagoparaju commented 5 years ago

Would love to see this PR in the client. Currently, we are using ugly promise wrapper around the APIs.

tot-ra commented 4 years ago

Duplicate of https://github.com/pipedrive/client-nodejs/issues/81