aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/http.js
blob: 74c51b0a3465e29a054d5ef5ce095b625271b753 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import axios from 'axios';
var debug = require('debug')('http');


/**
 * @external Promise
 * @see {@link http://api.jquery.com/Types/#Promise Promise}
 */
/**
 * fetch message subscribers
 *
 * @param {URLSearchParams} params - request params
 * @returns {Promise<import('../client').SecureUser[]>} subscribers list
 */
export function subscribers(params) {
    return new Promise((resolve, reject) => {
        client.get(`/notifications?${params.toString()}`)
            .then(response => {
                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 = process.env.JUICK_SERVICE_URL;
const user = process.env.JUICK_SERVICE_USER;
const 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')
    } : {}
});
debug(`HTTP client initialized with base URL ${baseURL} ${ user? `and ${user} user` : ''}`);