mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-10 10:53:24 +00:00
- Add RondevuConnection class for high-level WebRTC management - Handles offer/answer exchange, ICE candidates, and data channels - Fix race condition in answer() method (register answerer before sending ICE) - Add event-driven API (connecting, connected, disconnected, error, datachannel, track) - Update README with connection manager examples - Export new connection types and classes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
export interface Credentials {
|
|
peerId: string;
|
|
secret: string;
|
|
}
|
|
|
|
// Fetch-compatible function type
|
|
export type FetchFunction = (
|
|
input: RequestInfo | URL,
|
|
init?: RequestInit
|
|
) => Promise<Response>;
|
|
|
|
export class RondevuAuth {
|
|
private fetchFn: FetchFunction;
|
|
|
|
constructor(
|
|
private baseUrl: string,
|
|
fetchFn?: FetchFunction
|
|
) {
|
|
// Use provided fetch or fall back to global fetch
|
|
this.fetchFn = fetchFn || ((...args) => {
|
|
if (typeof globalThis.fetch === 'function') {
|
|
return globalThis.fetch(...args);
|
|
}
|
|
throw new Error(
|
|
'fetch is not available. Please provide a fetch implementation in the constructor options.'
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Register a new peer and receive credentials
|
|
*/
|
|
async register(): Promise<Credentials> {
|
|
const response = await this.fetchFn(`${this.baseUrl}/register`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
throw new Error(`Registration failed: ${error.error || response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return {
|
|
peerId: data.peerId,
|
|
secret: data.secret,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create Authorization header value
|
|
*/
|
|
static createAuthHeader(credentials: Credentials): string {
|
|
return `Bearer ${credentials.peerId}:${credentials.secret}`;
|
|
}
|
|
}
|