@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.mjs CHANGED
@@ -47,6 +47,7 @@ var NetworkError = class extends GymSpaceError {
47
47
  // src/client.ts
48
48
  var ApiClient = class {
49
49
  constructor(config) {
50
+ this.refreshPromise = null;
50
51
  this.config = config;
51
52
  this.axiosInstance = axios.create({
52
53
  baseURL: config.baseURL,
@@ -64,6 +65,9 @@ var ApiClient = class {
64
65
  if (this.config.apiKey && config.headers) {
65
66
  config.headers["Authorization"] = `Bearer ${this.config.apiKey}`;
66
67
  }
68
+ if (this.config.refreshToken && config.headers) {
69
+ config.headers["X-Refresh-Token"] = this.config.refreshToken;
70
+ }
67
71
  return config;
68
72
  },
69
73
  (error) => {
@@ -71,28 +75,68 @@ var ApiClient = class {
71
75
  }
72
76
  );
73
77
  this.axiosInstance.interceptors.response.use(
74
- (response) => response,
75
- (error) => {
78
+ (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
+ return response;
86
+ },
87
+ 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
+ originalRequest.headers = originalRequest.headers || {};
99
+ originalRequest.headers["Authorization"] = `Bearer ${newTokens.access_token}`;
100
+ return this.axiosInstance(originalRequest);
101
+ }
102
+ } catch (refreshError) {
103
+ this.refreshPromise = null;
104
+ this.clearAuth();
105
+ this.onAuthError?.(refreshError);
106
+ }
107
+ }
76
108
  throw this.handleError(error);
77
109
  }
78
110
  );
79
111
  }
112
+ /**
113
+ * Refresh the access token using the stored refresh token
114
+ */
115
+ async refreshAccessToken() {
116
+ if (!this.config.refreshToken) {
117
+ throw new AuthenticationError("No refresh token available");
118
+ }
119
+ try {
120
+ const response = await axios.post(
121
+ `${this.config.baseURL}/auth/refresh`,
122
+ { refresh_token: this.config.refreshToken },
123
+ {
124
+ headers: {
125
+ "Content-Type": "application/json"
126
+ },
127
+ timeout: this.config.timeout || 3e4
128
+ }
129
+ );
130
+ const newTokens = response.data;
131
+ this.setTokens(newTokens.access_token, newTokens.refresh_token);
132
+ return newTokens;
133
+ } catch (error) {
134
+ throw new AuthenticationError("Failed to refresh token");
135
+ }
136
+ }
80
137
  handleError(error) {
81
138
  const requestPath = error.config?.url || "unknown";
82
139
  const method = error.config?.method?.toUpperCase() || "unknown";
83
- console.error(
84
- "API Error:",
85
- JSON.stringify(
86
- {
87
- method,
88
- path: requestPath,
89
- message: error.message,
90
- baseURL: error.config?.baseURL
91
- },
92
- null,
93
- 3
94
- )
95
- );
96
140
  if (!error.response) {
97
141
  console.error("Network Error:", {
98
142
  method,
@@ -238,8 +282,8 @@ var AuthResource = class extends BaseResource {
238
282
  async login(data, options) {
239
283
  return this.client.post(`${this.basePath}/login`, data, options);
240
284
  }
241
- async refreshToken(options) {
242
- return this.client.post(`${this.basePath}/refresh`, void 0, options);
285
+ async refreshToken(refreshToken, options) {
286
+ return this.client.post(`${this.basePath}/refresh`, { refresh_token: refreshToken }, options);
243
287
  }
244
288
  async verifyEmail(data, options) {
245
289
  return this.client.post(`${this.basePath}/verify-email`, data, options);
@@ -353,6 +397,12 @@ var GymsResource = class extends BaseResource {
353
397
  async updateCurrentGym(data, options) {
354
398
  return this.client.put(`${this.basePath}/current`, data, options);
355
399
  }
400
+ async updateGymSchedule(id, data, options) {
401
+ return this.client.put(`${this.basePath}/${id}/schedule`, data, options);
402
+ }
403
+ async updateGymSocialMedia(id, data, options) {
404
+ return this.client.put(`${this.basePath}/${id}/social-media`, data, options);
405
+ }
356
406
  };
357
407
 
358
408
  // src/resources/clients.ts
@@ -379,6 +429,15 @@ var ClientsResource = class extends BaseResource {
379
429
  async getClientStats(id, options) {
380
430
  return this.client.get(`${this.basePath}/${id}/stats`, void 0, options);
381
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
+ }
382
441
  async searchClientsForCheckIn(params, options) {
383
442
  return this.client.get(
384
443
  `${this.basePath}/search/check-in`,
@@ -454,27 +513,63 @@ var ContractsResource = class extends BaseResource {
454
513
  // src/resources/dashboard.ts
455
514
  var DashboardResource = class extends BaseResource {
456
515
  /**
457
- * Get dashboard statistics
458
- * @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
459
518
  */
460
519
  async getStats() {
461
520
  return this.client.get("/dashboard/stats");
462
521
  }
463
522
  /**
464
- * Get recent activity
465
- * @param limit Maximum number of activities to return (default: 10)
466
- * @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
467
534
  */
468
- async getRecentActivity(limit = 10) {
469
- return this.client.get("/dashboard/recent-activity", { limit });
535
+ async getSalesRevenue(params) {
536
+ return this.client.get("/dashboard/sales-revenue", params);
470
537
  }
471
538
  /**
472
- * Get expiring contracts
539
+ * Get total debts within date range
540
+ * @param params Optional date range parameters
541
+ * @returns Outstanding debts data for the specified period
542
+ */
543
+ async getDebts(params) {
544
+ return this.client.get("/dashboard/debts", params);
545
+ }
546
+ /**
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
473
564
  * @param limit Maximum number of contracts to return (default: 10)
474
- * @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
475
567
  */
476
- async getExpiringContracts(limit = 10) {
477
- 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
+ });
478
573
  }
479
574
  };
480
575
 
@@ -859,6 +954,9 @@ var ProductsResource = class extends BaseResource {
859
954
  async createProduct(data, options) {
860
955
  return this.client.post(this.basePath, data, options);
861
956
  }
957
+ async createService(data, options) {
958
+ return this.client.post(`${this.basePath}/services`, data, options);
959
+ }
862
960
  async searchProducts(params, options) {
863
961
  return this.paginate(this.basePath, params, options);
864
962
  }
@@ -884,6 +982,9 @@ var ProductsResource = class extends BaseResource {
884
982
  options
885
983
  );
886
984
  }
985
+ async getProductStockMovements(id, options) {
986
+ return this.client.get(`${this.basePath}/${id}/stock-movements`, void 0, options);
987
+ }
887
988
  };
888
989
 
889
990
  // src/resources/sales.ts
@@ -904,8 +1005,8 @@ var SalesResource = class extends BaseResource {
904
1005
  async updateSale(id, data, options) {
905
1006
  return this.client.put(`${this.basePath}/${id}`, data, options);
906
1007
  }
907
- async updatePaymentStatus(id, data, options) {
908
- 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);
909
1010
  }
910
1011
  async deleteSale(id, options) {
911
1012
  return this.client.delete(`${this.basePath}/${id}`, options);
@@ -930,6 +1031,16 @@ var SalesResource = class extends BaseResource {
930
1031
  options
931
1032
  );
932
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
+ }
933
1044
  };
934
1045
 
935
1046
  // src/resources/suppliers.ts
@@ -979,6 +1090,81 @@ var UsersResource = class extends BaseResource {
979
1090
  }
980
1091
  };
981
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
+
982
1168
  // src/sdk.ts
983
1169
  var GymSpaceSdk = class {
984
1170
  constructor(config) {
@@ -1003,6 +1189,8 @@ var GymSpaceSdk = class {
1003
1189
  this.sales = new SalesResource(this.client);
1004
1190
  this.suppliers = new SuppliersResource(this.client);
1005
1191
  this.users = new UsersResource(this.client);
1192
+ this.subscriptions = new SubscriptionsResource(this.client);
1193
+ this.paymentMethods = new PaymentMethodsResource(this.client);
1006
1194
  }
1007
1195
  /**
1008
1196
  * Set the authentication token
@@ -1010,6 +1198,12 @@ var GymSpaceSdk = class {
1010
1198
  setAuthToken(token) {
1011
1199
  this.client.setAuthToken(token);
1012
1200
  }
1201
+ /**
1202
+ * Set both access and refresh tokens
1203
+ */
1204
+ setTokens(accessToken, refreshToken) {
1205
+ this.client.setTokens(accessToken, refreshToken);
1206
+ }
1013
1207
  /**
1014
1208
  * Set the current gym context
1015
1209
  */
@@ -1316,6 +1510,6 @@ var OnboardingStep = /* @__PURE__ */ ((OnboardingStep2) => {
1316
1510
  return OnboardingStep2;
1317
1511
  })(OnboardingStep || {});
1318
1512
 
1319
- export { ApiClient, AssetCategory, AssetStatus, AssetsResource, AuthResource, AuthenticationError, AuthorizationError, CACHE_TTL, CheckInsResource, ClientStatus, ClientsResource, CollaboratorStatus, CommentType, ContractAssetType, ContractStatus, ContractsResource, DATE_FORMATS, DashboardResource, EvaluationAssetCategory, EvaluationAssetStage, EvaluationStatus, EvaluationType, EvaluationsResource, FILE_LIMITS, FilesResource, GymSpaceError, GymSpaceSdk, GymsResource, HEADERS, HealthResource, InvitationStatus, InvitationsResource, LeadStatus, LeadsResource, MembershipPlansResource, NetworkError, NotFoundError, OnboardingResource, OnboardingStep, OrganizationsResource, PAGINATION_DEFAULTS, PERMISSIONS, PaymentFrequency, PlanStatus, ProductsResource, PublicCatalogResource, ROLE_PERMISSIONS, SalesResource, SubscriptionStatus, SuppliersResource, UserType, UsersResource, ValidationError };
1513
+ export { ApiClient, AssetCategory, AssetStatus, AssetsResource, AuthResource, AuthenticationError, AuthorizationError, CACHE_TTL, CheckInsResource, ClientStatus, ClientsResource, CollaboratorStatus, CommentType, ContractAssetType, ContractStatus, ContractsResource, DATE_FORMATS, DashboardResource, EvaluationAssetCategory, EvaluationAssetStage, EvaluationStatus, EvaluationType, EvaluationsResource, FILE_LIMITS, FilesResource, GymSpaceError, GymSpaceSdk, GymsResource, HEADERS, HealthResource, InvitationStatus, InvitationsResource, LeadStatus, LeadsResource, MembershipPlansResource, NetworkError, NotFoundError, OnboardingResource, OnboardingStep, OrganizationsResource, PAGINATION_DEFAULTS, PERMISSIONS, PaymentFrequency, PaymentMethodsResource, PlanStatus, ProductsResource, PublicCatalogResource, ROLE_PERMISSIONS, SalesResource, SubscriptionStatus, SubscriptionsResource, SuppliersResource, UserType, UsersResource, ValidationError };
1320
1514
  //# sourceMappingURL=index.mjs.map
1321
1515
  //# sourceMappingURL=index.mjs.map