node-ts-screeps-api 0.0.13 → 0.0.15

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.
@@ -15,6 +15,21 @@ export declare class RawApi<T extends AuthType> {
15
15
  auth(): Promise<RawApiReturnData<"signinByPassword">>;
16
16
  getSegment(args: RawApiPostData<"getSegment">): Promise<RawApiReturnData<"getSegment">>;
17
17
  postSegment(args: RawApiPostData<"postSegment">): Promise<RawApiReturnData<"postSegment">>;
18
+ getMemory(args: {
19
+ path?: string;
20
+ shard?: string;
21
+ }): Promise<{
22
+ ok: number;
23
+ data?: string;
24
+ }>;
25
+ postMemory(args: {
26
+ path?: string;
27
+ value: any;
28
+ shard?: string;
29
+ }): Promise<{
30
+ ok: number;
31
+ data?: string;
32
+ }>;
18
33
  findUser(username: string): Promise<{
19
34
  ok: number;
20
35
  user: {
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ts-screeps-api",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "node-ts-screeps-api",
5
5
  "main": "dist/index.js",
6
6
  "typings": "./dist/src/index.d.ts",
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: any;
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: {
@@ -1,29 +1,28 @@
1
1
  import * as assert from "assert";
2
2
 
3
- import { apiConfig } from "../../authInfo";
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(apiConfig);
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, apiConfig);
13
+ assert.deepStrictEqual(api.apiConfig, localServerApiConfig);
15
14
  });
16
15
 
17
16
  it("throw Error when unauthorized", async () => {
18
- // let anErr;
19
- // try {
20
- // await rawApi.postSegment({ shard: "shard3", segment: 30, data: "test" });
21
- // } catch (err) {
22
- // anErr = err;
23
- // const message = (err as Error).message;
24
- // assert.strictEqual(message, `{"error":"unauthorized"}`);
25
- // }
26
- // if (!anErr) assert.fail("no error was thrown");
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
- // const data = await rawApi.postSegment({ shard: "shard3", segment: 30, data: testStr });
65
- // console.log(data);
66
- // assert.strictEqual(data.ok, 1);
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
- // it("get segment", async () => {
70
- // const data = await rawApi.getSegment({ shard: "shard3", segment: 30 });
71
- // console.log(data);
72
- // assert.strictEqual(data.data, testStr);
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,55 @@
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
+ it("test getMemory with invalid path", async () => {
34
+ const data = await rawApi.getMemory({ path: "__path_not_exist", shard: "shard3" });
35
+ const name = "memory-invalid-path";
36
+ writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
37
+ });
38
+
39
+ it("test postMemory", async () => {
40
+ const data = await rawApi.postMemory({ value: { testValue: 1 }, path: "__test__", shard: "shard3" });
41
+ const name = "post-memory";
42
+ writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
43
+ });
44
+
45
+ it("test postMemory with modify", async () => {
46
+ const data = await rawApi.postMemory({
47
+ value: { testValue: 2, anotherValue: 3 },
48
+ path: "__test__",
49
+ shard: "shard3"
50
+ });
51
+ const name = "post-memory-modify";
52
+ writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
53
+ });
54
+ });
55
+ });