syntro-js-client 1.0.8 → 1.0.10
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/index.js +28 -2
- package/package.json +2 -4
package/index.js
CHANGED
|
@@ -7,9 +7,10 @@ class Syntro {
|
|
|
7
7
|
if (!apiKey) throw new Error('Syntro: apiKey is required');
|
|
8
8
|
this.apiKey = apiKey;
|
|
9
9
|
this._token = null;
|
|
10
|
+
this._refreshToken = null;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
async _request(method, path, body, useJwt = false) {
|
|
13
|
+
async _request(method, path, body, useJwt = false, isRetry = false) {
|
|
13
14
|
const headers = { 'Content-Type': 'application/json' };
|
|
14
15
|
|
|
15
16
|
if (useJwt && this._token) {
|
|
@@ -18,12 +19,28 @@ class Syntro {
|
|
|
18
19
|
headers['x-api-key'] = this.apiKey;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
let response = await fetch(`${BASE_URL}/api/v1${path}`, {
|
|
22
23
|
method,
|
|
23
24
|
headers,
|
|
24
25
|
body: body ? JSON.stringify(body) : undefined,
|
|
25
26
|
});
|
|
26
27
|
|
|
28
|
+
// Handle auto-refresh for JWTs on 401 (if not already retrying)
|
|
29
|
+
if (response.status === 401 && useJwt && this._refreshToken && !isRetry) {
|
|
30
|
+
try {
|
|
31
|
+
const refreshResult = await this.refresh();
|
|
32
|
+
this._token = refreshResult.accessToken;
|
|
33
|
+
this._refreshToken = refreshResult.refreshToken;
|
|
34
|
+
|
|
35
|
+
// Retry the original request
|
|
36
|
+
return this._request(method, path, body, useJwt, true);
|
|
37
|
+
} catch (err) {
|
|
38
|
+
// If refresh fails, fall through to default error handling
|
|
39
|
+
this._token = null;
|
|
40
|
+
this._refreshToken = null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
27
44
|
const data = await response.json();
|
|
28
45
|
|
|
29
46
|
if (!response.ok) {
|
|
@@ -39,6 +56,15 @@ class Syntro {
|
|
|
39
56
|
async login(email, password) {
|
|
40
57
|
const result = await this._request('POST', '/auth/login', { email, password });
|
|
41
58
|
this._token = result.accessToken;
|
|
59
|
+
this._refreshToken = result.refreshToken;
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async refresh() {
|
|
64
|
+
if (!this._refreshToken) throw new Error('Syntro: no refresh token available');
|
|
65
|
+
const result = await this._request('POST', '/auth/refresh', { refreshToken: this._refreshToken });
|
|
66
|
+
this._token = result.accessToken;
|
|
67
|
+
this._refreshToken = result.refreshToken;
|
|
42
68
|
return result;
|
|
43
69
|
}
|
|
44
70
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "syntro-js-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "Official Node.js SDK for Syntro — plug-and-play auth, billing, and analytics for your SaaS.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,9 +27,7 @@
|
|
|
27
27
|
],
|
|
28
28
|
"author": "Syntro",
|
|
29
29
|
"license": "MIT",
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"node-fetch": "^3.3.2"
|
|
32
|
-
},
|
|
30
|
+
"dependencies": {},
|
|
33
31
|
"peerDependencies": {
|
|
34
32
|
"react": ">=16.8.0"
|
|
35
33
|
}
|