@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.
- package/lib/module/api/errors.js +105 -18
- package/lib/module/api/index.js +15 -15
- package/lib/module/api/nitro-adapter.js +290 -0
- package/lib/module/api/retry-strategy.js +42 -25
- package/lib/module/api/stores/auth-store.js +17 -3
- package/lib/module/api/types.js +0 -2
- package/lib/module/api.js +1 -1
- package/lib/module/components/Carousel/components/see-all-button.js +2 -1
- package/lib/module/index.js +1 -1
- package/lib/typescript/src/api/errors.d.ts +98 -15
- package/lib/typescript/src/api/index.d.ts +13 -12
- package/lib/typescript/src/api/nitro-adapter.d.ts +121 -0
- package/lib/typescript/src/api/retry-strategy.d.ts +28 -9
- package/lib/typescript/src/api/stores/auth-store.d.ts +26 -3
- package/lib/typescript/src/api/types.d.ts +54 -20
- package/lib/typescript/src/api.d.ts +2 -2
- package/lib/typescript/src/components/Card/index.d.ts +9 -9
- package/lib/typescript/src/components/Card/styles.d.ts +9 -9
- package/lib/typescript/src/components/Carousel/components/see-all-button.d.ts +2 -1
- package/lib/typescript/src/components/Carousel/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts +2 -2
- package/package.json +30 -29
- package/src/api/errors.ts +99 -18
- package/src/api/index.ts +15 -15
- package/src/api/nitro-adapter.ts +357 -0
- package/src/api/retry-strategy.ts +45 -26
- package/src/api/stores/auth-store.ts +26 -3
- package/src/api/types.ts +61 -21
- package/src/api.tsx +2 -2
- package/src/components/Carousel/components/see-all-button.tsx +3 -1
- package/src/components/Carousel/index.tsx +1 -1
- package/src/index.tsx +2 -2
- package/lib/module/api/axios-adapter.js +0 -154
- package/lib/typescript/src/api/axios-adapter.d.ts +0 -57
- package/src/api/axios-adapter.ts +0 -239
|
@@ -7,6 +7,7 @@ import { SeeAllButton } from "../styles.js";
|
|
|
7
7
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
8
8
|
const CarouselSeeAllButton = ({
|
|
9
9
|
color,
|
|
10
|
+
text = 'Ver todos',
|
|
10
11
|
...props
|
|
11
12
|
}) => {
|
|
12
13
|
return /*#__PURE__*/_jsxs(SeeAllButton, {
|
|
@@ -14,7 +15,7 @@ const CarouselSeeAllButton = ({
|
|
|
14
15
|
children: [/*#__PURE__*/_jsx(Text, {
|
|
15
16
|
weight: "bold",
|
|
16
17
|
color: color ?? '#014661',
|
|
17
|
-
children:
|
|
18
|
+
children: text
|
|
18
19
|
}), /*#__PURE__*/_jsx(FontAwesomeIcon, {
|
|
19
20
|
icon: faChevronRight,
|
|
20
21
|
size: 12,
|
package/lib/module/index.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
// API
|
|
15
15
|
export { ApiError, AuthError, NetworkError, NotFoundError, RateLimitError, ServerError, TimeoutError, ValidationError } from "./api/errors.js";
|
|
16
|
-
export {
|
|
16
|
+
export { apiRequest, NitroAdapter, useAuthStore } from "./api/index.js";
|
|
17
17
|
// Components
|
|
18
18
|
export { Button } from "./components/Button/index.js";
|
|
19
19
|
export { Card } from "./components/Card/index.js";
|
|
@@ -1,63 +1,146 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Base class for all API errors
|
|
3
|
-
* Provides common structure and debugging information
|
|
2
|
+
* Base class for all API errors.
|
|
3
|
+
* Provides common structure and debugging information for HTTP-layer failures.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* try {
|
|
8
|
+
* await api.get('/users/me');
|
|
9
|
+
* } catch (error) {
|
|
10
|
+
* if (error instanceof ApiError) {
|
|
11
|
+
* console.error(error.toDebugString());
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
4
15
|
*/
|
|
5
16
|
export declare class ApiError extends Error {
|
|
17
|
+
/** HTTP status code associated with the error (0 for network-layer failures). */
|
|
6
18
|
readonly statusCode: number;
|
|
19
|
+
/** URL of the request that produced this error. */
|
|
7
20
|
readonly url?: string;
|
|
21
|
+
/** HTTP method of the request that produced this error. */
|
|
8
22
|
readonly method?: string;
|
|
23
|
+
/** Timestamp at which the error was created. */
|
|
9
24
|
readonly timestamp: Date;
|
|
25
|
+
/**
|
|
26
|
+
* @param message Human-readable error description.
|
|
27
|
+
* @param statusCode HTTP status code (0 for network-level errors).
|
|
28
|
+
* @param url Request URL, if available.
|
|
29
|
+
* @param method HTTP method, if available.
|
|
30
|
+
*/
|
|
10
31
|
constructor(message: string, statusCode: number, url?: string, method?: string);
|
|
11
32
|
/**
|
|
12
|
-
* Returns a formatted error
|
|
33
|
+
* Returns a formatted multi-line string with all error details, useful for
|
|
34
|
+
* logging and debugging.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* console.error(error.toDebugString());
|
|
39
|
+
* // [AuthError] Unauthorized
|
|
40
|
+
* // Status: 401
|
|
41
|
+
* // URL: /users/me
|
|
42
|
+
* // Method: GET
|
|
43
|
+
* // Time: 2026-05-06T12:00:00.000Z
|
|
44
|
+
* ```
|
|
13
45
|
*/
|
|
14
46
|
toDebugString(): string;
|
|
15
47
|
}
|
|
16
48
|
/**
|
|
17
|
-
* Network-
|
|
18
|
-
* HTTP Status: N/A (
|
|
49
|
+
* Network-layer errors: no internet connection, DNS failure, or connection refused.
|
|
50
|
+
* HTTP Status: N/A (statusCode is always 0).
|
|
19
51
|
*/
|
|
20
52
|
export declare class NetworkError extends ApiError {
|
|
53
|
+
/**
|
|
54
|
+
* @param message Human-readable description of the network failure.
|
|
55
|
+
* @param url Request URL, if available.
|
|
56
|
+
* @param method HTTP method, if available.
|
|
57
|
+
*/
|
|
21
58
|
constructor(message: string, url?: string, method?: string);
|
|
22
59
|
}
|
|
23
60
|
/**
|
|
24
|
-
* Authentication errors (401, 403)
|
|
25
|
-
*
|
|
61
|
+
* Authentication or authorisation errors (401, 403).
|
|
62
|
+
* The client either lacks valid credentials or does not have permission.
|
|
63
|
+
*
|
|
64
|
+
* On a **401**, the {@link NitroAdapter} automatically clears the stored
|
|
65
|
+
* auth token before throwing this error.
|
|
66
|
+
*
|
|
67
|
+
* @param message Human-readable description.
|
|
68
|
+
* @param statusCode 401 or 403.
|
|
69
|
+
* @param url Request URL, if available.
|
|
70
|
+
* @param method HTTP method, if available.
|
|
26
71
|
*/
|
|
27
72
|
export declare class AuthError extends ApiError {
|
|
28
73
|
}
|
|
29
74
|
/**
|
|
30
|
-
* Client validation errors (400, 422)
|
|
31
|
-
*
|
|
75
|
+
* Client validation errors (400, 422).
|
|
76
|
+
* The request was malformed or failed server-side validation.
|
|
32
77
|
*/
|
|
33
78
|
export declare class ValidationError extends ApiError {
|
|
79
|
+
/** Field-level validation error messages keyed by field name. */
|
|
34
80
|
readonly validationErrors?: Record<string, string[]>;
|
|
81
|
+
/**
|
|
82
|
+
* @param message Human-readable summary.
|
|
83
|
+
* @param statusCode 400 or 422.
|
|
84
|
+
* @param url Request URL, if available.
|
|
85
|
+
* @param method HTTP method, if available.
|
|
86
|
+
* @param validationErrors Optional map of field names to their error messages.
|
|
87
|
+
*/
|
|
35
88
|
constructor(message: string, statusCode: number, url?: string, method?: string, validationErrors?: Record<string, string[]>);
|
|
36
89
|
}
|
|
37
90
|
/**
|
|
38
|
-
* Server errors (500, 502, 503, 504)
|
|
39
|
-
* Something went wrong on the server
|
|
91
|
+
* Server errors (500, 502, 503, 504).
|
|
92
|
+
* Something went wrong on the server side.
|
|
93
|
+
*
|
|
94
|
+
* @param message Human-readable description.
|
|
95
|
+
* @param statusCode 5xx status code.
|
|
96
|
+
* @param url Request URL, if available.
|
|
97
|
+
* @param method HTTP method, if available.
|
|
40
98
|
*/
|
|
41
99
|
export declare class ServerError extends ApiError {
|
|
42
100
|
}
|
|
43
101
|
/**
|
|
44
|
-
* Request timeout errors
|
|
45
|
-
*
|
|
102
|
+
* Request timeout errors.
|
|
103
|
+
* The server did not respond within the configured timeout window.
|
|
104
|
+
* HTTP Status: 408.
|
|
46
105
|
*/
|
|
47
106
|
export declare class TimeoutError extends ApiError {
|
|
107
|
+
/**
|
|
108
|
+
* @param message Human-readable description.
|
|
109
|
+
* @param url Request URL, if available.
|
|
110
|
+
* @param method HTTP method, if available.
|
|
111
|
+
*/
|
|
48
112
|
constructor(message: string, url?: string, method?: string);
|
|
49
113
|
}
|
|
50
114
|
/**
|
|
51
|
-
* Resource not found errors
|
|
115
|
+
* Resource not found errors.
|
|
116
|
+
* HTTP Status: 404.
|
|
52
117
|
*/
|
|
53
118
|
export declare class NotFoundError extends ApiError {
|
|
119
|
+
/**
|
|
120
|
+
* @param message Human-readable description.
|
|
121
|
+
* @param url Request URL, if available.
|
|
122
|
+
* @param method HTTP method, if available.
|
|
123
|
+
*/
|
|
54
124
|
constructor(message: string, url?: string, method?: string);
|
|
55
125
|
}
|
|
56
126
|
/**
|
|
57
|
-
* Rate limit errors
|
|
127
|
+
* Rate limit errors.
|
|
128
|
+
* The server has rejected the request because too many have been sent.
|
|
129
|
+
* HTTP Status: 429.
|
|
58
130
|
*/
|
|
59
131
|
export declare class RateLimitError extends ApiError {
|
|
132
|
+
/**
|
|
133
|
+
* Number of seconds to wait before retrying, as advertised by the server
|
|
134
|
+
* in the `Retry-After` response header. May be `undefined` if the header
|
|
135
|
+
* was absent.
|
|
136
|
+
*/
|
|
60
137
|
readonly retryAfter?: number;
|
|
138
|
+
/**
|
|
139
|
+
* @param message Human-readable description.
|
|
140
|
+
* @param url Request URL, if available.
|
|
141
|
+
* @param method HTTP method, if available.
|
|
142
|
+
* @param retryAfter Seconds until the rate limit resets, if provided by the server.
|
|
143
|
+
*/
|
|
61
144
|
constructor(message: string, url?: string, method?: string, retryAfter?: number);
|
|
62
145
|
}
|
|
63
146
|
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NitroAdapter } from './nitro-adapter';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* Pre-configured with default settings
|
|
3
|
+
* Pre-configured default adapter instance.
|
|
5
4
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* import { apiRequest } from '@navegarti/rn-design-system/api';
|
|
5
|
+
* The `baseURL` placeholder is intentionally generic — consuming applications
|
|
6
|
+
* should create their own instance (or override it) with their actual API URL:
|
|
9
7
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { NitroAdapter } from '@navegarti/rn-design-system/api';
|
|
10
|
+
*
|
|
11
|
+
* export const api = new NitroAdapter({
|
|
12
|
+
* baseURL: 'https://api.myapp.com',
|
|
13
|
+
* timeout: 10_000,
|
|
14
|
+
* });
|
|
14
15
|
* ```
|
|
15
16
|
*/
|
|
16
|
-
export declare const apiRequest:
|
|
17
|
-
export { AxiosAdapter } from './axios-adapter';
|
|
17
|
+
export declare const apiRequest: NitroAdapter;
|
|
18
18
|
export * from './errors';
|
|
19
|
+
export { NitroAdapter } from './nitro-adapter';
|
|
19
20
|
export { useAuthStore } from './stores/auth-store';
|
|
20
21
|
export * from './types';
|
|
21
22
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { ApiConfig, IHttpAdapter, RequestConfig } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* HTTP adapter powered by react-native-nitro-fetch.
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Automatic `Authorization: Bearer` token injection from the Zustand auth store.
|
|
7
|
+
* - Exponential back-off retry for transient failures (408, 429, 5xx).
|
|
8
|
+
* - AbortController-based request timeout with configurable duration.
|
|
9
|
+
* - Automatic 401 logout — clears the auth token so store subscribers can redirect.
|
|
10
|
+
* - Typed error hierarchy for precise error handling at call sites.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const api = new NitroAdapter({ baseURL: 'https://api.example.com' });
|
|
15
|
+
* api.addToken(accessToken);
|
|
16
|
+
*
|
|
17
|
+
* const user = await api.get<User>('/users/me');
|
|
18
|
+
* const post = await api.post<Post>('/posts', { title: 'Hello world' });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class NitroAdapter implements IHttpAdapter {
|
|
22
|
+
private readonly baseURL;
|
|
23
|
+
private readonly timeout;
|
|
24
|
+
private readonly defaultHeaders;
|
|
25
|
+
private readonly retryConfig;
|
|
26
|
+
/**
|
|
27
|
+
* @param config Adapter configuration: base URL, timeout, retry attempts,
|
|
28
|
+
* and optional default headers merged into every request.
|
|
29
|
+
*/
|
|
30
|
+
constructor(config: ApiConfig);
|
|
31
|
+
/**
|
|
32
|
+
* Resolves a path or a full URL.
|
|
33
|
+
* Full URLs (starting with `http`) are used as-is; relative paths are
|
|
34
|
+
* prefixed with `baseURL`.
|
|
35
|
+
*/
|
|
36
|
+
private resolveUrl;
|
|
37
|
+
/**
|
|
38
|
+
* Builds the merged headers object for a single request.
|
|
39
|
+
* Merge priority (lowest → highest): defaults → auth token → per-request overrides.
|
|
40
|
+
* The token is read fresh from the auth store on every call.
|
|
41
|
+
*/
|
|
42
|
+
private buildHeaders;
|
|
43
|
+
/**
|
|
44
|
+
* Wraps the nitro-fetch `fetch` call with an AbortController-based timeout.
|
|
45
|
+
*
|
|
46
|
+
* - Throws {@link TimeoutError} when **our** timeout fires.
|
|
47
|
+
* - Propagates a user-provided `AbortSignal` so both explicit cancellation
|
|
48
|
+
* and timeout work simultaneously.
|
|
49
|
+
* - Re-throws any other error untouched.
|
|
50
|
+
*/
|
|
51
|
+
private performFetch;
|
|
52
|
+
/**
|
|
53
|
+
* Reads a failed response body and throws the appropriate typed error.
|
|
54
|
+
*
|
|
55
|
+
* On **401 Unauthorized**, the auth token is cleared before throwing
|
|
56
|
+
* {@link AuthError} so that any Zustand subscriber watching `token` can
|
|
57
|
+
* redirect the user to the login screen automatically.
|
|
58
|
+
*
|
|
59
|
+
* @returns This method always throws — it never resolves.
|
|
60
|
+
*/
|
|
61
|
+
private parseErrorResponse;
|
|
62
|
+
/**
|
|
63
|
+
* Core request runner: resolves the URL, builds headers, executes the
|
|
64
|
+
* request with retry logic, and parses the response into `T`.
|
|
65
|
+
*
|
|
66
|
+
* A user-initiated `AbortError` (signal not caused by our timeout) is
|
|
67
|
+
* surfaced as a {@link NetworkError} with the message "Request was cancelled".
|
|
68
|
+
*/
|
|
69
|
+
private executeRequest;
|
|
70
|
+
/**
|
|
71
|
+
* Performs a GET request.
|
|
72
|
+
*
|
|
73
|
+
* @template T Expected response body type.
|
|
74
|
+
* @param url Request path or full URL.
|
|
75
|
+
* @param config Optional per-request overrides (headers, signal, cache, credentials).
|
|
76
|
+
*/
|
|
77
|
+
get<T = unknown>(url: string, config?: RequestConfig): Promise<T>;
|
|
78
|
+
/**
|
|
79
|
+
* Performs a POST request.
|
|
80
|
+
*
|
|
81
|
+
* @template T Expected response body type.
|
|
82
|
+
* @param url Request path or full URL.
|
|
83
|
+
* @param data Request body — will be JSON-serialised if provided.
|
|
84
|
+
* @param config Optional per-request overrides.
|
|
85
|
+
*/
|
|
86
|
+
post<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Performs a PUT request.
|
|
89
|
+
*
|
|
90
|
+
* @template T Expected response body type.
|
|
91
|
+
* @param url Request path or full URL.
|
|
92
|
+
* @param data Request body — will be JSON-serialised if provided.
|
|
93
|
+
* @param config Optional per-request overrides.
|
|
94
|
+
*/
|
|
95
|
+
put<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
96
|
+
/**
|
|
97
|
+
* Performs a DELETE request.
|
|
98
|
+
*
|
|
99
|
+
* @template T Expected response body type.
|
|
100
|
+
* @param url Request path or full URL.
|
|
101
|
+
* @param config Optional per-request overrides.
|
|
102
|
+
*/
|
|
103
|
+
delete<T = unknown>(url: string, config?: RequestConfig): Promise<T>;
|
|
104
|
+
/**
|
|
105
|
+
* Stores an auth token and attaches it as `Authorization: Bearer <token>`
|
|
106
|
+
* on all subsequent requests.
|
|
107
|
+
*
|
|
108
|
+
* @param token Bearer token value.
|
|
109
|
+
*/
|
|
110
|
+
addToken(token: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* Clears the stored auth token, preventing it from being sent on
|
|
113
|
+
* subsequent requests.
|
|
114
|
+
*/
|
|
115
|
+
removeToken(): void;
|
|
116
|
+
/**
|
|
117
|
+
* Returns the currently stored auth token, or `null` if none is set.
|
|
118
|
+
*/
|
|
119
|
+
getToken(): string | null;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=nitro-adapter.d.ts.map
|
|
@@ -1,21 +1,40 @@
|
|
|
1
|
-
import type { AxiosError } from 'axios';
|
|
2
1
|
import type { RetryConfig } from './types';
|
|
3
2
|
/**
|
|
4
|
-
* Calculates exponential
|
|
5
|
-
* Formula: min(baseDelay
|
|
3
|
+
* Calculates exponential back-off delay.
|
|
4
|
+
* Formula: `min(baseDelay × 2^attempt, maxDelay)`
|
|
5
|
+
*
|
|
6
|
+
* @param attempt Zero-based attempt index.
|
|
7
|
+
* @param baseDelay Starting delay in milliseconds.
|
|
8
|
+
* @param maxDelay Upper ceiling for the delay in milliseconds.
|
|
6
9
|
*/
|
|
7
10
|
export declare function calculateBackoffDelay(attempt: number, baseDelay: number, maxDelay: number): number;
|
|
8
11
|
/**
|
|
9
|
-
* Determines
|
|
12
|
+
* Determines whether an error should trigger another retry attempt.
|
|
13
|
+
*
|
|
14
|
+
* Uses duck-typing on the `statusCode` property so it works with any
|
|
15
|
+
* error class that carries an HTTP status code — no dependency on a
|
|
16
|
+
* specific HTTP library's error type.
|
|
17
|
+
*
|
|
18
|
+
* @param error The thrown error.
|
|
19
|
+
* @param retryConfig Retry configuration.
|
|
10
20
|
*/
|
|
11
|
-
export declare function isRetryableError(error:
|
|
21
|
+
export declare function isRetryableError(error: Error, retryConfig: RetryConfig): boolean;
|
|
12
22
|
/**
|
|
13
|
-
*
|
|
23
|
+
* Pauses execution for the specified duration.
|
|
24
|
+
*
|
|
25
|
+
* @param ms Duration in milliseconds.
|
|
14
26
|
*/
|
|
15
27
|
export declare function sleep(ms: number): Promise<void>;
|
|
16
28
|
/**
|
|
17
|
-
* Executes
|
|
18
|
-
*
|
|
29
|
+
* Executes `requestFn` with exponential back-off retry logic.
|
|
30
|
+
* Retries only when `isRetryableError` returns `true` and there are
|
|
31
|
+
* remaining attempts.
|
|
32
|
+
*
|
|
33
|
+
* @template T The expected resolved type of `requestFn`.
|
|
34
|
+
* @param requestFn Async function to call on each attempt.
|
|
35
|
+
* @param retryConfig Retry strategy configuration.
|
|
36
|
+
* @param onRetry Optional callback invoked before each retry,
|
|
37
|
+
* receiving the attempt number (1-based), delay in ms, and the error.
|
|
19
38
|
*/
|
|
20
|
-
export declare function executeWithRetry<T>(requestFn: () => Promise<T>, retryConfig: RetryConfig, onRetry?: (attempt: number, delay: number, error:
|
|
39
|
+
export declare function executeWithRetry<T>(requestFn: () => Promise<T>, retryConfig: RetryConfig, onRetry?: (attempt: number, delay: number, error: Error) => void): Promise<T>;
|
|
21
40
|
//# sourceMappingURL=retry-strategy.d.ts.map
|
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Shape of the authentication store state.
|
|
3
3
|
*/
|
|
4
4
|
interface AuthState {
|
|
5
|
+
/** The current Bearer token, or `null` when the user is not authenticated. */
|
|
5
6
|
token: string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Stores a new auth token.
|
|
9
|
+
* @param token Bearer token received after a successful login or refresh.
|
|
10
|
+
*/
|
|
6
11
|
setToken: (token: string) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Clears the stored token, effectively logging the user out.
|
|
14
|
+
* Called automatically by {@link NitroAdapter} when a 401 response is received.
|
|
15
|
+
*/
|
|
7
16
|
clearToken: () => void;
|
|
8
17
|
}
|
|
9
18
|
/**
|
|
10
|
-
* Zustand store for managing authentication token
|
|
11
|
-
*
|
|
19
|
+
* Zustand store for managing the authentication token.
|
|
20
|
+
*
|
|
21
|
+
* The token is kept in memory and automatically injected as an
|
|
22
|
+
* `Authorization: Bearer` header by {@link NitroAdapter} on every request.
|
|
23
|
+
*
|
|
24
|
+
* Subscribe to `token` changes to trigger navigation to the login screen
|
|
25
|
+
* when the user is logged out:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* useAuthStore.subscribe(
|
|
29
|
+
* (state) => state.token,
|
|
30
|
+
* (token) => {
|
|
31
|
+
* if (!token) navigation.replace('Login');
|
|
32
|
+
* },
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
12
35
|
*/
|
|
13
36
|
export declare const useAuthStore: import("zustand").UseBoundStore<import("zustand").StoreApi<AuthState>>;
|
|
14
37
|
export {};
|
|
@@ -1,58 +1,92 @@
|
|
|
1
|
-
import type { AxiosRequestConfig } from 'axios';
|
|
2
1
|
/**
|
|
3
|
-
* Configuration options for API adapter
|
|
2
|
+
* Configuration options for the API adapter.
|
|
4
3
|
*/
|
|
5
4
|
export interface ApiConfig {
|
|
5
|
+
/** Base URL prepended to all relative request paths. */
|
|
6
6
|
baseURL: string;
|
|
7
|
+
/** Request timeout in milliseconds. Defaults to 30 000 (30 s). */
|
|
7
8
|
timeout?: number;
|
|
9
|
+
/** Maximum number of retry attempts on transient failures. Defaults to 3. */
|
|
8
10
|
retryAttempts?: number;
|
|
11
|
+
/** Default headers merged into every request. */
|
|
9
12
|
headers?: Record<string, string>;
|
|
10
13
|
}
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
13
|
-
|
|
15
|
+
* Per-request configuration options accepted by each HTTP method.
|
|
16
|
+
*/
|
|
17
|
+
export interface RequestConfig {
|
|
18
|
+
/** Extra headers merged on top of the adapter's default headers. */
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
/** AbortSignal for explicit request cancellation via AbortController. */
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
/**
|
|
23
|
+
* Cache directive forwarded to the underlying fetch call.
|
|
24
|
+
* Supported by react-native-nitro-fetch via Cronet / URLSession.
|
|
25
|
+
*/
|
|
26
|
+
cache?: 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload';
|
|
27
|
+
/** Credentials mode for cross-origin requests. */
|
|
28
|
+
credentials?: 'include' | 'omit' | 'same-origin';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Contract every HTTP adapter implementation must satisfy.
|
|
14
32
|
*/
|
|
15
33
|
export interface IHttpAdapter {
|
|
16
34
|
/**
|
|
17
|
-
* Performs a GET request
|
|
18
|
-
* @template T
|
|
35
|
+
* Performs a GET request.
|
|
36
|
+
* @template T Expected response body type.
|
|
37
|
+
* @param url Request path or full URL.
|
|
38
|
+
* @param config Optional per-request overrides.
|
|
19
39
|
*/
|
|
20
|
-
get<T = unknown>(url: string, config?:
|
|
40
|
+
get<T = unknown>(url: string, config?: RequestConfig): Promise<T>;
|
|
21
41
|
/**
|
|
22
|
-
* Performs a POST request
|
|
23
|
-
* @template T
|
|
42
|
+
* Performs a POST request.
|
|
43
|
+
* @template T Expected response body type.
|
|
44
|
+
* @param url Request path or full URL.
|
|
45
|
+
* @param data Request body — will be JSON-serialised.
|
|
46
|
+
* @param config Optional per-request overrides.
|
|
24
47
|
*/
|
|
25
|
-
post<T = unknown>(url: string, data?: unknown, config?:
|
|
48
|
+
post<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
26
49
|
/**
|
|
27
|
-
* Performs a PUT request
|
|
28
|
-
* @template T
|
|
50
|
+
* Performs a PUT request.
|
|
51
|
+
* @template T Expected response body type.
|
|
52
|
+
* @param url Request path or full URL.
|
|
53
|
+
* @param data Request body — will be JSON-serialised.
|
|
54
|
+
* @param config Optional per-request overrides.
|
|
29
55
|
*/
|
|
30
|
-
put<T = unknown>(url: string, data?: unknown, config?:
|
|
56
|
+
put<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
31
57
|
/**
|
|
32
|
-
* Performs a DELETE request
|
|
33
|
-
* @template T
|
|
58
|
+
* Performs a DELETE request.
|
|
59
|
+
* @template T Expected response body type.
|
|
60
|
+
* @param url Request path or full URL.
|
|
61
|
+
* @param config Optional per-request overrides.
|
|
34
62
|
*/
|
|
35
|
-
delete<T = unknown>(url: string, config?:
|
|
63
|
+
delete<T = unknown>(url: string, config?: RequestConfig): Promise<T>;
|
|
36
64
|
/**
|
|
37
|
-
*
|
|
65
|
+
* Stores an auth token that will be injected as `Authorization: Bearer <token>`
|
|
66
|
+
* on every subsequent request.
|
|
67
|
+
* @param token Bearer token value.
|
|
38
68
|
*/
|
|
39
69
|
addToken(token: string): void;
|
|
40
70
|
/**
|
|
41
|
-
*
|
|
71
|
+
* Clears the stored auth token, preventing it from being sent on subsequent requests.
|
|
42
72
|
*/
|
|
43
73
|
removeToken(): void;
|
|
44
74
|
/**
|
|
45
|
-
*
|
|
75
|
+
* Returns the currently stored auth token, or `null` if none is set.
|
|
46
76
|
*/
|
|
47
77
|
getToken(): string | null;
|
|
48
78
|
}
|
|
49
79
|
/**
|
|
50
|
-
*
|
|
80
|
+
* Configuration for the exponential back-off retry strategy.
|
|
51
81
|
*/
|
|
52
82
|
export interface RetryConfig {
|
|
83
|
+
/** Maximum number of attempts including the first try. */
|
|
53
84
|
maxAttempts: number;
|
|
85
|
+
/** Base delay in milliseconds between retries. */
|
|
54
86
|
baseDelay: number;
|
|
87
|
+
/** Upper bound on the calculated back-off delay, in milliseconds. */
|
|
55
88
|
maxDelay: number;
|
|
89
|
+
/** HTTP status codes that trigger a retry. */
|
|
56
90
|
retryableStatusCodes: number[];
|
|
57
91
|
}
|
|
58
92
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
* Exports HTTP client, types, errors, and stores.
|
|
4
4
|
*/
|
|
5
5
|
export { ApiError, AuthError, NetworkError, NotFoundError, RateLimitError, ServerError, TimeoutError, ValidationError, } from './api/errors';
|
|
6
|
-
export {
|
|
7
|
-
export type { ApiConfig, IHttpAdapter } from './api/types';
|
|
6
|
+
export { apiRequest, NitroAdapter, useAuthStore } from './api/index';
|
|
7
|
+
export type { ApiConfig, IHttpAdapter, RequestConfig } from './api/types';
|
|
8
8
|
//# sourceMappingURL=api.d.ts.map
|