@kivacore/api-client 0.0.0 → 0.3.8
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/README.md +792 -1
- package/REVIEW.md +292 -0
- package/dist/index.d.ts +597 -0
- package/dist/index.js +1486 -0
- package/dist/index.js.map +1 -0
- package/dist/react-native/index.d.ts +75 -0
- package/dist/react-native/index.js +1534 -0
- package/dist/react-native/index.js.map +1 -0
- package/docyrus-api-query-guide.md +2057 -0
- package/package.json +40 -3
- package/src/clients/base-client.ts +570 -0
- package/src/clients/cf-client.ts +82 -0
- package/src/clients/edge-client.ts +34 -0
- package/src/clients/rest-client.ts +58 -0
- package/src/core/crypto.ts +63 -0
- package/src/core/errors.ts +183 -0
- package/src/core/token-manager.ts +59 -0
- package/src/core/types.ts +96 -0
- package/src/core/utils.ts +138 -0
- package/src/index.ts +112 -0
- package/src/oauth2/index.ts +37 -0
- package/src/oauth2/oauth2-client.ts +668 -0
- package/src/oauth2/oauth2-token-manager.ts +128 -0
- package/src/oauth2/oauth2-types.ts +234 -0
- package/src/oauth2/pkce.ts +121 -0
- package/src/react-native/index.ts +112 -0
- package/src/react-native/rn-crypto.ts +115 -0
- package/src/react-native/rn-helpers.ts +89 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +13 -0
- package/vitest.config.mts +13 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
2
|
+
type ApiBackend = 'api' | 'edge' | 'cf';
|
|
3
|
+
type QueryParamPrimitive = string | number | boolean | null | undefined;
|
|
4
|
+
type QueryParamValue = QueryParamPrimitive | QueryParamPrimitive[];
|
|
5
|
+
interface RequestConfig extends Omit<RequestInit, 'method' | 'body'> {
|
|
6
|
+
params?: Record<string, QueryParamValue>;
|
|
7
|
+
responseType?: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'formData';
|
|
8
|
+
timeout?: number;
|
|
9
|
+
retries?: number;
|
|
10
|
+
retryDelay?: number;
|
|
11
|
+
retryCondition?: (error: Error, attempt: number) => boolean;
|
|
12
|
+
}
|
|
13
|
+
interface ApiResponse<T = unknown> {
|
|
14
|
+
data: T;
|
|
15
|
+
headers: Headers;
|
|
16
|
+
status: number;
|
|
17
|
+
statusText: string;
|
|
18
|
+
}
|
|
19
|
+
interface ApiClientConfig {
|
|
20
|
+
baseURL?: string;
|
|
21
|
+
backend?: ApiBackend;
|
|
22
|
+
timeout?: number;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
tokenManager?: TokenManager;
|
|
25
|
+
storage?: Storage;
|
|
26
|
+
fetch?: typeof fetch;
|
|
27
|
+
FormData?: typeof FormData;
|
|
28
|
+
AbortController?: typeof AbortController;
|
|
29
|
+
/** Custom download handler for non-browser environments (e.g., React Native) */
|
|
30
|
+
downloadHandler?: (blob: Blob, filename: string) => Promise<void> | void;
|
|
31
|
+
}
|
|
32
|
+
interface TokenManager {
|
|
33
|
+
getToken(): Promise<string | null> | string | null;
|
|
34
|
+
setToken(token: string): Promise<void> | void;
|
|
35
|
+
clearToken(): Promise<void> | void;
|
|
36
|
+
refreshToken?(): Promise<string>;
|
|
37
|
+
}
|
|
38
|
+
interface Interceptor {
|
|
39
|
+
request?: (config: RequestConfig) => RequestConfig | Promise<RequestConfig>;
|
|
40
|
+
response?: <T>(response: ApiResponse<T>) => ApiResponse<T> | Promise<ApiResponse<T>>;
|
|
41
|
+
error?: (error: Error) => Error | Promise<Error> | never;
|
|
42
|
+
}
|
|
43
|
+
interface SSEOptions {
|
|
44
|
+
onMessage: (data: unknown) => void;
|
|
45
|
+
onError?: (error: Error) => void;
|
|
46
|
+
onComplete?: () => void;
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
headers?: Record<string, string>;
|
|
49
|
+
}
|
|
50
|
+
interface StreamOptions extends SSEOptions {
|
|
51
|
+
method?: 'GET' | 'POST';
|
|
52
|
+
body?: unknown;
|
|
53
|
+
}
|
|
54
|
+
interface Html2PdfMargin {
|
|
55
|
+
top?: number;
|
|
56
|
+
right?: number;
|
|
57
|
+
bottom?: number;
|
|
58
|
+
left?: number;
|
|
59
|
+
}
|
|
60
|
+
interface Html2PdfOptions {
|
|
61
|
+
body?: string;
|
|
62
|
+
headerTemplate?: string;
|
|
63
|
+
footerTemplate?: string;
|
|
64
|
+
format?: string;
|
|
65
|
+
landscape?: boolean;
|
|
66
|
+
margin?: Html2PdfMargin;
|
|
67
|
+
filename?: string;
|
|
68
|
+
bucket?: string;
|
|
69
|
+
style?: string;
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
}
|
|
72
|
+
type SimpleRule = {
|
|
73
|
+
field: string;
|
|
74
|
+
operator: string;
|
|
75
|
+
value: unknown;
|
|
76
|
+
};
|
|
77
|
+
type GroupRule = {
|
|
78
|
+
rules: FilterRule[];
|
|
79
|
+
condition?: 'and' | 'or';
|
|
80
|
+
};
|
|
81
|
+
type FilterRule = SimpleRule | GroupRule | FilterQuery;
|
|
82
|
+
type FilterQuery = {
|
|
83
|
+
rules?: FilterRule[];
|
|
84
|
+
condition?: 'and' | 'or';
|
|
85
|
+
};
|
|
86
|
+
interface EnvironmentConfig {
|
|
87
|
+
apiURL?: string;
|
|
88
|
+
edgeURL?: string;
|
|
89
|
+
cfURL?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
declare class ApiError extends Error {
|
|
93
|
+
readonly status?: number;
|
|
94
|
+
readonly code?: string;
|
|
95
|
+
readonly details?: Record<string, unknown>;
|
|
96
|
+
readonly action?: string;
|
|
97
|
+
constructor(message: string, status?: number, code?: string, details?: Record<string, unknown>, action?: string);
|
|
98
|
+
toJSON(): {
|
|
99
|
+
name: string;
|
|
100
|
+
message: string;
|
|
101
|
+
status: number | undefined;
|
|
102
|
+
code: string | undefined;
|
|
103
|
+
details: Record<string, unknown> | undefined;
|
|
104
|
+
action: string | undefined;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
declare class NetworkError extends ApiError {
|
|
108
|
+
constructor(message: string, details?: Record<string, unknown>);
|
|
109
|
+
}
|
|
110
|
+
declare class TimeoutError extends ApiError {
|
|
111
|
+
constructor(message: string, timeout: number);
|
|
112
|
+
}
|
|
113
|
+
declare class AbortError extends ApiError {
|
|
114
|
+
constructor(message?: string);
|
|
115
|
+
}
|
|
116
|
+
declare class ValidationError extends ApiError {
|
|
117
|
+
constructor(message: string, details?: Record<string, unknown>);
|
|
118
|
+
}
|
|
119
|
+
declare class AuthenticationError extends ApiError {
|
|
120
|
+
constructor(message?: string);
|
|
121
|
+
}
|
|
122
|
+
declare class AuthorizationError extends ApiError {
|
|
123
|
+
constructor(message?: string);
|
|
124
|
+
}
|
|
125
|
+
declare class NotFoundError extends ApiError {
|
|
126
|
+
constructor(resource: string);
|
|
127
|
+
}
|
|
128
|
+
declare class ConflictError extends ApiError {
|
|
129
|
+
constructor(message: string, details?: Record<string, unknown>);
|
|
130
|
+
}
|
|
131
|
+
declare class RateLimitError extends ApiError {
|
|
132
|
+
constructor(retryAfter?: number);
|
|
133
|
+
}
|
|
134
|
+
declare class ServerError extends ApiError {
|
|
135
|
+
constructor(message?: string, status?: number, details?: Record<string, unknown>);
|
|
136
|
+
}
|
|
137
|
+
declare class GenericError extends ApiError {
|
|
138
|
+
readonly payload: Record<string, unknown>;
|
|
139
|
+
constructor(error: Error, action?: string, payload?: Record<string, unknown>);
|
|
140
|
+
}
|
|
141
|
+
declare class OAuth2Error extends ApiError {
|
|
142
|
+
readonly errorCode: string;
|
|
143
|
+
readonly errorDescription?: string;
|
|
144
|
+
readonly errorUri?: string;
|
|
145
|
+
constructor(errorCode: string, errorDescription?: string, errorUri?: string);
|
|
146
|
+
}
|
|
147
|
+
declare class InvalidGrantError extends OAuth2Error {
|
|
148
|
+
constructor(description?: string);
|
|
149
|
+
}
|
|
150
|
+
declare class InvalidClientError extends OAuth2Error {
|
|
151
|
+
constructor(description?: string);
|
|
152
|
+
}
|
|
153
|
+
declare class InvalidScopeError extends OAuth2Error {
|
|
154
|
+
constructor(description?: string);
|
|
155
|
+
}
|
|
156
|
+
declare class AuthorizationPendingError extends OAuth2Error {
|
|
157
|
+
constructor();
|
|
158
|
+
}
|
|
159
|
+
declare class SlowDownError extends OAuth2Error {
|
|
160
|
+
constructor();
|
|
161
|
+
}
|
|
162
|
+
declare class ExpiredTokenError extends OAuth2Error {
|
|
163
|
+
constructor();
|
|
164
|
+
}
|
|
165
|
+
declare class AccessDeniedError extends OAuth2Error {
|
|
166
|
+
constructor(description?: string);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
declare abstract class BaseApiClient {
|
|
170
|
+
protected baseURL: string;
|
|
171
|
+
protected tokenManager: TokenManager;
|
|
172
|
+
protected interceptors: Interceptor[];
|
|
173
|
+
protected defaultHeaders: Record<string, string>;
|
|
174
|
+
protected fetchImpl: typeof fetch;
|
|
175
|
+
protected FormDataImpl: typeof FormData;
|
|
176
|
+
protected AbortControllerImpl: typeof AbortController;
|
|
177
|
+
protected storage?: Storage;
|
|
178
|
+
protected defaultTimeout?: number;
|
|
179
|
+
protected downloadHandler?: (blob: Blob, filename: string) => Promise<void> | void;
|
|
180
|
+
constructor(config?: ApiClientConfig);
|
|
181
|
+
addInterceptor(interceptor: Interceptor): void;
|
|
182
|
+
removeInterceptor(interceptor: Interceptor): void;
|
|
183
|
+
setAccessToken(token: string): Promise<void>;
|
|
184
|
+
getAccessToken(): Promise<string | null>;
|
|
185
|
+
clearAccessToken(): Promise<void>;
|
|
186
|
+
protected applyRequestInterceptors(config: RequestConfig): Promise<RequestConfig>;
|
|
187
|
+
protected applyResponseInterceptors<T>(response: ApiResponse<T>): Promise<ApiResponse<T>>;
|
|
188
|
+
protected applyErrorInterceptors(error: Error): Promise<never>;
|
|
189
|
+
protected prepareHeaders(config: RequestConfig): Promise<Headers>;
|
|
190
|
+
protected prepareBody(data: unknown, headers: Headers): BodyInit | undefined;
|
|
191
|
+
protected createApiError(response: Response, errorData?: any): ApiError;
|
|
192
|
+
protected request<T = unknown>(method: HttpMethod, endpoint: string, data?: unknown, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
193
|
+
get<T = unknown>(endpoint: string, params?: Record<string, QueryParamValue>, config?: RequestConfig): Promise<T>;
|
|
194
|
+
post<T = unknown>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
195
|
+
put<T = unknown>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
196
|
+
runCustomQuery<T = {
|
|
197
|
+
[key: string]: any;
|
|
198
|
+
}[]>(customQueryId: string, options?: Record<string, any>, config?: RequestConfig): Promise<T>;
|
|
199
|
+
patch<T = unknown>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
200
|
+
delete<T = unknown>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
201
|
+
download(endpoint: string, data?: unknown, config?: RequestConfig): Promise<void>;
|
|
202
|
+
stream(endpoint: string, options: Partial<StreamOptions>): AsyncGenerator<string, void, unknown>;
|
|
203
|
+
postStream(endpoint: string, data?: unknown, onMessage?: (chunk: string) => void, onError?: (error: Error) => void, onComplete?: () => void): Promise<void>;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
declare class RestApiClient extends BaseApiClient {
|
|
207
|
+
constructor(config?: ApiClientConfig);
|
|
208
|
+
html2pdf(options?: Html2PdfOptions): Promise<any>;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
declare class EdgeFunctionClient extends BaseApiClient {
|
|
212
|
+
constructor(config?: ApiClientConfig);
|
|
213
|
+
streamFunction(functionName: string, data?: unknown, signal?: AbortSignal): AsyncGenerator<string, void, unknown>;
|
|
214
|
+
invokeFunction<T = unknown>(functionName: string, data?: unknown, config?: ApiClientConfig): Promise<T>;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
declare class CloudflareWorkerClient extends BaseApiClient {
|
|
218
|
+
constructor(config?: ApiClientConfig);
|
|
219
|
+
protected request<T = unknown>(method: any, endpoint: string, data?: unknown, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
220
|
+
invokeWorker<T = unknown>(workerName: string, route?: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
221
|
+
durableObject<T = unknown>(namespace: string, id: string, method: string, data?: unknown): Promise<T>;
|
|
222
|
+
kvGet<T = unknown>(namespace: string, key: string): Promise<T>;
|
|
223
|
+
kvPut<T = unknown>(namespace: string, key: string, value: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
224
|
+
kvDelete(namespace: string, key: string): Promise<void>;
|
|
225
|
+
kvList(namespace: string, options?: {
|
|
226
|
+
prefix?: string;
|
|
227
|
+
limit?: number;
|
|
228
|
+
cursor?: string;
|
|
229
|
+
}): Promise<{
|
|
230
|
+
keys: string[];
|
|
231
|
+
cursor?: string;
|
|
232
|
+
}>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
declare class MemoryTokenManager implements TokenManager {
|
|
236
|
+
private token;
|
|
237
|
+
getToken(): string | null;
|
|
238
|
+
setToken(token: string): void;
|
|
239
|
+
clearToken(): void;
|
|
240
|
+
}
|
|
241
|
+
declare class StorageTokenManager implements TokenManager {
|
|
242
|
+
private storage;
|
|
243
|
+
private key;
|
|
244
|
+
constructor(storage?: Storage, key?: string);
|
|
245
|
+
getToken(): string | null;
|
|
246
|
+
setToken(token: string): void;
|
|
247
|
+
clearToken(): void;
|
|
248
|
+
}
|
|
249
|
+
declare class AsyncTokenManager implements TokenManager {
|
|
250
|
+
private getTokenFn;
|
|
251
|
+
private setTokenFn;
|
|
252
|
+
private clearTokenFn;
|
|
253
|
+
private refreshTokenFn?;
|
|
254
|
+
constructor(getTokenFn: () => Promise<string | null>, setTokenFn: (token: string) => Promise<void>, clearTokenFn: () => Promise<void>, refreshTokenFn?: (() => Promise<string>) | undefined);
|
|
255
|
+
getToken(): Promise<string | null>;
|
|
256
|
+
setToken(token: string): Promise<void>;
|
|
257
|
+
clearToken(): Promise<void>;
|
|
258
|
+
refreshToken(): Promise<string>;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* CryptoProvider interface for abstracting platform-specific crypto operations.
|
|
263
|
+
* Enables PKCE and other crypto features to work across browsers, React Native, and Node.js.
|
|
264
|
+
*/
|
|
265
|
+
interface CryptoProvider {
|
|
266
|
+
/** Fill buffer with cryptographically random values */
|
|
267
|
+
getRandomValues(buffer: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
268
|
+
/** Compute SHA-256 hash of the given data */
|
|
269
|
+
sha256(data: Uint8Array<ArrayBuffer>): Promise<ArrayBuffer>;
|
|
270
|
+
/** Base64 encode a binary string */
|
|
271
|
+
btoa(input: string): string;
|
|
272
|
+
/** Base64 decode to a binary string */
|
|
273
|
+
atob(input: string): string;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Default CryptoProvider that uses browser/global Web Crypto APIs.
|
|
277
|
+
* Works in browsers and Node.js 18+ (which provides globalThis.crypto).
|
|
278
|
+
*/
|
|
279
|
+
declare class BrowserCryptoProvider implements CryptoProvider {
|
|
280
|
+
getRandomValues(buffer: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
281
|
+
sha256(data: Uint8Array<ArrayBuffer>): Promise<ArrayBuffer>;
|
|
282
|
+
btoa(input: string): string;
|
|
283
|
+
atob(input: string): string;
|
|
284
|
+
}
|
|
285
|
+
declare function getDefaultCryptoProvider(): CryptoProvider;
|
|
286
|
+
|
|
287
|
+
declare function jsonToQueryString(data: Record<string, QueryParamValue> | null | undefined): string;
|
|
288
|
+
declare function withRetry<T>(fn: () => Promise<T>, retries?: number, retryDelay?: number, retryCondition?: (error: Error, attempt: number) => boolean): Promise<T>;
|
|
289
|
+
declare function isAbortError(error: unknown): boolean;
|
|
290
|
+
declare function parseContentDisposition(header: string | null): string | null;
|
|
291
|
+
declare function buildUrl(baseURL: string, path: string, params?: Record<string, QueryParamValue>): string;
|
|
292
|
+
declare function isFormData(data: unknown): data is FormData;
|
|
293
|
+
declare function isBlob(data: unknown): data is Blob;
|
|
294
|
+
declare function isStream(data: unknown): data is ReadableStream;
|
|
295
|
+
declare function prepareFilterQueryForApi(query: FilterQuery): string;
|
|
296
|
+
declare function createAbortSignal(timeout?: number, signal?: AbortSignal): {
|
|
297
|
+
signal: AbortSignal;
|
|
298
|
+
cleanup: () => void;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* OAuth2 TypeScript type definitions for Docyrus API
|
|
303
|
+
*/
|
|
304
|
+
|
|
305
|
+
type OAuth2GrantType = 'authorization_code' | 'refresh_token' | 'client_credentials' | 'urn:ietf:params:oauth:grant-type:device_code';
|
|
306
|
+
type TokenTypeHint = 'access_token' | 'refresh_token';
|
|
307
|
+
interface TokenResponse {
|
|
308
|
+
access_token: string;
|
|
309
|
+
token_type: 'Bearer';
|
|
310
|
+
expires_in: number;
|
|
311
|
+
scope: string;
|
|
312
|
+
refresh_token?: string;
|
|
313
|
+
id_token?: string;
|
|
314
|
+
}
|
|
315
|
+
interface IntrospectTokenResponse {
|
|
316
|
+
active: boolean;
|
|
317
|
+
scope?: string;
|
|
318
|
+
client_id?: string;
|
|
319
|
+
username?: string;
|
|
320
|
+
token_type?: string;
|
|
321
|
+
exp?: number;
|
|
322
|
+
iat?: number;
|
|
323
|
+
nbf?: number;
|
|
324
|
+
sub?: string;
|
|
325
|
+
aud?: string | string[];
|
|
326
|
+
iss?: string;
|
|
327
|
+
jti?: string;
|
|
328
|
+
}
|
|
329
|
+
interface DeviceAuthorizationResponse {
|
|
330
|
+
device_code: string;
|
|
331
|
+
user_code: string;
|
|
332
|
+
verification_uri: string;
|
|
333
|
+
verification_uri_complete?: string;
|
|
334
|
+
expires_in: number;
|
|
335
|
+
interval: number;
|
|
336
|
+
}
|
|
337
|
+
interface DeviceVerifyResponse {
|
|
338
|
+
userCode: string;
|
|
339
|
+
scopes: string[];
|
|
340
|
+
clientId: string;
|
|
341
|
+
}
|
|
342
|
+
interface RateLimitInfo {
|
|
343
|
+
policy: string;
|
|
344
|
+
reset: number;
|
|
345
|
+
remaining: number;
|
|
346
|
+
limit: number;
|
|
347
|
+
}
|
|
348
|
+
interface OAuth2ClientConfig {
|
|
349
|
+
/** OAuth2 server base URL (e.g., 'https://api.docyrus.com') */
|
|
350
|
+
baseURL: string;
|
|
351
|
+
/** Client ID */
|
|
352
|
+
clientId: string;
|
|
353
|
+
/** Client Secret (optional for public clients using PKCE) */
|
|
354
|
+
clientSecret?: string;
|
|
355
|
+
/** Redirect URI for authorization code flow */
|
|
356
|
+
redirectUri?: string;
|
|
357
|
+
/** Default scopes to request */
|
|
358
|
+
defaultScopes?: string[];
|
|
359
|
+
/** Token storage implementation */
|
|
360
|
+
tokenStorage?: OAuth2TokenStorage;
|
|
361
|
+
/** Enable automatic PKCE for authorization code flow (default: true) */
|
|
362
|
+
usePKCE?: boolean;
|
|
363
|
+
/** Custom fetch implementation */
|
|
364
|
+
fetch?: typeof fetch;
|
|
365
|
+
/** Request timeout in milliseconds */
|
|
366
|
+
timeout?: number;
|
|
367
|
+
/** Custom crypto provider for PKCE operations (default: browser Web Crypto API) */
|
|
368
|
+
cryptoProvider?: CryptoProvider;
|
|
369
|
+
}
|
|
370
|
+
interface OAuth2TokenStorage {
|
|
371
|
+
getTokens(): OAuth2Tokens | Promise<OAuth2Tokens | null> | null;
|
|
372
|
+
setTokens(tokens: OAuth2Tokens): void | Promise<void>;
|
|
373
|
+
clearTokens(): void | Promise<void>;
|
|
374
|
+
getPKCEState?(): PKCEState | Promise<PKCEState | null> | null;
|
|
375
|
+
setPKCEState?(state: PKCEState): void | Promise<void>;
|
|
376
|
+
clearPKCEState?(): void | Promise<void>;
|
|
377
|
+
}
|
|
378
|
+
interface OAuth2Tokens {
|
|
379
|
+
accessToken: string;
|
|
380
|
+
refreshToken?: string;
|
|
381
|
+
idToken?: string;
|
|
382
|
+
expiresAt: number;
|
|
383
|
+
scope: string;
|
|
384
|
+
}
|
|
385
|
+
interface PKCEState {
|
|
386
|
+
codeVerifier: string;
|
|
387
|
+
state: string;
|
|
388
|
+
nonce?: string;
|
|
389
|
+
}
|
|
390
|
+
interface AuthorizationCallbackResult {
|
|
391
|
+
code: string;
|
|
392
|
+
state: string;
|
|
393
|
+
}
|
|
394
|
+
interface DevicePollingOptions {
|
|
395
|
+
onUserCode?: (response: DeviceAuthorizationResponse) => void;
|
|
396
|
+
onExpired?: () => void;
|
|
397
|
+
signal?: AbortSignal;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
declare class OAuth2Client {
|
|
401
|
+
private readonly config;
|
|
402
|
+
private readonly fetchImpl;
|
|
403
|
+
private readonly storage;
|
|
404
|
+
private readonly cryptoProvider;
|
|
405
|
+
/** Shared in-flight refresh, so concurrent callers don't each spend the refresh token. */
|
|
406
|
+
private refreshInFlight;
|
|
407
|
+
constructor(config: OAuth2ClientConfig);
|
|
408
|
+
/**
|
|
409
|
+
* Generate authorization URL for the authorization code flow
|
|
410
|
+
* Handles PKCE automatically if enabled
|
|
411
|
+
*/
|
|
412
|
+
getAuthorizationUrl(options?: {
|
|
413
|
+
scope?: string;
|
|
414
|
+
state?: string;
|
|
415
|
+
nonce?: string;
|
|
416
|
+
redirectUri?: string;
|
|
417
|
+
}): Promise<{
|
|
418
|
+
url: string;
|
|
419
|
+
state: string;
|
|
420
|
+
codeVerifier?: string;
|
|
421
|
+
}>;
|
|
422
|
+
/**
|
|
423
|
+
* Handle the authorization callback and exchange code for tokens
|
|
424
|
+
*/
|
|
425
|
+
handleCallback(callbackUrl: string | AuthorizationCallbackResult, options?: {
|
|
426
|
+
codeVerifier?: string;
|
|
427
|
+
expectedState?: string;
|
|
428
|
+
}): Promise<OAuth2Tokens>;
|
|
429
|
+
/**
|
|
430
|
+
* Exchange authorization code for tokens
|
|
431
|
+
*/
|
|
432
|
+
exchangeCode(code: string, codeVerifier?: string, redirectUri?: string): Promise<OAuth2Tokens>;
|
|
433
|
+
/**
|
|
434
|
+
* Refresh the access token using a refresh token.
|
|
435
|
+
*
|
|
436
|
+
* Concurrent calls share a single in-flight request. Without this, several
|
|
437
|
+
* requests crossing the expiry buffer at once each fire their own refresh;
|
|
438
|
+
* with refresh-token rotation only the first succeeds and the losers can
|
|
439
|
+
* leave the client holding an already-expired token (surfacing as backend
|
|
440
|
+
* "JWTExpired" errors on subsequent calls).
|
|
441
|
+
*/
|
|
442
|
+
refreshAccessToken(refreshToken?: string): Promise<OAuth2Tokens>;
|
|
443
|
+
/**
|
|
444
|
+
* Refresh tokens rotate, and the Docyrus API refuses a rotated refresh token presented again (and can end
|
|
445
|
+
* the whole sign-in for it). Tabs and windows sharing one token storage therefore refresh one at a time,
|
|
446
|
+
* under a Web Lock, and with the token as stored *now*: if another tab refreshed while this one waited, its
|
|
447
|
+
* tokens are used instead of refreshing again. Outside browsers (no Web Locks) only the per-instance
|
|
448
|
+
* single-flight above applies.
|
|
449
|
+
*/
|
|
450
|
+
private performRefresh;
|
|
451
|
+
private requestRefresh;
|
|
452
|
+
/**
|
|
453
|
+
* Get access token using client credentials
|
|
454
|
+
*/
|
|
455
|
+
getClientCredentialsToken(options: {
|
|
456
|
+
scope: string;
|
|
457
|
+
delegatedUserId: string;
|
|
458
|
+
clientAssertion?: string;
|
|
459
|
+
}): Promise<OAuth2Tokens>;
|
|
460
|
+
/**
|
|
461
|
+
* Start device authorization flow
|
|
462
|
+
*/
|
|
463
|
+
startDeviceAuthorization(scope?: string): Promise<DeviceAuthorizationResponse>;
|
|
464
|
+
/**
|
|
465
|
+
* Poll for device authorization completion
|
|
466
|
+
* Returns tokens when user completes authorization
|
|
467
|
+
*/
|
|
468
|
+
pollDeviceAuthorization(deviceCode: string, interval?: number, expiresIn?: number, options?: DevicePollingOptions): Promise<OAuth2Tokens>;
|
|
469
|
+
/**
|
|
470
|
+
* Verify a device user code
|
|
471
|
+
*/
|
|
472
|
+
verifyDeviceCode(userCode: string): Promise<DeviceVerifyResponse>;
|
|
473
|
+
/**
|
|
474
|
+
* Revoke a refresh token
|
|
475
|
+
*/
|
|
476
|
+
revokeToken(refreshToken: string, clientAssertion?: string): Promise<void>;
|
|
477
|
+
/**
|
|
478
|
+
* Introspect a token to check its validity and metadata
|
|
479
|
+
*/
|
|
480
|
+
introspectToken(token: string, tokenTypeHint?: TokenTypeHint, clientAssertion?: string): Promise<IntrospectTokenResponse>;
|
|
481
|
+
/**
|
|
482
|
+
* Check rate limit status without consuming units
|
|
483
|
+
*/
|
|
484
|
+
checkRateLimit(): Promise<RateLimitInfo>;
|
|
485
|
+
/**
|
|
486
|
+
* Get current stored tokens
|
|
487
|
+
*/
|
|
488
|
+
getTokens(): Promise<OAuth2Tokens | null>;
|
|
489
|
+
/**
|
|
490
|
+
* Check if the current access token is expired
|
|
491
|
+
*/
|
|
492
|
+
isTokenExpired(bufferSeconds?: number): Promise<boolean>;
|
|
493
|
+
/**
|
|
494
|
+
* Get a valid access token, refreshing if necessary
|
|
495
|
+
*/
|
|
496
|
+
getValidAccessToken(): Promise<string>;
|
|
497
|
+
/**
|
|
498
|
+
* Clear all stored tokens (logout)
|
|
499
|
+
*/
|
|
500
|
+
logout(): Promise<void>;
|
|
501
|
+
private requestToken;
|
|
502
|
+
private handleErrorResponse;
|
|
503
|
+
private createOAuth2Error;
|
|
504
|
+
private sleep;
|
|
505
|
+
/** Extract exp claim from a JWT access token as a fallback for expires_in. */
|
|
506
|
+
private parseJwtExp;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* In-memory OAuth2 token storage
|
|
511
|
+
*/
|
|
512
|
+
declare class MemoryOAuth2TokenStorage implements OAuth2TokenStorage {
|
|
513
|
+
private tokens;
|
|
514
|
+
private pkceState;
|
|
515
|
+
getTokens(): OAuth2Tokens | null;
|
|
516
|
+
setTokens(tokens: OAuth2Tokens): void;
|
|
517
|
+
clearTokens(): void;
|
|
518
|
+
getPKCEState(): PKCEState | null;
|
|
519
|
+
setPKCEState(state: PKCEState): void;
|
|
520
|
+
clearPKCEState(): void;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Browser storage (localStorage/sessionStorage) OAuth2 token storage
|
|
524
|
+
*/
|
|
525
|
+
declare class BrowserOAuth2TokenStorage implements OAuth2TokenStorage {
|
|
526
|
+
private readonly tokensKey;
|
|
527
|
+
private readonly pkceKey;
|
|
528
|
+
private storage;
|
|
529
|
+
constructor(storage?: Storage, keyPrefix?: string);
|
|
530
|
+
getTokens(): OAuth2Tokens | null;
|
|
531
|
+
setTokens(tokens: OAuth2Tokens): void;
|
|
532
|
+
clearTokens(): void;
|
|
533
|
+
getPKCEState(): PKCEState | null;
|
|
534
|
+
setPKCEState(state: PKCEState): void;
|
|
535
|
+
clearPKCEState(): void;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Adapter to bridge OAuth2TokenStorage to the existing TokenManager interface
|
|
539
|
+
* This allows using OAuth2 tokens with RestApiClient
|
|
540
|
+
*/
|
|
541
|
+
declare class OAuth2TokenManagerAdapter implements TokenManager {
|
|
542
|
+
private oauth2Storage;
|
|
543
|
+
private onTokenExpired?;
|
|
544
|
+
constructor(oauth2Storage: OAuth2TokenStorage, onTokenExpired?: (() => Promise<string>) | undefined);
|
|
545
|
+
getToken(): string | null | Promise<string | null>;
|
|
546
|
+
private extractToken;
|
|
547
|
+
setToken(token: string): void | Promise<void>;
|
|
548
|
+
clearToken(): void | Promise<void>;
|
|
549
|
+
refreshToken(): Promise<string>;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* PKCE (Proof Key for Code Exchange) utilities for OAuth2
|
|
554
|
+
* Implements RFC 7636
|
|
555
|
+
*
|
|
556
|
+
* All functions accept an optional CryptoProvider parameter.
|
|
557
|
+
* When omitted, the default BrowserCryptoProvider is used (browser/Node.js globals).
|
|
558
|
+
*/
|
|
559
|
+
|
|
560
|
+
interface PKCEChallenge {
|
|
561
|
+
codeVerifier: string;
|
|
562
|
+
codeChallenge: string;
|
|
563
|
+
codeChallengeMethod: 'S256';
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Generate a cryptographically random code verifier
|
|
567
|
+
* RFC 7636 requires 43-128 characters from unreserved URI characters
|
|
568
|
+
*/
|
|
569
|
+
declare function generateCodeVerifier(length?: number, cryptoProvider?: CryptoProvider): string;
|
|
570
|
+
/**
|
|
571
|
+
* Base64URL encode (RFC 4648)
|
|
572
|
+
*/
|
|
573
|
+
declare function base64UrlEncode(buffer: Uint8Array, cryptoProvider?: CryptoProvider): string;
|
|
574
|
+
/**
|
|
575
|
+
* Base64URL decode
|
|
576
|
+
*/
|
|
577
|
+
declare function base64UrlDecode(input: string, cryptoProvider?: CryptoProvider): Uint8Array;
|
|
578
|
+
/**
|
|
579
|
+
* Generate code challenge from code verifier using SHA-256
|
|
580
|
+
* Returns Base64URL encoded hash
|
|
581
|
+
*/
|
|
582
|
+
declare function generateCodeChallenge(codeVerifier: string, cryptoProvider?: CryptoProvider): Promise<string>;
|
|
583
|
+
/**
|
|
584
|
+
* Generate both code verifier and code challenge
|
|
585
|
+
*/
|
|
586
|
+
declare function generatePKCEChallenge(cryptoProvider?: CryptoProvider): Promise<PKCEChallenge>;
|
|
587
|
+
/**
|
|
588
|
+
* Generate a cryptographically random state parameter
|
|
589
|
+
*/
|
|
590
|
+
declare function generateState(length?: number, cryptoProvider?: CryptoProvider): string;
|
|
591
|
+
/**
|
|
592
|
+
* Generate a cryptographically random nonce for ID token validation
|
|
593
|
+
*/
|
|
594
|
+
declare function generateNonce(length?: number, cryptoProvider?: CryptoProvider): string;
|
|
595
|
+
|
|
596
|
+
export { AbortError, AccessDeniedError, ApiError, AsyncTokenManager, AuthenticationError, AuthorizationError, AuthorizationPendingError, BaseApiClient, BrowserCryptoProvider, BrowserOAuth2TokenStorage, CloudflareWorkerClient, ConflictError, EdgeFunctionClient, ExpiredTokenError, GenericError, InvalidClientError, InvalidGrantError, InvalidScopeError, MemoryOAuth2TokenStorage, MemoryTokenManager, NetworkError, NotFoundError, OAuth2Client, OAuth2Error, OAuth2TokenManagerAdapter, RateLimitError, RestApiClient, ServerError, SlowDownError, StorageTokenManager, TimeoutError, ValidationError, base64UrlDecode, base64UrlEncode, buildUrl, createAbortSignal, RestApiClient as default, generateCodeChallenge, generateCodeVerifier, generateNonce, generatePKCEChallenge, generateState, getDefaultCryptoProvider, isAbortError, isBlob, isFormData, isStream, jsonToQueryString, parseContentDisposition, prepareFilterQueryForApi, withRetry };
|
|
597
|
+
export type { ApiBackend, ApiClientConfig, ApiResponse, AuthorizationCallbackResult, CryptoProvider, DeviceAuthorizationResponse, DevicePollingOptions, DeviceVerifyResponse, EnvironmentConfig, FilterQuery, FilterRule, GroupRule, Html2PdfMargin, Html2PdfOptions, HttpMethod, Interceptor, IntrospectTokenResponse, OAuth2ClientConfig, OAuth2GrantType, OAuth2TokenStorage, OAuth2Tokens, PKCEState, QueryParamPrimitive, QueryParamValue, RateLimitInfo, RequestConfig, SSEOptions, SimpleRule, StreamOptions, TokenManager, TokenResponse, TokenTypeHint };
|