{"size": 3682, "ext": "js", "max_stars_repo_path": "lib/v1/endpoints/subscriptions/index.js", "max_stars_repo_name": "francoispqt/instagram-node-client", "max_stars_repo_stars_event_min_datetime": "2018-02-11T03:10:52.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-08T11:00:14.000Z", "max_issues_repo_path": "lib/v1/endpoints/subscriptions/index.js", "max_issues_repo_name": "francoispqt/instagram-node-client", "max_issues_repo_issues_event_min_datetime": "2018-02-11T03:14:53.000Z", "max_issues_repo_issues_event_max_datetime": "2018-02-12T04:00:17.000Z", "max_forks_repo_path": "lib/v1/endpoints/subscriptions/index.js", "max_forks_repo_name": "francoispqt/instagram-node-client", "max_forks_repo_forks_event_min_datetime": "2018-02-11T03:12:24.000Z", "max_forks_repo_forks_event_max_datetime": "2018-02-11T03:12:24.000Z"}
{"max_stars_count": 3, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 31.4700854701, "max_line_length": 110, "alphanum_fraction": 0.5578489951}
const Endpoint = require("../../../endpoint")

/**
 * Client.subscriptions
 * ```js
 * // example
 * Client.subscriptions.create({
 *  object: "",
 *  aspect: "",
 *  verifyToken: "",
 *  callbackURL: "",
 *  accessToken: "SOMEACCESSTOKEN",
 * })
 * .then(result => {})
 * ```
 * @augments Endpoint
 */
class Subscriptions extends Endpoint {
    constructor(client) {
        super("subscriptions", client)
    }

    /**
     * Creates a subscription
     * @param {object} opts the options object { accessToken, sign, object, aspect, verifyToken, callbackURL }
     * @param {function} cb callback called if paseed, otherwise returns a promise 
     */
    create(opts = {}, cb) {
        const CLIENT_ID = this.client.opts.CLIENT_ID || opts.CLIENT_ID
        if (!CLIENT_ID) {
            const error = new Error("You must provide a client id")
            return cb ? cb(error) : Promise.reject(error)
        }

        const CLIENT_SECRET =
            this.client.opts.CLIENT_SECRET || opts.CLIENT_SECRET
        if (!CLIENT_SECRET) {
            const error = new Error("You must provide a client secret")
            return cb ? cb(error) : Promise.reject(error)
        }

        if (!opts.object){
            const error = new Error("You must provide an object")
            return cb ? cb(error) : Promise.reject(error)
        } 

        if (!opts.aspect) { 
            const error = new Error("You must provide an aspect")
            return cb ? cb(error) : Promise.reject(error)
        } 

        if (!opts.verifyToken) {
            const error = new Error("You must provide a verify token")
            return cb ? cb(error) : Promise.reject(error)
        }

        if (!opts.callbackURL) {
            const error = new Error("You must provide a callback url")
            return cb ? cb(error) : Promise.reject(error)
        }

        return this.api.call({
            uri: "/v1/subscriptions",
            method: "POST",
            formData: {
                client_id: CLIENT_ID,
                client_secret: CLIENT_SECRET,
                object: opts.object,
                aspect: opts.aspect,
                verify_token: opts.verifyToken,
                callback_url: opts.callbackURL,
            },
            json: true,
            sign: opts.sign,
        }, cb)
    }

    /**
     * Lists the subscriptions for the client
     * @param {object} opts the options object { accessToken, sign, object, aspect, verifyToken, callbackURL }
     * @param {function} cb callback called if paseed, otherwise returns a promise 
     */
    list(opts = {}, cb) {
        
        const CLIENT_ID = this.client.opts.CLIENT_ID || opts.CLIENT_ID
        if (!CLIENT_ID) {
            const error = new Error("You must provide a client id")
            return cb ? cb(error) : Promise.reject(error)
        }

        const CLIENT_SECRET =
            this.client.opts.CLIENT_SECRET || opts.CLIENT_SECRET
        if (!CLIENT_SECRET) {
            const error = new Error("You must provide a client secret")
            return cb ? cb(error) : Promise.reject(error)
        }

        return this.api.call({
            uri: "/v1/subscriptions",
            method: "GET",
            qs: {
                client_id: CLIENT_ID,
                client_secret: CLIENT_SECRET,
            },
        }, cb)
    }

    /**
     * Deletes subscriptions for the client
     * @param {object} opts the options object { accessToken, sign, object, aspect, verifyToken, callbackURL }
     * @param {function} cb callback called if paseed, otherwise returns a promise 
     */
    del(opts = {}) {}
}

module.exports = client => new Subscriptions(client)
