vg-x07df 0.1.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/.azure-pipelines/publish-public.yml +37 -0
- package/.azure-pipelines/publish.yml +39 -0
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/AUTO_JOIN_GUIDE.md +411 -0
- package/README.md +215 -0
- package/Screenshot 2025-09-24 at 14.34.48.png +0 -0
- package/Screenshot 2025-10-04 at 12.58.54.png +0 -0
- package/biome.json +48 -0
- package/examples/demo/.env.example +19 -0
- package/examples/demo/CHANGELOG.md +22 -0
- package/examples/demo/README.md +72 -0
- package/examples/demo/eslint.config.js +23 -0
- package/examples/demo/index.html +13 -0
- package/examples/demo/package.json +34 -0
- package/examples/demo/pnpm-lock.yaml +2098 -0
- package/examples/demo/pnpm-workspace.yaml +1 -0
- package/examples/demo/public/vite.svg +1 -0
- package/examples/demo/src/App.css +52 -0
- package/examples/demo/src/App.tsx +176 -0
- package/examples/demo/src/assets/react.svg +1 -0
- package/examples/demo/src/components/auth/LoginForm.css +144 -0
- package/examples/demo/src/components/auth/LoginForm.tsx +80 -0
- package/examples/demo/src/components/calling/AutoJoinSettings.tsx +213 -0
- package/examples/demo/src/components/calling/AutoJoinStatus.tsx +72 -0
- package/examples/demo/src/components/calling/CallInitiator.css +258 -0
- package/examples/demo/src/components/calling/CallInitiator.tsx +142 -0
- package/examples/demo/src/components/calling/CallNotifications.css +119 -0
- package/examples/demo/src/components/calling/CallNotifications.tsx +108 -0
- package/examples/demo/src/components/calling/IncomingCallModal.css +192 -0
- package/examples/demo/src/components/calling/IncomingCallModal.tsx +78 -0
- package/examples/demo/src/components/calling/MinimizedCall.css +156 -0
- package/examples/demo/src/components/calling/MinimizedCall.tsx +78 -0
- package/examples/demo/src/components/conference/ConferenceHeader.css +265 -0
- package/examples/demo/src/components/conference/ConferenceHeader.tsx +78 -0
- package/examples/demo/src/components/conference/EnhancedControlBar.css +356 -0
- package/examples/demo/src/components/conference/EnhancedControlBar.tsx +262 -0
- package/examples/demo/src/components/conference/PaginationControls.css +67 -0
- package/examples/demo/src/components/conference/PaginationControls.tsx +64 -0
- package/examples/demo/src/components/conference/ParticipantGrid.css +153 -0
- package/examples/demo/src/components/conference/ParticipantGrid.tsx +87 -0
- package/examples/demo/src/components/conference/ParticipantTile.css +210 -0
- package/examples/demo/src/components/conference/ParticipantTile.tsx +114 -0
- package/examples/demo/src/components/conference/VideoConference.css +214 -0
- package/examples/demo/src/components/conference/VideoConference.tsx +93 -0
- package/examples/demo/src/contexts/AuthContext.tsx +105 -0
- package/examples/demo/src/hooks/useAuth.ts +5 -0
- package/examples/demo/src/hooks/useCallTimer.ts +42 -0
- package/examples/demo/src/index.css +68 -0
- package/examples/demo/src/main.tsx +10 -0
- package/examples/demo/src/services/auth.service.ts +153 -0
- package/examples/demo/src/types/auth.types.ts +31 -0
- package/examples/demo/tsconfig.app.json +28 -0
- package/examples/demo/tsconfig.json +7 -0
- package/examples/demo/tsconfig.node.json +26 -0
- package/examples/demo/vite.config.ts +15 -0
- package/images/callpad-without-ai.png +0 -0
- package/package.json +28 -0
- package/packages/sdk/CHANGELOG.md +33 -0
- package/packages/sdk/LICENSE +21 -0
- package/packages/sdk/README.md +97 -0
- package/packages/sdk/documentation.md +1132 -0
- package/packages/sdk/openapi-ts.config.ts +7 -0
- package/packages/sdk/package.json +88 -0
- package/packages/sdk/src/core/auth.manager.ts +52 -0
- package/packages/sdk/src/core/events/event-bus.ts +301 -0
- package/packages/sdk/src/core/events/index.ts +8 -0
- package/packages/sdk/src/core/events/types.ts +165 -0
- package/packages/sdk/src/core/index.ts +3 -0
- package/packages/sdk/src/core/signal/api.config.ts +49 -0
- package/packages/sdk/src/core/signal/index.ts +16 -0
- package/packages/sdk/src/core/signal/signal.client.ts +101 -0
- package/packages/sdk/src/core/signal/types.ts +110 -0
- package/packages/sdk/src/core/socketio/handlers/base.handler.ts +212 -0
- package/packages/sdk/src/core/socketio/handlers/call-accepted.handler.ts +34 -0
- package/packages/sdk/src/core/socketio/handlers/call-canceled.handler.ts +34 -0
- package/packages/sdk/src/core/socketio/handlers/call-declined.handler.ts +29 -0
- package/packages/sdk/src/core/socketio/handlers/call-ended.handler.ts +40 -0
- package/packages/sdk/src/core/socketio/handlers/call-incoming.handler.ts +72 -0
- package/packages/sdk/src/core/socketio/handlers/call-join-info.handler.ts +181 -0
- package/packages/sdk/src/core/socketio/handlers/call-participant-joined.handler.ts +42 -0
- package/packages/sdk/src/core/socketio/handlers/call-participant-joining.handler.ts +42 -0
- package/packages/sdk/src/core/socketio/handlers/call-timeout.handler.ts +31 -0
- package/packages/sdk/src/core/socketio/handlers/handler.registry.ts +62 -0
- package/packages/sdk/src/core/socketio/handlers/index.ts +21 -0
- package/packages/sdk/src/core/socketio/handlers/participant-left.handler.ts +37 -0
- package/packages/sdk/src/core/socketio/handlers/schema.ts +130 -0
- package/packages/sdk/src/core/socketio/index.ts +5 -0
- package/packages/sdk/src/core/socketio/socket.manager.ts +187 -0
- package/packages/sdk/src/core/socketio/types.ts +14 -0
- package/packages/sdk/src/core/types.ts +23 -0
- package/packages/sdk/src/generated/api/core/ApiError.ts +21 -0
- package/packages/sdk/src/generated/api/core/ApiRequestOptions.ts +13 -0
- package/packages/sdk/src/generated/api/core/ApiResult.ts +7 -0
- package/packages/sdk/src/generated/api/core/CancelablePromise.ts +126 -0
- package/packages/sdk/src/generated/api/core/OpenAPI.ts +55 -0
- package/packages/sdk/src/generated/api/core/request.ts +339 -0
- package/packages/sdk/src/generated/api/index.ts +5 -0
- package/packages/sdk/src/generated/api/models.ts +219 -0
- package/packages/sdk/src/generated/api/services.ts +225 -0
- package/packages/sdk/src/hooks/index.ts +21 -0
- package/packages/sdk/src/hooks/useAutoJoin.ts +66 -0
- package/packages/sdk/src/hooks/useCallActions.ts +28 -0
- package/packages/sdk/src/hooks/useCallQuality.ts +416 -0
- package/packages/sdk/src/hooks/useCallState.ts +23 -0
- package/packages/sdk/src/hooks/useConnection.ts +15 -0
- package/packages/sdk/src/hooks/useDevices.ts +296 -0
- package/packages/sdk/src/hooks/useErrorRecovery.ts +299 -0
- package/packages/sdk/src/hooks/useErrors.ts +84 -0
- package/packages/sdk/src/hooks/useEvent.ts +188 -0
- package/packages/sdk/src/hooks/useMediaControls.ts +215 -0
- package/packages/sdk/src/hooks/useParticipantStatus.ts +318 -0
- package/packages/sdk/src/hooks/useParticipants.ts +111 -0
- package/packages/sdk/src/index.ts +66 -0
- package/packages/sdk/src/livekit/constants.ts +76 -0
- package/packages/sdk/src/livekit/device.manager.ts +172 -0
- package/packages/sdk/src/livekit/error-classifier.ts +155 -0
- package/packages/sdk/src/livekit/events/eventBridge.ts +371 -0
- package/packages/sdk/src/livekit/events/trackRegistry.ts +114 -0
- package/packages/sdk/src/livekit/index.ts +49 -0
- package/packages/sdk/src/livekit/livekit.service.ts +110 -0
- package/packages/sdk/src/livekit/media.controls.ts +315 -0
- package/packages/sdk/src/livekit/room.manager.ts +79 -0
- package/packages/sdk/src/livekit/track.utils.ts +230 -0
- package/packages/sdk/src/livekit/types.ts +135 -0
- package/packages/sdk/src/provider/RtcProvider.tsx +78 -0
- package/packages/sdk/src/services/call-actions.ts +260 -0
- package/packages/sdk/src/services/error-recovery.ts +461 -0
- package/packages/sdk/src/services/index.ts +2 -0
- package/packages/sdk/src/services/sdk-builder.ts +104 -0
- package/packages/sdk/src/state/errors.ts +163 -0
- package/packages/sdk/src/state/selectors.ts +28 -0
- package/packages/sdk/src/state/store.ts +36 -0
- package/packages/sdk/src/state/types.ts +151 -0
- package/packages/sdk/src/utils/logger.ts +183 -0
- package/packages/sdk/tsconfig.json +49 -0
- package/packages/sdk/tsup.config.ts +51 -0
- package/pnpm-workspace.yaml +4 -0
- package/tsconfig.base.json +19 -0
- package/turbo.json +34 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { type Socket, io } from "socket.io-client";
|
|
2
|
+
import { createLogger } from "../../utils/logger";
|
|
3
|
+
import type { AuthManager } from "../auth.manager";
|
|
4
|
+
import type { AutoJoinConfig, Nullable } from "../types";
|
|
5
|
+
import { SocketHandlerRegistry } from "./handlers";
|
|
6
|
+
import type { SocketHandlerOptions } from "./handlers/base.handler";
|
|
7
|
+
import type { ConnectionConfig, ConnectionState } from "./types";
|
|
8
|
+
|
|
9
|
+
export class SocketManager {
|
|
10
|
+
private static instance: Nullable<SocketManager> = null;
|
|
11
|
+
private logger = createLogger("socket");
|
|
12
|
+
|
|
13
|
+
private socket: Nullable<Socket> = null;
|
|
14
|
+
private connectionState: ConnectionState = "DISCONNECTED";
|
|
15
|
+
private livekit: any = null;
|
|
16
|
+
private autoJoinConfig: Nullable<AutoJoinConfig> = null;
|
|
17
|
+
private handlerRegistry: Nullable<SocketHandlerRegistry> = null;
|
|
18
|
+
private authManager: Nullable<AuthManager> = null;
|
|
19
|
+
|
|
20
|
+
private constructor() {}
|
|
21
|
+
|
|
22
|
+
static getInstance(): SocketManager {
|
|
23
|
+
if (!SocketManager.instance) {
|
|
24
|
+
SocketManager.instance = new SocketManager();
|
|
25
|
+
}
|
|
26
|
+
return SocketManager.instance;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async initialize(
|
|
30
|
+
baseUrl: string,
|
|
31
|
+
authManager: AuthManager,
|
|
32
|
+
config: ConnectionConfig = {},
|
|
33
|
+
livekit?: any,
|
|
34
|
+
autoJoinConfig?: AutoJoinConfig
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
this.livekit = livekit;
|
|
37
|
+
this.autoJoinConfig = autoJoinConfig || null;
|
|
38
|
+
this.authManager = authManager;
|
|
39
|
+
if (this.socket?.connected) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.updateConnectionState("CONNECTING");
|
|
44
|
+
const token = authManager.getCurrentToken();
|
|
45
|
+
if (!token) {
|
|
46
|
+
throw new Error("No authentication token available");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
this.socket = io(baseUrl, {
|
|
51
|
+
auth: { token },
|
|
52
|
+
autoConnect: false,
|
|
53
|
+
reconnection: true,
|
|
54
|
+
reconnectionAttempts: config.reconnectAttempts ?? 5,
|
|
55
|
+
reconnectionDelay: config.reconnectDelay ?? 1000,
|
|
56
|
+
reconnectionDelayMax: config.reconnectDelayMax ?? 30000,
|
|
57
|
+
timeout: config.timeout ?? 10000,
|
|
58
|
+
forceNew: true,
|
|
59
|
+
path: "/signal/socket.io",
|
|
60
|
+
transports: ["websocket"],
|
|
61
|
+
withCredentials: false,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
this.setupConnectionHandlers(authManager);
|
|
65
|
+
this.setupEventHandlers();
|
|
66
|
+
this.socket.connect();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
this.updateConnectionState("ERROR");
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private setupConnectionHandlers(authManager: AuthManager): void {
|
|
74
|
+
if (!this.socket) return;
|
|
75
|
+
|
|
76
|
+
this.socket.on("connect", () => {
|
|
77
|
+
this.updateConnectionState("CONNECTED");
|
|
78
|
+
this.logger.info("Connected to server");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
this.socket.on("disconnect", (reason: string) => {
|
|
82
|
+
this.updateConnectionState("DISCONNECTED");
|
|
83
|
+
this.logger.info("Disconnected", { reason });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
this.socket.on("connect_error", (error: Error) => {
|
|
87
|
+
this.updateConnectionState("ERROR");
|
|
88
|
+
this.logger.error("Connection error", { error: error.message });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
this.socket.io.on("reconnect_attempt", () => {
|
|
92
|
+
this.updateConnectionState("RECONNECTING");
|
|
93
|
+
const freshToken = authManager.getCurrentToken();
|
|
94
|
+
if (freshToken && this.socket) {
|
|
95
|
+
this.logger.debug("Refreshing auth token for reconnection");
|
|
96
|
+
this.socket.auth = { token: freshToken };
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
this.socket.io.on("reconnect", (attemptNumber: number) => {
|
|
101
|
+
this.logger.info("Reconnected successfully", { attemptNumber });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
this.socket.io.on("reconnect_error", (error: Error) => {
|
|
105
|
+
this.logger.error("Reconnection error", { error: error.message });
|
|
106
|
+
if (this.isAuthError(error)) {
|
|
107
|
+
this.updateConnectionState("FAILED");
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
this.socket.io.on("reconnect_failed", () => {
|
|
112
|
+
this.updateConnectionState("FAILED");
|
|
113
|
+
this.logger.error("All reconnection attempts failed");
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private setupEventHandlers(): void {
|
|
118
|
+
if (!this.socket) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this.logger.debug("Setting up event handlers via registry");
|
|
123
|
+
|
|
124
|
+
console.log("[SOCKET_MANAGER] Setting up event handlers", {
|
|
125
|
+
socketId: this.socket.id,
|
|
126
|
+
connected: this.socket.connected,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Clean up existing handlers to prevent duplicate registrations
|
|
130
|
+
if (this.handlerRegistry) {
|
|
131
|
+
this.handlerRegistry.removeEventListeners(this.socket);
|
|
132
|
+
this.handlerRegistry.destroy();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const options: SocketHandlerOptions = {
|
|
136
|
+
livekit: this.livekit,
|
|
137
|
+
autoJoinConfig: this.autoJoinConfig,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (this.authManager) {
|
|
141
|
+
options.authManager = this.authManager;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this.handlerRegistry = new SocketHandlerRegistry(options);
|
|
145
|
+
|
|
146
|
+
this.handlerRegistry.registerEventListeners(this.socket);
|
|
147
|
+
|
|
148
|
+
console.log("[SOCKET_MANAGER] Event handlers registered successfully");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private isAuthError(error: Error): boolean {
|
|
152
|
+
const authErrorIndicators = [
|
|
153
|
+
"unauthorized",
|
|
154
|
+
"authentication",
|
|
155
|
+
"token",
|
|
156
|
+
"401",
|
|
157
|
+
"403",
|
|
158
|
+
];
|
|
159
|
+
const errorMessage = error.message.toLowerCase();
|
|
160
|
+
return authErrorIndicators.some((indicator) =>
|
|
161
|
+
errorMessage.includes(indicator)
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private updateConnectionState(newState: ConnectionState): void {
|
|
166
|
+
if (this.connectionState !== newState) {
|
|
167
|
+
this.connectionState = newState;
|
|
168
|
+
this.logger.debug("Connection state changed", { newState });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
destroy(): void {
|
|
173
|
+
if (this.socket) {
|
|
174
|
+
if (this.handlerRegistry) {
|
|
175
|
+
this.handlerRegistry.removeEventListeners(this.socket);
|
|
176
|
+
this.handlerRegistry.destroy();
|
|
177
|
+
this.handlerRegistry = null;
|
|
178
|
+
}
|
|
179
|
+
this.socket.removeAllListeners();
|
|
180
|
+
this.socket.disconnect();
|
|
181
|
+
this.socket = null;
|
|
182
|
+
}
|
|
183
|
+
this.connectionState = "DISCONNECTED";
|
|
184
|
+
SocketManager.instance = null;
|
|
185
|
+
this.logger.debug("Destroyed and cleaned up");
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface ConnectionConfig {
|
|
2
|
+
reconnectAttempts?: number;
|
|
3
|
+
reconnectDelay?: number;
|
|
4
|
+
reconnectDelayMax?: number;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type ConnectionState =
|
|
9
|
+
| "DISCONNECTED"
|
|
10
|
+
| "CONNECTING"
|
|
11
|
+
| "CONNECTED"
|
|
12
|
+
| "RECONNECTING"
|
|
13
|
+
| "ERROR"
|
|
14
|
+
| "FAILED";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type AuthProvider = () => string | null;
|
|
2
|
+
|
|
3
|
+
export interface TokenInfo {
|
|
4
|
+
token: string;
|
|
5
|
+
isExpired: boolean;
|
|
6
|
+
expiresAt?: number | undefined;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface JWTPayload {
|
|
10
|
+
exp?: number;
|
|
11
|
+
iat?: number;
|
|
12
|
+
sub?: string;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type Nullable<T> = T | null;
|
|
17
|
+
|
|
18
|
+
// Auto-join configuration types
|
|
19
|
+
export interface AutoJoinConfig {
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
retryOnFailure: boolean;
|
|
22
|
+
maxRetries: number;
|
|
23
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ApiRequestOptions } from './ApiRequestOptions';
|
|
2
|
+
import type { ApiResult } from './ApiResult';
|
|
3
|
+
|
|
4
|
+
export class ApiError extends Error {
|
|
5
|
+
public readonly url: string;
|
|
6
|
+
public readonly status: number;
|
|
7
|
+
public readonly statusText: string;
|
|
8
|
+
public readonly body: unknown;
|
|
9
|
+
public readonly request: ApiRequestOptions;
|
|
10
|
+
|
|
11
|
+
constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
|
|
12
|
+
super(message);
|
|
13
|
+
|
|
14
|
+
this.name = 'ApiError';
|
|
15
|
+
this.url = response.url;
|
|
16
|
+
this.status = response.status;
|
|
17
|
+
this.statusText = response.statusText;
|
|
18
|
+
this.body = response.body;
|
|
19
|
+
this.request = request;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type ApiRequestOptions = {
|
|
2
|
+
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
|
|
3
|
+
readonly url: string;
|
|
4
|
+
readonly path?: Record<string, unknown>;
|
|
5
|
+
readonly cookies?: Record<string, unknown>;
|
|
6
|
+
readonly headers?: Record<string, unknown>;
|
|
7
|
+
readonly query?: Record<string, unknown>;
|
|
8
|
+
readonly formData?: Record<string, unknown>;
|
|
9
|
+
readonly body?: any;
|
|
10
|
+
readonly mediaType?: string;
|
|
11
|
+
readonly responseHeader?: string;
|
|
12
|
+
readonly errors?: Record<number, string>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export class CancelError extends Error {
|
|
2
|
+
constructor(message: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'CancelError';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
public get isCancelled(): boolean {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface OnCancel {
|
|
13
|
+
readonly isResolved: boolean;
|
|
14
|
+
readonly isRejected: boolean;
|
|
15
|
+
readonly isCancelled: boolean;
|
|
16
|
+
|
|
17
|
+
(cancelHandler: () => void): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class CancelablePromise<T> implements Promise<T> {
|
|
21
|
+
private _isResolved: boolean;
|
|
22
|
+
private _isRejected: boolean;
|
|
23
|
+
private _isCancelled: boolean;
|
|
24
|
+
readonly cancelHandlers: (() => void)[];
|
|
25
|
+
readonly promise: Promise<T>;
|
|
26
|
+
private _resolve?: (value: T | PromiseLike<T>) => void;
|
|
27
|
+
private _reject?: (reason?: unknown) => void;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
executor: (
|
|
31
|
+
resolve: (value: T | PromiseLike<T>) => void,
|
|
32
|
+
reject: (reason?: unknown) => void,
|
|
33
|
+
onCancel: OnCancel
|
|
34
|
+
) => void
|
|
35
|
+
) {
|
|
36
|
+
this._isResolved = false;
|
|
37
|
+
this._isRejected = false;
|
|
38
|
+
this._isCancelled = false;
|
|
39
|
+
this.cancelHandlers = [];
|
|
40
|
+
this.promise = new Promise<T>((resolve, reject) => {
|
|
41
|
+
this._resolve = resolve;
|
|
42
|
+
this._reject = reject;
|
|
43
|
+
|
|
44
|
+
const onResolve = (value: T | PromiseLike<T>): void => {
|
|
45
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this._isResolved = true;
|
|
49
|
+
if (this._resolve) this._resolve(value);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const onReject = (reason?: unknown): void => {
|
|
53
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this._isRejected = true;
|
|
57
|
+
if (this._reject) this._reject(reason);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const onCancel = (cancelHandler: () => void): void => {
|
|
61
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.cancelHandlers.push(cancelHandler);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
Object.defineProperty(onCancel, 'isResolved', {
|
|
68
|
+
get: (): boolean => this._isResolved,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
Object.defineProperty(onCancel, 'isRejected', {
|
|
72
|
+
get: (): boolean => this._isRejected,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
Object.defineProperty(onCancel, 'isCancelled', {
|
|
76
|
+
get: (): boolean => this._isCancelled,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return executor(onResolve, onReject, onCancel as OnCancel);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get [Symbol.toStringTag]() {
|
|
84
|
+
return "Cancellable Promise";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public then<TResult1 = T, TResult2 = never>(
|
|
88
|
+
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
|
|
89
|
+
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
|
|
90
|
+
): Promise<TResult1 | TResult2> {
|
|
91
|
+
return this.promise.then(onFulfilled, onRejected);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public catch<TResult = never>(
|
|
95
|
+
onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null
|
|
96
|
+
): Promise<T | TResult> {
|
|
97
|
+
return this.promise.catch(onRejected);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public finally(onFinally?: (() => void) | null): Promise<T> {
|
|
101
|
+
return this.promise.finally(onFinally);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public cancel(): void {
|
|
105
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
this._isCancelled = true;
|
|
109
|
+
if (this.cancelHandlers.length) {
|
|
110
|
+
try {
|
|
111
|
+
for (const cancelHandler of this.cancelHandlers) {
|
|
112
|
+
cancelHandler();
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
// Silently ignore cancellation errors
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
this.cancelHandlers.length = 0;
|
|
120
|
+
if (this._reject) this._reject(new CancelError('Request aborted'));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public get isCancelled(): boolean {
|
|
124
|
+
return this._isCancelled;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ApiRequestOptions } from './ApiRequestOptions';
|
|
2
|
+
|
|
3
|
+
type Headers = Record<string, string>;
|
|
4
|
+
type Middleware<T> = (value: T) => T | Promise<T>;
|
|
5
|
+
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
|
6
|
+
|
|
7
|
+
export class Interceptors<T> {
|
|
8
|
+
_fns: Middleware<T>[];
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
this._fns = [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
eject(fn: Middleware<T>) {
|
|
15
|
+
const index = this._fns.indexOf(fn);
|
|
16
|
+
if (index !== -1) {
|
|
17
|
+
this._fns = [
|
|
18
|
+
...this._fns.slice(0, index),
|
|
19
|
+
...this._fns.slice(index + 1),
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
use(fn: Middleware<T>) {
|
|
25
|
+
this._fns = [...this._fns, fn];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type OpenAPIConfig = {
|
|
30
|
+
BASE: string;
|
|
31
|
+
CREDENTIALS: 'include' | 'omit' | 'same-origin';
|
|
32
|
+
ENCODE_PATH?: ((path: string) => string) | undefined;
|
|
33
|
+
HEADERS?: Headers | Resolver<Headers> | undefined;
|
|
34
|
+
PASSWORD?: string | Resolver<string> | undefined;
|
|
35
|
+
TOKEN?: string | Resolver<string> | undefined;
|
|
36
|
+
USERNAME?: string | Resolver<string> | undefined;
|
|
37
|
+
VERSION: string;
|
|
38
|
+
WITH_CREDENTIALS: boolean;
|
|
39
|
+
interceptors: {request: Interceptors<RequestInit>;
|
|
40
|
+
response: Interceptors<Response>;};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const OpenAPI: OpenAPIConfig = {
|
|
44
|
+
BASE: '',
|
|
45
|
+
CREDENTIALS: 'include',
|
|
46
|
+
ENCODE_PATH: undefined,
|
|
47
|
+
HEADERS: undefined,
|
|
48
|
+
PASSWORD: undefined,
|
|
49
|
+
TOKEN: undefined,
|
|
50
|
+
USERNAME: undefined,
|
|
51
|
+
VERSION: '0.0.1',
|
|
52
|
+
WITH_CREDENTIALS: false,
|
|
53
|
+
interceptors: {request: new Interceptors(),response: new Interceptors(),
|
|
54
|
+
},
|
|
55
|
+
};
|