Format with prettier. Use screen mode for invitation import - dialog mode is broken.
This commit is contained in:
@@ -4,14 +4,17 @@ import { SSESession, type SSEvent } from "./sse-client.js";
|
||||
import { decodeExtendedJson, encodeExtendedJson } from "./ext-json.js";
|
||||
|
||||
export type SyncServerEventMap = {
|
||||
'connected': void;
|
||||
'disconnected': void;
|
||||
'error': Error;
|
||||
'message': SSEvent;
|
||||
}
|
||||
connected: void;
|
||||
disconnected: void;
|
||||
error: Error;
|
||||
message: SSEvent;
|
||||
};
|
||||
|
||||
export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
static async from(baseUrl: string, invitationIdentifier: string): Promise<SyncServer> {
|
||||
static async from(
|
||||
baseUrl: string,
|
||||
invitationIdentifier: string,
|
||||
): Promise<SyncServer> {
|
||||
const server = new SyncServer(baseUrl, invitationIdentifier);
|
||||
await server.connect();
|
||||
return server;
|
||||
@@ -19,22 +22,32 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
|
||||
private sse: SSESession;
|
||||
|
||||
constructor(private readonly baseUrl: string, private readonly invitationIdentifier: string) {
|
||||
constructor(
|
||||
private readonly baseUrl: string,
|
||||
private readonly invitationIdentifier: string,
|
||||
) {
|
||||
super();
|
||||
|
||||
// Create an SSE Session
|
||||
this.sse = new SSESession(`${baseUrl}/invitations?invitationIdentifier=${invitationIdentifier}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'text/event-stream',
|
||||
},
|
||||
this.sse = new SSESession(
|
||||
`${baseUrl}/invitations?invitationIdentifier=${invitationIdentifier}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "text/event-stream",
|
||||
},
|
||||
|
||||
// Create our event bubblers
|
||||
onMessage: (event: SSEvent) => this.emit('message', event),
|
||||
onError: (error: unknown) => this.emit('error', error instanceof Error ? error : new Error(String(error))),
|
||||
onDisconnected: () => this.emit('disconnected', undefined),
|
||||
onConnected: () => this.emit('connected', undefined),
|
||||
});
|
||||
// Create our event bubblers
|
||||
onMessage: (event: SSEvent) => this.emit("message", event),
|
||||
onError: (error: unknown) =>
|
||||
this.emit(
|
||||
"error",
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
),
|
||||
onDisconnected: () => this.emit("disconnected", undefined),
|
||||
onConnected: () => this.emit("connected", undefined),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,13 +73,17 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
*/
|
||||
async getInvitation(identifier: string): Promise<XOInvitation | undefined> {
|
||||
// Send a GET request to the sync server
|
||||
const response = await fetch(`${this.baseUrl}/invitations?invitationIdentifier=${identifier}`);
|
||||
|
||||
if(!response.ok) {
|
||||
const response = await fetch(
|
||||
`${this.baseUrl}/invitations?invitationIdentifier=${identifier}`,
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get invitation: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const invitation = decodeExtendedJson(await response.text()) as XOInvitation | undefined;
|
||||
|
||||
const invitation = decodeExtendedJson(await response.text()) as
|
||||
| XOInvitation
|
||||
| undefined;
|
||||
return invitation;
|
||||
}
|
||||
|
||||
@@ -78,10 +95,10 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
async publishInvitation(invitation: XOInvitation): Promise<XOInvitation> {
|
||||
// Send a POST request to the sync server
|
||||
const response = await fetch(`${this.baseUrl}/invitations`, {
|
||||
method: 'POST',
|
||||
method: "POST",
|
||||
body: encodeExtendedJson(invitation),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -96,4 +113,4 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user