node-ts-screeps-api 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/src/rawApi.d.ts +7 -0
- package/package.json +1 -1
- package/src/rawApi.ts +183 -175
package/dist/src/rawApi.d.ts
CHANGED
|
@@ -100,4 +100,11 @@ export declare class RawApi<T extends AuthType> {
|
|
|
100
100
|
width: number;
|
|
101
101
|
height: number;
|
|
102
102
|
}>;
|
|
103
|
+
getUserRooms(args: {
|
|
104
|
+
interval: number;
|
|
105
|
+
}): Promise<{
|
|
106
|
+
ok: number;
|
|
107
|
+
shards: Record<string, string[]>;
|
|
108
|
+
reservations: Record<string, string[]>;
|
|
109
|
+
}>;
|
|
103
110
|
}
|
package/package.json
CHANGED
package/src/rawApi.ts
CHANGED
|
@@ -1,175 +1,183 @@
|
|
|
1
|
-
/* eslint-disable camelcase */
|
|
2
|
-
import { RawApiPostData, RawApiReturnData } from "./apiType";
|
|
3
|
-
import axios, { AxiosInstance } from "axios";
|
|
4
|
-
import { promisify } from "util";
|
|
5
|
-
import { gunzip, inflate, InputType } from "zlib";
|
|
6
|
-
import { ApiConfig, AuthType, Badge, BasicRequestMethod, MyUserInfo, RequestOpts } from "./type";
|
|
7
|
-
import { RoomObjectReturn } from "./rawApiType/roomObjects";
|
|
8
|
-
|
|
9
|
-
const gunzipAsync = promisify(gunzip);
|
|
10
|
-
const inflateAsync = promisify(inflate);
|
|
11
|
-
export class RawApi<T extends AuthType> {
|
|
12
|
-
public apiConfig: ApiConfig<T>;
|
|
13
|
-
public xToken?: string;
|
|
14
|
-
private http?: AxiosInstance;
|
|
15
|
-
public opts?: Partial<RequestOpts>;
|
|
16
|
-
private baseUrl: string;
|
|
17
|
-
public constructor(apiConfig: ApiConfig<T>) {
|
|
18
|
-
this.apiConfig = apiConfig;
|
|
19
|
-
const { protocol, hostname, path, port } = this.apiConfig.hostInfo;
|
|
20
|
-
this.baseUrl =
|
|
21
|
-
protocol !== "localhost" ? `${protocol}://${hostname}${path}` : `http://${hostname}:${port}${path}`;
|
|
22
|
-
}
|
|
23
|
-
private setServer(opts: Partial<RequestOpts>): void {
|
|
24
|
-
if (!this.opts) {
|
|
25
|
-
this.opts = {};
|
|
26
|
-
}
|
|
27
|
-
Object.assign(this.opts, opts);
|
|
28
|
-
const xToken = opts.xToken;
|
|
29
|
-
if (xToken) {
|
|
30
|
-
this.xToken = xToken;
|
|
31
|
-
}
|
|
32
|
-
this.http = axios.create({
|
|
33
|
-
baseURL: this.opts.url
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
private async gz<T>(data: string): Promise<T> {
|
|
37
|
-
const buf = Buffer.from(data.slice(3), "base64");
|
|
38
|
-
const ret = (await gunzipAsync(buf as unknown as InputType)) as { toString: () => string };
|
|
39
|
-
return JSON.parse(ret.toString()) as T;
|
|
40
|
-
}
|
|
41
|
-
public async req<T>(method: BasicRequestMethod, path: string, body = {}, headers = {}): Promise<T> {
|
|
42
|
-
this.setServer({ method, url: this.baseUrl, headers });
|
|
43
|
-
const opts: RequestOpts = {
|
|
44
|
-
method,
|
|
45
|
-
url: path,
|
|
46
|
-
headers: {}
|
|
47
|
-
};
|
|
48
|
-
if (this.xToken) {
|
|
49
|
-
Object.assign(opts.headers, {
|
|
50
|
-
"X-Token": this.xToken,
|
|
51
|
-
"X-Username": this.xToken
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
if (method === "GET") {
|
|
55
|
-
opts.params = body;
|
|
56
|
-
} else {
|
|
57
|
-
opts.data = body;
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
if (!this.http) throw new Error("no server");
|
|
61
|
-
const res: { data: { data: string }; headers: { "x-token": string } } = await this.http(opts);
|
|
62
|
-
const token = res.headers["x-token"];
|
|
63
|
-
if (token) {
|
|
64
|
-
this.xToken = token;
|
|
65
|
-
}
|
|
66
|
-
if (typeof res.data.data === "string" && res.data.data.slice(0, 3) === "gz:") {
|
|
67
|
-
res.data.data = await this.gz(res.data.data);
|
|
68
|
-
}
|
|
69
|
-
return res.data as unknown as T;
|
|
70
|
-
} catch (err) {
|
|
71
|
-
if ((err as { response: any }).response) {
|
|
72
|
-
throw new Error(JSON.stringify(((err as { response: any }).response as { data: any }).data));
|
|
73
|
-
}
|
|
74
|
-
throw err;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public async inflate(data: string): Promise<string> {
|
|
79
|
-
const buf = Buffer.from(data.slice(3), "base64");
|
|
80
|
-
const ret = (await inflateAsync(buf as unknown as InputType)) as Buffer;
|
|
81
|
-
return JSON.parse(ret.toString()) as string;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public async auth(): Promise<RawApiReturnData<"signinByPassword">> {
|
|
85
|
-
const data = await this.req("POST", "/api/auth/signin", this.apiConfig.authInfo);
|
|
86
|
-
return data as Promise<RawApiReturnData<"signinByPassword">>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public async getSegment(args: RawApiPostData<"getSegment">): Promise<RawApiReturnData<"getSegment">> {
|
|
90
|
-
return this.req("GET", "/api/user/memory-segment", args);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public async postSegment(args: RawApiPostData<"postSegment">): Promise<RawApiReturnData<"postSegment">> {
|
|
94
|
-
return this.req("POST", "/api/user/memory-segment", args);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public async getMemory(args: { path?: string; shard?: string }): Promise<{ ok: number; data?: any }> {
|
|
98
|
-
return this.req("GET", "/api/user/memory", args);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public async postMemory(args: { path?: string; value: any; shard?: string }): Promise<{ ok: number; data?: any }> {
|
|
102
|
-
return this.req("POST", "/api/user/memory", args);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public async findUser(username: string): Promise<{
|
|
106
|
-
ok: number;
|
|
107
|
-
user: {
|
|
108
|
-
_id: string;
|
|
109
|
-
username: string;
|
|
110
|
-
badge: Badge;
|
|
111
|
-
gcl: number;
|
|
112
|
-
};
|
|
113
|
-
}> {
|
|
114
|
-
return this.req("GET", "/api/user/find", { username });
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public async me(): Promise<MyUserInfo> {
|
|
118
|
-
return this.req("GET", "/api/auth/me");
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public async queryToken(args: { token: string }): Promise<{ token: string }> {
|
|
122
|
-
return this.req("GET", "/api/auth/query-token", args);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public async myName(): Promise<{ username: string }> {
|
|
126
|
-
return this.req("GET", "/api/user/name");
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
public async createConstruction(args: {
|
|
130
|
-
room: string;
|
|
131
|
-
x: number;
|
|
132
|
-
y: number;
|
|
133
|
-
structureType: string;
|
|
134
|
-
name: string;
|
|
135
|
-
shard?: string;
|
|
136
|
-
}): Promise<{
|
|
137
|
-
ok: number;
|
|
138
|
-
result: {
|
|
139
|
-
ok: number;
|
|
140
|
-
n: number;
|
|
141
|
-
};
|
|
142
|
-
ops: [
|
|
143
|
-
{
|
|
144
|
-
type: string;
|
|
145
|
-
room: string;
|
|
146
|
-
x: number;
|
|
147
|
-
y: number;
|
|
148
|
-
structureType: string;
|
|
149
|
-
user: string;
|
|
150
|
-
progress: number;
|
|
151
|
-
progressTotal: number;
|
|
152
|
-
_id: string;
|
|
153
|
-
}
|
|
154
|
-
];
|
|
155
|
-
insertedCount: number;
|
|
156
|
-
insertedIds: string[];
|
|
157
|
-
}> {
|
|
158
|
-
return this.req("POST", "/api/game/create-construction", args);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public async getRoomObjects(args: { room: string; shard?: string }): Promise<RoomObjectReturn> {
|
|
162
|
-
return this.req("GET", "/api/game/room-objects", args);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public async getEncodedRoomTerrain(args: {
|
|
166
|
-
room: string;
|
|
167
|
-
shard?: string;
|
|
168
|
-
}): Promise<{ ok: number; terrain: [{ _id: string; room: string; terrain: string; type: string }] }> {
|
|
169
|
-
return this.req("GET", "/api/game/room-terrain", { ...args, encoded: 1 });
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
public async getWorldSize(args: { shard: string }): Promise<{ ok: number; width: number; height: number }> {
|
|
173
|
-
return this.req("GET", "/api/game/world-size", args);
|
|
174
|
-
}
|
|
175
|
-
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import { RawApiPostData, RawApiReturnData } from "./apiType";
|
|
3
|
+
import axios, { AxiosInstance } from "axios";
|
|
4
|
+
import { promisify } from "util";
|
|
5
|
+
import { gunzip, inflate, InputType } from "zlib";
|
|
6
|
+
import { ApiConfig, AuthType, Badge, BasicRequestMethod, MyUserInfo, RequestOpts } from "./type";
|
|
7
|
+
import { RoomObjectReturn } from "./rawApiType/roomObjects";
|
|
8
|
+
|
|
9
|
+
const gunzipAsync = promisify(gunzip);
|
|
10
|
+
const inflateAsync = promisify(inflate);
|
|
11
|
+
export class RawApi<T extends AuthType> {
|
|
12
|
+
public apiConfig: ApiConfig<T>;
|
|
13
|
+
public xToken?: string;
|
|
14
|
+
private http?: AxiosInstance;
|
|
15
|
+
public opts?: Partial<RequestOpts>;
|
|
16
|
+
private baseUrl: string;
|
|
17
|
+
public constructor(apiConfig: ApiConfig<T>) {
|
|
18
|
+
this.apiConfig = apiConfig;
|
|
19
|
+
const { protocol, hostname, path, port } = this.apiConfig.hostInfo;
|
|
20
|
+
this.baseUrl =
|
|
21
|
+
protocol !== "localhost" ? `${protocol}://${hostname}${path}` : `http://${hostname}:${port}${path}`;
|
|
22
|
+
}
|
|
23
|
+
private setServer(opts: Partial<RequestOpts>): void {
|
|
24
|
+
if (!this.opts) {
|
|
25
|
+
this.opts = {};
|
|
26
|
+
}
|
|
27
|
+
Object.assign(this.opts, opts);
|
|
28
|
+
const xToken = opts.xToken;
|
|
29
|
+
if (xToken) {
|
|
30
|
+
this.xToken = xToken;
|
|
31
|
+
}
|
|
32
|
+
this.http = axios.create({
|
|
33
|
+
baseURL: this.opts.url
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
private async gz<T>(data: string): Promise<T> {
|
|
37
|
+
const buf = Buffer.from(data.slice(3), "base64");
|
|
38
|
+
const ret = (await gunzipAsync(buf as unknown as InputType)) as { toString: () => string };
|
|
39
|
+
return JSON.parse(ret.toString()) as T;
|
|
40
|
+
}
|
|
41
|
+
public async req<T>(method: BasicRequestMethod, path: string, body = {}, headers = {}): Promise<T> {
|
|
42
|
+
this.setServer({ method, url: this.baseUrl, headers });
|
|
43
|
+
const opts: RequestOpts = {
|
|
44
|
+
method,
|
|
45
|
+
url: path,
|
|
46
|
+
headers: {}
|
|
47
|
+
};
|
|
48
|
+
if (this.xToken) {
|
|
49
|
+
Object.assign(opts.headers, {
|
|
50
|
+
"X-Token": this.xToken,
|
|
51
|
+
"X-Username": this.xToken
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (method === "GET") {
|
|
55
|
+
opts.params = body;
|
|
56
|
+
} else {
|
|
57
|
+
opts.data = body;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
if (!this.http) throw new Error("no server");
|
|
61
|
+
const res: { data: { data: string }; headers: { "x-token": string } } = await this.http(opts);
|
|
62
|
+
const token = res.headers["x-token"];
|
|
63
|
+
if (token) {
|
|
64
|
+
this.xToken = token;
|
|
65
|
+
}
|
|
66
|
+
if (typeof res.data.data === "string" && res.data.data.slice(0, 3) === "gz:") {
|
|
67
|
+
res.data.data = await this.gz(res.data.data);
|
|
68
|
+
}
|
|
69
|
+
return res.data as unknown as T;
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if ((err as { response: any }).response) {
|
|
72
|
+
throw new Error(JSON.stringify(((err as { response: any }).response as { data: any }).data));
|
|
73
|
+
}
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public async inflate(data: string): Promise<string> {
|
|
79
|
+
const buf = Buffer.from(data.slice(3), "base64");
|
|
80
|
+
const ret = (await inflateAsync(buf as unknown as InputType)) as Buffer;
|
|
81
|
+
return JSON.parse(ret.toString()) as string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public async auth(): Promise<RawApiReturnData<"signinByPassword">> {
|
|
85
|
+
const data = await this.req("POST", "/api/auth/signin", this.apiConfig.authInfo);
|
|
86
|
+
return data as Promise<RawApiReturnData<"signinByPassword">>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public async getSegment(args: RawApiPostData<"getSegment">): Promise<RawApiReturnData<"getSegment">> {
|
|
90
|
+
return this.req("GET", "/api/user/memory-segment", args);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public async postSegment(args: RawApiPostData<"postSegment">): Promise<RawApiReturnData<"postSegment">> {
|
|
94
|
+
return this.req("POST", "/api/user/memory-segment", args);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public async getMemory(args: { path?: string; shard?: string }): Promise<{ ok: number; data?: any }> {
|
|
98
|
+
return this.req("GET", "/api/user/memory", args);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public async postMemory(args: { path?: string; value: any; shard?: string }): Promise<{ ok: number; data?: any }> {
|
|
102
|
+
return this.req("POST", "/api/user/memory", args);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public async findUser(username: string): Promise<{
|
|
106
|
+
ok: number;
|
|
107
|
+
user: {
|
|
108
|
+
_id: string;
|
|
109
|
+
username: string;
|
|
110
|
+
badge: Badge;
|
|
111
|
+
gcl: number;
|
|
112
|
+
};
|
|
113
|
+
}> {
|
|
114
|
+
return this.req("GET", "/api/user/find", { username });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async me(): Promise<MyUserInfo> {
|
|
118
|
+
return this.req("GET", "/api/auth/me");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public async queryToken(args: { token: string }): Promise<{ token: string }> {
|
|
122
|
+
return this.req("GET", "/api/auth/query-token", args);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public async myName(): Promise<{ username: string }> {
|
|
126
|
+
return this.req("GET", "/api/user/name");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public async createConstruction(args: {
|
|
130
|
+
room: string;
|
|
131
|
+
x: number;
|
|
132
|
+
y: number;
|
|
133
|
+
structureType: string;
|
|
134
|
+
name: string;
|
|
135
|
+
shard?: string;
|
|
136
|
+
}): Promise<{
|
|
137
|
+
ok: number;
|
|
138
|
+
result: {
|
|
139
|
+
ok: number;
|
|
140
|
+
n: number;
|
|
141
|
+
};
|
|
142
|
+
ops: [
|
|
143
|
+
{
|
|
144
|
+
type: string;
|
|
145
|
+
room: string;
|
|
146
|
+
x: number;
|
|
147
|
+
y: number;
|
|
148
|
+
structureType: string;
|
|
149
|
+
user: string;
|
|
150
|
+
progress: number;
|
|
151
|
+
progressTotal: number;
|
|
152
|
+
_id: string;
|
|
153
|
+
}
|
|
154
|
+
];
|
|
155
|
+
insertedCount: number;
|
|
156
|
+
insertedIds: string[];
|
|
157
|
+
}> {
|
|
158
|
+
return this.req("POST", "/api/game/create-construction", args);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public async getRoomObjects(args: { room: string; shard?: string }): Promise<RoomObjectReturn> {
|
|
162
|
+
return this.req("GET", "/api/game/room-objects", args);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public async getEncodedRoomTerrain(args: {
|
|
166
|
+
room: string;
|
|
167
|
+
shard?: string;
|
|
168
|
+
}): Promise<{ ok: number; terrain: [{ _id: string; room: string; terrain: string; type: string }] }> {
|
|
169
|
+
return this.req("GET", "/api/game/room-terrain", { ...args, encoded: 1 });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public async getWorldSize(args: { shard: string }): Promise<{ ok: number; width: number; height: number }> {
|
|
173
|
+
return this.req("GET", "/api/game/world-size", args);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public async getUserRooms(args: { interval: number }): Promise<{
|
|
177
|
+
ok: number;
|
|
178
|
+
shards: Record<string, string[]>;
|
|
179
|
+
reservations: Record<string, string[]>;
|
|
180
|
+
}> {
|
|
181
|
+
return this.req("GET", "/api/user/rooms", args);
|
|
182
|
+
}
|
|
183
|
+
}
|