Skip to content

Commit

Permalink
feat(base url flexibility): enables switching the base url
Browse files Browse the repository at this point in the history
the user can now provide a proxy url to the library

#97
  • Loading branch information
AndreVarandas committed Sep 13, 2024
1 parent ea68285 commit 3925db8
Show file tree
Hide file tree
Showing 5 changed files with 650 additions and 1,143 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ This wrapper will only work from a nodejs server app. The official clash royale

## Getting started

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).
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.

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.

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

Expand Down Expand Up @@ -55,8 +57,25 @@ api
.catch(err => {
// handle errors
})

// Using the proxy server (https://proxy.royaleapi.dev/v1)
// Create a key on the the official API and whitelist (include) this IP: 45.79.218.79
// Use https://proxy.royaleapi.dev/v1
const api = new ClashRoyaleAPI(
'the token you got from the api',
'https://proxy.royaleapi.dev/v1',
)

// Use the api to get cards
...
```

## About the proxy server

The proxy server is a service provided by the RoyaleAPI team. It allows you to make requests to the official API from the browser.

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).

## Methods

All api calls are asynchronous and used in the same way as above in the getCards example.
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/ClashRoyaleAPI.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ClashRoyaleAPI } from '../'
import * as library from '../index' // Workaround to spy on default function

describe('ClashRoyaleAPI', () => {
const ClashRoyale = new ClashRoyaleAPI('sometoken')
Expand Down
7 changes: 6 additions & 1 deletion src/communications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ const defaultRequestConfig: AxiosRequestConfig = {
* Authorization headers to use the official api.
*
* @param {string} token - The api token from https://developer.clashroyale.com
* @param {string} baseUrl - The base url for the api (optional)
*/
const getAxiosInstance = (token: string) => {
const getAxiosInstance = (token: string, baseUrl?: string) => {
const authorization = `Bearer ${token}`
defaultRequestConfig.headers = {
...defaultRequestConfig.headers,
authorization,
}

if (baseUrl) {
defaultRequestConfig.baseURL = baseUrl
}

return axios.create(defaultRequestConfig)
}

Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ export class ClashRoyaleAPI {
* Initializes the axios instance with the token
* from the developer site https://developer.clashroyale.com
*
* @param {string} token
* @param {string} token - The api token from https://developer.clashroyale.com
* @param {string} baseUrl - The base url for the api (optional)
*/
constructor(token: string) {
this.apiClient = getAxiosInstance(token)
constructor(token: string, baseUrl?: string) {
this.apiClient = getAxiosInstance(token, baseUrl)
}

/**
Expand Down
Loading

0 comments on commit 3925db8

Please sign in to comment.