@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.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);
@@ -379,6 +423,15 @@ var ClientsResource = class extends BaseResource {
379
423
  async getClientStats(id, options) {
380
424
  return this.client.get(`${this.basePath}/${id}/stats`, void 0, options);
381
425
  }
426
+ async getClientStat(id, statKey, options) {
427
+ return this.client.get(`${this.basePath}/${id}/stats/${statKey}`, void 0, options);
428
+ }
429
+ async getClientStatsByCategory(id, category, options) {
430
+ return this.client.get(`${this.basePath}/${id}/stats/category/${category}`, void 0, options);
431
+ }
432
+ async getAvailableStats(options) {
433
+ return this.client.get(`${this.basePath}/stats/available`, void 0, options);
434
+ }
382
435
  async searchClientsForCheckIn(params, options) {
383
436
  return this.client.get(
384
437
  `${this.basePath}/search/check-in`,
@@ -454,27 +507,63 @@ var ContractsResource = class extends BaseResource {
454
507
  // src/resources/dashboard.ts
455
508
  var DashboardResource = class extends BaseResource {
456
509
  /**
457
- * Get dashboard statistics
458
- * @returns Dashboard statistics including clients, contracts, revenue, and check-ins
510
+ * Get dashboard statistics (lightweight counts only)
511
+ * @returns Dashboard statistics including client and contract counts
459
512
  */
460
513
  async getStats() {
461
514
  return this.client.get("/dashboard/stats");
462
515
  }
463
516
  /**
464
- * Get recent activity
465
- * @param limit Maximum number of activities to return (default: 10)
466
- * @returns List of recent activities in the gym
517
+ * Get contracts revenue within date range
518
+ * @param params Optional date range parameters
519
+ * @returns Contracts revenue data for the specified period
520
+ */
521
+ async getContractsRevenue(params) {
522
+ return this.client.get("/dashboard/contracts-revenue", params);
523
+ }
524
+ /**
525
+ * Get sales revenue within date range
526
+ * @param params Optional date range parameters
527
+ * @returns Sales revenue data for the specified period
528
+ */
529
+ async getSalesRevenue(params) {
530
+ return this.client.get("/dashboard/sales-revenue", params);
531
+ }
532
+ /**
533
+ * Get total debts within date range
534
+ * @param params Optional date range parameters
535
+ * @returns Outstanding debts data for the specified period
467
536
  */
468
- async getRecentActivity(limit = 10) {
469
- return this.client.get("/dashboard/recent-activity", { limit });
537
+ async getDebts(params) {
538
+ return this.client.get("/dashboard/debts", params);
470
539
  }
471
540
  /**
472
- * Get expiring contracts
541
+ * Get check-ins count within date range
542
+ * @param params Optional date range parameters
543
+ * @returns Check-ins data for the specified period
544
+ */
545
+ async getCheckIns(params) {
546
+ return this.client.get("/dashboard/check-ins", params);
547
+ }
548
+ /**
549
+ * Get new clients count within date range
550
+ * @param params Optional date range parameters
551
+ * @returns New clients data for the specified period
552
+ */
553
+ async getNewClients(params) {
554
+ return this.client.get("/dashboard/new-clients", params);
555
+ }
556
+ /**
557
+ * Get expiring contracts within date range
473
558
  * @param limit Maximum number of contracts to return (default: 10)
474
- * @returns List of contracts expiring in the next 30 days
559
+ * @param params Optional date range parameters
560
+ * @returns List of contracts expiring in the specified period
475
561
  */
476
- async getExpiringContracts(limit = 10) {
477
- return this.client.get("/dashboard/expiring-contracts", { limit });
562
+ async getExpiringContracts(limit = 10, params) {
563
+ return this.client.get("/dashboard/expiring-contracts", {
564
+ limit,
565
+ ...params
566
+ });
478
567
  }
479
568
  };
480
569
 
@@ -859,6 +948,9 @@ var ProductsResource = class extends BaseResource {
859
948
  async createProduct(data, options) {
860
949
  return this.client.post(this.basePath, data, options);
861
950
  }
951
+ async createService(data, options) {
952
+ return this.client.post(`${this.basePath}/services`, data, options);
953
+ }
862
954
  async searchProducts(params, options) {
863
955
  return this.paginate(this.basePath, params, options);
864
956
  }
@@ -884,6 +976,9 @@ var ProductsResource = class extends BaseResource {
884
976
  options
885
977
  );
886
978
  }
979
+ async getProductStockMovements(id, options) {
980
+ return this.client.get(`${this.basePath}/${id}/stock-movements`, void 0, options);
981
+ }
887
982
  };
888
983
 
889
984
  // src/resources/sales.ts
@@ -904,8 +999,8 @@ var SalesResource = class extends BaseResource {
904
999
  async updateSale(id, data, options) {
905
1000
  return this.client.put(`${this.basePath}/${id}`, data, options);
906
1001
  }
907
- async updatePaymentStatus(id, data, options) {
908
- return this.client.put(`${this.basePath}/${id}/payment-status`, data, options);
1002
+ async updatePaymentStatus(id, paymentStatus, options) {
1003
+ return this.client.put(`${this.basePath}/${id}/payment-status`, { paymentStatus }, options);
909
1004
  }
910
1005
  async deleteSale(id, options) {
911
1006
  return this.client.delete(`${this.basePath}/${id}`, options);
@@ -930,6 +1025,16 @@ var SalesResource = class extends BaseResource {
930
1025
  options
931
1026
  );
932
1027
  }
1028
+ async getSalesByCustomer(startDate, endDate, options) {
1029
+ const params = {};
1030
+ if (startDate) params.startDate = startDate;
1031
+ if (endDate) params.endDate = endDate;
1032
+ return this.client.get(
1033
+ `${this.basePath}/reports/by-customer`,
1034
+ Object.keys(params).length > 0 ? params : void 0,
1035
+ options
1036
+ );
1037
+ }
933
1038
  };
934
1039
 
935
1040
  // src/resources/suppliers.ts
@@ -979,6 +1084,81 @@ var UsersResource = class extends BaseResource {
979
1084
  }
980
1085
  };
981
1086
 
1087
+ // src/resources/subscriptions.ts
1088
+ var SubscriptionsResource = class extends BaseResource {
1089
+ constructor() {
1090
+ super(...arguments);
1091
+ this.basePath = "subscriptions";
1092
+ }
1093
+ /**
1094
+ * Get available subscription plans (currently only free plans)
1095
+ */
1096
+ async getAvailablePlans(options) {
1097
+ return this.client.get(`${this.basePath}/plans`, void 0, options);
1098
+ }
1099
+ /**
1100
+ * Get subscription status for an organization
1101
+ */
1102
+ async getSubscriptionStatus(organizationId, options) {
1103
+ return this.client.get(
1104
+ `${this.basePath}/organizations/${organizationId}/status`,
1105
+ void 0,
1106
+ options
1107
+ );
1108
+ }
1109
+ /**
1110
+ * Affiliate organization to a subscription plan
1111
+ */
1112
+ async affiliateOrganization(organizationId, data, options) {
1113
+ return this.client.post(
1114
+ `${this.basePath}/organizations/${organizationId}/affiliate`,
1115
+ data,
1116
+ options
1117
+ );
1118
+ }
1119
+ /**
1120
+ * Check subscription limits
1121
+ * @param organizationId Organization ID
1122
+ * @param limitType Type of limit to check: 'gyms', 'clients', or 'users'
1123
+ */
1124
+ async checkSubscriptionLimit(organizationId, limitType, options) {
1125
+ return this.client.get(
1126
+ `${this.basePath}/organizations/${organizationId}/limits/${limitType}`,
1127
+ void 0,
1128
+ options
1129
+ );
1130
+ }
1131
+ };
1132
+
1133
+ // src/resources/payment-methods.ts
1134
+ var PaymentMethodsResource = class extends BaseResource {
1135
+ constructor() {
1136
+ super(...arguments);
1137
+ this.basePath = "payment-methods";
1138
+ }
1139
+ async createPaymentMethod(data, options) {
1140
+ return this.client.post(this.basePath, data, options);
1141
+ }
1142
+ async searchPaymentMethods(params, options) {
1143
+ return this.paginate(this.basePath, params, options);
1144
+ }
1145
+ async getPaymentMethod(id, options) {
1146
+ return this.client.get(`${this.basePath}/${id}`, void 0, options);
1147
+ }
1148
+ async updatePaymentMethod(id, data, options) {
1149
+ return this.client.put(`${this.basePath}/${id}`, data, options);
1150
+ }
1151
+ async deletePaymentMethod(id, options) {
1152
+ return this.client.delete(`${this.basePath}/${id}`, options);
1153
+ }
1154
+ async togglePaymentMethod(id, options) {
1155
+ return this.client.put(`${this.basePath}/${id}/toggle`, void 0, options);
1156
+ }
1157
+ async getPaymentMethodStats(options) {
1158
+ return this.client.get(`${this.basePath}/stats`, void 0, options);
1159
+ }
1160
+ };
1161
+
982
1162
  // src/sdk.ts
983
1163
  var GymSpaceSdk = class {
984
1164
  constructor(config) {
@@ -1003,6 +1183,8 @@ var GymSpaceSdk = class {
1003
1183
  this.sales = new SalesResource(this.client);
1004
1184
  this.suppliers = new SuppliersResource(this.client);
1005
1185
  this.users = new UsersResource(this.client);
1186
+ this.subscriptions = new SubscriptionsResource(this.client);
1187
+ this.paymentMethods = new PaymentMethodsResource(this.client);
1006
1188
  }
1007
1189
  /**
1008
1190
  * Set the authentication token
@@ -1010,6 +1192,12 @@ var GymSpaceSdk = class {
1010
1192
  setAuthToken(token) {
1011
1193
  this.client.setAuthToken(token);
1012
1194
  }
1195
+ /**
1196
+ * Set both access and refresh tokens
1197
+ */
1198
+ setTokens(accessToken, refreshToken) {
1199
+ this.client.setTokens(accessToken, refreshToken);
1200
+ }
1013
1201
  /**
1014
1202
  * Set the current gym context
1015
1203
  */
@@ -1030,7 +1218,7 @@ var GymSpaceSdk = class {
1030
1218
  }
1031
1219
  };
1032
1220
 
1033
- // node_modules/@gymspace/shared/dist/index.mjs
1221
+ // ../../node_modules/@gymspace/shared/dist/index.mjs
1034
1222
  var UserType = /* @__PURE__ */ ((UserType2) => {
1035
1223
  UserType2["OWNER"] = "owner";
1036
1224
  UserType2["COLLABORATOR"] = "collaborator";
@@ -1316,6 +1504,6 @@ var OnboardingStep = /* @__PURE__ */ ((OnboardingStep2) => {
1316
1504
  return OnboardingStep2;
1317
1505
  })(OnboardingStep || {});
1318
1506
 
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 };
1507
+ 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
1508
  //# sourceMappingURL=index.mjs.map
1321
1509
  //# sourceMappingURL=index.mjs.map