@navegarti/rn-design-system 0.8.6 → 0.8.7

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 (35) hide show
  1. package/lib/module/api/errors.js +105 -18
  2. package/lib/module/api/index.js +15 -15
  3. package/lib/module/api/nitro-adapter.js +290 -0
  4. package/lib/module/api/retry-strategy.js +42 -25
  5. package/lib/module/api/stores/auth-store.js +17 -3
  6. package/lib/module/api/types.js +0 -2
  7. package/lib/module/api.js +1 -1
  8. package/lib/module/components/Carousel/components/see-all-button.js +2 -1
  9. package/lib/module/index.js +1 -1
  10. package/lib/typescript/src/api/errors.d.ts +98 -15
  11. package/lib/typescript/src/api/index.d.ts +13 -12
  12. package/lib/typescript/src/api/nitro-adapter.d.ts +121 -0
  13. package/lib/typescript/src/api/retry-strategy.d.ts +28 -9
  14. package/lib/typescript/src/api/stores/auth-store.d.ts +26 -3
  15. package/lib/typescript/src/api/types.d.ts +54 -20
  16. package/lib/typescript/src/api.d.ts +2 -2
  17. package/lib/typescript/src/components/Card/index.d.ts +9 -9
  18. package/lib/typescript/src/components/Card/styles.d.ts +9 -9
  19. package/lib/typescript/src/components/Carousel/components/see-all-button.d.ts +2 -1
  20. package/lib/typescript/src/components/Carousel/index.d.ts +2 -1
  21. package/lib/typescript/src/index.d.ts +2 -2
  22. package/package.json +30 -29
  23. package/src/api/errors.ts +99 -18
  24. package/src/api/index.ts +15 -15
  25. package/src/api/nitro-adapter.ts +357 -0
  26. package/src/api/retry-strategy.ts +45 -26
  27. package/src/api/stores/auth-store.ts +26 -3
  28. package/src/api/types.ts +61 -21
  29. package/src/api.tsx +2 -2
  30. package/src/components/Carousel/components/see-all-button.tsx +3 -1
  31. package/src/components/Carousel/index.tsx +1 -1
  32. package/src/index.tsx +2 -2
  33. package/lib/module/api/axios-adapter.js +0 -154
  34. package/lib/typescript/src/api/axios-adapter.d.ts +0 -57
  35. package/src/api/axios-adapter.ts +0 -239
