Clean up and fixes
This commit is contained in:
46
src/utils/logger.ts
Normal file
46
src/utils/logger.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export class Logger {
|
||||
constructor(
|
||||
private readonly endpoint: string,
|
||||
private readonly token: string,
|
||||
private readonly path: string,
|
||||
) {}
|
||||
|
||||
send(level: 'log' | 'error' | 'warn' | 'info', message: string, ...metadata: unknown[]) {
|
||||
const data = {
|
||||
level,
|
||||
message: `${this.path}: ${message}`,
|
||||
metadata,
|
||||
};
|
||||
|
||||
fetch(`${this.endpoint}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': this.token,
|
||||
},
|
||||
}).catch(error => {
|
||||
console.error('Failed to send log to logger:', error);
|
||||
});
|
||||
}
|
||||
|
||||
log(message: string, ...metadata: unknown[]) {
|
||||
this.send('log', message, ...metadata);
|
||||
}
|
||||
|
||||
error(message: string, ...metadata: unknown[]) {
|
||||
this.send('error', message, ...metadata);
|
||||
}
|
||||
|
||||
warn(message: string, ...metadata: unknown[]) {
|
||||
this.send('warn', message, ...metadata);
|
||||
}
|
||||
|
||||
info(message: string, ...metadata: unknown[]) {
|
||||
this.send('info', message, ...metadata);
|
||||
}
|
||||
|
||||
child(path: string): Logger {
|
||||
return new Logger(this.endpoint, this.token, `${this.path}.${path}`);
|
||||
}
|
||||
}
|
||||
@@ -58,10 +58,15 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
* @param identifier - The invitation identifier.
|
||||
* @returns The invitation.
|
||||
*/
|
||||
async getInvitation(identifier: string): Promise<XOInvitation> {
|
||||
async getInvitation(identifier: string): Promise<XOInvitation | undefined> {
|
||||
// Send a GET request to the sync server
|
||||
const response = await fetch(`${this.baseUrl}/invitations/${identifier}`);
|
||||
const invitation = await response.json() as XOInvitation;
|
||||
const response = await fetch(`${this.baseUrl}/invitations?invitationIdentifier=${identifier}`);
|
||||
|
||||
if(!response.ok) {
|
||||
throw new Error(`Failed to get invitation: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const invitation = await response.json() as XOInvitation | undefined;
|
||||
return invitation;
|
||||
}
|
||||
|
||||
@@ -75,6 +80,9 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
const response = await fetch(`${this.baseUrl}/invitations`, {
|
||||
method: 'POST',
|
||||
body: encodeExtendedJson(invitation),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Throw is there was an issue with the request
|
||||
|
||||
Reference in New Issue
Block a user