aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/http.js
blob: a90afef3f0ddd0d682fbff38a85e76474ba235d3 (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
47
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<import('../client').SecureUser[]>} 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` : ''}`)