slash-create-modify
Version:
Create and sync Discord slash commands!
51 lines (50 loc) • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AzureFunctionServer = void 0;
const server_1 = require("../server");
const multipartData_1 = require("../util/multipartData");
/**
* A server for Azure Function integration
* @see https://docs.microsoft.com/en-us/azure/azure-functions/
*/
class AzureFunctionServer extends server_1.Server {
constructor(moduleExports, target = 'interactions') {
super({ alreadyListening: true });
moduleExports[target] = this._onRequest.bind(this);
}
_onRequest(context, req) {
if (!this._handler) {
context.res.status = 503;
context.res.send('Server has no handler');
}
if (req.method !== 'POST') {
context.res.status = 400;
context.res.send('Server only supports POST requests.');
}
this._handler({
headers: req.headers,
body: req.body,
request: req,
response: context.res
}, async (response) => {
context.res.status = response.status || 200;
if (response.files) {
const data = new multipartData_1.MultipartData();
context.res.header('Content-Type', 'multipart/form-data; boundary=' + data.boundary);
for (const i in response.files)
data.attach(`files[${i}]`, response.files[i].file, response.files[i].name);
data.attach('payload_json', JSON.stringify(response.body));
context.res.send(Buffer.concat(data.finish()));
}
else {
context.res.header('Content-Type', 'application/json');
context.res.send(response.body);
}
});
}
/** @private */
createEndpoint(path, handler) {
this._handler = handler;
}
}
exports.AzureFunctionServer = AzureFunctionServer;