@gymspace/sdk 1.0.0 → 1.0.2

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);
@@ -359,6 +403,12 @@ var GymsResource = class extends BaseResource {
359
403
  async updateCurrentGym(data, options) {
360
404
  return this.client.put(`${this.basePath}/current`, data, options);
361
405
  }
406
+ async updateGymSchedule(id, data, options) {
407
+ return this.client.put(`${this.basePath}/${id}/schedule`, data, options);
408
+ }
409
+ async updateGymSocialMedia(id, data, options) {
410
+ return this.client.put(`${this.basePath}/${id}/social-media`, data, options);
411
+ }
362
412
  };
363
413
 
364
414
  // src/resources/clients.ts
@@ -385,6 +435,15 @@ var ClientsResource = class extends BaseResource {
385
435
  async getClientStats(id, options) {
386
436
  return this.client.get(`${this.basePath}/${id}/stats`, void 0, options);
387
437
  }
438
+ async getClientStat(id, statKey, options) {
439
+ return this.client.get(`${this.basePath}/${id}/stats/${statKey}`, void 0, options);
440
+ }
441
+ async getClientStatsByCategory(id, category, options) {
442
+ return this.client.get(`${this.basePath}/${id}/stats/category/${category}`, void 0, options);
443
+ }
444
+ async getAvailableStats(options) {
445
+ return this.client.get(`${this.basePath}/stats/available`, void 0, options);
446
+ }
388
447
  async searchClientsForCheckIn(params, options) {
389
448
  return this.client.get(
390
449
  `${this.basePath}/search/check-in`,
@@ -460,27 +519,63 @@ var ContractsResource = class extends BaseResource {
460
519
  // src/resources/dashboard.ts
461
520
  var DashboardResource = class extends BaseResource {
462
521
  /**
463
- * Get dashboard statistics
464
- * @returns Dashboard statistics including clients, contracts, revenue, and check-ins
522
+ * Get dashboard statistics (lightweight counts only)
523
+ * @returns Dashboard statistics including client and contract counts
465
524
  */
466
525
  async getStats() {
467
526
  return this.client.get("/dashboard/stats");
468
527
  }
469
528
  /**
470
- * Get recent activity
471
- * @param limit Maximum number of activities to return (default: 10)
472
- * @returns List of recent activities in the gym
529
+ * Get contracts revenue within date range
530
+ * @param params Optional date range parameters
531
+ * @returns Contracts revenue data for the specified period
532
+ */
533
+ async getContractsRevenue(params) {
534
+ return this.client.get("/dashboard/contracts-revenue", params);
535
+ }
536
+ /**
537
+ * Get sales revenue within date range
538
+ * @param params Optional date range parameters
539
+ * @returns Sales revenue data for the specified period
473
540
  */
474
- async getRecentActivity(limit = 10) {
475
- return this.client.get("/dashboard/recent-activity", { limit });
541
+ async getSalesRevenue(params) {
542
+ return this.client.get("/dashboard/sales-revenue", params);
476
543
  }
477
544
  /**
478
- * Get expiring contracts
545
+ * Get total debts within date range
546
+ * @param params Optional date range parameters
547
+ * @returns Outstanding debts data for the specified period
548
+ */
549
+ async getDebts(params) {
550
+ return this.client.get("/dashboard/debts", params);
551
+ }
552
+ /**
553
+ * Get check-ins count within date range
554
+ * @param params Optional date range parameters
555
+ * @returns Check-ins data for the specified period
556
+ */
557
+ async getCheckIns(params) {
558
+ return this.client.get("/dashboard/check-ins", params);
559
+ }
560
+ /**
561
+ * Get new clients count within date range
562
+ * @param params Optional date range parameters
563
+ * @returns New clients data for the specified period
564
+ */
565
+ async getNewClients(params) {
566
+ return this.client.get("/dashboard/new-clients", params);
567
+ }
568
+ /**
569
+ * Get expiring contracts within date range
479
570
  * @param limit Maximum number of contracts to return (default: 10)
480
- * @returns List of contracts expiring in the next 30 days
571
+ * @param params Optional date range parameters
572
+ * @returns List of contracts expiring in the specified period
481
573
  */
482
- async getExpiringContracts(limit = 10) {
483
- return this.client.get("/dashboard/expiring-contracts", { limit });
574
+ async getExpiringContracts(limit = 10, params) {
575
+ return this.client.get("/dashboard/expiring-contracts", {
576
+ limit,
577
+ ...params
578
+ });
484
579
  }
485
580
  };
486
581
 
@@ -865,6 +960,9 @@ var ProductsResource = class extends BaseResource {
865
960
  async createProduct(data, options) {
866
961
  return this.client.post(this.basePath, data, options);
867
962
  }
963
+ async createService(data, options) {
964
+ return this.client.post(`${this.basePath}/services`, data, options);
965
+ }
868
966
  async searchProducts(params, options) {
869
967
  return this.paginate(this.basePath, params, options);
870
968
  }
@@ -890,6 +988,9 @@ var ProductsResource = class extends BaseResource {
890
988
  options
891
989
  );
892
990
  }
991
+ async getProductStockMovements(id, options) {
992
+ return this.client.get(`${this.basePath}/${id}/stock-movements`, void 0, options);
993
+ }
893
994
  };
894
995
 
895
996
  // src/resources/sales.ts
@@ -910,8 +1011,8 @@ var SalesResource = class extends BaseResource {
910
1011
  async updateSale(id, data, options) {
911
1012
  return this.client.put(`${this.basePath}/${id}`, data, options);
912
1013
  }
913
- async updatePaymentStatus(id, data, options) {
914
- return this.client.put(`${this.basePath}/${id}/payment-status`, data, options);
1014
+ async updatePaymentStatus(id, paymentStatus, options) {
1015
+ return this.client.put(`${this.basePath}/${id}/payment-status`, { paymentStatus }, options);
915
1016
  }
916
1017
  async deleteSale(id, options) {
917
1018
  return this.client.delete(`${this.basePath}/${id}`, options);
@@ -936,6 +1037,16 @@ var SalesResource = class extends BaseResource {
936
1037
  options
937
1038
  );
938
1039
  }
1040
+ async getSalesByCustomer(startDate, endDate, options) {
1041
+ const params = {};
1042
+ if (startDate) params.startDate = startDate;
1043
+ if (endDate) params.endDate = endDate;
1044
+ return this.client.get(
1045
+ `${this.basePath}/reports/by-customer`,
1046
+ Object.keys(params).length > 0 ? params : void 0,
1047
+ options
1048
+ );
1049
+ }
939
1050
  };
940
1051
 
941
1052
  // src/resources/suppliers.ts
@@ -985,6 +1096,81 @@ var UsersResource = class extends BaseResource {
985
1096
  }
986
1097
  };
987
1098
 
1099
+ // src/resources/subscriptions.ts
1100
+ var SubscriptionsResource = class extends BaseResource {
1101
+ constructor() {
1102
+ super(...arguments);
1103
+ this.basePath = "subscriptions";
1104
+ }
1105
+ /**
1106
+ * Get available subscription plans (currently only free plans)
1107
+ */
1108
+ async getAvailablePlans(options) {
1109
+ return this.client.get(`${this.basePath}/plans`, void 0, options);
1110
+ }
1111
+ /**
1112
+ * Get subscription status for an organization
1113
+ */
1114
+ async getSubscriptionStatus(organizationId, options) {
1115
+ return this.client.get(
1116
+ `${this.basePath}/organizations/${organizationId}/status`,
1117
+ void 0,
1118
+ options
1119
+ );
1120
+ }
1121
+ /**
1122
+ * Affiliate organization to a subscription plan
1123
+ */
1124
+ async affiliateOrganization(organizationId, data, options) {
1125
+ return this.client.post(
1126
+ `${this.basePath}/organizations/${organizationId}/affiliate`,
1127
+ data,
1128
+ options
1129
+ );
1130
+ }
1131
+ /**
1132
+ * Check subscription limits
1133
+ * @param organizationId Organization ID
1134
+ * @param limitType Type of limit to check: 'gyms', 'clients', or 'users'
1135
+ */
1136
+ async checkSubscriptionLimit(organizationId, limitType, options) {
1137
+ return this.client.get(
1138
+ `${this.basePath}/organizations/${organizationId}/limits/${limitType}`,
1139
+ void 0,
1140
+ options
1141
+ );
1142
+ }
1143
+ };
1144
+
1145
+ // src/resources/payment-methods.ts
1146
+ var PaymentMethodsResource = class extends BaseResource {
1147
+ constructor() {
1148
+ super(...arguments);
1149
+ this.basePath = "payment-methods";
1150
+ }
1151
+ async createPaymentMethod(data, options) {
1152
+ return this.client.post(this.basePath, data, options);
1153
+ }
1154
+ async searchPaymentMethods(params, options) {
1155
+ return this.paginate(this.basePath, params, options);
1156
+ }
1157
+ async getPaymentMethod(id, options) {
1158
+ return this.client.get(`${this.basePath}/${id}`, void 0, options);
1159
+ }
1160
+ async updatePaymentMethod(id, data, options) {
1161
+ return this.client.put(`${this.basePath}/${id}`, data, options);
1162
+ }
1163
+ async deletePaymentMethod(id, options) {
1164
+ return this.client.delete(`${this.basePath}/${id}`, options);
1165
+ }
1166
+ async togglePaymentMethod(id, options) {
1167
+ return this.client.put(`${this.basePath}/${id}/toggle`, void 0, options);
1168
+ }
1169
+ async getPaymentMethodStats(options) {
1170
+ return this.client.get(`${this.basePath}/stats`, void 0, options);
1171
+ }
1172
+ };
1173
+
988
1174
  // src/sdk.ts
989
1175
  var GymSpaceSdk = class {
990
1176
  constructor(config) {
@@ -1009,6 +1195,8 @@ var GymSpaceSdk = class {
1009
1195
  this.sales = new SalesResource(this.client);
1010
1196
  this.suppliers = new SuppliersResource(this.client);
1011
1197
  this.users = new UsersResource(this.client);
1198
+ this.subscriptions = new SubscriptionsResource(this.client);
1199
+ this.paymentMethods = new PaymentMethodsResource(this.client);
1012
1200
  }
1013
1201
  /**
1014
1202
  * Set the authentication token
@@ -1016,6 +1204,12 @@ var GymSpaceSdk = class {
1016
1204
  setAuthToken(token) {
1017
1205
  this.client.setAuthToken(token);
1018
1206
  }
1207
+ /**
1208
+ * Set both access and refresh tokens
1209
+ */
1210
+ setTokens(accessToken, refreshToken) {
1211
+ this.client.setTokens(accessToken, refreshToken);
1212
+ }
1019
1213
  /**
1020
1214
  * Set the current gym context
1021
1215
  */
@@ -1365,12 +1559,14 @@ exports.OrganizationsResource = OrganizationsResource;
1365
1559
  exports.PAGINATION_DEFAULTS = PAGINATION_DEFAULTS;
1366
1560
  exports.PERMISSIONS = PERMISSIONS;
1367
1561
  exports.PaymentFrequency = PaymentFrequency;
1562
+ exports.PaymentMethodsResource = PaymentMethodsResource;
1368
1563
  exports.PlanStatus = PlanStatus;
1369
1564
  exports.ProductsResource = ProductsResource;
1370
1565
  exports.PublicCatalogResource = PublicCatalogResource;
1371
1566
  exports.ROLE_PERMISSIONS = ROLE_PERMISSIONS;
1372
1567
  exports.SalesResource = SalesResource;
1373
1568
  exports.SubscriptionStatus = SubscriptionStatus;
1569
+ exports.SubscriptionsResource = SubscriptionsResource;
1374
1570
  exports.SuppliersResource = SuppliersResource;
1375
1571
  exports.UserType = UserType;
1376
1572
  exports.UsersResource = UsersResource;