@gymspace/sdk 1.0.3 → 1.1.0
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/dist/index.d.mts +1 -12
- package/dist/index.d.ts +1 -12
- package/dist/index.js +3 -71
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -71
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +2 -89
- package/src/models/auth.ts +1 -0
- package/src/resources/onboarding.ts +2 -2
- package/src/sdk.ts +0 -6
package/dist/index.mjs
CHANGED
|
@@ -47,7 +47,6 @@ var NetworkError = class extends GymSpaceError {
|
|
|
47
47
|
// src/client.ts
|
|
48
48
|
var ApiClient = class {
|
|
49
49
|
constructor(config) {
|
|
50
|
-
this.refreshPromise = null;
|
|
51
50
|
this.config = config;
|
|
52
51
|
this.axiosInstance = axios.create({
|
|
53
52
|
baseURL: config.baseURL,
|
|
@@ -65,9 +64,6 @@ var ApiClient = class {
|
|
|
65
64
|
if (this.config.apiKey && config.headers) {
|
|
66
65
|
config.headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
67
66
|
}
|
|
68
|
-
if (this.config.refreshToken && config.headers) {
|
|
69
|
-
config.headers["X-Refresh-Token"] = this.config.refreshToken;
|
|
70
|
-
}
|
|
71
67
|
return config;
|
|
72
68
|
},
|
|
73
69
|
(error) => {
|
|
@@ -76,66 +72,13 @@ var ApiClient = class {
|
|
|
76
72
|
);
|
|
77
73
|
this.axiosInstance.interceptors.response.use(
|
|
78
74
|
(response) => {
|
|
79
|
-
const newAccessToken = response.headers["x-new-access-token"];
|
|
80
|
-
const newRefreshToken = response.headers["x-new-refresh-token"];
|
|
81
|
-
if (newAccessToken && newRefreshToken) {
|
|
82
|
-
this.setTokens(newAccessToken, newRefreshToken);
|
|
83
|
-
this.onTokensUpdated?.(newAccessToken, newRefreshToken);
|
|
84
|
-
}
|
|
85
75
|
return response;
|
|
86
76
|
},
|
|
87
77
|
async (error) => {
|
|
88
|
-
const originalRequest = error.config;
|
|
89
|
-
if (error.response?.status === 401 && !originalRequest._retry && this.config.refreshToken) {
|
|
90
|
-
originalRequest._retry = true;
|
|
91
|
-
try {
|
|
92
|
-
if (!this.refreshPromise) {
|
|
93
|
-
this.refreshPromise = this.refreshAccessToken();
|
|
94
|
-
}
|
|
95
|
-
const newTokens = await this.refreshPromise;
|
|
96
|
-
this.refreshPromise = null;
|
|
97
|
-
if (newTokens) {
|
|
98
|
-
if (!originalRequest.headers) {
|
|
99
|
-
originalRequest.headers = {};
|
|
100
|
-
}
|
|
101
|
-
originalRequest.headers["Authorization"] = `Bearer ${newTokens.access_token}`;
|
|
102
|
-
return this.axiosInstance(originalRequest);
|
|
103
|
-
}
|
|
104
|
-
} catch (refreshError) {
|
|
105
|
-
this.refreshPromise = null;
|
|
106
|
-
this.clearAuth();
|
|
107
|
-
this.onAuthError?.(refreshError);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
78
|
throw this.handleError(error);
|
|
111
79
|
}
|
|
112
80
|
);
|
|
113
81
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Refresh the access token using the stored refresh token
|
|
116
|
-
*/
|
|
117
|
-
async refreshAccessToken() {
|
|
118
|
-
if (!this.config.refreshToken) {
|
|
119
|
-
throw new AuthenticationError("No refresh token available");
|
|
120
|
-
}
|
|
121
|
-
try {
|
|
122
|
-
const response = await axios.post(
|
|
123
|
-
`${this.config.baseURL}/auth/refresh`,
|
|
124
|
-
{ refresh_token: this.config.refreshToken },
|
|
125
|
-
{
|
|
126
|
-
headers: {
|
|
127
|
-
"Content-Type": "application/json"
|
|
128
|
-
},
|
|
129
|
-
timeout: this.config.timeout || 3e4
|
|
130
|
-
}
|
|
131
|
-
);
|
|
132
|
-
const newTokens = response.data;
|
|
133
|
-
this.setTokens(newTokens.access_token, newTokens.refresh_token);
|
|
134
|
-
return newTokens;
|
|
135
|
-
} catch (error) {
|
|
136
|
-
throw new AuthenticationError("Failed to refresh token");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
82
|
handleError(error) {
|
|
140
83
|
const requestPath = error.config?.url || "unknown";
|
|
141
84
|
const method = error.config?.method?.toUpperCase() || "unknown";
|
|
@@ -218,16 +161,11 @@ var ApiClient = class {
|
|
|
218
161
|
setAuthToken(token) {
|
|
219
162
|
this.config.apiKey = token;
|
|
220
163
|
}
|
|
221
|
-
setTokens(accessToken, refreshToken) {
|
|
222
|
-
this.config.apiKey = accessToken;
|
|
223
|
-
this.config.refreshToken = refreshToken;
|
|
224
|
-
}
|
|
225
164
|
setGymId(gymId) {
|
|
226
165
|
this.axiosInstance.defaults.headers.common["X-Gym-Id"] = gymId;
|
|
227
166
|
}
|
|
228
167
|
clearAuth() {
|
|
229
168
|
delete this.config.apiKey;
|
|
230
|
-
delete this.config.refreshToken;
|
|
231
169
|
delete this.axiosInstance.defaults.headers.common["Authorization"];
|
|
232
170
|
delete this.axiosInstance.defaults.headers.common["X-Gym-Id"];
|
|
233
171
|
}
|
|
@@ -902,8 +840,8 @@ var OnboardingResource = class extends BaseResource {
|
|
|
902
840
|
*/
|
|
903
841
|
async start(data) {
|
|
904
842
|
const response = await this.client.post("/onboarding/start", data);
|
|
905
|
-
if (response.access_token
|
|
906
|
-
this.client.
|
|
843
|
+
if (response.access_token) {
|
|
844
|
+
this.client.setAuthToken(response.access_token);
|
|
907
845
|
}
|
|
908
846
|
return response;
|
|
909
847
|
}
|
|
@@ -1203,12 +1141,6 @@ var GymSpaceSdk = class {
|
|
|
1203
1141
|
setAuthToken(token) {
|
|
1204
1142
|
this.client.setAuthToken(token);
|
|
1205
1143
|
}
|
|
1206
|
-
/**
|
|
1207
|
-
* Set both access and refresh tokens
|
|
1208
|
-
*/
|
|
1209
|
-
setTokens(accessToken, refreshToken) {
|
|
1210
|
-
this.client.setTokens(accessToken, refreshToken);
|
|
1211
|
-
}
|
|
1212
1144
|
/**
|
|
1213
1145
|
* Set the current gym context
|
|
1214
1146
|
*/
|
|
@@ -1229,7 +1161,7 @@ var GymSpaceSdk = class {
|
|
|
1229
1161
|
}
|
|
1230
1162
|
};
|
|
1231
1163
|
|
|
1232
|
-
// node_modules/@gymspace/shared/dist/index.mjs
|
|
1164
|
+
// ../../node_modules/@gymspace/shared/dist/index.mjs
|
|
1233
1165
|
var UserType = /* @__PURE__ */ ((UserType2) => {
|
|
1234
1166
|
UserType2["OWNER"] = "owner";
|
|
1235
1167
|
UserType2["COLLABORATOR"] = "collaborator";
|