
// JavaScript Document
// v1.0.0
exports.onesignal_export_subscriptions_csv = async function (options) {
    // get appid
    const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
    console.log(options.appid + " => " + appid)

    const extra_fields = this.parseOptional(options.extra_fields, '*', null)
    console.log(extra_fields + " => " + extra_fields)

    const fetch = require('node-fetch');

    const url = `https://api.onesignal.com/players/csv_export?app_id=${appid}`;
    const os_options = {
        method: 'POST',
        headers: {
            accept: 'application/json',
            Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`,
            'content-type': 'application/json'
        },
        body: JSON.stringify({ extra_fields: [extra_fields] })
    };

    return fetch(url, os_options)
        .then(res => {

            return res.json().then(json => {
                json.status = res.status; // Add status to json response
                return json;
            });
        })
        .catch(err => console.error('Error:', err));
}