-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
178 lines (161 loc) · 4.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Types
*/
type TRegions = IRegion[];
interface IRegion {
id: string;
name: string
}
type TFriends = string[];
interface IEmission {
currentStart: number;
previousStart: number;
previousEnd: number;
};
interface IAuctionHistory {
total: number;
prices: IAuctionItem[];
}
interface IAuctionItem {
amount: number;
price: number;
time: string;
}
interface IAuctionLots {
total: number;
lots: IAuctionLot[];
}
interface IAuctionLot {
itemId: string;
startPrice: number;
currentPrice: number;
buyoutPrice: number;
startTime: string;
endTime: string;
additional: {
property1: string;
property2: string;
}
}
type TCharacters = ICharacter[];
interface ICharacter {
information: {
id: string;
name: string;
creationTime: string;
},
clan: {
info: IClan,
member: {
name: string;
rank: string;
joinTime: string;
}
}
}
interface IClan {
id: string;
name: string;
tag: string;
level: number;
levelPoints: number;
registrationTime: string;
alliance: string;
description: string;
leader: string;
memberCount: number;
}
interface IClans {
totalClans: number;
data: IClan[];
};
type TClanMembers = IClanMember[];
interface IClanMember {
name: string;
rank: string;
joinTime: string;
}
interface IError {
title: string;
status: number;
details: object;
}
class Utils {
public generateOAuthUrl(appId: string, redirectUrl: string): string {
return `https://exbo.net/oauth/authorize?client_id=${appId}&redirect_uri=${redirectUrl}&scope=&response_type=code`;
}
}
/**
* Implementation
*/
class Stalcraft {
private utils: Utils = new Utils();
private demoUrl: string = "https://dapi.stalcraft.net";
private prodUrl: string = "https://eapi.stalcraft.net";
private githubDbUrl: string = "https://api.github.com/repos/EXBO-Studio/stalcraft-database/contents/";
private url: string;
constructor(private appToken: string, private appId: string, demoMode: boolean = false) {
this.url = demoMode ? this.demoUrl : this.prodUrl;
}
private async request(url: string, endpoint: string, method: string = "GET", params?: string, userToken?: string) {
const requestUrl = params ? `${endpoint}?${params}` : endpoint;
try {
const req = await fetch(`${url}/${requestUrl}`, {
method: method,
headers: {
Authorization: userToken ? `Bearer ${userToken}` : `Bearer ${this.appToken}`,
},
});
if (!req.ok) {
return { data: null, error: await req.json() as IError };
}
return { data: await req.json(), error: null };
} catch (error) {
return { data: null, error: error as IError };
}
}
public getOAuthUrl(redirectUrl: string) {
return this.utils.generateOAuthUrl(this.appId, redirectUrl);
}
public async getRegions() {
const { data, error } = await this.request(this.url, `regions`);
return { data: data as TRegions, error };
}
public async getAuctionHistory(region: string, item: string) {
const { data, error } = await this.request(this.url, `${region}/auction/${item}/history`);
return { data: data as IAuctionHistory, error };
}
public async getAuctionLots(region: string, item: string) {
const { data, error } = await this.request(this.url, `${region}/action/${item}/lots`);
return { data: data as IAuctionLots, error };
}
public async getCharacters(region: string, userToken: string) {
const { data, error } = await this.request(this.url, `${region}/characters`, "GET", undefined, userToken);
return { data: data as TCharacters, error };
}
public async getClan(region: string, clanId: string) {
const { data, error } = await this.request(this.url, `${region}/clan/${clanId}/info`);
return { data: data as IClan, error };
}
public async getClanMembers(region: string, clanId: string, userToken: string) {
const { data, error } = await this.request(this.url, `${region}/clan/${clanId}/members`, "GET", undefined, userToken);
return { data: data as TClanMembers, error };
}
public async getClans(region: string) {
const { data, error } = await this.request(this.url, `${region}/clans`);
return { data: data as IClans, error };
}
public async getEmission(region: string) {
const { data, error } = await this.request(this.url, `${region}/emission`);
return { data: data as IEmission, error };
}
public async getFriends(region: string, character: string, userToken: string) {
const { data, error } = await this.request(this.url, `${region}/friends/${character}`, "GET", undefined, userToken);
return { data: data as TFriends, error };
}
public async getItems(region: "global" | "ru") {
const { data, error } = await this.request(this.githubDbUrl, `${region}/`);
return { data, error };
}
}
export default Stalcraft;