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

    const template_id = this.parseRequired(options.template_id, '*', 'No Template ID specified')
    console.log(options.template_id + " => " + template_id)

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

    const url = `https://api.onesignal.com/templates/${template_id}?app_id=${appid}`;
    const os_options = {
        method: 'GET',
        headers: {
            accept: 'application/json',
            Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`,
            'Content-Type': 'application/json; charset=utf-8'
        }
    };

    return fetch(url, os_options)
        .then(res => {
            if (!res.ok) {
                throw new Error(`HTTP error! status: ${res.status}`);
            }
            return res.json().then(json => {
                json.status = res.status; // Add status to json response
                return json;
            });
        })
        .catch(err => console.error('Error:', err));



}