all files / lib/Endpoints/ TournamentEndpointV4.js

100% Statements 27/27
100% Branches 6/6
100% Functions 7/7
100% Lines 26/26
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171           10×   10× 10× 10× 10× 10× 10×   10×   10×                                                                                                                                                                                                                                                                                  
import Endpoint from 'Endpoint'
import Request from 'RequestClient/Request'
import METHOD_NAMES from 'Enums/method-names'
 
class TournamentEndpointV4 extends Endpoint {
    constructor(config, limiter) {
        super()
 
        this.config = config
 
        this.create = this.createTournamentCode.bind(this)
        this.update = this.updateTournamentCode.bind(this)
        this.get = this.getTournamentByCode.bind(this)
        this.register = this.register.bind(this)
        this.registerProviderData = this.registerProviderData.bind(this)
        this.lobbyEvents = this.lobbyEvents.bind(this)
 
        this.serviceName = 'tournament'
 
        this.limiter = limiter
    }
 
    /**
     * Create a tournament code for the given tournament.
     *
     * Implements POST `/lol/tournament/v4/codes`.
     *
     * @param {number} tournamentID - The ID of the tournament from /lol/tournament/v4/tournaments.
     * @param {object} body - The optional POST body to pass in. See official docs.
     */
    createTournamentCode(tournamentID, body) {
        return new Request(
            this.config,
            this.serviceName,
            'codes',
            METHOD_NAMES.TOURNAMENT.CREATE_TOURNAMENT_CODE,
            'POST',
            this.limiter,
            body,
            true,
            4,
        ).query({ tournamentId: tournamentID })
    }
 
    /**
     * Update the pick type, map, spectator type, or allowed summoners for a code.
     *
     * Implements PUT `/lol/tournament/v4/codes/{tournamentCode}`.
     *
     * @param {string} tournamentCode - The code of the tournament to update.
     * @param {object} body - The update body. This shouldn't be empty (it would be pointless).
     */
    updateTournamentCode(tournamentCode, body) {
        if (typeof body !== 'object')
            throw new Error(
                'The body of updateTournamentCode should have an object argument',
            )
        if (Object.keys(body).length === 0)
            throw new Error(
                'The body of updateTournamentCode should not be empty',
            )
        return new Request(
            this.config,
            this.serviceName,
            `codes/${tournamentCode}`,
            METHOD_NAMES.TOURNAMENT.UPDATE_TOURNAMENT_CODE,
            'PUT',
            this.limiter,
            body,
            true,
            4,
        )
    }
 
    /**
     * Returns the tournament code DTO associated with a tournament code string.
     *
     * Implements GET `/lol/tournament/v4/codes/{tournamentCode}`.
     *
     * @param {string} tournamentCode - The code of the tournament.
     */
    getTournamentByCode(tournamentCode) {
        return new Request(
            this.config,
            this.serviceName,
            `codes/${tournamentCode}`,
            METHOD_NAMES.TOURNAMENT.GET_TOURNAMENT_CODE,
            'GET',
            this.limiter,
            null,
            true,
            4,
        )
    }
 
    /**
     * Creates a tournament and returns its ID.
     *
     * Implements POST `/lol/tournament/v4/tournaments`.
     *
     * @param {number} providerID - The ID of the provider from /lol/tournament/v4/providers.
     * @param {string} name - An optional name to pass in. It'll only be used if the length is > 0.
     */
    register(providerID, name = '') {
        const body = {
            providerId: providerID,
        }
        if (name.length > 0) body.name = name
        return new Request(
            this.config,
            this.serviceName,
            'tournaments',
            METHOD_NAMES.TOURNAMENT.REGISTER_TOURNAMENT,
            'POST',
            this.limiter,
            body,
            true,
            4,
        )
    }
 
    /**
     * Creates a tournament provider and return its ID.
     *
     * Implements POST `/lol/tournament/v4/providers`.
     *
     * @param {string} region - A region string ('na'/'euw'). Just use kayn's REGIONS dictionary.
     * @param {url} url - The provider's callback URL to which tournament game results in this region should be posted. See official docs.
     */
    registerProviderData(region = this.config.region.toUpperCase(), url) {
        const body = {
            region: region.toUpperCase(),
            url,
        }
        return new Request(
            this.config,
            this.serviceName,
            'providers',
            METHOD_NAMES.TOURNAMENT.REGISTER_PROVIDER_DATA,
            'POST',
            this.limiter,
            body,
            true,
            4,
        )
    }
 
    /**
     * Gets a list of lobby events by tournament code.
     *
     * Implements GET `/lol/tournament/v4/lobby-events/by-code/{tournamentCode}`.
     *
     * @param {string} tournamentCode - The short code to look up lobby events for.
     */
    lobbyEvents(tournamentCode) {
        return new Request(
            this.config,
            this.serviceName,
            `lobby-events/by-code/${tournamentCode}`,
            METHOD_NAMES.TOURNAMENT.GET_LOBBY_EVENTS_BY_CODE,
            'GET',
            this.limiter,
            null,
            true,
            4,
        )
    }
}
 
export default TournamentEndpointV4