@oxyhq/services 5.13.15 → 5.13.17

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.
Files changed (138) hide show
  1. package/README.md +10 -0
  2. package/lib/commonjs/core/OxyServices.base.js +271 -0
  3. package/lib/commonjs/core/OxyServices.base.js.map +1 -0
  4. package/lib/commonjs/core/OxyServices.errors.js +26 -0
  5. package/lib/commonjs/core/OxyServices.errors.js.map +1 -0
  6. package/lib/commonjs/core/OxyServices.js +58 -2168
  7. package/lib/commonjs/core/OxyServices.js.map +1 -1
  8. package/lib/commonjs/core/mixins/OxyServices.analytics.js +60 -0
  9. package/lib/commonjs/core/mixins/OxyServices.analytics.js.map +1 -0
  10. package/lib/commonjs/core/mixins/OxyServices.assets.js +424 -0
  11. package/lib/commonjs/core/mixins/OxyServices.assets.js.map +1 -0
  12. package/lib/commonjs/core/mixins/OxyServices.auth.js +303 -0
  13. package/lib/commonjs/core/mixins/OxyServices.auth.js.map +1 -0
  14. package/lib/commonjs/core/mixins/OxyServices.developer.js +115 -0
  15. package/lib/commonjs/core/mixins/OxyServices.developer.js.map +1 -0
  16. package/lib/commonjs/core/mixins/OxyServices.devices.js +119 -0
  17. package/lib/commonjs/core/mixins/OxyServices.devices.js.map +1 -0
  18. package/lib/commonjs/core/mixins/OxyServices.karma.js +117 -0
  19. package/lib/commonjs/core/mixins/OxyServices.karma.js.map +1 -0
  20. package/lib/commonjs/core/mixins/OxyServices.language.js +124 -0
  21. package/lib/commonjs/core/mixins/OxyServices.language.js.map +1 -0
  22. package/lib/commonjs/core/mixins/OxyServices.location.js +55 -0
  23. package/lib/commonjs/core/mixins/OxyServices.location.js.map +1 -0
  24. package/lib/commonjs/core/mixins/OxyServices.payment.js +66 -0
  25. package/lib/commonjs/core/mixins/OxyServices.payment.js.map +1 -0
  26. package/lib/commonjs/core/mixins/OxyServices.privacy.js +174 -0
  27. package/lib/commonjs/core/mixins/OxyServices.privacy.js.map +1 -0
  28. package/lib/commonjs/core/mixins/OxyServices.totp.js +53 -0
  29. package/lib/commonjs/core/mixins/OxyServices.totp.js.map +1 -0
  30. package/lib/commonjs/core/mixins/OxyServices.user.js +388 -0
  31. package/lib/commonjs/core/mixins/OxyServices.user.js.map +1 -0
  32. package/lib/commonjs/core/mixins/OxyServices.utility.js +161 -0
  33. package/lib/commonjs/core/mixins/OxyServices.utility.js.map +1 -0
  34. package/lib/commonjs/core/mixins/index.js +39 -0
  35. package/lib/commonjs/core/mixins/index.js.map +1 -0
  36. package/lib/commonjs/core/mixins/mixinHelpers.js +62 -0
  37. package/lib/commonjs/core/mixins/mixinHelpers.js.map +1 -0
  38. package/lib/commonjs/ui/context/OxyContext.js +27 -2
  39. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  40. package/lib/module/core/OxyServices.base.js +265 -0
  41. package/lib/module/core/OxyServices.base.js.map +1 -0
  42. package/lib/module/core/OxyServices.errors.js +20 -0
  43. package/lib/module/core/OxyServices.errors.js.map +1 -0
  44. package/lib/module/core/OxyServices.js +43 -2164
  45. package/lib/module/core/OxyServices.js.map +1 -1
  46. package/lib/module/core/mixins/OxyServices.analytics.js +56 -0
  47. package/lib/module/core/mixins/OxyServices.analytics.js.map +1 -0
  48. package/lib/module/core/mixins/OxyServices.assets.js +420 -0
  49. package/lib/module/core/mixins/OxyServices.assets.js.map +1 -0
  50. package/lib/module/core/mixins/OxyServices.auth.js +299 -0
  51. package/lib/module/core/mixins/OxyServices.auth.js.map +1 -0
  52. package/lib/module/core/mixins/OxyServices.developer.js +111 -0
  53. package/lib/module/core/mixins/OxyServices.developer.js.map +1 -0
  54. package/lib/module/core/mixins/OxyServices.devices.js +115 -0
  55. package/lib/module/core/mixins/OxyServices.devices.js.map +1 -0
  56. package/lib/module/core/mixins/OxyServices.karma.js +113 -0
  57. package/lib/module/core/mixins/OxyServices.karma.js.map +1 -0
  58. package/lib/module/core/mixins/OxyServices.language.js +120 -0
  59. package/lib/module/core/mixins/OxyServices.language.js.map +1 -0
  60. package/lib/module/core/mixins/OxyServices.location.js +51 -0
  61. package/lib/module/core/mixins/OxyServices.location.js.map +1 -0
  62. package/lib/module/core/mixins/OxyServices.payment.js +62 -0
  63. package/lib/module/core/mixins/OxyServices.payment.js.map +1 -0
  64. package/lib/module/core/mixins/OxyServices.privacy.js +170 -0
  65. package/lib/module/core/mixins/OxyServices.privacy.js.map +1 -0
  66. package/lib/module/core/mixins/OxyServices.totp.js +49 -0
  67. package/lib/module/core/mixins/OxyServices.totp.js.map +1 -0
  68. package/lib/module/core/mixins/OxyServices.user.js +384 -0
  69. package/lib/module/core/mixins/OxyServices.user.js.map +1 -0
  70. package/lib/module/core/mixins/OxyServices.utility.js +156 -0
  71. package/lib/module/core/mixins/OxyServices.utility.js.map +1 -0
  72. package/lib/module/core/mixins/index.js +36 -0
  73. package/lib/module/core/mixins/index.js.map +1 -0
  74. package/lib/module/core/mixins/mixinHelpers.js +56 -0
  75. package/lib/module/core/mixins/mixinHelpers.js.map +1 -0
  76. package/lib/module/ui/context/OxyContext.js +27 -2
  77. package/lib/module/ui/context/OxyContext.js.map +1 -1
  78. package/lib/typescript/core/OxyServices.base.d.ts +123 -0
  79. package/lib/typescript/core/OxyServices.base.d.ts.map +1 -0
  80. package/lib/typescript/core/OxyServices.d.ts +970 -746
  81. package/lib/typescript/core/OxyServices.d.ts.map +1 -1
  82. package/lib/typescript/core/OxyServices.errors.d.ts +12 -0
  83. package/lib/typescript/core/OxyServices.errors.d.ts.map +1 -0
  84. package/lib/typescript/core/mixins/OxyServices.analytics.d.ts +70 -0
  85. package/lib/typescript/core/mixins/OxyServices.analytics.d.ts.map +1 -0
  86. package/lib/typescript/core/mixins/OxyServices.assets.d.ts +166 -0
  87. package/lib/typescript/core/mixins/OxyServices.assets.d.ts.map +1 -0
  88. package/lib/typescript/core/mixins/OxyServices.auth.d.ts +168 -0
  89. package/lib/typescript/core/mixins/OxyServices.auth.d.ts.map +1 -0
  90. package/lib/typescript/core/mixins/OxyServices.developer.d.ts +103 -0
  91. package/lib/typescript/core/mixins/OxyServices.developer.d.ts.map +1 -0
  92. package/lib/typescript/core/mixins/OxyServices.devices.d.ts +93 -0
  93. package/lib/typescript/core/mixins/OxyServices.devices.d.ts.map +1 -0
  94. package/lib/typescript/core/mixins/OxyServices.karma.d.ts +89 -0
  95. package/lib/typescript/core/mixins/OxyServices.karma.d.ts.map +1 -0
  96. package/lib/typescript/core/mixins/OxyServices.language.d.ts +85 -0
  97. package/lib/typescript/core/mixins/OxyServices.language.d.ts.map +1 -0
  98. package/lib/typescript/core/mixins/OxyServices.location.d.ts +68 -0
  99. package/lib/typescript/core/mixins/OxyServices.location.d.ts.map +1 -0
  100. package/lib/typescript/core/mixins/OxyServices.payment.d.ts +74 -0
  101. package/lib/typescript/core/mixins/OxyServices.payment.d.ts.map +1 -0
  102. package/lib/typescript/core/mixins/OxyServices.privacy.d.ts +126 -0
  103. package/lib/typescript/core/mixins/OxyServices.privacy.d.ts.map +1 -0
  104. package/lib/typescript/core/mixins/OxyServices.totp.d.ts +69 -0
  105. package/lib/typescript/core/mixins/OxyServices.totp.d.ts.map +1 -0
  106. package/lib/typescript/core/mixins/OxyServices.user.d.ts +189 -0
  107. package/lib/typescript/core/mixins/OxyServices.user.d.ts.map +1 -0
  108. package/lib/typescript/core/mixins/OxyServices.utility.d.ts +97 -0
  109. package/lib/typescript/core/mixins/OxyServices.utility.d.ts.map +1 -0
  110. package/lib/typescript/core/mixins/index.d.ts +899 -0
  111. package/lib/typescript/core/mixins/index.d.ts.map +1 -0
  112. package/lib/typescript/core/mixins/mixinHelpers.d.ts +32 -0
  113. package/lib/typescript/core/mixins/mixinHelpers.d.ts.map +1 -0
  114. package/lib/typescript/models/interfaces.d.ts +10 -0
  115. package/lib/typescript/models/interfaces.d.ts.map +1 -1
  116. package/lib/typescript/ui/context/OxyContext.d.ts +2 -0
  117. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  118. package/package.json +1 -1
  119. package/src/core/OxyServices.base.ts +311 -0
  120. package/src/core/OxyServices.errors.ts +26 -0
  121. package/src/core/OxyServices.ts +43 -2199
  122. package/src/core/mixins/OxyServices.analytics.ts +53 -0
  123. package/src/core/mixins/OxyServices.assets.ts +410 -0
  124. package/src/core/mixins/OxyServices.auth.ts +275 -0
  125. package/src/core/mixins/OxyServices.developer.ts +114 -0
  126. package/src/core/mixins/OxyServices.devices.ts +103 -0
  127. package/src/core/mixins/OxyServices.karma.ts +111 -0
  128. package/src/core/mixins/OxyServices.language.ts +127 -0
  129. package/src/core/mixins/OxyServices.location.ts +46 -0
  130. package/src/core/mixins/OxyServices.payment.ts +59 -0
  131. package/src/core/mixins/OxyServices.privacy.ts +182 -0
  132. package/src/core/mixins/OxyServices.totp.ts +36 -0
  133. package/src/core/mixins/OxyServices.user.ts +384 -0
  134. package/src/core/mixins/OxyServices.utility.ts +187 -0
  135. package/src/core/mixins/index.ts +58 -0
  136. package/src/core/mixins/mixinHelpers.ts +69 -0
  137. package/src/models/interfaces.ts +12 -0
  138. package/src/ui/context/OxyContext.tsx +36 -0
