node-ts-screeps-api 0.0.13 → 0.0.14
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/authInfoSample.ts +16 -1
- package/package.json +1 -1
- package/src/rawApi.ts +12 -0
- package/test/unit/api.test.ts +20 -21
- package/test/unit/officialServerApi.test.ts +33 -0
package/authInfoSample.ts
CHANGED
|
@@ -5,7 +5,8 @@ const userData = {
|
|
|
5
5
|
email: "notMyEmail@abc.com",
|
|
6
6
|
password: "notMyPassword"
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
export const localServerApiConfig: ApiConfig<"signinByPassword"> = {
|
|
9
10
|
authInfo: {
|
|
10
11
|
type: "signinByPassword",
|
|
11
12
|
email: userData.email,
|
|
@@ -18,3 +19,17 @@ export const apiConfig: ApiConfig<"signinByPassword"> = {
|
|
|
18
19
|
hostname: "127.0.0.1"
|
|
19
20
|
}
|
|
20
21
|
};
|
|
22
|
+
|
|
23
|
+
export const officialServerApiConfig: ApiConfig<"signinByPassword"> = {
|
|
24
|
+
authInfo: {
|
|
25
|
+
type: "signinByPassword",
|
|
26
|
+
email: userData.email,
|
|
27
|
+
password: userData.password
|
|
28
|
+
},
|
|
29
|
+
hostInfo: {
|
|
30
|
+
protocol: "https",
|
|
31
|
+
port: 443,
|
|
32
|
+
path: "/",
|
|
33
|
+
hostname: "screeps.com"
|
|
34
|
+
}
|
|
35
|
+
};
|
package/package.json
CHANGED
package/src/rawApi.ts
CHANGED
|
@@ -94,6 +94,18 @@ export class RawApi<T extends AuthType> {
|
|
|
94
94
|
return this.req("POST", "/api/user/memory-segment", args);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
public async getMemory(args: { path?: string; shard?: string }): Promise<{ ok: number; data: string }> {
|
|
98
|
+
return this.req("GET", "/api/user/memory", args);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public async postMemory(args: {
|
|
102
|
+
path?: string;
|
|
103
|
+
value: string;
|
|
104
|
+
shard?: string;
|
|
105
|
+
}): Promise<{ ok: number; data: string }> {
|
|
106
|
+
return this.req("POST", "/api/user/memory", args);
|
|
107
|
+
}
|
|
108
|
+
|
|
97
109
|
public async findUser(username: string): Promise<{
|
|
98
110
|
ok: number;
|
|
99
111
|
user: {
|
package/test/unit/api.test.ts
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
import * as assert from "assert";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { localServerApiConfig } from "../../authInfo";
|
|
4
4
|
import { writeFileSync } from "fs";
|
|
5
5
|
import { ScreepsApi } from "../../src";
|
|
6
|
-
import { ApiConfig } from "../../src/type";
|
|
7
6
|
// 上面的userData需要自己在根目录创建,示例参照根目录的authInfoSample.ts
|
|
8
7
|
describe("api", () => {
|
|
9
8
|
describe("create api", () => {
|
|
10
|
-
const api = new ScreepsApi(
|
|
9
|
+
const api = new ScreepsApi(localServerApiConfig);
|
|
11
10
|
const rawApi = api.rawApi;
|
|
12
11
|
const socket = api.socket;
|
|
13
12
|
it("is apiConfig stored properly", () => {
|
|
14
|
-
assert.deepStrictEqual(api.apiConfig,
|
|
13
|
+
assert.deepStrictEqual(api.apiConfig, localServerApiConfig);
|
|
15
14
|
});
|
|
16
15
|
|
|
17
16
|
it("throw Error when unauthorized", async () => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
let anErr;
|
|
18
|
+
try {
|
|
19
|
+
await rawApi.postSegment({ segment: 30, data: "test" });
|
|
20
|
+
} catch (err) {
|
|
21
|
+
anErr = err;
|
|
22
|
+
const message = (err as Error).message;
|
|
23
|
+
assert.strictEqual(message, `{"error":"unauthorized"}`);
|
|
24
|
+
}
|
|
25
|
+
if (!anErr) assert.fail("no error was thrown");
|
|
27
26
|
});
|
|
28
27
|
|
|
29
28
|
it("test socket", async () => {
|
|
@@ -61,16 +60,16 @@ describe("api", () => {
|
|
|
61
60
|
|
|
62
61
|
const testStr = `test${Date.now()}`;
|
|
63
62
|
it("post segment", async () => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
const data = await rawApi.postSegment({ shard: "shard3", segment: 30, data: testStr });
|
|
64
|
+
console.log(data);
|
|
65
|
+
assert.strictEqual(data.ok, 1);
|
|
67
66
|
});
|
|
68
67
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
68
|
+
it("get segment", async () => {
|
|
69
|
+
const data = await rawApi.getSegment({ shard: "shard3", segment: 30 });
|
|
70
|
+
console.log(data);
|
|
71
|
+
assert.strictEqual(data.data, testStr);
|
|
72
|
+
});
|
|
74
73
|
|
|
75
74
|
it("get world size", async () => {
|
|
76
75
|
const data = await rawApi.getWorldSize({ shard: "shard3" });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as assert from "assert";
|
|
2
|
+
|
|
3
|
+
import { officialServerApiConfig } from "../../authInfo";
|
|
4
|
+
import { ScreepsApi } from "../../src";
|
|
5
|
+
import { writeFileSync } from "fs";
|
|
6
|
+
// 上面的userData需要自己在根目录创建,示例参照根目录的authInfoSample.ts
|
|
7
|
+
describe("api", () => {
|
|
8
|
+
describe("create api", () => {
|
|
9
|
+
const api = new ScreepsApi(officialServerApiConfig);
|
|
10
|
+
const rawApi = api.rawApi;
|
|
11
|
+
const socket = api.socket;
|
|
12
|
+
it("sign in", async () => {
|
|
13
|
+
const data = await api.auth();
|
|
14
|
+
console.log(data);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("is apiConfig stored properly", async () => {
|
|
18
|
+
assert.deepStrictEqual(api.apiConfig, officialServerApiConfig);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("test getMemory", async () => {
|
|
22
|
+
const data = await rawApi.getMemory({ shard: "shard3" });
|
|
23
|
+
const name = "memory";
|
|
24
|
+
writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("test getMemory by path", async () => {
|
|
28
|
+
const data = await rawApi.getMemory({ path: "creeps", shard: "shard3" });
|
|
29
|
+
const name = "memory-creeps";
|
|
30
|
+
writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|