christophergregory / shopify-node-api

OAuth2 Module for Shopify API
MIT License
216 stars 74 forks source link

Helper functions #72

Closed samjross closed 7 years ago

samjross commented 7 years ago

I've used your API to do some cool things, and some of them are things everybody might want. I'm wondering if you want me to create a pull request to implement them into the api.

For example, I have a function which gets all the products on a store:

function getAllProducts() {
    let products = [];

    function getpage(pagenum) {
        return new Promise((resolve, reject) => {
            Shopify.get('/admin/products.json', {limit: 250, page: pagenum}, function (err, data, headers) {
                if (err) {
                    reject(err);
                }
                resolve(data.products);
            });
        });
    }

    let pagenum = 1;
    return new Promise((resolve, reject) => {
        function getNextPage() {
            getpage(pagenum)
                .then((data) => {
                    products = products.concat(data);

                    if (data.length < 250) {
                        resolve(products);
                    } else {
                        pagenum += 1;
                        getNextPage();
                    }
                })
        }

        getNextPage();
    });
}

If you like I can work on implementing this into a pull request. Let me know 😄

christophergregory commented 7 years ago

I've thought about providing something like this before but I think we should leave this to ancillary modules. I haven't gotten any requests (aside from this one) to do this so I don't think the demand is really there to justify the extra maintenance.

I'd like to keep this module as simple as possible so I would opt for creating a separate "helper" module that includes these functions. If it is fairly comprehensive and well tested i'm happy to link to it from this module.