@@ -0,0 +1,384 @@
1
+ /**
2
+ * User Management Methods Mixin
3
+ */
4
+ import type { User, Notification, SearchProfilesResponse, PaginationInfo } from '../../models/interfaces';
5
+ import type { OxyServicesBase } from '../OxyServices.base';
6
+ import { buildSearchParams, buildPaginationParams, type PaginationParams } from '../../utils/apiUtils';
7
+
8
+ export function OxyServicesUserMixin<T extends typeof OxyServicesBase>(Base: T) {
9
+ return class extends Base {
10
+ constructor(...args: any[]) {
11
+ super(...(args as [any]));
12
+ }
13
+ /**
14
+ * Get profile by username
15
+ */
16
+ async getProfileByUsername(username: string): Promise<User> {
17
+ try {
18
+ return await this.makeRequest<User>('GET', `/api/profiles/username/${username}`, undefined, {
19
+ cache: true,
20
+ cacheTTL: 5 * 60 * 1000, // 5 minutes cache for profiles
21
+ });
22
+ } catch (error) {
23
+ throw this.handleError(error);
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Search user profiles
29
+ */
30
+ async searchProfiles(query: string, pagination?: PaginationParams): Promise<SearchProfilesResponse> {
31
+ try {
32
+ const params = { query, ...pagination };
33
+ const searchParams = buildSearchParams(params);
34
+ const paramsObj = Object.fromEntries(searchParams.entries());
35
+
36
+ const response = await this.makeRequest<SearchProfilesResponse | User[]>(
37
+ 'GET',
38
+ '/api/profiles/search',
39
+ paramsObj,
40
+ {
41
+ cache: true,
42
+ cacheTTL: 2 * 60 * 1000, // 2 minutes cache
43
+ }
44
+ );
45
+
46
+ // New API shape: { data: User[], pagination: {...} }
47
+ const isSearchProfilesResponse = (payload: unknown): payload is SearchProfilesResponse =>
48
+ typeof payload === 'object' &&
49
+ payload !== null &&
50
+ Array.isArray((payload as SearchProfilesResponse).data);
51
+
52
+ if (isSearchProfilesResponse(response)) {
53
+ const typedResponse = response;
54
+ const paginationInfo: PaginationInfo = typedResponse.pagination ?? {
55
+ total: typedResponse.data.length,
56
+ limit: pagination?.limit ?? typedResponse.data.length,
57
+ offset: pagination?.offset ?? 0,
58
+ hasMore: typedResponse.data.length === (pagination?.limit ?? typedResponse.data.length) &&
59
+ (pagination?.limit ?? typedResponse.data.length) > 0,
60
+ };
61
+
62
+ return {
63
+ data: typedResponse.data,
64
+ pagination: paginationInfo,
65
+ };
66
+ }
67
+
68
+ // Legacy API shape: returns raw User[]
69
+ if (Array.isArray(response)) {
70
+ const fallbackLimit = pagination?.limit ?? response.length;
71
+ const fallbackPagination: PaginationInfo = {
72
+ total: response.length,
73
+ limit: fallbackLimit,
74
+ offset: pagination?.offset ?? 0,
75
+ hasMore: fallbackLimit > 0 && response.length === fallbackLimit,
76
+ };
77
+
78
+ return { data: response, pagination: fallbackPagination };
79
+ }
80
+
81
+ // If response is unexpected, throw an error
82
+ throw new Error('Unexpected search response format');
83
+ } catch (error) {
84
+ throw this.handleError(error);
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Get profile recommendations
90
+ */
91
+ async getProfileRecommendations(): Promise<Array<{
92
+ id: string;
93
+ username: string;
94
+ name?: { first?: string; last?: string; full?: string };
95
+ description?: string;
96
+ _count?: { followers: number; following: number };
97
+ [key: string]: any;
98
+ }>> {
99
+ return this.withAuthRetry(async () => {
100
+ return await this.makeRequest('GET', '/api/profiles/recommendations', undefined, { cache: true });
101
+ }, 'getProfileRecommendations');
102
+ }
103
+
104
+ /**
105
+ * Get user by ID
106
+ */
107
+ async getUserById(userId: string): Promise<User> {
108
+ try {
109
+ return await this.makeRequest<User>('GET', `/api/users/${userId}`, undefined, {
110
+ cache: true,
111
+ cacheTTL: 5 * 60 * 1000, // 5 minutes cache
112
+ });
113
+ } catch (error) {
114
+ throw this.handleError(error);
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Get current user
120
+ */
121
+ async getCurrentUser(): Promise<User> {
122
+ return this.withAuthRetry(async () => {
123
+ return await this.makeRequest<User>('GET', '/api/users/me', undefined, {
124
+ cache: true,
125
+ cacheTTL: 1 * 60 * 1000, // 1 minute cache for current user
126
+ });
127
+ }, 'getCurrentUser');
128
+ }
129
+
130
+ /**
131
+ * Update user profile
132
+ */
133
+ async updateProfile(updates: Record<string, any>): Promise<User> {
134
+ try {
135
+ return await this.makeRequest<User>('PUT', '/api/users/me', updates, { cache: false });
136
+ } catch (error) {
137
+ throw this.handleError(error);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Get privacy settings for a user
143
+ * @param userId - The user ID (defaults to current user)
144
+ */
145
+ async getPrivacySettings(userId?: string): Promise<any> {
146
+ try {
147
+ const id = userId || (await this.getCurrentUser()).id;
148
+ return await this.makeRequest<any>('GET', `/api/privacy/${id}/privacy`, undefined, {
149
+ cache: true,
150
+ cacheTTL: 2 * 60 * 1000, // 2 minutes cache
151
+ });
152
+ } catch (error) {
153
+ throw this.handleError(error);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Update privacy settings
159
+ * @param settings - Partial privacy settings object
160
+ * @param userId - The user ID (defaults to current user)
161
+ */
162
+ async updatePrivacySettings(settings: Record<string, any>, userId?: string): Promise<any> {
163
+ try {
164
+ const id = userId || (await this.getCurrentUser()).id;
165
+ return await this.makeRequest<any>('PATCH', `/api/privacy/${id}/privacy`, settings, {
166
+ cache: false,
167
+ });
168
+ } catch (error) {
169
+ throw this.handleError(error);
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Request account verification
175
+ */
176
+ async requestAccountVerification(reason: string, evidence?: string): Promise<{ message: string; requestId: string }> {
177
+ try {
178
+ return await this.makeRequest<{ message: string; requestId: string }>('POST', '/api/users/verify/request', {
179
+ reason,
180
+ evidence,
181
+ }, { cache: false });
182
+ } catch (error) {
183
+ throw this.handleError(error);
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Download account data export
189
+ */
190
+ async downloadAccountData(format: 'json' | 'csv' = 'json'): Promise<Blob> {
191
+ try {
192
+ // Use axios instance directly for blob responses since RequestManager doesn't handle blobs
193
+ const axiosInstance = this.getClient().getAxiosInstance();
194
+
195
+ const response = await axiosInstance.get(`/api/users/me/data?format=${format}`, {
196
+ responseType: 'blob',
197
+ });
198
+
199
+ return response.data as Blob;
200
+ } catch (error) {
201
+ throw this.handleError(error);
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Delete account permanently
207
+ * @param password - User password for confirmation
208
+ * @param confirmText - Confirmation text (usually username)
209
+ */
210
+ async deleteAccount(password: string, confirmText: string): Promise<{ message: string }> {
211
+ try {
212
+ return await this.makeRequest<{ message: string }>('DELETE', '/api/users/me', {
213
+ password,
214
+ confirmText,
215
+ }, { cache: false });
216
+ } catch (error) {
217
+ throw this.handleError(error);
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Update user by ID (admin function)
223
+ */
224
+ async updateUser(userId: string, updates: Record<string, any>): Promise<User> {
225
+ try {
226
+ return await this.makeRequest<User>('PUT', `/api/users/${userId}`, updates, { cache: false });
227
+ } catch (error) {
228
+ throw this.handleError(error);
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Follow a user
234
+ */
235
+ async followUser(userId: string): Promise<{ success: boolean; message: string }> {
236
+ try {
237
+ return await this.makeRequest('POST', `/api/users/${userId}/follow`, undefined, { cache: false });
238
+ } catch (error) {
239
+ throw this.handleError(error);
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Unfollow a user
245
+ */
246
+ async unfollowUser(userId: string): Promise<{ success: boolean; message: string }> {
247
+ try {
248
+ return await this.makeRequest('DELETE', `/api/users/${userId}/follow`, undefined, { cache: false });
249
+ } catch (error) {
250
+ throw this.handleError(error);
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Get follow status
256
+ */
257
+ async getFollowStatus(userId: string): Promise<{ isFollowing: boolean }> {
258
+ try {
259
+ return await this.makeRequest('GET', `/api/users/${userId}/follow-status`, undefined, {
260
+ cache: true,
261
+ cacheTTL: 1 * 60 * 1000, // 1 minute cache
262
+ });
263
+ } catch (error) {
264
+ throw this.handleError(error);
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Get user followers
270
+ */
271
+ async getUserFollowers(
272
+ userId: string,
273
+ pagination?: PaginationParams
274
+ ): Promise<{ followers: User[]; total: number; hasMore: boolean }> {
275
+ try {
276
+ const params = buildPaginationParams(pagination || {});
277
+ const response = await this.makeRequest<{ data: User[]; pagination: { total: number; hasMore: boolean } }>('GET', `/api/users/${userId}/followers`, params, {
278
+ cache: true,
279
+ cacheTTL: 2 * 60 * 1000, // 2 minutes cache
280
+ });
281
+ return {
282
+ followers: response.data || [],
283
+ total: response.pagination.total,
284
+ hasMore: response.pagination.hasMore,
285
+ };
286
+ } catch (error) {
287
+ throw this.handleError(error);
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Get user following
293
+ */
294
+ async getUserFollowing(
295
+ userId: string,
296
+ pagination?: PaginationParams
297
+ ): Promise<{ following: User[]; total: number; hasMore: boolean }> {
298
+ try {
299
+ const params = buildPaginationParams(pagination || {});
300
+ const response = await this.makeRequest<{ data: User[]; pagination: { total: number; hasMore: boolean } }>('GET', `/api/users/${userId}/following`, params, {
301
+ cache: true,
302
+ cacheTTL: 2 * 60 * 1000, // 2 minutes cache
303
+ });
304
+ return {
305
+ following: response.data || [],
306
+ total: response.pagination.total,
307
+ hasMore: response.pagination.hasMore,
308
+ };
309
+ } catch (error) {
310
+ throw this.handleError(error);
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Get notifications
316
+ */
317
+ async getNotifications(): Promise<Notification[]> {
318
+ return this.withAuthRetry(async () => {
319
+ return await this.makeRequest<Notification[]>('GET', '/api/notifications', undefined, {
320
+ cache: false, // Don't cache notifications - always get fresh data
321
+ });
322
+ }, 'getNotifications');
323
+ }
324
+
325
+ /**
326
+ * Get unread notification count
327
+ */
328
+ async getUnreadCount(): Promise<number> {
329
+ try {
330
+ const res = await this.makeRequest<{ count: number }>('GET', '/api/notifications/unread-count', undefined, {
331
+ cache: false, // Don't cache unread count - always get fresh data
332
+ });
333
+ return res.count;
334
+ } catch (error) {
335
+ throw this.handleError(error);
336
+ }
337
+ }
338
+
339
+ /**
340
+ * Create notification
341
+ */
342
+ async createNotification(data: Partial<Notification>): Promise<Notification> {
343
+ try {
344
+ return await this.makeRequest<Notification>('POST', '/api/notifications', data, { cache: false });
345
+ } catch (error) {
346
+ throw this.handleError(error);
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Mark notification as read
352
+ */
353
+ async markNotificationAsRead(notificationId: string): Promise<void> {
354
+ try {
355
+ await this.makeRequest('PUT', `/api/notifications/${notificationId}/read`, undefined, { cache: false });
356
+ } catch (error) {
357
+ throw this.handleError(error);
358
+ }
359
+ }
360
+
361
+ /**
362
+ * Mark all notifications as read
363
+ */
364
+ async markAllNotificationsAsRead(): Promise<void> {
365
+ try {
366
+ await this.makeRequest('PUT', '/api/notifications/read-all', undefined, { cache: false });
367
+ } catch (error) {
368
+ throw this.handleError(error);
369
+ }
370
+ }
371
+
372
+ /**
373
+ * Delete notification
374
+ */
375
+ async deleteNotification(notificationId: string): Promise<void> {
376
+ try {
377
+ await this.makeRequest('DELETE', `/api/notifications/${notificationId}`, undefined, { cache: false });
378
+ } catch (error) {
379
+ throw this.handleError(error);
380
+ }
381
+ }
382
+ };
383
+ }
384
+
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Utility Methods Mixin
3
+ *
4
+ * Provides utility methods including link metadata fetching
5
+ * and Express.js authentication middleware
6
+ */
7
+ import { jwtDecode } from 'jwt-decode';
8
+ import type { ApiError, User } from '../../models/interfaces';
9
+ import type { OxyServicesBase } from '../OxyServices.base';
10
+ import { CACHE_TIMES } from './mixinHelpers';
11
+
12
+ interface JwtPayload {
13
+ exp?: number;
14
+ userId?: string;
15
+ id?: string;
16
+ sessionId?: string;
17
+ [key: string]: any;
18
+ }
19
+
20
+ export function OxyServicesUtilityMixin<T extends typeof OxyServicesBase>(Base: T) {
21
+ return class extends Base {
22
+ constructor(...args: any[]) {
23
+ super(...(args as [any]));
24
+ }
25
+ /**
26
+ * Fetch link metadata
27
+ */
28
+ async fetchLinkMetadata(url: string): Promise<{
29
+ url: string;
30
+ title: string;
31
+ description: string;
32
+ image?: string;
33
+ }> {
34
+ try {
35
+ return await this.makeRequest<{
36
+ url: string;
37
+ title: string;
38
+ description: string;
39
+ image?: string;
40
+ }>('GET', '/api/link-metadata', { url }, {
41
+ cache: true,
42
+ cacheTTL: CACHE_TIMES.EXTRA_LONG,
43
+ });
44
+ } catch (error) {
45
+ throw this.handleError(error);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Simple Express.js authentication middleware
51
+ *
52
+ * Built-in authentication middleware that validates JWT tokens and adds user data to requests.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * // Basic usage - just add it to your routes
57
+ * app.use('/api/protected', oxyServices.auth());
58
+ *
59
+ * // With debug logging
60
+ * app.use('/api/protected', oxyServices.auth({ debug: true }));
61
+ *
62
+ * // With custom error handling
63
+ * app.use('/api/protected', oxyServices.auth({
64
+ * onError: (error) => console.error('Auth failed:', error)
65
+ * }));
66
+ *
67
+ * // Load full user data
68
+ * app.use('/api/protected', oxyServices.auth({ loadUser: true }));
69
+ * ```
70
+ *
71
+ * @param options Optional configuration
72
+ * @param options.debug Enable debug logging (default: false)
73
+ * @param options.onError Custom error handler
74
+ * @param options.loadUser Load full user data (default: false for performance)
75
+ * @param options.session Use session-based validation (default: false)
76
+ * @returns Express middleware function
77
+ */
78
+ auth(options: {
79
+ debug?: boolean;
80
+ onError?: (error: ApiError) => any;
81
+ loadUser?: boolean;
82
+ session?: boolean;
83
+ } = {}) {
84
+ const { debug = false, onError, loadUser = false, session = false } = options;
85
+
86
+ // Return a synchronous middleware function
87
+ return (req: any, res: any, next: any) => {
88
+ try {
89
+ // Extract token from Authorization header
90
+ const authHeader = req.headers['authorization'];
91
+ const token = authHeader?.startsWith('Bearer ') ? authHeader.substring(7) : null;
92
+
93
+ if (debug) {
94
+ console.log(`🔐 Auth: Processing ${req.method} ${req.path}`);
95
+ console.log(`🔐 Auth: Token present: ${!!token}`);
96
+ }
97
+
98
+ if (!token) {
99
+ const error = {
100
+ message: 'Access token required',
101
+ code: 'MISSING_TOKEN',
102
+ status: 401
103
+ };
104
+
105
+ if (debug) console.log(`❌ Auth: Missing token`);
106
+
107
+ if (onError) return onError(error);
108
+ return res.status(401).json(error);
109
+ }
110
+
111
+ // Decode and validate token
112
+ let decoded: JwtPayload;
113
+ try {
114
+ decoded = jwtDecode<JwtPayload>(token);
115
+
116
+ if (debug) {
117
+ console.log(`🔐 Auth: Token decoded, User ID: ${decoded.userId || decoded.id}`);
118
+ }
119
+ } catch (decodeError) {
120
+ const error = {
121
+ message: 'Invalid token format',
122
+ code: 'INVALID_TOKEN_FORMAT',
123
+ status: 403
124
+ };
125
+
126
+ if (debug) console.log(`❌ Auth: Token decode failed`);
127
+
128
+ if (onError) return onError(error);
129
+ return res.status(403).json(error);
130
+ }
131
+
132
+ const userId = decoded.userId || decoded.id;
133
+ if (!userId) {
134
+ const error = {
135
+ message: 'Token missing user ID',
136
+ code: 'INVALID_TOKEN_PAYLOAD',
137
+ status: 403
138
+ };
139
+
140
+ if (debug) console.log(`❌ Auth: Token missing user ID`);
141
+
142
+ if (onError) return onError(error);
143
+ return res.status(403).json(error);
144
+ }
145
+
146
+ // Check token expiration
147
+ if (decoded.exp && decoded.exp < Math.floor(Date.now() / 1000)) {
148
+ const error = {
149
+ message: 'Token expired',
150
+ code: 'TOKEN_EXPIRED',
151
+ status: 403
152
+ };
153
+
154
+ if (debug) console.log(`❌ Auth: Token expired`);
155
+
156
+ if (onError) return onError(error);
157
+ return res.status(403).json(error);
158
+ }
159
+
160
+ // For now, skip session validation to keep it simple
161
+ // Session validation can be added later if needed
162
+
163
+ // Set request properties immediately
164
+ req.userId = userId;
165
+ req.accessToken = token;
166
+ req.user = { id: userId } as User;
167
+
168
+ if (debug) {
169
+ console.log(`✅ Auth: Authentication successful for user ${userId}`);
170
+ }
171
+
172
+ next();
173
+ } catch (error) {
174
+ const apiError = this.handleError(error) as any;
175
+
176
+ if (debug) {
177
+ console.log(`❌ Auth: Unexpected error:`, apiError);
178
+ }
179
+
180
+ if (onError) return onError(apiError);
181
+ return res.status((apiError && apiError.status) || 500).json(apiError);
182
+ }
183
+ };
184
+ }
185
+ };
186
+ }
187
+
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Centralized mixin exports and composition helper
3
+ *
4
+ * This module provides a clean way to compose all mixins
5
+ * and ensures consistent ordering for better maintainability
6
+ */
7
+
8
+ import { OxyServicesBase } from '../OxyServices.base';
9
+ import { OxyServicesAuthMixin } from './OxyServices.auth';
10
+ import { OxyServicesUserMixin } from './OxyServices.user';
11
+ import { OxyServicesTotpMixin } from './OxyServices.totp';
12
+ import { OxyServicesPrivacyMixin } from './OxyServices.privacy';
13
+ import { OxyServicesLanguageMixin } from './OxyServices.language';
14
+ import { OxyServicesPaymentMixin } from './OxyServices.payment';
15
+ import { OxyServicesKarmaMixin } from './OxyServices.karma';
16
+ import { OxyServicesAssetsMixin } from './OxyServices.assets';
17
+ import { OxyServicesDeveloperMixin } from './OxyServices.developer';
18
+ import { OxyServicesLocationMixin } from './OxyServices.location';
19
+ import { OxyServicesAnalyticsMixin } from './OxyServices.analytics';
20
+ import { OxyServicesDevicesMixin } from './OxyServices.devices';
21
+ import { OxyServicesUtilityMixin } from './OxyServices.utility';
22
+
23
+ /**
24
+ * Composes all OxyServices mixins in the correct order
25
+ *
26
+ * Order matters for mixins - dependencies should be applied first.
27
+ * This function ensures consistent composition across the codebase.
28
+ *
29
+ * @returns The fully composed OxyServices class with all mixins applied
30
+ */
31
+ export function composeOxyServices() {
32
+ return OxyServicesUtilityMixin(
33
+ OxyServicesDevicesMixin(
34
+ OxyServicesAnalyticsMixin(
35
+ OxyServicesLocationMixin(
36
+ OxyServicesDeveloperMixin(
37
+ OxyServicesAssetsMixin(
38
+ OxyServicesKarmaMixin(
39
+ OxyServicesPaymentMixin(
40
+ OxyServicesLanguageMixin(
41
+ OxyServicesPrivacyMixin(
42
+ OxyServicesTotpMixin(
43
+ OxyServicesUserMixin(
44
+ OxyServicesAuthMixin(OxyServicesBase)
45
+ )
46
+ )
47
+ )
48
+ )
49
+ )
50
+ )
51
+ )
52
+ )
53
+ )
54
+ )
55
+ )
56
+ );
57
+ }
58
+
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Helper utilities for mixin classes
3
+ * Provides common patterns to reduce code duplication
4
+ */
5
+
6
+ import type { OxyServicesBase } from '../OxyServices.base';
7
+
8
+ /**
9
+ * Wraps an async method with standard error handling
10
+ * Reduces boilerplate in mixin methods
11
+ */
12
+ export async function withErrorHandling<T>(
13
+ operation: () => Promise<T>,
14
+ handleError: (error: any) => Error
15
+ ): Promise<T> {
16
+ try {
17
+ return await operation();
18
+ } catch (error) {
19
+ throw handleError(error);
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Creates a standard API request method with error handling
25
+ * Reduces duplication across mixin methods
26
+ */
27
+ export function createApiMethod<T>(
28
+ makeRequest: OxyServicesBase['makeRequest'],
29
+ handleError: OxyServicesBase['handleError'],
30
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
31
+ url: string | ((...args: any[]) => string),
32
+ options: {
33
+ cache?: boolean;
34
+ cacheTTL?: number;
35
+ retry?: boolean;
36
+ transformData?: (data: any) => any;
37
+ transformResponse?: (response: any) => T;
38
+ } = {}
39
+ ) {
40
+ return async (...args: any[]): Promise<T> => {
41
+ const urlString = typeof url === 'function' ? url(...args) : url;
42
+ const requestData = options.transformData ? options.transformData(args) : args[0];
43
+
44
+ const requestOptions = {
45
+ cache: options.cache ?? true,
46
+ cacheTTL: options.cacheTTL,
47
+ retry: options.retry ?? true,
48
+ };
49
+
50
+ try {
51
+ const response = await makeRequest<T>(method, urlString, requestData, requestOptions);
52
+ return options.transformResponse ? options.transformResponse(response) : response;
53
+ } catch (error) {
54
+ throw handleError(error);
55
+ }
56
+ };
57
+ }
58
+
59
+ /**
60
+ * Cache time constants (in milliseconds)
61
+ */
62
+ export const CACHE_TIMES = {
63
+ SHORT: 1 * 60 * 1000, // 1 minute
64
+ MEDIUM: 2 * 60 * 1000, // 2 minutes
65
+ LONG: 5 * 60 * 1000, // 5 minutes
66
+ VERY_LONG: 10 * 60 * 1000, // 10 minutes
67
+ EXTRA_LONG: 30 * 60 * 1000, // 30 minutes
68
+ } as const;
69
+