monday-sdk-js 0.3.0 → 0.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monday-sdk-js",
3
- "version": "0.3.0",
3
+ "version": "0.3.3",
4
4
  "private": false,
5
5
  "repository": "https://github.com/mondaycom/monday-sdk-js",
6
6
  "main": "src/index.js",
@@ -33,30 +33,30 @@ describe("Monday Client Test", () => {
33
33
 
34
34
  describe("setters", () => {
35
35
  it("should set client id correctly", () => {
36
- const newId = "newId";
37
- mondayClient.setClientId(newId);
38
- expect(mondayClient._clientId).to.equal(newId);
39
- });
40
-
41
- it("should set client id correctly (check biding)", () => {
42
36
  const newId = "newId";
43
37
  const { setClientId } = mondayClient;
38
+
44
39
  setClientId(newId);
40
+
45
41
  expect(mondayClient._clientId).to.equal(newId);
46
42
  });
47
43
 
48
44
  it("should set the api token correctly", () => {
49
45
  const newToken = "new toekn";
50
- mondayClient.setToken(newToken);
46
+ const { setToken } = mondayClient;
47
+
48
+ setToken(newToken);
49
+
51
50
  expect(mondayClient._apiToken).to.equal(newToken);
52
51
  });
53
52
 
54
- it("should set the api token correctly (check bindings)", () => {
55
- const newToken = "new toekn";
53
+ it("should set the version correctly", () => {
54
+ const newVersion = "2023-01";
55
+ const { setApiVersion } = mondayClient;
56
56
 
57
- const { setToken } = mondayClient;
58
- setToken(newToken);
59
- expect(mondayClient._apiToken).to.equal(newToken);
57
+ setApiVersion(newVersion);
58
+
59
+ expect(mondayClient._apiVersion).to.eq(newVersion);
60
60
  });
61
61
  });
62
62
 
package/src/client.js CHANGED
@@ -10,11 +10,13 @@ class MondayClientSdk {
10
10
  constructor(options = {}) {
11
11
  this._clientId = options.clientId;
12
12
  this._apiToken = options.apiToken;
13
+ this._apiVersion = options.apiVersion;
13
14
 
14
15
  this.listeners = {};
15
16
 
16
17
  this.setClientId = this.setClientId.bind(this);
17
18
  this.setToken = this.setToken.bind(this);
19
+ this.setApiVersion = this.setApiVersion.bind(this);
18
20
  this.api = this.api.bind(this);
19
21
  this.listen = this.listen.bind(this);
20
22
  this.get = this.get.bind(this);
@@ -46,11 +48,17 @@ class MondayClientSdk {
46
48
  this._apiToken = token;
47
49
  }
48
50
 
51
+ setApiVersion(apiVersion) {
52
+ this._apiVersion = apiVersion;
53
+ }
54
+
49
55
  api(query, options = {}) {
50
56
  const params = { query, variables: options.variables };
51
57
  const token = options.token || this._apiToken;
58
+ const apiVersion = options.apiVersion || this._apiVersion;
59
+
52
60
  if (token) {
53
- return mondayApiClient.execute(params, token);
61
+ return mondayApiClient.execute(params, token, { apiVersion });
54
62
  } else {
55
63
  return new Promise((resolve, reject) => {
56
64
  this._localApi("api", { params })
@@ -34,6 +34,17 @@ describe("mondayApiClient", () => {
34
34
  });
35
35
  });
36
36
 
37
+ it("should call node fetch with the version header", async () => {
38
+ const apiVersion = "2023-01";
39
+ await mondayApiClient.execute("query { boards { id, name }}", "api_token", { apiVersion });
40
+ assert.calledOnce(nodeFetchStub);
41
+ assert.calledWithExactly(nodeFetchStub, "https://api.monday.com/v2", {
42
+ body: '"query { boards { id, name }}"',
43
+ headers: { Authorization: "api_token", "Content-Type": "application/json", "API-Version": apiVersion },
44
+ method: "POST"
45
+ });
46
+ });
47
+
37
48
  it(`should throw ${mondayApiClient.TOKEN_IS_REQUIRED_ERROR}`, async () => {
38
49
  let errorMessage;
39
50
  try {
@@ -11,7 +11,8 @@ function apiRequest(url, data, token, options = {}) {
11
11
  body: JSON.stringify(data || {}),
12
12
  headers: {
13
13
  Authorization: token,
14
- "Content-Type": "application/json"
14
+ "Content-Type": "application/json",
15
+ ...(options.apiVersion ? { "API-Version": options.apiVersion } : {})
15
16
  }
16
17
  });
17
18
  }
@@ -17,6 +17,11 @@ describe("server sdk", () => {
17
17
  const serverSdk = initServerSdk({ token: "sometoken123" });
18
18
  expect(serverSdk._token).to.eq("sometoken123");
19
19
  });
20
+
21
+ it("should be able to init serverSdk with API version", () => {
22
+ const serverSdk = initServerSdk({ apiVersion: "2023-01" });
23
+ expect(serverSdk._apiVersion).to.eq("2023-01");
24
+ });
20
25
  });
21
26
 
22
27
  describe("setToken", () => {
@@ -36,6 +41,18 @@ describe("server sdk", () => {
36
41
  });
37
42
  });
38
43
 
44
+ describe("setApiVersion", () => {
45
+ it("should be able to set the version", () => {
46
+ const serverSdk = initServerSdk();
47
+ expect(serverSdk._apiVersion).to.eq(undefined);
48
+
49
+ const { setApiVersion } = serverSdk;
50
+ setApiVersion("2023-01");
51
+
52
+ expect(serverSdk._apiVersion).to.eq("2023-01");
53
+ });
54
+ });
55
+
39
56
  describe("api", () => {
40
57
  let mondayApiClientExecuteStub;
41
58
 
@@ -60,7 +77,8 @@ describe("server sdk", () => {
60
77
  assert.calledWithExactly(
61
78
  mondayApiClientExecuteStub,
62
79
  { query: "query { boards { id, name }}", variables: undefined },
63
- "api_token"
80
+ "api_token",
81
+ { apiVersion: undefined }
64
82
  );
65
83
  });
66
84
 
@@ -101,7 +119,8 @@ describe("server sdk", () => {
101
119
  `,
102
120
  variables: { columnValues: { numbers: 3 }, pulseName: "new pulse" }
103
121
  },
104
- "api_token"
122
+ "api_token",
123
+ { apiVersion: undefined }
105
124
  );
106
125
  });
107
126
 
@@ -120,7 +139,47 @@ describe("server sdk", () => {
120
139
  assert.calledWithExactly(
121
140
  mondayApiClientExecuteStub,
122
141
  { query: "query { boards { id, name }}", variables: undefined },
123
- "api_token_2"
142
+ "api_token_2",
143
+ { apiVersion: undefined }
144
+ );
145
+ });
146
+
147
+ it("should provide version to mondayApi", async () => {
148
+ const serverSdk = initServerSdk({ token: "api_token", apiVersion: "2023-01" });
149
+ serverSdk.setApiVersion("2023-04");
150
+ await serverSdk.api("query { boards { id, name }}");
151
+ assert.calledOnce(mondayApiClientExecuteStub);
152
+ assert.calledWithExactly(
153
+ mondayApiClientExecuteStub,
154
+ { query: "query { boards { id, name }}", variables: undefined },
155
+ "api_token",
156
+ { apiVersion: "2023-04" }
157
+ );
158
+ });
159
+
160
+ it("should prefer version from method options when calling mondayApi", async () => {
161
+ const serverSdk = initServerSdk({ token: "api_token", apiVersion: "2023-01" });
162
+ serverSdk.setApiVersion("2023-04");
163
+ await serverSdk.api("query { boards { id, name }}", { apiVersion: "2023-07" });
164
+ assert.calledOnce(mondayApiClientExecuteStub);
165
+ assert.calledWithExactly(
166
+ mondayApiClientExecuteStub,
167
+ { query: "query { boards { id, name }}", variables: undefined },
168
+ "api_token",
169
+ { apiVersion: "2023-07" }
170
+ );
171
+ });
172
+
173
+ it("should call to mondayApi prefer version from options", async () => {
174
+ const serverSdk = initServerSdk({ token: "api_token", apiVersion: "2023-01" });
175
+ serverSdk.setApiVersion("2023-04");
176
+ await serverSdk.api("query { boards { id, name }}", { apiVersion: "2023-07" });
177
+ assert.calledOnce(mondayApiClientExecuteStub);
178
+ assert.calledWithExactly(
179
+ mondayApiClientExecuteStub,
180
+ { query: "query { boards { id, name }}", variables: undefined },
181
+ "api_token",
182
+ { apiVersion: "2023-07" }
124
183
  );
125
184
  });
126
185
 
package/src/server.js CHANGED
@@ -6,8 +6,10 @@ const TOKEN_MISSING_ERROR = "Should send 'token' as an option or call mondaySdk.
6
6
  class MondayServerSdk {
7
7
  constructor(options = {}) {
8
8
  this._token = options.token;
9
+ this._apiVersion = options.apiVersion;
9
10
 
10
11
  this.setToken = this.setToken.bind(this);
12
+ this.setApiVersion = this.setApiVersion.bind(this);
11
13
  this.api = this.api.bind(this);
12
14
  }
13
15
 
@@ -15,13 +17,18 @@ class MondayServerSdk {
15
17
  this._token = token;
16
18
  }
17
19
 
20
+ setApiVersion(apiVersion) {
21
+ this._apiVersion = apiVersion;
22
+ }
23
+
18
24
  async api(query, options = {}) {
19
25
  const params = { query, variables: options.variables };
20
26
  const token = options.token || this._token;
27
+ const apiVersion = options.apiVersion || this._apiVersion;
21
28
 
22
29
  if (!token) throw new Error(TOKEN_MISSING_ERROR);
23
30
 
24
- return await mondayApiClient.execute(params, token);
31
+ return await mondayApiClient.execute(params, token, { apiVersion });
25
32
  }
26
33
 
27
34
  oauthToken(code, clientId, clientSecret) {
@@ -9,6 +9,12 @@ interface APIOptions {
9
9
  * An object containing GraphQL query variables
10
10
  */
11
11
  variables?: object | undefined;
12
+
13
+ /**
14
+ * A string specifying which version of the API should be used
15
+ * If not set, will use the default version (stable)
16
+ */
17
+ apiVersion?: string;
12
18
  }
13
19
 
14
20
  interface OAuthOptions {
@@ -3,15 +3,42 @@ type SubscribableEvents = 'context' | 'settings' | 'itemIds' | 'events';
3
3
  type SettableTypes = 'settings';
4
4
 
5
5
  interface GetResponse {
6
- value: any;
7
- version: any;
6
+ data: {
7
+ success: boolean;
8
+ value: any;
9
+ version?: any;
10
+ };
11
+ errorMessage?: string | undefined;
12
+ method: string;
13
+ requestId: string;
14
+ type?: string | undefined;
15
+ }
16
+
17
+ interface DeleteResponse {
18
+ data: {
19
+ success: boolean;
20
+ value: any;
21
+ };
22
+ errorMessage?: string | undefined;
23
+ method: string;
24
+ requestId: string;
25
+ type?: string | undefined;
8
26
  }
9
27
 
10
28
  interface SetResponse {
11
- success: boolean;
12
- reason?: string | undefined;
29
+ data: {
30
+ success: boolean;
31
+ version: string;
32
+ reason?: string | undefined;
33
+ error?: string | undefined;
34
+ };
35
+ errorMessage?: string | undefined;
36
+ requestId: string;
37
+ method: string;
38
+ type?: string | undefined;
13
39
  }
14
40
 
41
+
15
42
  export interface ClientData {
16
43
  /**
17
44
  * Used for retrieving data from the parent monday.com application where your app is currently running.
@@ -62,14 +89,21 @@ export interface ClientData {
62
89
  * Returns a stored value from the database under `key`
63
90
  * @param key
64
91
  */
65
- getItem(key: string): Promise<{ data: GetResponse }>;
92
+ getItem(key: string): Promise<GetResponse>;
93
+
94
+ /**
95
+ * Deletes a stored value from the database under `key`
96
+ * @param key
97
+ */
98
+ deleteItem(key: string): Promise<DeleteResponse>;
66
99
 
67
100
  /**
68
101
  * Stores `value` under `key` in the database
69
102
  * @param key
70
103
  * @param value
104
+ * @param options
71
105
  */
72
- setItem(key: string, value: any): Promise<SetResponse>;
106
+ setItem(key: string, value: any, options?: { previous_version?: string }): Promise<SetResponse>;
73
107
  };
74
108
  };
75
- }
109
+ }
package/types/index.d.ts CHANGED
@@ -10,7 +10,7 @@ type MondayClientSdk = ClientData & ClientExecute & ClientApi;
10
10
  interface MondayServerSdk {
11
11
  setToken(token: string): void;
12
12
 
13
- api(query: string, options?: Partial<{ token: string, variables: object } >): Promise<any>;
13
+ api(query: string, options?: Partial<{ token: string, variables: object, apiVersion: string } >): Promise<any>;
14
14
 
15
15
  oauthToken(code: string, clientId: string, clientSecret: string): Promise<any>;
16
16
  }