Skip to content

Commit 039fa93

Browse files
committed
feat: moved to fetch instead of ajax call
1 parent 4248082 commit 039fa93

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/node/hooks/express/apicalls.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
'use strict';
22

3+
import express from "express";
4+
35
const log4js = require('log4js');
46
const clientLogger = log4js.getLogger('client');
57
const {Formidable} = require('formidable');
68
const apiHandler = require('../../handler/APIHandler');
79
const util = require('util');
810

11+
12+
function objectAsString(obj) {
13+
var output = '';
14+
for (var property in obj) {
15+
if(obj.hasOwnProperty(property) && typeof obj[property] !== 'function') {
16+
var value = obj[property];
17+
if(typeof value === 'object' && !Array.isArray(value) && value !== null) {
18+
value = '{' + objectAsString(value) + '}';
19+
}
20+
output += property + ': ' + value +'; ';
21+
}
22+
}
23+
return output;
24+
}
25+
926
exports.expressPreSession = async (hookName:string, {app}:any) => {
27+
app.use(express.json());
1028
// The Etherpad client side sends information about how a disconnect happened
1129
app.post('/ep/pad/connection-diagnostic-info', async (req:any, res:any) => {
12-
const [fields, files] = await (new Formidable({})).parse(req);
13-
clientLogger.info(`DIAGNOSTIC-INFO: ${fields.diagnosticInfo}`);
30+
if (!req.body ||!req.body.diagnosticInfo || typeof req.body.diagnosticInfo !== 'object') {
31+
clientLogger.warn('DIAGNOSTIC-INFO: No diagnostic info provided');
32+
res.status(400).end('No diagnostic info provided');
33+
return;
34+
}
35+
36+
clientLogger.info(`DIAGNOSTIC-INFO: ${objectAsString(req.body.diagnosticInfo)}`);
1437
res.end('OK');
1538
});
1639

src/static/js/pad.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -725,18 +725,17 @@ const pad = {
725725
}
726726
},
727727
asyncSendDiagnosticInfo: () => {
728-
window.setTimeout(() => {
729-
$.ajax(
730-
{
731-
type: 'post',
732-
url: '../ep/pad/connection-diagnostic-info',
733-
data: {
734-
diagnosticInfo: JSON.stringify(pad.diagnosticInfo),
735-
},
736-
success: () => {},
737-
error: () => {},
738-
});
739-
}, 0);
728+
fetch('../ep/pad/connection-diagnostic-info', {
729+
method: 'POST',
730+
headers: {
731+
'Content-Type': 'application/json',
732+
},
733+
body: JSON.stringify({
734+
diagnosticInfo: pad.diagnosticInfo,
735+
}),
736+
}).catch((error) => {
737+
console.error('Error sending diagnostic info:', error);
738+
})
740739
},
741740
forceReconnect: () => {
742741
$('form#reconnectform input.padId').val(pad.getPadId());

0 commit comments

Comments
 (0)