@gymspace/sdk 1.0.0 → 1.0.1

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.js CHANGED
@@ -53,6 +53,7 @@ var NetworkError = class extends GymSpaceError {
53
53
  // src/client.ts
54
54
  var ApiClient = class {
55
55
  constructor(config) {
56
+ this.refreshPromise = null;
56
57
  this.config = config;
57
58
  this.axiosInstance = axios__default.default.create({
58
59
  baseURL: config.baseURL,
@@ -70,6 +71,9 @@ var ApiClient = class {
70
71
  if (this.config.apiKey && config.headers) {
71
72
  config.headers["Authorization"] = `Bearer ${this.config.apiKey}`;
72
73
  }
74
+ if (this.config.refreshToken && config.headers) {
75
+ config.headers["X-Refresh-Token"] = this.config.refreshToken;
76
+ }
73
77
  return config;
74
78
  },
75
79
  (error) => {
@@ -77,28 +81,68 @@ var ApiClient = class {
77
81
  }
78
82
  );
79
83
  this.axiosInstance.interceptors.response.use(
80
- (response) => response,
81
- (error) => {
84
+ (response) => {
85
+ const newAccessToken = response.headers["x-new-access-token"];
86
+ const newRefreshToken = response.headers["x-new-refresh-token"];
87
+ if (newAccessToken && newRefreshToken) {
88
+ this.setTokens(newAccessToken, newRefreshToken);
89
+ this.onTokensUpdated?.(newAccessToken, newRefreshToken);
90
+ }
91
+ return response;
92
+ },
93
+ async (error) => {
94
+ const originalRequest = error.config;
95
+ if (error.response?.status === 401 && !originalRequest._retry && this.config.refreshToken) {
96
+ originalRequest._retry = true;
97
+ try {
98
+ if (!this.refreshPromise) {
99
+ this.refreshPromise = this.refreshAccessToken();
100
+ }
101
+ const newTokens = await this.refreshPromise;
102
+ this.refreshPromise = null;
103
+ if (newTokens) {
104
+ originalRequest.headers = originalRequest.headers || {};
105
+ originalRequest.headers["Authorization"] = `Bearer ${newTokens.access_token}`;
106
+ return this.axiosInstance(originalRequest);
107
+ }
108
+ } catch (refreshError) {
109
+ this.refreshPromise = null;
110
+ this.clearAuth();
111
+ this.onAuthError?.(refreshError);
112
+ }
113
+ }
82
114
  throw this.handleError(error);
83
115
  }
84
116
  );
85
117
  }
118
+ /**
119
+ * Refresh the access token using the stored refresh token
120
+ */
121
+ async refreshAccessToken() {
122
+ if (!this.config.refreshToken) {
123
+ throw new AuthenticationError("No refresh token available");
124
+ }
125
+ try {
126
+ const response = await axios__default.default.post(
127
+ `${this.config.baseURL}/auth/refresh`,
128
+ { refresh_token: this.config.refreshToken },
129
+ {
130
+ headers: {
131
+ "Content-Type": "application/json"
132
+ },
133
+ timeout: this.config.timeout || 3e4
134
+ }
135
+ );
136
+ const newTokens = response.data;
137
+ this.setTokens(newTokens.access_token, newTokens.refresh_token);
138
+ return newTokens;
139
+ } catch (error) {
140
+ throw new AuthenticationError("Failed to refresh token");
141
+ }
142
+ }
86
143
  handleError(error) {
87
144
  const requestPath = error.config?.url || "unknown";
88
145
  const method = error.config?.method?.toUpperCase() || "unknown";
89
- console.error(
90
- "API Error:",
91
- JSON.stringify(
92
- {
93
- method,
94
- path: requestPath,
95
- message: error.message,
96
- baseURL: error.config?.baseURL
97
- },
98
- null,
99
- 3
100
- )
101
- );
102
146
  if (!error.response) {
103
147
  console.error("Network Error:", {
104
148
  method,
@@ -244,8 +288,8 @@ var AuthResource = class extends BaseResource {
244
288
  async login(data, options) {
245
289
  return this.client.post(`${this.basePath}/login`, data, options);
246
290
  }
247
- async refreshToken(options) {
248
- return this.client.post(`${this.basePath}/refresh`, void 0, options);
291
+ async refreshToken(refreshToken, options) {
292
+ return this.client.post(`${this.basePath}/refresh`, { refresh_token: refreshToken }, options);
249
293
  }
250
294
  async verifyEmail(data, options) {
251
295
  return this.client.post(`${this.basePath}/verify-email`, data, options);
@@ -385,6 +429,15 @@ var ClientsResource = class extends BaseResource {
385
429
  async getClientStats(id, options) {
386
430
  return this.client.get(`${this.basePath}/${id}/stats`, void 0, options);
387
431
  }
432
+ async getClientStat(id, statKey, options) {
433
+ return this.client.get(`${this.basePath}/${id}/stats/${statKey}`, void 0, options);
434
+ }
435
+ async getClientStatsByCategory(id, category, options) {
436
+ return this.client.get(`${this.basePath}/${id}/stats/category/${category}`, void 0, options);
437
+ }
438
+ async getAvailableStats(options) {
439
+ return this.client.get(`${this.basePath}/stats/available`, void 0, options);
440
+ }
388
441
  async searchClientsForCheckIn(params, options) {
389
442
  return this.client.get(
390
443
  `${this.basePath}/search/check-in`,
@@ -460,27 +513,63 @@ var ContractsResource = class extends BaseResource {
460
513
  // src/resources/dashboard.ts
461
514
  var DashboardResource = class extends BaseResource {
462
515
  /**
463
- * Get dashboard statistics
464
- * @returns Dashboard statistics including clients, contracts, revenue, and check-ins
516
+ * Get dashboard statistics (lightweight counts only)
517
+ * @returns Dashboard statistics including client and contract counts
465
518
  */
466
519
  async getStats() {
467
520
  return this.client.get("/dashboard/stats");
468
521
  }
469
522
  /**
470
- * Get recent activity
471
- * @param limit Maximum number of activities to return (default: 10)
472
- * @returns List of recent activities in the gym
523
+ * Get contracts revenue within date range
524
+ * @param params Optional date range parameters
525
+ * @returns Contracts revenue data for the specified period
526
+ */
527
+ async getContractsRevenue(params) {
528
+ return this.client.get("/dashboard/contracts-revenue", params);
529
+ }
530
+ /**
531
+ * Get sales revenue within date range
532
+ * @param params Optional date range parameters
533
+ * @returns Sales revenue data for the specified period
534
+ */
535
+ async getSalesRevenue(params) {
536
+ return this.client.get("/dashboard/sales-revenue", params);
537
+ }
538
+ /**
539
+ * Get total debts within date range
540
+ * @param params Optional date range parameters
541
+ * @returns Outstanding debts data for the specified period
473
542
  */
474
- async getRecentActivity(limit = 10) {
475
- return this.client.get("/dashboard/recent-activity", { limit });
543
+ async getDebts(params) {
544
+ return this.client.get("/dashboard/debts", params);
476
545
  }
477
546
  /**
478
- * Get expiring contracts
547
+ * Get check-ins count within date range
548
+ * @param params Optional date range parameters
549
+ * @returns Check-ins data for the specified period
550
+ */
551
+ async getCheckIns(params) {
552
+ return this.client.get("/dashboard/check-ins", params);
553
+ }
554
+ /**
555
+ * Get new clients count within date range
556
+ * @param params Optional date range parameters
557
+ * @returns New clients data for the specified period
558
+ */
559
+ async getNewClients(params) {
560
+ return this.client.get("/dashboard/new-clients", params);
561
+ }
562
+ /**
563
+ * Get expiring contracts within date range
479
564
  * @param limit Maximum number of contracts to return (default: 10)
480
- * @returns List of contracts expiring in the next 30 days
565
+ * @param params Optional date range parameters
566
+ * @returns List of contracts expiring in the specified period
481
567
  */
482
- async getExpiringContracts(limit = 10) {
483
- return this.client.get("/dashboard/expiring-contracts", { limit });
568
+ async getExpiringContracts(limit = 10, params) {
569
+ return this.client.get("/dashboard/expiring-contracts", {
570
+ limit,
571
+ ...params
572
+ });
484
573
  }
485
574
  };
486
575
 
@@ -865,6 +954,9 @@ var ProductsResource = class extends BaseResource {
865
954
  async createProduct(data, options) {
866
955
  return this.client.post(this.basePath, data, options);
867
956
  }
957
+ async createService(data, options) {
958
+ return this.client.post(`${this.basePath}/services`, data, options);
959
+ }
868
960
  async searchProducts(params, options) {
869
961
  return this.paginate(this.basePath, params, options);
870
962
  }
@@ -890,6 +982,9 @@ var ProductsResource = class extends BaseResource {
890
982
  options
891
983
  );
892
984
  }
985
+ async getProductStockMovements(id, options) {
986
+ return this.client.get(`${this.basePath}/${id}/stock-movements`, void 0, options);
987
+ }
893
988
  };
894
989
 
895
990
  // src/resources/sales.ts
@@ -910,8 +1005,8 @@ var SalesResource = class extends BaseResource {
910
1005
  async updateSale(id, data, options) {
911
1006
  return this.client.put(`${this.basePath}/${id}`, data, options);
912
1007
  }
913
- async updatePaymentStatus(id, data, options) {
914
- return this.client.put(`${this.basePath}/${id}/payment-status`, data, options);
1008
+ async updatePaymentStatus(id, paymentStatus, options) {
1009
+ return this.client.put(`${this.basePath}/${id}/payment-status`, { paymentStatus }, options);
915
1010
  }
916
1011
  async deleteSale(id, options) {
917
1012
  return this.client.delete(`${this.basePath}/${id}`, options);
@@ -936,6 +1031,16 @@ var SalesResource = class extends BaseResource {
936
1031
  options
937
1032
  );
938
1033
  }
1034
+ async getSalesByCustomer(startDate, endDate, options) {
1035
+ const params = {};
1036
+ if (startDate) params.startDate = startDate;
1037
+ if (endDate) params.endDate = endDate;
1038
+ return this.client.get(
1039
+ `${this.basePath}/reports/by-customer`,
1040
+ Object.keys(params).length > 0 ? params : void 0,
1041
+ options
1042
+ );
1043
+ }
939
1044
  };
940
1045
 
941
1046
  // src/resources/suppliers.ts
@@ -985,6 +1090,81 @@ var UsersResource = class extends BaseResource {
985
1090
  }
986
1091
  };
987
1092
 
1093
+ // src/resources/subscriptions.ts
1094
+ var SubscriptionsResource = class extends BaseResource {
1095
+ constructor() {
1096
+ super(...arguments);
1097
+ this.basePath = "subscriptions";
1098
+ }
1099
+ /**
1100
+ * Get available subscription plans (currently only free plans)
1101
+ */
1102
+ async getAvailablePlans(options) {
1103
+ return this.client.get(`${this.basePath}/plans`, void 0, options);
1104
+ }
1105
+ /**
1106
+ * Get subscription status for an organization
1107
+ */
1108
+ async getSubscriptionStatus(organizationId, options) {
1109
+ return this.client.get(
1110
+ `${this.basePath}/organizations/${organizationId}/status`,
1111
+ void 0,
1112
+ options
1113
+ );
1114
+ }
1115
+ /**
1116
+ * Affiliate organization to a subscription plan
1117
+ */
1118
+ async affiliateOrganization(organizationId, data, options) {
1119
+ return this.client.post(
1120
+ `${this.basePath}/organizations/${organizationId}/affiliate`,
1121
+ data,
1122
+ options
1123
+ );
1124
+ }
1125
+ /**
1126
+ * Check subscription limits
1127
+ * @param organizationId Organization ID
1128
+ * @param limitType Type of limit to check: 'gyms', 'clients', or 'users'
1129
+ */
1130
+ async checkSubscriptionLimit(organizationId, limitType, options) {
1131
+ return this.client.get(
1132
+ `${this.basePath}/organizations/${organizationId}/limits/${limitType}`,
1133
+ void 0,
1134
+ options
1135
+ );
1136
+ }
1137
+ };
1138
+
1139
+ // src/resources/payment-methods.ts
1140
+ var PaymentMethodsResource = class extends BaseResource {
1141
+ constructor() {
1142
+ super(...arguments);
1143
+ this.basePath = "payment-methods";
1144
+ }
1145
+ async createPaymentMethod(data, options) {
1146
+ return this.client.post(this.basePath, data, options);
1147
+ }
1148
+ async searchPaymentMethods(params, options) {
1149
+ return this.paginate(this.basePath, params, options);
1150
+ }
1151
+ async getPaymentMethod(id, options) {
1152
+ return this.client.get(`${this.basePath}/${id}`, void 0, options);
1153
+ }
1154
+ async updatePaymentMethod(id, data, options) {
1155
+ return this.client.put(`${this.basePath}/${id}`, data, options);
1156
+ }
1157
+ async deletePaymentMethod(id, options) {
1158
+ return this.client.delete(`${this.basePath}/${id}`, options);
1159
+ }
1160
+ async togglePaymentMethod(id, options) {
1161
+ return this.client.put(`${this.basePath}/${id}/toggle`, void 0, options);
1162
+ }
1163
+ async getPaymentMethodStats(options) {
1164
+ return this.client.get(`${this.basePath}/stats`, void 0, options);
1165
+ }
1166
+ };
1167
+
988
1168
  // src/sdk.ts
989
1169
  var GymSpaceSdk = class {
990
1170
  constructor(config) {
@@ -1009,6 +1189,8 @@ var GymSpaceSdk = class {
1009
1189
  this.sales = new SalesResource(this.client);
1010
1190
  this.suppliers = new SuppliersResource(this.client);
1011
1191
  this.users = new UsersResource(this.client);
1192
+ this.subscriptions = new SubscriptionsResource(this.client);
1193
+ this.paymentMethods = new PaymentMethodsResource(this.client);
1012
1194
  }
1013
1195
  /**
1014
1196
  * Set the authentication token
@@ -1016,6 +1198,12 @@ var GymSpaceSdk = class {
1016
1198
  setAuthToken(token) {
1017
1199
  this.client.setAuthToken(token);
1018
1200
  }
1201
+ /**
1202
+ * Set both access and refresh tokens
1203
+ */
1204
+ setTokens(accessToken, refreshToken) {
1205
+ this.client.setTokens(accessToken, refreshToken);
1206
+ }
1019
1207
  /**
1020
1208
  * Set the current gym context
1021
1209
  */
@@ -1036,7 +1224,7 @@ var GymSpaceSdk = class {
1036
1224
  }
1037
1225
  };
1038
1226
 
1039
- // node_modules/@gymspace/shared/dist/index.mjs
1227
+ // ../../node_modules/@gymspace/shared/dist/index.mjs
1040
1228
  var UserType = /* @__PURE__ */ ((UserType2) => {
1041
1229
  UserType2["OWNER"] = "owner";
1042
1230
  UserType2["COLLABORATOR"] = "collaborator";
@@ -1365,12 +1553,14 @@ exports.OrganizationsResource = OrganizationsResource;
1365
1553
  exports.PAGINATION_DEFAULTS = PAGINATION_DEFAULTS;
1366
1554
  exports.PERMISSIONS = PERMISSIONS;
1367
1555
  exports.PaymentFrequency = PaymentFrequency;
1556
+ exports.PaymentMethodsResource = PaymentMethodsResource;
1368
1557
  exports.PlanStatus = PlanStatus;
1369
1558
  exports.ProductsResource = ProductsResource;
1370
1559
  exports.PublicCatalogResource = PublicCatalogResource;
1371
1560
  exports.ROLE_PERMISSIONS = ROLE_PERMISSIONS;
1372
1561
  exports.SalesResource = SalesResource;
1373
1562
  exports.SubscriptionStatus = SubscriptionStatus;
1563
+ exports.SubscriptionsResource = SubscriptionsResource;
1374
1564
  exports.SuppliersResource = SuppliersResource;
1375
1565
  exports.UserType = UserType;
1376
1566
  exports.UsersResource = UsersResource;