Created
August 5, 2023 12:59
-
-
Save neverlane/9a98e095038740660e68b3a2a076acc6 to your computer and use it in GitHub Desktop.
backend.arizona-rp.com API | typescript | nodejs
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
import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios'; | |
export class ArizonaAPIError extends Error { | |
name = 'ArizonaAPIError'; | |
} | |
const exportDataField = <T>(res: AxiosResponse<T>) => res.data; | |
const catchAPIError = (v: unknown) => { | |
if ( | |
v instanceof AxiosError && | |
v.response?.status && | |
v.response.status >= 400 | |
) { | |
const convertToArizonaError = ( | |
v: Record<'field' | 'name' | 'message', string | undefined> | |
) => `${v?.field ?? v?.name ?? 'error'}: ${v?.message}`; | |
const message = Array.isArray(v.response.data) | |
? v.response.data.map(convertToArizonaError).join(', ') | |
: convertToArizonaError(v.response.data); | |
throw new ArizonaAPIError(message); | |
} | |
throw new ArizonaAPIError('unknown'); | |
}; | |
export interface ArizonaAPIOptions { | |
accessToken?: string; | |
apiUrl?: string; | |
} | |
export interface ArizonaAPIGameServer { | |
id: number; | |
serverId: number; | |
name: string; | |
ip: string; | |
port: number; | |
vkCommunityId: number; | |
fullName: string; | |
urlEncoded: string; | |
players: number; | |
maxplayers: number; | |
updatedAt: number; | |
} | |
export interface ArizonaAPIAuthTokens { | |
accessToken: string; | |
refreshToken: string; | |
} | |
export interface ArizonaAPIProfileUpgrades { | |
feFinder: boolean; | |
pack: boolean; | |
more: boolean; | |
plancet: boolean; | |
unlimCarSkill: boolean; | |
house: number; | |
antiTax: boolean; | |
haveTwoFarm: boolean; | |
} | |
export interface ArizonaAPIProfileWeaponSkills { | |
m4: number; | |
ak47: number; | |
pistol: number; | |
sdpistol: number; | |
deagle: number; | |
shotgun: number; | |
mp5: number; | |
sniper: number; | |
} | |
export interface ArizonaAPIProfileUserInfo { | |
id: number; | |
username: string; | |
donate: number; | |
notActivatedDonate: number; | |
cash: number; | |
bank: number; | |
individualAccount: number; | |
depozit: number; | |
level: number; | |
isOnline: boolean; | |
hp: number; | |
fullness: number; | |
email: string; | |
vipLevel: number; | |
job: number; | |
skin: number; | |
fraction: number; | |
rank: number; | |
phoneNumber: number; | |
twoFactorAuthByEmail: boolean; | |
twoFactorAuthByTotp: boolean; | |
adminLevel: number; | |
force: number; | |
musculature: number; | |
endurance: number; | |
fightStyles: number[]; | |
carSkill: number; | |
dSkill: number; | |
taxiSkill: number; | |
productJobSkill: number; | |
addVipIsActive: boolean; | |
euro: number; | |
bitcoin: number; | |
vipName: string; | |
jobName: string; | |
orgName: string; | |
isLeader: boolean; | |
upgrades: ArizonaAPIProfileUpgrades; | |
tidexAccess: null; // TODO: find | |
loadedAt: number; | |
serverId: number; | |
serverName: string; | |
weaponSkills: ArizonaAPIProfileWeaponSkills; | |
} | |
export interface ArizonaAPIIntentoryItem { | |
itemId: number; | |
slotId: number; | |
modelId: number; | |
label: string; | |
amount: string; | |
} | |
export interface ArizonaAPIServerRatingPlayer { | |
name: string; | |
isOnline: boolean; | |
} | |
export interface ArizonaAPIServerRatingOldestPlayer extends ArizonaAPIServerRatingPlayer { | |
level: number; | |
} | |
export type ArizonaAPIServerRatingType = 'oldest-players' | 'richest-players'; | |
export interface ArizonaAPIFractionPlayer { | |
id: number; | |
name: string; | |
rank: number; | |
label: string; | |
rankLabel: string; | |
isLeader: boolean; | |
} | |
export interface ArizonaAPIFractionItem { | |
id: number; | |
name: string; | |
} | |
export interface ArizonaAPIMapHouse { | |
id: number; | |
cost: number; | |
isOwned: boolean; | |
x: number; | |
y: number; | |
} | |
export interface ArizonaAPIMapCasino { | |
balance: number; | |
name: string; | |
cost: number; | |
deal: number; | |
x: number; | |
y: number; | |
} | |
export interface ArizonaAPIMapBusiness { | |
id: number; | |
name: string; | |
cost: number; | |
isOwned: boolean; | |
x: number; | |
y: number; | |
} | |
// options | |
export interface ArizonaAPIServerRatingOptions<T extends ArizonaAPIServerRatingType = ArizonaAPIServerRatingType> { | |
type: T; | |
serverId: number; | |
} | |
export interface ArizonaAPIServerMapOptions { | |
serverId: number; | |
} | |
export interface ArizonaAPIAccountChangePasswordCheckOptions { | |
resetToken: string; | |
timestamp: string; | |
accessString: string; | |
} | |
export interface ArizonaAPIAccountChangePasswordOptions { | |
currentPassword: string; | |
newPassword: string; | |
token: string; | |
recaptchaToken: string; | |
} | |
export interface ArizonaAPIAccountSwitchOff2FaTotpOptions { | |
resetToken: string; | |
timestamp: string; | |
accessString: string; | |
} | |
export interface ArizonaAPITwoFactorCheckOptions { | |
type: string; | |
hash: string; | |
secret: string; | |
} | |
export interface ArizonaAPITwoFactorCheckTotpOptions { | |
type: 'totp'; | |
hash: string; | |
secret: string; | |
reason?: 'off'; | |
} | |
export interface ArizonaAPIProfilePayHouseTaxOptions { | |
houseId: number; | |
} | |
export interface ArizonaAPIProfilePayBusinessTaxOptions { | |
businessId: number; | |
} | |
export interface ArizonaAPIAuthPasswordOptions { | |
username: string; | |
password: string; | |
serverId: number; | |
recaptchaToken: string; | |
} | |
export interface ArizonaAPIGamerFindOptions { | |
username: string; | |
serverId: number; | |
recaptchaToken: string; | |
} | |
export interface ArizonaAPIFractionGetPlayersOptions { | |
serverId: number; | |
fractionId: number; | |
} | |
// responses | |
export interface ArizonaAPIServerRatingResponse<RatingType extends ArizonaAPIServerRatingOptions['type'] = ArizonaAPIServerRatingOptions['type']> { | |
status: 1; | |
ratingType: RatingType; | |
rationName: string; | |
availableRatings: Record<RatingType, string>; | |
serverId: number; | |
items: | |
(RatingType extends 'oldest-players' ? ArizonaAPIServerRatingOldestPlayer : ArizonaAPIServerRatingPlayer)[] | |
} | |
export type ArizonaAPIServerGetAllResponse = ArizonaAPIGameServer[]; | |
export interface ArizonaAPIServerMapResponse { | |
businesses: ArizonaAPIMapBusiness[]; | |
casinos: ArizonaAPIMapCasino[]; | |
houses: ArizonaAPIMapHouse[]; | |
} | |
export interface ArizonaAPIAccountChangePasswordRequestResponse { | |
status: 'success'; | |
} | |
export interface ArizonaAPIAccountChangePasswordCheckResponse { | |
status: 'success'; | |
resetToken: string; | |
} | |
export interface ArizonaAPIAccountChangePasswordResponse { | |
status: 'success'; | |
} | |
export interface ArizonaAPIAccountSwitch2FaTotpResponse { | |
status: 'success'; | |
type: 'totp'; | |
hash: string; | |
secret: string; | |
uri: string; | |
action: 'switch-on'; | |
} | |
export interface ArizonaAPIAccountSwitchOff2FaTotpRequestResponse { | |
status: 'success'; | |
} | |
export interface ArizonaAPIAccountSwitchOff2FaTotpResponse { | |
status: 'success'; | |
} | |
export type ArizonaAPITwoFactorCheckResponse = ArizonaAPIAuthTokens; | |
export interface ArizonaAPITwoFactorCheckTotpResponse { | |
status: 'success'; | |
} | |
export interface ArizonaAPIProfileGetGeneralDataResponse { | |
user: ArizonaAPIProfileUserInfo; | |
} | |
export interface ArizonaAPIProfileGetInventoryDataResponse { | |
items: ArizonaAPIIntentoryItem[]; | |
} | |
export interface ArizonaAPIProfilePayHouseTaxResponse { | |
status: 'success'; | |
ids: number[]; | |
} | |
export interface ArizonaAPIProfilePayBusinessTaxResponse { | |
status: 'success'; | |
ids: number[]; | |
} | |
export interface ArizonaAPIAuthPasswordTokensResponse { | |
need2FA: false; | |
tokens: ArizonaAPIAuthTokens; | |
} | |
export interface ArizonaAPIAuthPasswordNeed2FAEmailResponse { | |
need2FA: true; | |
type: 'email'; | |
hash: string; | |
} | |
export type ArizonaAPIAuthPasswordNeed2FA = | |
| ArizonaAPIAuthPasswordNeed2FAEmailResponse; | |
export type ArizonaAPIAuthPasswordResponse = | |
| ArizonaAPIAuthPasswordTokensResponse | |
| ArizonaAPIAuthPasswordNeed2FA; | |
export interface ArizonaAPIGamerFindResponse { | |
status: 1; | |
id: number; | |
username: string; | |
cash: number; | |
bank: number; | |
individualAccount: number; | |
deposit: number; | |
level: number; | |
isOnline: number; | |
vipLevel: number; | |
job: number; | |
fraction: number; | |
rank: number; | |
phoneNumber: number; | |
vipName: string; | |
jobName: string; | |
orgName: string; | |
isLeader: boolean; | |
rangName: string; | |
} | |
export interface ArizonaAPIFractionGetPlayersResponse { | |
status: 1; | |
items: ArizonaAPIFractionPlayer[]; | |
organizationLabel: string; | |
organizations: ArizonaAPIFractionItem[]; | |
} | |
export class ArizonaAPIMethodsBase { | |
constructor(private api: ArizonaAPI) {} | |
protected get session(): AxiosInstance { | |
// @ts-ignore | |
return this.api.session; | |
} | |
} | |
export class ArizonaAPIServerMethods extends ArizonaAPIMethodsBase { | |
getAll(): Promise<ArizonaAPIServerGetAllResponse> { | |
return this.session | |
.get('/server/get-all') | |
.then(exportDataField); | |
} | |
rating<T extends ArizonaAPIServerRatingType>(data: ArizonaAPIServerRatingOptions<T>): Promise<ArizonaAPIServerRatingResponse<T>> { | |
return this.session | |
.get(`/rating`, { | |
params: data | |
}) | |
.then(exportDataField); | |
} | |
map(data: ArizonaAPIServerMapOptions): Promise<ArizonaAPIServerMapResponse> { | |
return this.session | |
.get(`/map`, { | |
params: data | |
}) | |
.then(exportDataField); | |
} | |
} | |
export class ArizonaAPIAccountMethods extends ArizonaAPIMethodsBase { | |
changePasswordRequest(): Promise<ArizonaAPIAccountChangePasswordRequestResponse> { | |
return this.session | |
.post('/account/change-password-request') | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
changePasswordCheck(data: ArizonaAPIAccountChangePasswordCheckOptions): Promise<ArizonaAPIAccountChangePasswordCheckResponse> { | |
return this.session | |
.post('/account/change-password-check', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
changePassword(data: ArizonaAPIAccountChangePasswordOptions): Promise<ArizonaAPIAccountChangePasswordResponse> { | |
return this.session | |
.post('/account/change-password', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
switch2FaTotp(): Promise<ArizonaAPIAccountSwitch2FaTotpResponse> { | |
return this.session | |
.post('/account/switch-2fatotp') | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
switchOff2FaTotpRequest(): Promise<ArizonaAPIAccountSwitchOff2FaTotpRequestResponse> { | |
return this.session | |
.post('/account/switch-off-2fatotp-request') | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
switchOff2FaTotp(data: ArizonaAPIAccountSwitchOff2FaTotpOptions): Promise<ArizonaAPIAccountSwitchOff2FaTotpResponse> { | |
return this.session | |
.post('/account/switch-off-2fatotp', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
} | |
export class ArizonaAPITwoFactorMethods extends ArizonaAPIMethodsBase { | |
check(data: ArizonaAPITwoFactorCheckOptions): Promise<ArizonaAPITwoFactorCheckResponse> { | |
return this.session | |
.post('/two-factor/check', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
checkTotp(data: ArizonaAPITwoFactorCheckTotpOptions): Promise<ArizonaAPITwoFactorCheckTotpResponse> { | |
return this.session | |
.post('/two-factor/check-totp', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
} | |
export class ArizonaAPIProfileMethods extends ArizonaAPIMethodsBase { | |
getGeneralData(): Promise<ArizonaAPIProfileGetGeneralDataResponse> { | |
return this.session | |
.get('/profile/general-data') | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
getInventoryData(): Promise<ArizonaAPIProfileGetInventoryDataResponse> { | |
return this.session | |
.get('/profile/inventory-data') | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
payHouseTax(data: ArizonaAPIProfilePayHouseTaxOptions): Promise<ArizonaAPIProfilePayHouseTaxResponse> { | |
return this.session | |
.post('/profile/pay-house-tax', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
payBusinessTax(data: ArizonaAPIProfilePayBusinessTaxOptions): Promise<ArizonaAPIProfilePayBusinessTaxResponse> { | |
return this.session | |
.post('/profile/pay-business-tax', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
} | |
export class ArizonaAPIAuthMethods extends ArizonaAPIMethodsBase { | |
async password( | |
data: ArizonaAPIAuthPasswordOptions | |
): Promise<ArizonaAPIAuthPasswordResponse> { | |
const res = await this.session | |
.post('/auth/by-password', data, { | |
validateStatus: (status) => status < 400 || status === 423, | |
}) | |
.catch(catchAPIError); | |
if (res.status === 423) | |
return {need2FA: true, ...res.data}; | |
return {need2FA: false, tokens: res.data}; | |
} | |
} | |
export class ArizonaAPIGamerMethods extends ArizonaAPIMethodsBase { | |
find(data: ArizonaAPIGamerFindOptions): Promise<ArizonaAPIGamerFindResponse> { | |
return this.session | |
.post('/gamer/find', data) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
} | |
export class ArizonaAPIFractionMethods extends ArizonaAPIMethodsBase { | |
getPlayers(data: ArizonaAPIFractionGetPlayersOptions): Promise<ArizonaAPIFractionGetPlayersResponse> { | |
return this.session | |
.get('/fraction/get-players', { | |
params: data | |
}) | |
.then(exportDataField) | |
.catch(catchAPIError); | |
} | |
} | |
export class ArizonaAPI { | |
private session = axios.create({ | |
baseURL: 'https://backend.arizona-rp.com', | |
headers: { | |
referer: 'https://arizona-rp.com', | |
}, | |
}); | |
public server = new ArizonaAPIServerMethods(this); | |
public account = new ArizonaAPIAccountMethods(this); | |
public twoFactor = new ArizonaAPITwoFactorMethods(this); | |
public profile = new ArizonaAPIProfileMethods(this); | |
public auth = new ArizonaAPIAuthMethods(this); | |
public gamer = new ArizonaAPIGamerMethods(this); | |
public fraction = new ArizonaAPIFractionMethods(this); | |
constructor(opts: ArizonaAPIOptions = {}) { | |
if (opts.accessToken) this.setAccessToken(opts.accessToken); | |
if (opts.apiUrl) this.session.defaults.baseURL = opts.apiUrl; | |
} | |
setAccessToken(token: string) { | |
this.session.defaults.headers.common['Authorization'] = `Bearer ${token}`; | |
return this; | |
} | |
resetAccessToken() { | |
delete this.session.defaults.headers.common['Authorization']; | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment