import axios from 'axios' import config from 'config' import debug from 'debug' var log = debug('http') /** * @external Promise * @see {@link http://api.jquery.com/Types/#Promise Promise} */ /** * fetch message subscribers * @param {URLSearchParams} params - request params * @returns {Promise} subscribers list */ export function subscribers(params) { return new Promise((resolve, reject) => { client.get(`/notifications?${params.toString()}`) .then(response => { log(`CODE: ${response.status}`) resolve(response.data) }) .catch(reason => { reject(reason) }) }) } /** * delete invalid tokens * @param {import('../client').Token[]} tokens tokens */ export const deleteSubscribers = async (tokens) => { const response = await client.post('/notifications/delete', tokens) return response.data } const baseURL = config.get('service.baseURL') + '/api' const user = config.get('service.user') || process.env.JUICK_SERVICE_USER const password = config.get('service.password') || process.env.JUICK_SERVICE_PASSWORD /** @type { import('axios').AxiosInstance } */ let client = axios.create({ baseURL: baseURL, headers: user ? { 'Authorization': 'Basic ' + Buffer.from(user + ':' + password).toString('base64') } : {} }) log(`HTTP client initialized with base URL ${baseURL} ${ user? `and ${user} user` : ''}`)