@@ -1,239 +0,0 @@
1
- import axios, {
2
- type AxiosError,
3
- type AxiosInstance,
4
- type AxiosRequestConfig,
5
- type AxiosResponse,
6
- } from 'axios';
7
- import {
8
- AuthError,
9
- NetworkError,
10
- NotFoundError,
11
- RateLimitError,
12
- ServerError,
13
- TimeoutError,
14
- ValidationError,
15
- } from './errors';
16
- import { executeWithRetry } from './retry-strategy';
17
- import { useAuthStore } from './stores/auth-store';
18
- import type { ApiConfig, IHttpAdapter, RetryConfig } from './types';
19
-
20
- /**
21
- * Axios-based HTTP adapter implementation
22
- * Implements retry logic, error handling, and token management
23
- */
24
- export class AxiosAdapter implements IHttpAdapter {
25
- private readonly axiosInstance: AxiosInstance;
26
- private readonly retryConfig: RetryConfig;
27
-
28
- constructor(config: ApiConfig) {
29
- // Initialize axios instance with configuration
30
- this.axiosInstance = axios.create({
31
- baseURL: config.baseURL,
32
- timeout: config.timeout ?? 30000, // 30 seconds default
33
- headers: {
34
- 'Content-Type': 'application/json',
35
- ...config.headers,
36
- },
37
- });
38
-
39
- // Configure retry behavior
40
- this.retryConfig = {
41
- maxAttempts: config.retryAttempts ?? 3,
42
- baseDelay: 1000, // 1 second
43
- maxDelay: 10000, // 10 seconds
44
- retryableStatusCodes: [408, 429, 500, 502, 503, 504],
45
- };
46
-
47
- // Setup request interceptor to inject auth token
48
- this.setupRequestInterceptor();
49
- }
50
-
51
- /**
52
- * Sets up request interceptor to automatically add auth token
53
- */
54
- private setupRequestInterceptor(): void {
55
- this.axiosInstance.interceptors.request.use(
56
- (config) => {
57
- const token = useAuthStore.getState().token;
58
-
59
- if (token && config.headers) {
60
- config.headers.Authorization = `Bearer ${token}`;
61
- }
62
-
63
- return config;
64
- },
65
- (error) => Promise.reject(error),
66
- );
67
- }
68
-
69
- /**
70
- * Maps axios error to appropriate custom error type
71
- */
72
- private mapError(error: AxiosError, url?: string, method?: string): Error {
73
- // Network errors (no response received)
74
- if (!error.response) {
75
- if (error.code === 'ECONNABORTED' || error.message.includes('timeout')) {
76
- return new TimeoutError(
77
- 'Request timeout - server took too long to respond',
78
- url,
79
- method,
80
- );
81
- }
82
- return new NetworkError(
83
- error.message || 'Network error - please check your connection',
84
- url,
85
- method,
86
- );
87
- }
88
-
89
- const statusCode = error.response.status;
90
- const responseData = error.response.data as {
91
- message?: string;
92
- errors?: Record<string, string[]>;
93
- };
94
- const message =
95
- responseData?.message || error.message || 'An error occurred';
96
-
97
- // Map status codes to error types
98
- switch (true) {
99
- case statusCode === 401 || statusCode === 403:
100
- return new AuthError(message, statusCode, url, method);
101
-
102
- case statusCode === 404:
103
- return new NotFoundError(message, url, method);
104
-
105
- case statusCode === 429:
106
- return new RateLimitError(
107
- message,
108
- url,
109
- method,
110
- Number(error.response.headers['retry-after']),
111
- );
112
-
113
- case statusCode === 400 || statusCode === 422:
114
- return new ValidationError(
115
- message,
116
- statusCode,
117
- url,
118
- method,
119
- responseData?.errors,
120
- );
121
-
122
- case statusCode >= 500:
123
- return new ServerError(message, statusCode, url, method);
124
-
125
- default:
126
- return new NetworkError(message, url, method);
127
- }
128
- }
129
-
130
- /**
131
- * Transforms axios response to standardized ApiResponse format
132
- */
133
- private transformResponse<T>(response: AxiosResponse<T>): T {
134
- return response.data;
135
- }
136
-
137
- /**
138
- * Executes HTTP request with retry logic and error handling
139
- */
140
- private async executeRequest<T>(
141
- requestFn: () => Promise<AxiosResponse<T>>,
142
- url?: string,
143
- method?: string,
144
- ): Promise<T> {
145
- try {
146
- const response = await executeWithRetry(
147
- requestFn,
148
- this.retryConfig,
149
- (attempt, delay, error) => {
150
- console.log(
151
- `[API Retry] Attempt ${attempt} failed, retrying in ${delay}ms...`,
152
- error.message,
153
- );
154
- },
155
- );
156
-
157
- return this.transformResponse(response);
158
- } catch (error) {
159
- throw this.mapError(error as AxiosError, url, method);
160
- }
161
- }
162
-
163
- /**
164
- * Performs GET request
165
- */
166
- async get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T> {
167
- return this.executeRequest(
168
- () => this.axiosInstance.get<T>(url, config),
169
- url,
170
- 'GET',
171
- );
172
- }
173
-
174
- /**
175
- * Performs POST request
176
- */
177
- async post<T = unknown>(
178
- url: string,
179
- data?: unknown,
180
- config?: AxiosRequestConfig,
181
- ): Promise<T> {
182
- return this.executeRequest(
183
- () => this.axiosInstance.post<T>(url, data, config),
184
- url,
185
- 'POST',
186
- );
187
- }
188
-
189
- /**
190
- * Performs PUT request
191
- */
192
- async put<T = unknown>(
193
- url: string,
194
- data?: unknown,
195
- config?: AxiosRequestConfig,
196
- ): Promise<T> {
197
- return this.executeRequest(
198
- () => this.axiosInstance.put<T>(url, data, config),
199
- url,
200
- 'PUT',
201
- );
202
- }
203
-
204
- /**
205
- * Performs DELETE request
206
- */
207
- async delete<T = unknown>(
208
- url: string,
209
- config?: AxiosRequestConfig,
210
- ): Promise<T> {
211
- return this.executeRequest(
212
- () => this.axiosInstance.delete<T>(url, config),
213
- url,
214
- 'DELETE',
215
- );
216
- }
217
-
218
- /**
219
- * Adds authentication token to store
220
- * Token will be automatically included in all subsequent requests
221
- */
222
- addToken(token: string): void {
223
- useAuthStore.getState().setToken(token);
224
- }
225
-
226
- /**
227
- * Removes authentication token from store
228
- */
229
- removeToken(): void {
230
- useAuthStore.getState().clearToken();
231
- }
232
-
233
- /**
234
- * Get token from store
235
- */
236
- getToken(): string | null {
237
- return useAuthStore.getState().token;
238
- }
239
- }