@oxyhq/services 6.3.0 → 6.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.
- package/lib/commonjs/index.js +0 -39
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ui/context/OxyContext.js +1 -0
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js +5 -5
- package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js.map +1 -1
- package/lib/commonjs/ui/hooks/queries/useAccountQueries.js +2 -2
- package/lib/commonjs/ui/hooks/queries/useAccountQueries.js.map +1 -1
- package/lib/commonjs/ui/hooks/queries/useServicesQueries.js +2 -2
- package/lib/commonjs/ui/hooks/queries/useServicesQueries.js.map +1 -1
- package/lib/commonjs/ui/hooks/useSessionSocket.js +22 -38
- package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/commonjs/ui/utils/avatarUtils.js +2 -2
- package/lib/commonjs/ui/utils/avatarUtils.js.map +1 -1
- package/lib/module/index.js +0 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/ui/context/OxyContext.js +1 -0
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/hooks/mutations/useAccountMutations.js +1 -1
- package/lib/module/ui/hooks/mutations/useAccountMutations.js.map +1 -1
- package/lib/module/ui/hooks/queries/useAccountQueries.js +1 -1
- package/lib/module/ui/hooks/queries/useAccountQueries.js.map +1 -1
- package/lib/module/ui/hooks/queries/useServicesQueries.js +1 -1
- package/lib/module/ui/hooks/queries/useServicesQueries.js.map +1 -1
- package/lib/module/ui/hooks/useSessionSocket.js +22 -38
- package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/module/ui/utils/avatarUtils.js +1 -1
- package/lib/module/ui/utils/avatarUtils.js.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +0 -2
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/hooks/queries/useServicesQueries.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/hooks/useSessionSocket.d.ts +2 -1
- package/lib/typescript/commonjs/ui/hooks/useSessionSocket.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/utils/avatarUtils.d.ts +1 -2
- package/lib/typescript/commonjs/ui/utils/avatarUtils.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +0 -2
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/module/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
- package/lib/typescript/module/ui/hooks/queries/useServicesQueries.d.ts.map +1 -1
- package/lib/typescript/module/ui/hooks/useSessionSocket.d.ts +2 -1
- package/lib/typescript/module/ui/hooks/useSessionSocket.d.ts.map +1 -1
- package/lib/typescript/module/ui/utils/avatarUtils.d.ts +1 -2
- package/lib/typescript/module/ui/utils/avatarUtils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +0 -7
- package/src/ui/context/OxyContext.tsx +1 -0
- package/src/ui/hooks/mutations/useAccountMutations.ts +1 -1
- package/src/ui/hooks/queries/useAccountQueries.ts +1 -1
- package/src/ui/hooks/queries/useServicesQueries.ts +1 -1
- package/src/ui/hooks/useSessionSocket.ts +24 -37
- package/src/ui/utils/avatarUtils.ts +2 -3
- package/lib/commonjs/ui/utils/authHelpers.js +0 -164
- package/lib/commonjs/ui/utils/authHelpers.js.map +0 -1
- package/lib/module/ui/utils/authHelpers.js +0 -154
- package/lib/module/ui/utils/authHelpers.js.map +0 -1
- package/lib/typescript/commonjs/ui/utils/authHelpers.d.ts +0 -99
- package/lib/typescript/commonjs/ui/utils/authHelpers.d.ts.map +0 -1
- package/lib/typescript/module/ui/utils/authHelpers.d.ts +0 -99
- package/lib/typescript/module/ui/utils/authHelpers.d.ts.map +0 -1
- package/src/ui/utils/authHelpers.ts +0 -183
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Authentication helper utilities to reduce code duplication across hooks and utilities.
|
|
3
|
-
* These functions handle common token validation and authentication error patterns.
|
|
4
|
-
*/
|
|
5
|
-
import type { OxyServices } from '@oxyhq/core';
|
|
6
|
-
/**
|
|
7
|
-
* Error thrown when session sync is required
|
|
8
|
-
*/
|
|
9
|
-
export declare class SessionSyncRequiredError extends Error {
|
|
10
|
-
constructor(message?: string);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Error thrown when authentication fails
|
|
14
|
-
*/
|
|
15
|
-
export declare class AuthenticationFailedError extends Error {
|
|
16
|
-
constructor(message?: string);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Ensures a valid token exists before making authenticated API calls.
|
|
20
|
-
* If no valid token exists and an active session ID is available,
|
|
21
|
-
* attempts to refresh the token using the session.
|
|
22
|
-
*
|
|
23
|
-
* @param oxyServices - The OxyServices instance
|
|
24
|
-
* @param activeSessionId - The active session ID (if available)
|
|
25
|
-
* @throws {SessionSyncRequiredError} If the session needs to be synced (offline session)
|
|
26
|
-
* @throws {Error} If token refresh fails for other reasons
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* // In a mutation or query function:
|
|
31
|
-
* await ensureValidToken(oxyServices, activeSessionId);
|
|
32
|
-
* return await oxyServices.updateProfile(updates);
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export declare function ensureValidToken(oxyServices: OxyServices, activeSessionId: string | null | undefined): Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Options for handling API authentication errors
|
|
38
|
-
*/
|
|
39
|
-
export interface HandleApiErrorOptions {
|
|
40
|
-
/** Optional callback to attempt session sync and retry */
|
|
41
|
-
syncSession?: () => Promise<unknown>;
|
|
42
|
-
/** The active session ID for retry attempts */
|
|
43
|
-
activeSessionId?: string | null;
|
|
44
|
-
/** The OxyServices instance for retry attempts */
|
|
45
|
-
oxyServices?: OxyServices;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Checks if an error is an authentication error (401 or auth-related message)
|
|
49
|
-
*
|
|
50
|
-
* @param error - The error to check
|
|
51
|
-
* @returns True if the error is an authentication error
|
|
52
|
-
*/
|
|
53
|
-
export declare function isAuthenticationError(error: unknown): boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Wraps an API call with authentication error handling.
|
|
56
|
-
* If an authentication error occurs, it can optionally attempt to sync the session and retry.
|
|
57
|
-
*
|
|
58
|
-
* @param apiCall - The API call function to execute
|
|
59
|
-
* @param options - Optional error handling configuration
|
|
60
|
-
* @returns The result of the API call
|
|
61
|
-
* @throws {AuthenticationFailedError} If authentication fails and cannot be recovered
|
|
62
|
-
* @throws {Error} If the API call fails for non-auth reasons
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```ts
|
|
66
|
-
* // Simple usage:
|
|
67
|
-
* const result = await withAuthErrorHandling(
|
|
68
|
-
* () => oxyServices.updateProfile(updates)
|
|
69
|
-
* );
|
|
70
|
-
*
|
|
71
|
-
* // With retry on auth failure:
|
|
72
|
-
* const result = await withAuthErrorHandling(
|
|
73
|
-
* () => oxyServices.updateProfile(updates),
|
|
74
|
-
* { syncSession, activeSessionId, oxyServices }
|
|
75
|
-
* );
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export declare function withAuthErrorHandling<T>(apiCall: () => Promise<T>, options?: HandleApiErrorOptions): Promise<T>;
|
|
79
|
-
/**
|
|
80
|
-
* Combines token validation and auth error handling for a complete authenticated API call.
|
|
81
|
-
* This is the recommended helper for most authenticated API operations.
|
|
82
|
-
*
|
|
83
|
-
* @param oxyServices - The OxyServices instance
|
|
84
|
-
* @param activeSessionId - The active session ID
|
|
85
|
-
* @param apiCall - The API call function to execute
|
|
86
|
-
* @param syncSession - Optional callback to sync session on auth failure
|
|
87
|
-
* @returns The result of the API call
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```ts
|
|
91
|
-
* return await authenticatedApiCall(
|
|
92
|
-
* oxyServices,
|
|
93
|
-
* activeSessionId,
|
|
94
|
-
* () => oxyServices.updateProfile(updates)
|
|
95
|
-
* );
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export declare function authenticatedApiCall<T>(oxyServices: OxyServices, activeSessionId: string | null | undefined, apiCall: () => Promise<T>, syncSession?: () => Promise<unknown>): Promise<T>;
|
|
99
|
-
//# sourceMappingURL=authHelpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"authHelpers.d.ts","sourceRoot":"","sources":["../../../../../src/ui/utils/authHelpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;gBACrC,OAAO,SAAkD;CAItE;AAED;;GAEG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,OAAO,SAAiD;CAIrE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,gBAAgB,CACpC,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GACzC,OAAO,CAAC,IAAI,CAAC,CAgBf;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,0DAA0D;IAC1D,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IACrC,+CAA+C;IAC/C,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,kDAAkD;IAClD,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAc7D;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,qBAAqB,CAAC,CAAC,EAC3C,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACzB,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,CAAC,CAAC,CAsBZ;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,oBAAoB,CAAC,CAAC,EAC1C,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC1C,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACzB,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,GACnC,OAAO,CAAC,CAAC,CAAC,CAQZ"}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Authentication helper utilities to reduce code duplication across hooks and utilities.
|
|
3
|
-
* These functions handle common token validation and authentication error patterns.
|
|
4
|
-
*/
|
|
5
|
-
import type { OxyServices } from '@oxyhq/core';
|
|
6
|
-
/**
|
|
7
|
-
* Error thrown when session sync is required
|
|
8
|
-
*/
|
|
9
|
-
export declare class SessionSyncRequiredError extends Error {
|
|
10
|
-
constructor(message?: string);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Error thrown when authentication fails
|
|
14
|
-
*/
|
|
15
|
-
export declare class AuthenticationFailedError extends Error {
|
|
16
|
-
constructor(message?: string);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Ensures a valid token exists before making authenticated API calls.
|
|
20
|
-
* If no valid token exists and an active session ID is available,
|
|
21
|
-
* attempts to refresh the token using the session.
|
|
22
|
-
*
|
|
23
|
-
* @param oxyServices - The OxyServices instance
|
|
24
|
-
* @param activeSessionId - The active session ID (if available)
|
|
25
|
-
* @throws {SessionSyncRequiredError} If the session needs to be synced (offline session)
|
|
26
|
-
* @throws {Error} If token refresh fails for other reasons
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* // In a mutation or query function:
|
|
31
|
-
* await ensureValidToken(oxyServices, activeSessionId);
|
|
32
|
-
* return await oxyServices.updateProfile(updates);
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export declare function ensureValidToken(oxyServices: OxyServices, activeSessionId: string | null | undefined): Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Options for handling API authentication errors
|
|
38
|
-
*/
|
|
39
|
-
export interface HandleApiErrorOptions {
|
|
40
|
-
/** Optional callback to attempt session sync and retry */
|
|
41
|
-
syncSession?: () => Promise<unknown>;
|
|
42
|
-
/** The active session ID for retry attempts */
|
|
43
|
-
activeSessionId?: string | null;
|
|
44
|
-
/** The OxyServices instance for retry attempts */
|
|
45
|
-
oxyServices?: OxyServices;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Checks if an error is an authentication error (401 or auth-related message)
|
|
49
|
-
*
|
|
50
|
-
* @param error - The error to check
|
|
51
|
-
* @returns True if the error is an authentication error
|
|
52
|
-
*/
|
|
53
|
-
export declare function isAuthenticationError(error: unknown): boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Wraps an API call with authentication error handling.
|
|
56
|
-
* If an authentication error occurs, it can optionally attempt to sync the session and retry.
|
|
57
|
-
*
|
|
58
|
-
* @param apiCall - The API call function to execute
|
|
59
|
-
* @param options - Optional error handling configuration
|
|
60
|
-
* @returns The result of the API call
|
|
61
|
-
* @throws {AuthenticationFailedError} If authentication fails and cannot be recovered
|
|
62
|
-
* @throws {Error} If the API call fails for non-auth reasons
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```ts
|
|
66
|
-
* // Simple usage:
|
|
67
|
-
* const result = await withAuthErrorHandling(
|
|
68
|
-
* () => oxyServices.updateProfile(updates)
|
|
69
|
-
* );
|
|
70
|
-
*
|
|
71
|
-
* // With retry on auth failure:
|
|
72
|
-
* const result = await withAuthErrorHandling(
|
|
73
|
-
* () => oxyServices.updateProfile(updates),
|
|
74
|
-
* { syncSession, activeSessionId, oxyServices }
|
|
75
|
-
* );
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export declare function withAuthErrorHandling<T>(apiCall: () => Promise<T>, options?: HandleApiErrorOptions): Promise<T>;
|
|
79
|
-
/**
|
|
80
|
-
* Combines token validation and auth error handling for a complete authenticated API call.
|
|
81
|
-
* This is the recommended helper for most authenticated API operations.
|
|
82
|
-
*
|
|
83
|
-
* @param oxyServices - The OxyServices instance
|
|
84
|
-
* @param activeSessionId - The active session ID
|
|
85
|
-
* @param apiCall - The API call function to execute
|
|
86
|
-
* @param syncSession - Optional callback to sync session on auth failure
|
|
87
|
-
* @returns The result of the API call
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```ts
|
|
91
|
-
* return await authenticatedApiCall(
|
|
92
|
-
* oxyServices,
|
|
93
|
-
* activeSessionId,
|
|
94
|
-
* () => oxyServices.updateProfile(updates)
|
|
95
|
-
* );
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export declare function authenticatedApiCall<T>(oxyServices: OxyServices, activeSessionId: string | null | undefined, apiCall: () => Promise<T>, syncSession?: () => Promise<unknown>): Promise<T>;
|
|
99
|
-
//# sourceMappingURL=authHelpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"authHelpers.d.ts","sourceRoot":"","sources":["../../../../../src/ui/utils/authHelpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;gBACrC,OAAO,SAAkD;CAItE;AAED;;GAEG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,OAAO,SAAiD;CAIrE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,gBAAgB,CACpC,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GACzC,OAAO,CAAC,IAAI,CAAC,CAgBf;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,0DAA0D;IAC1D,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IACrC,+CAA+C;IAC/C,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,kDAAkD;IAClD,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAc7D;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,qBAAqB,CAAC,CAAC,EAC3C,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACzB,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,CAAC,CAAC,CAsBZ;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,oBAAoB,CAAC,CAAC,EAC1C,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC1C,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACzB,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,GACnC,OAAO,CAAC,CAAC,CAAC,CAQZ"}
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Authentication helper utilities to reduce code duplication across hooks and utilities.
|
|
3
|
-
* These functions handle common token validation and authentication error patterns.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { OxyServices } from '@oxyhq/core';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Error thrown when session sync is required
|
|
10
|
-
*/
|
|
11
|
-
export class SessionSyncRequiredError extends Error {
|
|
12
|
-
constructor(message = 'Session needs to be synced. Please try again.') {
|
|
13
|
-
super(message);
|
|
14
|
-
this.name = 'SessionSyncRequiredError';
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Error thrown when authentication fails
|
|
20
|
-
*/
|
|
21
|
-
export class AuthenticationFailedError extends Error {
|
|
22
|
-
constructor(message = 'Authentication failed. Please sign in again.') {
|
|
23
|
-
super(message);
|
|
24
|
-
this.name = 'AuthenticationFailedError';
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Ensures a valid token exists before making authenticated API calls.
|
|
30
|
-
* If no valid token exists and an active session ID is available,
|
|
31
|
-
* attempts to refresh the token using the session.
|
|
32
|
-
*
|
|
33
|
-
* @param oxyServices - The OxyServices instance
|
|
34
|
-
* @param activeSessionId - The active session ID (if available)
|
|
35
|
-
* @throws {SessionSyncRequiredError} If the session needs to be synced (offline session)
|
|
36
|
-
* @throws {Error} If token refresh fails for other reasons
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* // In a mutation or query function:
|
|
41
|
-
* await ensureValidToken(oxyServices, activeSessionId);
|
|
42
|
-
* return await oxyServices.updateProfile(updates);
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export async function ensureValidToken(
|
|
46
|
-
oxyServices: OxyServices,
|
|
47
|
-
activeSessionId: string | null | undefined
|
|
48
|
-
): Promise<void> {
|
|
49
|
-
if (oxyServices.hasValidToken() || !activeSessionId) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
await oxyServices.getTokenBySession(activeSessionId);
|
|
55
|
-
} catch (tokenError) {
|
|
56
|
-
const errorMessage = tokenError instanceof Error ? tokenError.message : String(tokenError);
|
|
57
|
-
|
|
58
|
-
if (errorMessage.includes('AUTH_REQUIRED_OFFLINE_SESSION') || errorMessage.includes('offline')) {
|
|
59
|
-
throw new SessionSyncRequiredError();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
throw tokenError;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Options for handling API authentication errors
|
|
68
|
-
*/
|
|
69
|
-
export interface HandleApiErrorOptions {
|
|
70
|
-
/** Optional callback to attempt session sync and retry */
|
|
71
|
-
syncSession?: () => Promise<unknown>;
|
|
72
|
-
/** The active session ID for retry attempts */
|
|
73
|
-
activeSessionId?: string | null;
|
|
74
|
-
/** The OxyServices instance for retry attempts */
|
|
75
|
-
oxyServices?: OxyServices;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Checks if an error is an authentication error (401 or auth-related message)
|
|
80
|
-
*
|
|
81
|
-
* @param error - The error to check
|
|
82
|
-
* @returns True if the error is an authentication error
|
|
83
|
-
*/
|
|
84
|
-
export function isAuthenticationError(error: unknown): boolean {
|
|
85
|
-
if (!error || typeof error !== 'object') {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const errorObj = error as { message?: string; status?: number; response?: { status?: number } };
|
|
90
|
-
const errorMessage = errorObj.message || '';
|
|
91
|
-
const status = errorObj.status || errorObj.response?.status;
|
|
92
|
-
|
|
93
|
-
return (
|
|
94
|
-
status === 401 ||
|
|
95
|
-
errorMessage.includes('Authentication required') ||
|
|
96
|
-
errorMessage.includes('Invalid or missing authorization header')
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Wraps an API call with authentication error handling.
|
|
102
|
-
* If an authentication error occurs, it can optionally attempt to sync the session and retry.
|
|
103
|
-
*
|
|
104
|
-
* @param apiCall - The API call function to execute
|
|
105
|
-
* @param options - Optional error handling configuration
|
|
106
|
-
* @returns The result of the API call
|
|
107
|
-
* @throws {AuthenticationFailedError} If authentication fails and cannot be recovered
|
|
108
|
-
* @throws {Error} If the API call fails for non-auth reasons
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```ts
|
|
112
|
-
* // Simple usage:
|
|
113
|
-
* const result = await withAuthErrorHandling(
|
|
114
|
-
* () => oxyServices.updateProfile(updates)
|
|
115
|
-
* );
|
|
116
|
-
*
|
|
117
|
-
* // With retry on auth failure:
|
|
118
|
-
* const result = await withAuthErrorHandling(
|
|
119
|
-
* () => oxyServices.updateProfile(updates),
|
|
120
|
-
* { syncSession, activeSessionId, oxyServices }
|
|
121
|
-
* );
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
124
|
-
export async function withAuthErrorHandling<T>(
|
|
125
|
-
apiCall: () => Promise<T>,
|
|
126
|
-
options?: HandleApiErrorOptions
|
|
127
|
-
): Promise<T> {
|
|
128
|
-
try {
|
|
129
|
-
return await apiCall();
|
|
130
|
-
} catch (error) {
|
|
131
|
-
if (!isAuthenticationError(error)) {
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// If we have sync capabilities, try to recover
|
|
136
|
-
if (options?.syncSession && options?.activeSessionId && options?.oxyServices) {
|
|
137
|
-
try {
|
|
138
|
-
await options.syncSession();
|
|
139
|
-
await options.oxyServices.getTokenBySession(options.activeSessionId);
|
|
140
|
-
// Retry the API call after refreshing token
|
|
141
|
-
return await apiCall();
|
|
142
|
-
} catch {
|
|
143
|
-
throw new AuthenticationFailedError();
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
throw new AuthenticationFailedError();
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Combines token validation and auth error handling for a complete authenticated API call.
|
|
153
|
-
* This is the recommended helper for most authenticated API operations.
|
|
154
|
-
*
|
|
155
|
-
* @param oxyServices - The OxyServices instance
|
|
156
|
-
* @param activeSessionId - The active session ID
|
|
157
|
-
* @param apiCall - The API call function to execute
|
|
158
|
-
* @param syncSession - Optional callback to sync session on auth failure
|
|
159
|
-
* @returns The result of the API call
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```ts
|
|
163
|
-
* return await authenticatedApiCall(
|
|
164
|
-
* oxyServices,
|
|
165
|
-
* activeSessionId,
|
|
166
|
-
* () => oxyServices.updateProfile(updates)
|
|
167
|
-
* );
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
export async function authenticatedApiCall<T>(
|
|
171
|
-
oxyServices: OxyServices,
|
|
172
|
-
activeSessionId: string | null | undefined,
|
|
173
|
-
apiCall: () => Promise<T>,
|
|
174
|
-
syncSession?: () => Promise<unknown>
|
|
175
|
-
): Promise<T> {
|
|
176
|
-
await ensureValidToken(oxyServices, activeSessionId);
|
|
177
|
-
|
|
178
|
-
return withAuthErrorHandling(apiCall, {
|
|
179
|
-
syncSession,
|
|
180
|
-
activeSessionId,
|
|
181
|
-
oxyServices,
|
|
182
|
-
});
|
|
183
|
-
}
|