Skip to content

Commit 3925db8

Browse files
committed
feat(base url flexibility): enables switching the base url
the user can now provide a proxy url to the library #97
1 parent ea68285 commit 3925db8

File tree

5 files changed

+650
-1143
lines changed

5 files changed

+650
-1143
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ This wrapper will only work from a nodejs server app. The official clash royale
2323

2424
## Getting started
2525

26-
In order to use the official api, you will need a token. This token can be obtained at the Official API website [https://developer.clashroyale.com](https://developer.clashroyale.com).
26+
In order to use the official api, you will need a token. This token can be obtained at the Official API website [https://developer.clashroyale.com](https://developer.clashroyale.com). - For each token an ip address is associated, so make sure to use the token in the same server where you got it.
27+
28+
You can also use a **proxy server** to make the requests, such as [https://docs.royaleapi.com/proxy.html](https://docs.royaleapi.com/proxy.html) - More details below in the usage section.
2729

2830
Once you register and get the token, you are ready to start.
2931

@@ -55,8 +57,25 @@ api
5557
.catch(err => {
5658
// handle errors
5759
})
60+
61+
// Using the proxy server (https://proxy.royaleapi.dev/v1)
62+
// Create a key on the the official API and whitelist (include) this IP: 45.79.218.79
63+
// Use https://proxy.royaleapi.dev/v1
64+
const api = new ClashRoyaleAPI(
65+
'the token you got from the api',
66+
'https://proxy.royaleapi.dev/v1',
67+
)
68+
69+
// Use the api to get cards
70+
...
5871
```
5972

73+
## About the proxy server
74+
75+
The proxy server is a service provided by the RoyaleAPI team. It allows you to make requests to the official API from the browser.
76+
77+
If you want to host your own proxy server, there is a nuxt web app you can use available at [https://github.com/AndreVarandas/royale-proxy-api](https://github.com/AndreVarandas/royale-proxy-api).
78+
6079
## Methods
6180

6281
All api calls are asynchronous and used in the same way as above in the getCards example.

src/__tests__/ClashRoyaleAPI.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ClashRoyaleAPI } from '../'
2-
import * as library from '../index' // Workaround to spy on default function
32

43
describe('ClashRoyaleAPI', () => {
54
const ClashRoyale = new ClashRoyaleAPI('sometoken')

src/communications/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ const defaultRequestConfig: AxiosRequestConfig = {
1818
* Authorization headers to use the official api.
1919
*
2020
* @param {string} token - The api token from https://developer.clashroyale.com
21+
* @param {string} baseUrl - The base url for the api (optional)
2122
*/
22-
const getAxiosInstance = (token: string) => {
23+
const getAxiosInstance = (token: string, baseUrl?: string) => {
2324
const authorization = `Bearer ${token}`
2425
defaultRequestConfig.headers = {
2526
...defaultRequestConfig.headers,
2627
authorization,
2728
}
2829

30+
if (baseUrl) {
31+
defaultRequestConfig.baseURL = baseUrl
32+
}
33+
2934
return axios.create(defaultRequestConfig)
3035
}
3136

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ export class ClashRoyaleAPI {
5353
* Initializes the axios instance with the token
5454
* from the developer site https://developer.clashroyale.com
5555
*
56-
* @param {string} token
56+
* @param {string} token - The api token from https://developer.clashroyale.com
57+
* @param {string} baseUrl - The base url for the api (optional)
5758
*/
58-
constructor(token: string) {
59-
this.apiClient = getAxiosInstance(token)
59+
constructor(token: string, baseUrl?: string) {
60+
this.apiClient = getAxiosInstance(token, baseUrl)
6061
}
6162

6263
/**

0 commit comments

Comments
 (0)