# all files / lib/Utils/ URLHelper.js

76.67% Statements 23/30
35.71% Branches 5/14
100% Functions 0/0
75.86% Lines 22/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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    901×

    1×

    3×
    3×

    1×

    1×

    1×
    7×

    1×
    4×

    1×
    5×

    5×
    7×

    7×

    7×

    5×

    1×
    2×
    2×

    1×

    1×

     import DateTimeHelper from './DateTimeHelper'

    const createRequestURL = (
        platformID,
        serviceName,
        endpoint,
        version,
        apiURLPrefix,
    ) => {
        const prefix = apiURLPrefix.replace(/%s/, platformID)
        return `${prefix}/lol/${serviceName}/v${version}/${endpoint}`
    }

    const getURLWithKey = (
        platformID,
        serviceName,
        endpoint,
        key,
        version,
        apiURLPrefix,
    ) => {
        const requestURL = createRequestURL(
            platformID,
            serviceName,
            endpoint,
            version,
            apiURLPrefix,
        )
        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,
        apiURLPrefix,
    ) =>
        createRequestURL(
            isTournament ? 'americas' : region,
            serviceName,
            endpoint,
            version,
            apiURLPrefix,
        ) + getQueryParamString(queryArray)

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