Skip to content

Commit

Permalink
Merge pull request #135 from KenEucker/develop
Browse files Browse the repository at this point in the history
caching biketag adapter requests
  • Loading branch information
KenEucker authored Dec 18, 2021
2 parents c2e4283 + 8f31d38 commit 49ae751
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/biketag/getAmbassadors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export async function getAmbassadors(
payload: getAmbassadorsPayload
): Promise<BikeTagApiResponse<Ambassador[]>> {
delete payload.source
const requestMethod = payload.cached ? client.cachedRequest : client.request

const response = await client.request({
const response = await requestMethod({
url: getApiUrl(payload.host, AMBASSADORS_ENDPOINT, payload.game),
data: payload,
})
Expand Down
10 changes: 5 additions & 5 deletions src/biketag/getGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export async function getGame(
payload: getGamePayload
): Promise<BikeTagApiResponse<Game[]>> {
delete payload.source
const requestMethod = payload.cached ? client.cachedRequest : client.request

return client
.request({
url: getApiUrl(payload.host, GAMES_ENDPOINT, payload.game),
data: payload,
})
return requestMethod({
url: getApiUrl(payload.host, GAMES_ENDPOINT, payload.game),
data: payload,
})
.then((response) => {
const success = response?.status !== 200
return {
Expand Down
3 changes: 2 additions & 1 deletion src/biketag/getPlayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export async function getPlayers(
payload: getPlayersPayload
): Promise<BikeTagApiResponse<Player[]>> {
delete payload.source
const requestMethod = payload.cached ? client.cachedRequest : client.request

const response = await client.request({
const response = await requestMethod({
url: getApiUrl(payload.host, PLAYERS_ENDPOINT, payload.game),
data: payload,
})
Expand Down
3 changes: 2 additions & 1 deletion src/biketag/getSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export async function getSettings(
payload: getSettingsPayload
): Promise<BikeTagApiResponse<Setting[]>> {
delete payload.source
const requestMethod = payload.cached ? client.cachedRequest : client.request

const response = await client.request({
const response = await requestMethod({
url: getApiUrl(payload.host, SETTINGS_ENDPOINT, payload.game),
data: payload,
})
Expand Down
3 changes: 2 additions & 1 deletion src/biketag/getTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export async function getTags(
payload: getTagsPayload
): Promise<BikeTagApiResponse<Tag[]>> {
delete payload.source
const requestMethod = payload.cached ? client.cachedRequest : client.request

const response = await client.request({
const response = await requestMethod({
url: getApiUrl(payload.host, TAGS_ENDPOINT, payload.game),
data: payload,
})
Expand Down
30 changes: 14 additions & 16 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,36 @@ export class BikeTagClient extends EventEmitter {

const initConfig = this.config(configuration ?? {}, true, true)
this.initializeClients(initConfig)
const headers = {
'user-agent': USERAGENT,
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': true,
}

headers['user-agent'] =
typeof window !== 'undefined' ? undefined : USERAGENT
const responseType = 'json'

/// Configure separate fetching strategies: plain, authed (default), cached (authed)
this.plainFetcher = axios.create({
headers: {
'user-agent': USERAGENT,
},
responseType: 'json',
responseType,
})

this.fetcher = axios.create({
// baseURL: BIKETAG_API_HOST,
headers: {
'user-agent': USERAGENT,
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': true,
},
responseType: 'json',
headers,
responseType,
})

this.cachedFetcher = setup({
baseURL: BIKETAG_API_HOST,
cache: {
maxAge: 15 * 60 * 1000,
exclude: {
// Only exclude PUT, PATCH and DELETE methods from cache
methods: ['put', 'patch', 'delete'],
},
},
headers: {
'user-agent': USERAGENT,
},
responseType: 'json',
headers,
responseType,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface CommonData {
host?: string
source?: AvailableApis | string
concise?: boolean
cached?: boolean
}
export interface AccessToken {
accessToken: string
Expand Down

0 comments on commit 49ae751

Please sign in to comment.