-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add steam info to players (#777)
* feat: add steam info to players * fix: make steam api key optional * fix: some tests :) * more test-fixing
- Loading branch information
1 parent
144f124
commit 9e6ec3d
Showing
14 changed files
with
577 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { AxiosError, AxiosInstance } from 'axios'; | ||
import axios from 'axios'; | ||
import { config } from '../config.js'; | ||
import { logger } from '@takaro/util'; | ||
|
||
interface IPlayerSummary { | ||
steamid: string; | ||
communityvisibilitystate: number; | ||
profilestate: number; | ||
personaname: string; | ||
commentpermission: number; | ||
profileurl: string; | ||
avatar: string; | ||
avatarmedium: string; | ||
avatarfull: string; | ||
avatarhash: string; | ||
lastlogoff: number; | ||
personastate: number; | ||
primaryclanid: string; | ||
timecreated: number; | ||
personastateflags: number; | ||
} | ||
|
||
interface IPlayerBans { | ||
SteamId: string; | ||
CommunityBanned: boolean; | ||
VACBanned: boolean; | ||
NumberOfVACBans: number; | ||
DaysSinceLastBan: number; | ||
NumberOfGameBans: number; | ||
EconomyBan: string; | ||
} | ||
|
||
class SteamApi { | ||
private apiKey = config.get('steam.apiKey'); | ||
private _client: AxiosInstance; | ||
private log = logger('steamApi'); | ||
|
||
get client() { | ||
if (!this._client) { | ||
this._client = axios.create({ | ||
baseURL: 'https://api.steampowered.com', | ||
timeout: 1000, | ||
}); | ||
|
||
this._client.interceptors.request.use((config) => { | ||
config.params = config.params || {}; | ||
config.params.key = this.apiKey; | ||
return config; | ||
}); | ||
|
||
axios.interceptors.request.use((request) => { | ||
this.log.info(`➡️ ${request.method?.toUpperCase()} ${request.url}`, { | ||
method: request.method, | ||
url: request.url, | ||
}); | ||
return request; | ||
}); | ||
|
||
axios.interceptors.response.use( | ||
(response) => { | ||
this.log.info( | ||
`⬅️ ${response.request.method?.toUpperCase()} ${response.request.path} ${response.status} ${ | ||
response.statusText | ||
}`, | ||
{ | ||
status: response.status, | ||
statusText: response.statusText, | ||
method: response.request.method, | ||
url: response.request.url, | ||
} | ||
); | ||
|
||
return response; | ||
}, | ||
(error: AxiosError) => { | ||
let details = {}; | ||
|
||
if (error.response?.data) { | ||
const data = error.response.data as Record<string, unknown>; | ||
details = JSON.stringify(data.meta); | ||
} | ||
|
||
this.log.error('☠️ Request errored', { | ||
traceId: error.response?.headers['x-trace-id'], | ||
details, | ||
status: error.response?.status, | ||
statusText: error.response?.statusText, | ||
method: error.config?.method, | ||
url: error.config?.url, | ||
response: error.response?.data, | ||
}); | ||
return Promise.reject(error); | ||
} | ||
); | ||
} | ||
return this._client; | ||
} | ||
|
||
async getPlayerSummaries(steamIds: string[]): Promise<IPlayerSummary[]> { | ||
const response = await this.client.get('/ISteamUser/GetPlayerSummaries/v2', { | ||
params: { | ||
steamids: steamIds.join(','), | ||
}, | ||
}); | ||
|
||
return response.data.response.players; | ||
} | ||
|
||
async getPlayerBans(steamIds: string[]): Promise<IPlayerBans[]> { | ||
const response = await this.client.get('/ISteamUser/GetPlayerBans/v1', { | ||
params: { | ||
steamids: steamIds.join(','), | ||
}, | ||
}); | ||
|
||
return response.data.players; | ||
} | ||
} | ||
|
||
export const steamApi = new SteamApi(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.