Skip to content

Commit

Permalink
feat(endpoint): endpoint update (#583)
Browse files Browse the repository at this point in the history
* feat(endpoints): update endpoints

* feat(utils): added a regionToCluster util method
  • Loading branch information
OllieJennings authored May 18, 2024
1 parent fb79e79 commit 43fda71
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 560 deletions.
71 changes: 38 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

> Node.JS minimal Riot API client written in Typescript

### Features

* Rate limiting through [@fightmegg/riot-rate-limiter](https://github.com/fightmegg/riot-rate-limiter)
* Automatic retries
* TypeScript typings
* 100% endpoint coverage (incl. DDragon)
* Caching with custom ttls per endpoint
* Request prioritization

- Rate limiting through [@fightmegg/riot-rate-limiter](https://github.com/fightmegg/riot-rate-limiter)
- Automatic retries
- TypeScript typings
- 100% endpoint coverage (incl. DDragon)
- Caching with custom ttls per endpoint
- Request prioritization

## Installation

Expand All @@ -26,35 +24,35 @@ $ npm install @fightmegg/riot-api
## Usage

```ts
import { RiotAPI, RiotAPITypes, PlatformId } from '@fightmegg/riot-api'
import { RiotAPI, RiotAPITypes, PlatformId } from "@fightmegg/riot-api";

(async () => {
const rAPI = new RiotAPI('RGAPI-KEY');
const rAPI = new RiotAPI("RGAPI-KEY");

const summoner = await rAPI.summoner.getBySummonerName({
region: PlatformId.EUW1,
summonerName: "Demos Kratos",
});
})()
const summoner = await rAPI.summoner.getBySummonerName({
region: PlatformId.EUW1,
summonerName: "Demos Kratos",
});
})();
```

## Config

```ts
const config: RiotAPITypes.Config = {
debug: false,
cache: {
cacheType: 'ioredis', // local or ioredis
client: 'redis://localhost:6379', // leave null if client is local
ttls: {
byMethod: {
[RiotAPITypes.METHOD_KEY.SUMMONER.GET_BY_SUMMONER_NAME]: 5000, // ms
}
}
}
}

const rAPI = new RiotAPI('RGAPI-TOKEN', config);
debug: false,
cache: {
cacheType: "ioredis", // local or ioredis
client: "redis://localhost:6379", // leave null if client is local
ttls: {
byMethod: {
[RiotAPITypes.METHOD_KEY.SUMMONER.GET_BY_SUMMONER_NAME]: 5000, // ms
},
},
},
};

const rAPI = new RiotAPI("RGAPI-TOKEN", config);
```

## Error handling
Expand All @@ -69,14 +67,13 @@ Caching is turned off by default, but with the cache property in the config you

When setting up the cache, you can change the `ttl` of each method / endpoint individually. This is done through the `METHOD_KEY` type which can be found in the [typings file](https://github.com/fightmegg/riot-api/blob/master/src/%40types/index.ts#L92).


## DDragon

We also fully support [DataDragon](https://developer.riotgames.com/docs/lol#data-dragon) which can be accessed in two ways:

```ts
// ...
const rAPI = new RiotAPI('RGAPI-KEY');
const rAPI = new RiotAPI("RGAPI-KEY");

const latestV = await rAPI.ddragon.versions.latest();
const champs = await rAPI.ddragon.champion.all();
Expand All @@ -85,14 +82,24 @@ const champs = await rAPI.ddragon.champion.all();
If you want to just use static data only, then you can do the following:

```ts
import { DDragon } from '@fightmegg/riot-api';
import { DDragon } from "@fightmegg/riot-api";

const ddragon = new DDragon();
const champs = await ddragon.champion.all();
```

Just like the main API, we have full TypeScript typings for DDragon endpoints. Please note we **do not** support caching for DDragon endpoints.

## regionToCluster

A helper method to make it easier to determing which cluster you want to hit based on the users region

```ts
import { regionToCluster } from "@fightmegg/riot-api";

const cluster = regionToCluster("EUW1"); // outputs "EUROPE"
```

## TypeScript typing

```ts
Expand All @@ -111,14 +118,12 @@ If you want to see want the rate-limiter is currently doing, we use the [debug](
DEBUG=riotapi* node ...
```


## Testing

Unit tests: `npm test`

E2E tests: `npm run test:e2e`


## Planned features

- [ ] Custom Caches
Expand Down
43 changes: 43 additions & 0 deletions __tests__/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,49 @@ describe("RiotAPI", () => {
);
});

describe("spectatorTftV5", () => {
test.each([
[
"getByPuuid",
{
region: PlatformId.EUW1,
puuid: "1",
},
[
PlatformId.EUW1,
RiotAPITypes.METHOD_KEY.SPECTATOR_TFT_V5.GET_GAME_BY_PUUID,
{ puuid: "1" },
{
id: "euw1.spectatorTftV5.getByPuuidId.1",
},
],
],
[
"getFeaturedGames",
{
region: PlatformId.EUW1,
},
[
PlatformId.EUW1,
RiotAPITypes.METHOD_KEY.SPECTATOR_TFT_V5.GET_FEATURED_GAMES,
{},
{
id: "euw1.spectatorTftV5.getFeaturedGames",
},
],
],
])(
"%s - calls request with correct params",
async (name, input, params) => {
const rAPI = new RiotAPI("1234");
rAPI.request = jest.fn().mockResolvedValue(null);

await getKeyValue(rAPI.spectatorTftV5)(name as any)(input as any);
expect(rAPI.request).toHaveBeenCalledWith(...params);
}
);
});

describe("spectator", () => {
test.each([
[
Expand Down
35 changes: 35 additions & 0 deletions __tests__/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PlatformId } from "@fightmegg/riot-rate-limiter";
import { regionToCluster } from "../../src/utils";

describe("utils", () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe("regionToCluster", () => {
test("should return correct cluster for each region", () => {
expect(regionToCluster(PlatformId.NA1)).toEqual(PlatformId.AMERICAS);
expect(regionToCluster(PlatformId.BR1)).toEqual(PlatformId.AMERICAS);
expect(regionToCluster(PlatformId.LA1)).toEqual(PlatformId.AMERICAS);
expect(regionToCluster(PlatformId.LA2)).toEqual(PlatformId.AMERICAS);

expect(regionToCluster(PlatformId.KR)).toEqual(PlatformId.ASIA);
expect(regionToCluster(PlatformId.JP1)).toEqual(PlatformId.ASIA);

expect(regionToCluster(PlatformId.EUW1)).toEqual(PlatformId.EUROPE);
expect(regionToCluster(PlatformId.EUNE1)).toEqual(PlatformId.EUROPE);
expect(regionToCluster(PlatformId.TR1)).toEqual(PlatformId.EUROPE);
expect(regionToCluster(PlatformId.RU)).toEqual(PlatformId.EUROPE);

expect(regionToCluster(PlatformId.OC1)).toEqual(PlatformId.SEA);
expect(regionToCluster(PlatformId.PH2)).toEqual(PlatformId.SEA);
expect(regionToCluster(PlatformId.SG2)).toEqual(PlatformId.SEA);
expect(regionToCluster(PlatformId.TH2)).toEqual(PlatformId.SEA);
expect(regionToCluster(PlatformId.TW2)).toEqual(PlatformId.SEA);
expect(regionToCluster(PlatformId.VN2)).toEqual(PlatformId.SEA);

// @ts-expect-error -- testing invalid input
expect(regionToCluster("invalid")).toEqual(PlatformId.AMERICAS);
});
});
});
Loading

0 comments on commit 43fda71

Please sign in to comment.