@rendomnet/apiservice 1.3.8 → 1.4.0

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.
@@ -1,25 +1,3 @@
1
- interface OAuthToken {
2
- access_token: string;
3
- expires_in: number;
4
- id_token: string;
5
- refresh_token: string;
6
- scope: string;
7
- token_type: string;
8
- }
9
- interface Token {
10
- accountId: string;
11
- access_token: string;
12
- refresh_token: string;
13
- provider: string;
14
- enabled?: boolean;
15
- updatedAt?: string;
16
- primary?: boolean;
17
- }
18
- interface AccountData {
19
- lastRequestTime?: number;
20
- lastFailed?: boolean;
21
- token?: Token;
22
- }
23
1
  interface DelayStrategy {
24
2
  calculate: (attempt: number, response?: any) => number;
25
3
  }
@@ -91,18 +69,72 @@ interface AuthProvider {
91
69
  */
92
70
  refresh?(refreshToken: string, accountId?: string): Promise<any>;
93
71
  }
94
- interface ApiKeyAuthProviderOptions {
95
- apiKey: string;
96
- headerName?: string;
97
- queryParamName?: string;
98
- }
99
- interface BasicAuthProviderOptions {
100
- username: string;
101
- password: string;
72
+
73
+ /**
74
+ * ApiService - Core API service for making authenticated API calls
75
+ * with caching, retry, and hook support.
76
+ */
77
+ declare class ApiService {
78
+ provider: string;
79
+ private authProvider;
80
+ private baseUrl;
81
+ private cacheManager;
82
+ private retryManager;
83
+ private hookManager;
84
+ private httpClient;
85
+ private accountManager;
86
+ private maxAttempts;
87
+ constructor();
88
+ /**
89
+ * Setup the API service
90
+ */
91
+ setup({ provider, authProvider, hooks, cacheTime, baseUrl, }: {
92
+ provider: string;
93
+ authProvider: AuthProvider;
94
+ hooks?: Record<StatusCode, HookSettings | null>;
95
+ cacheTime: number;
96
+ baseUrl?: string;
97
+ }): void;
98
+ /**
99
+ * Create a default handler for 401 (Unauthorized) errors
100
+ * that implements standard credential refresh behavior
101
+ */
102
+ private createDefaultAuthRefreshHandler;
103
+ /**
104
+ * Set the maximum number of retry attempts
105
+ */
106
+ setMaxAttempts(attempts: number): void;
107
+ /**
108
+ * Update account data
109
+ */
110
+ updateAccountData(accountId: string, data: Partial<Record<string, any>>): void;
111
+ /**
112
+ * Make an API call with all features (caching, retry, hooks)
113
+ */
114
+ call(apiCallParams: Omit<ApiCallParams, 'accountId'> & {
115
+ accountId?: string;
116
+ abortSignal?: AbortSignal;
117
+ }): Promise<any>;
118
+ /**
119
+ * Legacy method for backward compatibility
120
+ * @deprecated Use call() instead
121
+ */
122
+ makeApiCall(apiCallParams: Omit<ApiCallParams, 'accountId'> & {
123
+ accountId?: string;
124
+ abortSignal?: AbortSignal;
125
+ }): Promise<any>;
126
+ /**
127
+ * Make a request with retry capability
128
+ */
129
+ private makeRequestWithRetry;
130
+ /**
131
+ * Set the cache time in milliseconds
132
+ */
133
+ setCacheTime(milliseconds: number): void;
134
+ /**
135
+ * Clear the cache
136
+ */
137
+ clearCache(): void;
102
138
  }
103
- type TokenService = {
104
- get: (accountId?: string) => Promise<Token>;
105
- set: (token: Partial<Token>, accountId?: string) => Promise<void>;
106
- refresh?: (refreshToken: string, accountId?: string) => Promise<OAuthToken>;
107
- };
108
- export type { OAuthToken, DelayStrategy, Token, AccountData, ApiCallParams, HookSettings, StatusCode, AuthProvider, ApiKeyAuthProviderOptions, BasicAuthProviderOptions, TokenService, };
139
+
140
+ export { ApiService as default };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,75 @@
1
- import { AuthProvider, ApiCallParams, HookSettings, StatusCode } from './types';
1
+ interface DelayStrategy {
2
+ calculate: (attempt: number, response?: any) => number;
3
+ }
4
+ interface ApiCallParams {
5
+ accountId: string;
6
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
7
+ route: string;
8
+ base?: string;
9
+ body?: object;
10
+ data?: object;
11
+ headers?: Record<string, string>;
12
+ queryParams?: URLSearchParams;
13
+ accessToken?: string;
14
+ useAuth?: boolean;
15
+ noContentType?: boolean;
16
+ contentType?: string;
17
+ cacheTime?: number;
18
+ files?: File[];
19
+ abortSignal?: AbortSignal;
20
+ }
21
+ interface HookSettings {
22
+ /**
23
+ * Whether to retry the API call when this hook is triggered
24
+ */
25
+ shouldRetry: boolean;
26
+ /**
27
+ * Whether to apply delay between retries
28
+ */
29
+ useRetryDelay: boolean;
30
+ /**
31
+ * The maximum number of retry attempts for this status code
32
+ */
33
+ maxRetries?: number;
34
+ /**
35
+ * Wait for an existing hook to complete before starting a new one
36
+ * Useful for avoiding duplicate refresh token calls
37
+ */
38
+ preventConcurrentCalls?: boolean;
39
+ /**
40
+ * The main handler function called when this status code is encountered
41
+ * Return an object to update the API call parameters for the retry
42
+ */
43
+ handler: (accountId: string, response: any) => Promise<any>;
44
+ /**
45
+ * Called when all retry attempts for this status code have failed
46
+ */
47
+ onMaxRetriesExceeded?: (accountId: string, error: any) => Promise<void>;
48
+ /**
49
+ * Called when the handler function throws an error
50
+ */
51
+ onHandlerError?: (accountId: string, error: any) => Promise<void>;
52
+ /**
53
+ * Custom strategy for calculating delay between retries
54
+ */
55
+ delayStrategy?: DelayStrategy;
56
+ /**
57
+ * Maximum delay in milliseconds between retries
58
+ */
59
+ maxDelay?: number;
60
+ }
61
+ type StatusCode = string | number;
62
+ interface AuthProvider {
63
+ /**
64
+ * Returns headers or other auth data for a request
65
+ */
66
+ getAuthHeaders(accountId?: string): Promise<Record<string, string>>;
67
+ /**
68
+ * Optional: refresh credentials if supported (for OAuth, etc.)
69
+ */
70
+ refresh?(refreshToken: string, accountId?: string): Promise<any>;
71
+ }
72
+
2
73
  /**
3
74
  * ApiService - Core API service for making authenticated API calls
4
75
  * with caching, retry, and hook support.
@@ -65,4 +136,5 @@ declare class ApiService {
65
136
  */
66
137
  clearCache(): void;
67
138
  }
68
- export default ApiService;
139
+
140
+ export { ApiService as default };