Skip to content

Commit 0e974d3

Browse files
committed
Add a .URL() method to all actions and restructure .exec()
1 parent 913fef9 commit 0e974d3

File tree

15 files changed

+87
-43
lines changed

15 files changed

+87
-43
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3535
> const summonerData: dto.SummonerDTO = ...
3636
> // get summoner data
3737
> ```
38+
- Direct access to action URLs using the **`.URL()`** method
39+
>
40+
> ```typescript
41+
> const summonerURL = galeforce.lol.summoner().region(galeforce.regions.lol.NORTH_AMERICA).name('name').URL();
42+
> // https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/name
43+
> ```
3844
3945
#### Changed
4046

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A customizable, promise-based, and command-oriented TypeScript library and fluen
2424
- **Fluent interface** for seamless method chaining
2525
- **Built-in, customizable debugging** using `debug`
2626

27-
**Documentation** available [here](https://bcho04.github.io/galeforce/) and in the section [below](#guide).
27+
Automatically-generated **documentation** is available [here](https://bcho04.github.io/galeforce/), and code **examples** can be found the section [below](#guide).
2828

2929
## Table of Contents
3030

@@ -140,6 +140,26 @@ Each endpoint in the Galeforce library is an instance of an `Action` containing
140140
> ```
141141
>
142142
</details>
143+
144+
<details>
145+
<summary><code>.URL()</code></summary>
146+
147+
> Returns the endpoint URL associated with the `Action` and its previously-set parameters.
148+
>
149+
> **Example**
150+
>
151+
> ```javascript
152+
> /* Gets the Data Dragon URL associated with the Galeforce icon. */
153+
> const galeforceURL = galeforce.ddragon.item.art() // Fetch item icon art from Data Dragon
154+
> .version('11.9.1') // See the .<property>() section for documentation. Sets the version to retrieve data from.
155+
> .assetId('6671') // See below for documentation. Get the icon for the Galeforce item.
156+
> .URL(); // Get the encoded URL corresponding with the selected endpoint as a string.
157+
>
158+
> console.log(galeforceURL); // 'https://ddragon.leagueoflegends.com/cdn/11.9.1/img/item/6671.png'
159+
> ```
160+
>
161+
</details>
162+
143163
<details>
144164
<summary><code>.<em>&lt;property&gt;</em>()</code></summary>
145165
@@ -152,13 +172,11 @@ Each endpoint in the Galeforce library is an instance of an `Action` containing
152172
> const currentGameInfo = await galeforce.lol.spectator.active() // Target the /lol/spectator/v4/active-games/by-summoner/{summonerId} endpoint
153173
> .region(galeforce.regions.lol.NORTH_AMERICA) // Sets the request region to 'na1' (i.e., target the NA server)
154174
> .summonerId('summonerId') // Sets the request summonerId to 'summonerId'
155-
> .exec() // See .exec() above.
175+
> .exec(); // See .exec() above.
156176
> ```
157177
>
158178
> `.<property>()` methods may only be called once and are removed from the Action after being used.
159179
>
160-
> **Example**
161-
>
162180
> ```javascript
163181
> /* Gets current game info for a specific summonerId. */
164182
> const currentGameInfo = await galeforce.lol.spectator.active() // Target the /lol/spectator/v4/active-games/by-summoner/{summonerId} endpoint
@@ -188,26 +206,28 @@ Galeforce includes DTOs for all Riot API responses as TypeScript interfaces. Alt
188206
189207
### Config structure
190208
191-
When initializing Galeforce, a config object (JSON) or a path to a YAML file must be passed to the `GaleforceModule()` constructor as an argument:
209+
When initializing Galeforce, a config object (JSON) or a path to a YAML file may *optionally* be passed to the `GaleforceModule()` constructor as an argument:
192210
193211
```javascript
194212
const galeforce = new GaleforceModule(/* config file path or object */);
195213
```
196214
197-
Template string-like values (such as `${RIOT_KEY}`) will be evaluated using environment variables in `process.env`. The configuration file must have the following structure:
215+
Omitting the config will prevent Galeforce from being able to interface with the [Riot Games API](https://developer.riotgames.com/) (as no API key will be specified), although Data Dragon and the Live Client Data API will still be available.
216+
217+
Template string-like values (such as `${RIOT_KEY}`) will be evaluated using environment variables in `process.env`. The configuration file must have the following structure (all top-level fields are optional):
198218

199219
```yaml
200-
riot-api: # REQUIRED
220+
riot-api:
201221
key: ${RIOT_KEY} # (string) Your Riot API key from https://developer.riotgames.com
202-
cache: # OPTIONAL
222+
cache:
203223
type: ${CACHE_TYPE} # (string) What kind of cache to use ('redis', 'javascript', 'null')
204224
uri: ${CACHE_URI} # (string) The cache URI to connect to (required for 'redis' cache)
205-
rate-limit: # OPTIONAL, Requires a cache to be configured.
225+
rate-limit: # Requires a cache to be configured.
206226
prefix: riotapi-ratelimit- # The prefix for the Riot API rate limit keys in the cache.
207227
intervals: # key <secs>: value <number of requests>.
208228
120: 100
209229
1: 20
210-
debug: [] # OPTIONAL, A list containing any of 'action', 'payload', 'rate-limit', 'riot-api', '*' (all).
230+
debug: [] # A list containing any of 'action', 'payload', 'rate-limit', 'riot-api', '*' (all).
211231
```
212232
213233
### Documentation

src/galeforce/actions/action.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export default class Action<TResult> {
4949
* fails with an error.
5050
*/
5151
public async exec(): Promise<TResult> {
52+
this.inferEndpoint();
53+
5254
actionDebug(`${chalk.bold.magenta(this.payload._id)} | ${chalk.bold.yellow('execute')} \u00AB %O`, this.payload);
5355
try {
5456
if (typeof this.payload.endpoint === 'undefined') {
@@ -135,6 +137,32 @@ export default class Action<TResult> {
135137
}
136138
}
137139

140+
protected inferEndpoint(): void { /* Empty because this may be implemented by classes that inherit from Action */ }
141+
142+
/**
143+
* Returns the **encoded** target URL for the action without executing an HTTP request.
144+
* Useful if the URL string is needed for custom actions outside the scope of the library.
145+
* @throws Will throw an error if a required payload value (*region*,
146+
* *body* on POST or PUT requests, etc.) is missing or the HTTP request
147+
* fails with an error.
148+
*/
149+
public URL(): string {
150+
this.inferEndpoint();
151+
152+
if (typeof this.payload.endpoint === 'undefined') {
153+
throw new Error('[galeforce]: Action endpoint is required but undefined.');
154+
}
155+
156+
const request = this.submodules.RiotAPI.request(
157+
this.payload.endpoint,
158+
this.payload,
159+
this.payload.query,
160+
this.payload.body,
161+
);
162+
163+
return request.targetURL;
164+
}
165+
138166
private async getQueries(key: string, region: Region): Promise<number> {
139167
const { prefix } = this.submodules.cache.RLConfig;
140168
const value: string | null = await this.submodules.cache.get(prefix + key + region);

src/galeforce/actions/data-dragon/dragon-tail.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ export default class GetDataDragonTail extends BaseAction<Buffer> {
1212
this.payload.method = 'GET';
1313
}
1414

15-
public async exec(): Promise<Buffer> {
15+
protected inferEndpoint(): void {
1616
if (this.payload.version === '10.10.5') {
1717
this.payload.endpoint = ENDPOINTS.DATA_DRAGON.DRAGON_TAIL_ZIP;
1818
} else {
1919
this.payload.endpoint = ENDPOINTS.DATA_DRAGON.DRAGON_TAIL;
2020
}
21-
return super.exec();
2221
}
2322
}

src/galeforce/actions/lol/clash/tournaments.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ export default class GetClashTournament extends BaseAction<TournamentDTO> {
1818
this.payload.method = 'GET';
1919
}
2020

21-
public async exec(): Promise<TournamentDTO> {
21+
protected inferEndpoint(): void {
2222
if (this.payload.tournamentId) {
2323
this.payload.endpoint = ENDPOINTS.CLASH.GET_TOURNAMENT;
2424
} else if (this.payload.teamId) {
2525
this.payload.endpoint = ENDPOINTS.CLASH.GET_TEAM_TOURNAMENT;
2626
} else {
2727
throw new Error('[galeforce]: Not enough parameters provided to select API endpoint.');
2828
}
29-
return super.exec();
3029
}
3130
}

src/galeforce/actions/lol/league/entries.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class GetLeagueEntries extends BaseAction<LeagueEntryDTO[]> {
3030
this.payload.method = 'GET';
3131
}
3232

33-
public async exec(): Promise<LeagueEntryDTO[]> {
33+
protected inferEndpoint(): void {
3434
if (this.payload.summonerId) {
3535
this.payload.endpoint = ENDPOINTS.LEAGUE.SUMMONER_ID;
3636
} else if (this.payload.queue || this.payload.tier || this.payload.division) {
@@ -43,7 +43,5 @@ export default class GetLeagueEntries extends BaseAction<LeagueEntryDTO[]> {
4343
} else {
4444
throw new Error('[galeforce]: Not enough parameters provided to select API endpoint.');
4545
}
46-
47-
return super.exec();
4846
}
4947
}

src/galeforce/actions/lol/league/leagues.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class GetLeagueList extends BaseAction<LeagueListDTO> {
2323
this.payload.method = 'GET';
2424
}
2525

26-
public async exec(): Promise<LeagueListDTO> {
26+
protected inferEndpoint(): void {
2727
if (this.payload.leagueId) {
2828
this.payload.endpoint = ENDPOINTS.LEAGUE.LEAGUE_ID;
2929
} else if (this.payload.tier) {
@@ -43,7 +43,5 @@ export default class GetLeagueList extends BaseAction<LeagueListDTO> {
4343
} else {
4444
throw new Error('[galeforce]: Not enough parameters provided to select API endpoint.');
4545
}
46-
47-
return super.exec();
4846
}
4947
}

src/galeforce/actions/lol/match/match.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ export default class GetMatch extends BaseAction<MatchDTO> {
1818
this.payload.method = 'GET';
1919
}
2020

21-
public async exec(): Promise<MatchDTO> {
21+
protected inferEndpoint(): void {
2222
if (this.payload.tournamentCode) {
2323
this.payload.endpoint = ENDPOINTS.MATCH.MATCH_ID_TOURNAMENT;
2424
} else {
2525
this.payload.endpoint = ENDPOINTS.MATCH.MATCH_ID;
2626
}
27-
28-
return super.exec();
2927
}
3028
}

src/galeforce/actions/lol/summoner/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class GetSummoner extends BaseAction<SummonerDTO> {
2424
this.payload.method = 'GET';
2525
}
2626

27-
public async exec(): Promise<SummonerDTO> {
27+
protected inferEndpoint(): void {
2828
if (this.payload.summonerName) {
2929
this.payload.endpoint = ENDPOINTS.SUMMONER.SUMMONER_NAME;
3030
} else if (this.payload.summonerId) {
@@ -36,7 +36,5 @@ export default class GetSummoner extends BaseAction<SummonerDTO> {
3636
} else {
3737
throw new Error('[galeforce]: Not enough parameters provided to select API endpoint.');
3838
}
39-
40-
return super.exec();
4139
}
42-
}
40+
}

src/galeforce/actions/riot/account/account.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ export default class GetAccount extends BaseAction<AccountDTO> {
1919
this.payload.method = 'GET';
2020
}
2121

22-
public async exec(): Promise<AccountDTO> {
22+
protected inferEndpoint(): void {
2323
if (this.payload.puuid) {
2424
this.payload.endpoint = ENDPOINTS.ACCOUNT.PUUID;
2525
} else if (this.payload.gameName || this.payload.tagLine) {
2626
this.payload.endpoint = ENDPOINTS.ACCOUNT.RIOT_ID;
2727
} else {
2828
throw new Error('[galeforce]: Not enough parameters provided to select API endpoint.');
2929
}
30-
31-
return super.exec();
3230
}
3331
}

0 commit comments

Comments
 (0)