Skip to content

Commit 53e45cc

Browse files
author
William Duncan
committed
a function to check if the API and the client are compatible
1 parent 2ae2147 commit 53e45cc

File tree

8 files changed

+169
-5
lines changed

8 files changed

+169
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ console.log(newsletter._response.status); // 200
7777
- [`cloudnode.getNextPage<T>(response)`](#cloudnodegetnextpagetresponse)
7878
- [`cloudnode.getPreviousPage<T>(response)`](#cloudnodegetpreviouspagetresponse)
7979
- [`cloudnode.getAllPages<T>(response)`](#cloudnodegetallpagestresponse)
80+
- [`cloudnode.checkCompatibility()`](#cloudnodecheckcompatibility)
8081
- [`cloudnode.account.changePassword(currentPassword, newPassword)`](#cloudnodeaccountchangepasswordcurrentpassword-newpassword)
8182
- [`cloudnode.account.get()`](#cloudnodeaccountget)
8283
- [`cloudnode.account.getEmail()`](#cloudnodeaccountgetemail)
@@ -184,6 +185,16 @@ Get all other pages of paginated results and return the complete data
184185
- Returns: <code>[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[Cloudnode.PaginatedData](#interface-cloudnodepaginateddatat)&lt;T>></code> All of the data in 1 page
185186
- Throws: <code>[Cloudnode.Error](#interface-cloudnodeerror)</code> Error returned by the API
186187

188+
<a name="cloudnodecheckcompatibility"></a>
189+
190+
### `cloudnode.checkCompatibility()`
191+
192+
Check compatibility with the API
193+
194+
195+
- Returns: <code>[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)></code> True if this client is compatible with the API server
196+
197+
187198
<a name="cloudnodeaccountchangepasswordcurrentpassword-newpassword"></a>
188199

189200
### `cloudnode.account.changePassword(currentPassword, newPassword)`

browser/Cloudnode.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class Cloudnode {
2929
maxRetryDelay: 5,
3030
maxRetries: 3
3131
};
32+
/**
33+
* API version
34+
*/
35+
#apiVersion = `5.11.2`;
36+
/**
37+
* Client user agent
38+
*/
39+
#userAgent = `cloudnode/1.9.1`;
3240
/**
3341
* Construct a new Cloudnode API client
3442
* @param token API token to use for requests
@@ -72,7 +80,7 @@ class Cloudnode {
7280
options.headers["Content-Type"] = "text/plain";
7381
}
7482
}
75-
options.headers["User-Agent"] = `cloudnode/1.9.1`;
83+
options.headers["User-Agent"] = this.#userAgent;
7684
if (this.#token && operation.token !== undefined)
7785
options.headers["Authorization"] = `Bearer ${this.#token}`;
7886
if (operation.token !== undefined)
@@ -132,6 +140,31 @@ class Cloudnode {
132140
send(0);
133141
});
134142
}
143+
/**
144+
* Compare versions to determine compatibility
145+
* @param a First version
146+
* @param b Second version
147+
*/
148+
#compareVersions(a, b) {
149+
const partsA = a.split(".");
150+
const partsB = b.split(".");
151+
const verA = [partsA[0] || "0", partsA[1] || "0"];
152+
const verB = [partsB[0] || "0", partsB[1] || "0"];
153+
return verA[0] === verB[0] && verA[1] === verB[1];
154+
}
155+
/**
156+
* Check compatibility with the API
157+
* @returns True if this client is compatible with the API server
158+
*/
159+
async checkCompatibility() {
160+
const data = await (await fetch(new URL("../", this.#options.baseUrl).toString(), {
161+
method: "GET",
162+
headers: {
163+
"User-Agent": this.#userAgent
164+
}
165+
})).json();
166+
return this.#compareVersions(data.version, this.#apiVersion);
167+
}
135168
/**
136169
* Get another page of paginated results
137170
* @param response Response to get a different page of

browser/Cloudnode.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/docs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export function generateDocSchema (schema: Schema, config: Config, pkg: Package)
103103
type: `Promise<${config.name}.PaginatedData<T>>`,
104104
description: "All of the data in 1 page"
105105
}, [{type: `${config.name}.Error`, description: "Error returned by the API"}]));
106+
always.push(new DocSchema.Method(`${config.instanceName}.checkCompatibility`, "Check compatibility with the API", [], {
107+
type: `Promise<boolean>`,
108+
description: "True if this client is compatible with the API server"
109+
}, []));
106110
mainClass.properties.unshift(...always);
107111
mainNamespace.properties.sort((a, b) => a.displayName.localeCompare(b.displayName));
108112

gen/templates/main.mustache

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class {{config.name}} {
3535
maxRetries: 3
3636
};
3737

38+
/**
39+
* API version
40+
*/
41+
readonly #apiVersion = `{{config.apiVersion}}`;
42+
43+
/**
44+
* Client user agent
45+
*/
46+
readonly #userAgent = `{{config.instanceName}}/{{pkg.version}}`;
47+
3848
/**
3949
* Construct a new {{config.name}} API client
4050
* @param token API token to use for requests
@@ -81,7 +91,7 @@ class {{config.name}} {
8191
}
8292
}
8393

84-
options.headers["User-Agent"] = `{{config.instanceName}}/{{pkg.version}}`;
94+
options.headers["User-Agent"] = this.#userAgent;
8595

8696
if (this.#token && operation.token !== undefined)
8797
options.headers["Authorization"] = `Bearer ${this.#token}`;
@@ -139,6 +149,35 @@ class {{config.name}} {
139149
});
140150
}
141151

152+
/**
153+
* Compare versions to determine compatibility
154+
* @param a First version
155+
* @param b Second version
156+
*/
157+
#compareVersions(a: string, b: string): boolean {
158+
const partsA = a.split(".");
159+
const partsB = b.split(".");
160+
161+
const verA = [partsA[0] || "0", partsA[1] || "0"];
162+
const verB = [partsB[0] || "0", partsB[1] || "0"];
163+
164+
return verA[0] === verB[0] && verA[1] === verB[1];
165+
}
166+
167+
/**
168+
* Check compatibility with the API
169+
* @returns True if this client is compatible with the API server
170+
*/
171+
public async checkCompatibility(): Promise<boolean> {
172+
const data: any = await (await fetch(new URL("../", this.#options.baseUrl).toString(), {
173+
method: "GET",
174+
headers: {
175+
"User-Agent": this.#userAgent
176+
}
177+
})).json();
178+
return this.#compareVersions(data.version, this.#apiVersion);
179+
}
180+
142181
/**
143182
* Get another page of paginated results
144183
* @param response Response to get a different page of

src/Cloudnode.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ declare class Cloudnode {
1111
* @param [options] Options for the API client
1212
*/
1313
constructor(token?: string, options?: Partial<Cloudnode.Options>);
14+
/**
15+
* Check compatibility with the API
16+
* @returns True if this client is compatible with the API server
17+
*/
18+
checkCompatibility(): Promise<boolean>;
1419
/**
1520
* Get another page of paginated results
1621
* @param response Response to get a different page of

src/Cloudnode.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class Cloudnode {
2929
maxRetryDelay: 5,
3030
maxRetries: 3
3131
};
32+
/**
33+
* API version
34+
*/
35+
#apiVersion = `5.11.2`;
36+
/**
37+
* Client user agent
38+
*/
39+
#userAgent = `cloudnode/1.9.1`;
3240
/**
3341
* Construct a new Cloudnode API client
3442
* @param token API token to use for requests
@@ -72,7 +80,7 @@ class Cloudnode {
7280
options.headers["Content-Type"] = "text/plain";
7381
}
7482
}
75-
options.headers["User-Agent"] = `cloudnode/1.9.1`;
83+
options.headers["User-Agent"] = this.#userAgent;
7684
if (this.#token && operation.token !== undefined)
7785
options.headers["Authorization"] = `Bearer ${this.#token}`;
7886
if (operation.token !== undefined)
@@ -132,6 +140,31 @@ class Cloudnode {
132140
send(0);
133141
});
134142
}
143+
/**
144+
* Compare versions to determine compatibility
145+
* @param a First version
146+
* @param b Second version
147+
*/
148+
#compareVersions(a, b) {
149+
const partsA = a.split(".");
150+
const partsB = b.split(".");
151+
const verA = [partsA[0] || "0", partsA[1] || "0"];
152+
const verB = [partsB[0] || "0", partsB[1] || "0"];
153+
return verA[0] === verB[0] && verA[1] === verB[1];
154+
}
155+
/**
156+
* Check compatibility with the API
157+
* @returns True if this client is compatible with the API server
158+
*/
159+
async checkCompatibility() {
160+
const data = await (await fetch(new URL("../", this.#options.baseUrl).toString(), {
161+
method: "GET",
162+
headers: {
163+
"User-Agent": this.#userAgent
164+
}
165+
})).json();
166+
return this.#compareVersions(data.version, this.#apiVersion);
167+
}
135168
/**
136169
* Get another page of paginated results
137170
* @param response Response to get a different page of

src/Cloudnode.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class Cloudnode {
3535
maxRetries: 3
3636
};
3737

38+
/**
39+
* API version
40+
*/
41+
readonly #apiVersion = `5.11.2`;
42+
43+
/**
44+
* Client user agent
45+
*/
46+
readonly #userAgent = `cloudnode/1.9.1`;
47+
3848
/**
3949
* Construct a new Cloudnode API client
4050
* @param token API token to use for requests
@@ -81,7 +91,7 @@ class Cloudnode {
8191
}
8292
}
8393

84-
options.headers["User-Agent"] = `cloudnode/1.9.1`;
94+
options.headers["User-Agent"] = this.#userAgent;
8595

8696
if (this.#token && operation.token !== undefined)
8797
options.headers["Authorization"] = `Bearer ${this.#token}`;
@@ -139,6 +149,35 @@ class Cloudnode {
139149
});
140150
}
141151

152+
/**
153+
* Compare versions to determine compatibility
154+
* @param a First version
155+
* @param b Second version
156+
*/
157+
#compareVersions(a: string, b: string): boolean {
158+
const partsA = a.split(".");
159+
const partsB = b.split(".");
160+
161+
const verA = [partsA[0] || "0", partsA[1] || "0"];
162+
const verB = [partsB[0] || "0", partsB[1] || "0"];
163+
164+
return verA[0] === verB[0] && verA[1] === verB[1];
165+
}
166+
167+
/**
168+
* Check compatibility with the API
169+
* @returns True if this client is compatible with the API server
170+
*/
171+
public async checkCompatibility(): Promise<boolean> {
172+
const data: any = await (await fetch(new URL("../", this.#options.baseUrl).toString(), {
173+
method: "GET",
174+
headers: {
175+
"User-Agent": this.#userAgent
176+
}
177+
})).json();
178+
return this.#compareVersions(data.version, this.#apiVersion);
179+
}
180+
142181
/**
143182
* Get another page of paginated results
144183
* @param response Response to get a different page of

0 commit comments

Comments
 (0)