/
index.ts
198 lines (166 loc) · 4.42 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { AvailableLoan } from "./AvailableLoan";
import { AvailableShip } from "./AvailableShip";
import { Loan } from "./Loan";
import { LoanType } from "./LoanType";
import { Ship } from "./Ship";
import { System } from "./System";
import { User } from "./User";
import Bottleneck from "bottleneck";
import { Location } from "./Location";
const getUrl = (segment: string) => `https://api.spacetraders.io/${segment}`;
const limiter = new Bottleneck({
maxConcurrent: 2,
minTime: 500,
});
export type GetStatusResponse = {
status: string;
};
export const getStatus = async () => {
const json: GetStatusResponse = await get("game/status");
return json;
};
const makeRequest = async (
url: string,
method: "GET" | "POST",
headers: any,
data: any = undefined,
retry = 0
) => {
const response = await fetch(url, {
method,
headers: {
"Content-Type": "application/json",
...headers,
},
body: data ? JSON.stringify(data) : undefined,
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error.message);
}
return result;
};
const get = (urlSegment: string, headers = {}) => {
return limiter.schedule(() =>
makeRequest(getUrl(urlSegment), "GET", headers)
);
};
const post = (urlSegment: string, data?: any, headers = {}) => {
return limiter.schedule(() =>
makeRequest(getUrl(urlSegment), "POST", headers, data)
);
};
export interface GetTokenResponse {
token: string;
user: User;
}
export const getToken = async (username: string): Promise<GetTokenResponse> => {
const json = await post(`users/${username}/token`);
return json;
};
const postSecure = async (token: string, urlSegment: string, data?: any) => {
return post(urlSegment, data, {
Authorization: `Bearer ${token}`,
});
};
const getSecure = async (token: string, urlSegment: string) => {
return get(urlSegment, {
Authorization: `Bearer ${token}`,
});
};
interface GetAvailableLoansResponse {
loans: AvailableLoan[];
}
export const getAvailableLoans = (
token: string
): Promise<GetAvailableLoansResponse> => getSecure(token, "game/loans");
interface GetLoansResponse {
loans: Loan[];
}
export const getLoans = (
token: string,
username: string
): Promise<GetLoansResponse> => getSecure(token, `users/${username}/loans`);
export const requestNewLoan = (
token: string,
username: string,
type: LoanType
): Promise<GetAvailableLoansResponse> =>
postSecure(token, `users/${username}/loans`, { type });
interface GetAvailableShipsResponse {
ships: AvailableShip[];
}
export const getAvailableShips = (
token: string
): Promise<GetAvailableShipsResponse> => getSecure(token, "game/ships");
export interface GetSystemsResponse {
systems: System[];
}
export const getSystems = (token: string): Promise<GetSystemsResponse> =>
getSecure(token, "game/systems");
type GetMarketResponse = {
planet: Location;
};
export const getMarket = (
token: string,
symbol: string
): Promise<GetMarketResponse> =>
getSecure(token, `game/locations/${symbol}/marketplace`);
interface GetShipsResponse {
ships: Ship[];
}
export const getShips = (
token: string,
username: string
): Promise<GetShipsResponse> => getSecure(token, `users/${username}/ships`);
export const buyShip = (
token: string,
username: string,
location: string,
type: string
): Promise<AvailableShip> =>
postSecure(token, `users/${username}/ships`, { location, type });
export const purchaseOrder = (
token: string,
username: string,
shipId: string,
good: string,
quantity: number
): Promise<any> =>
postSecure(token, `users/${username}/purchase-orders`, {
shipId,
good,
quantity,
});
export const sellOrder = (
token: string,
username: string,
shipId: string,
good: string,
quantity: number
): Promise<any> =>
postSecure(token, `users/${username}/sell-orders`, {
shipId,
good,
quantity,
});
export const getFlightPlans = (token: string, symbol: string): Promise<any> =>
getSecure(token, `game/systems/${symbol}/flight-plans`);
export const newFlightPlan = (
token: string,
username: string,
shipId: string,
destination: string
): Promise<any> =>
postSecure(token, `users/${username}/flight-plans`, { shipId, destination });
type GetUserResponse = {
user: {
credits: number;
ships: Ship[];
loans: Loan[];
};
};
export const getUser = (
token: string,
username: string
): Promise<GetUserResponse> => getSecure(token, `users/${username}`);