# all files / lib/Utils/ URLHelper.js

73.33% Statements 22/30
35.71% Branches 5/14
100% Functions 0/0
72.41% Lines 21/29

    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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    741×

    1×

    1×

    1×

    1×

    1×
    7×

    1×
    4×

    1×
    5×

    5×
    7×

    7×

    7×

    5×

    1×
    2×
    2×

    1×

    1×

     import DateTimeHelper from './DateTimeHelper'

    const API_HOST = 'api.riotgames.com'

    const createRequestURL = (platformID, serviceName, endpoint, version) =>
        `https://${platformID}.${API_HOST}/lol/${serviceName}/v${version}/${endpoint}`

    const getURLWithKey = (platformID, serviceName, endpoint, key, version) => {
        const requestURL = createRequestURL(
            platformID,
            serviceName,
            endpoint,
            version,
        )
        return requestURL + getAPIKey(requestURL, key)
    }

    const getAPIKey = (url, key) =>
        `${url.lastIndexOf('?') === -1 ? '?' : '&'}api_key=${key ? key : ''}`

    const appendKey = (str, key, value) =>
        str + (str ? '&' : '') + `${key}=${value}`

    const arrayToOptions = array =>
        array.reduce((final, curr) => ({ ...final, ...curr }), {})

    const stringifyOptions = options => {
        let stringifiedOpts = ''

        for (const key of Object.keys(options)) {
            if (IArray.isArray(options[key])) {
                for (const value of options[key]) {
                    stringifiedOpts = appendKey(stringifiedOpts, key, value)
                }
            } else {
                if (IDateTimeHelper.checkIfDateParam(key)) {
                    options[key] = DateTimeHelper.getEpoch(options[key])
                }
                stringifiedOpts = appendKey(stringifiedOpts, key, options[key])
            }
        }

        return stringifiedOpts
    }

    const getQueryParamString = arrayOfOpts => {
        const str = stringifyOptions(arrayToOptions(arrayOfOpts))
        return str ? `?${encodeURI(str)}` : ''
    }

    const getURLWithQuery = (
        region,
        serviceName,
        endpoint,
        queryArray,
        isTournament,
        version,
    ) =>
        createRequestURL(
            isTournament ? 'americas' : region,
            serviceName,
            endpoint,
            version,
        ) + getQueryParamString(queryArray)

    export default {
        createRequestURL,
        getAPIKey,
        getQueryParamString,
        getURLWithKey,
        getURLWithQuery,
        stringifyOptions,
    }