UNPKG

slash-create-modify

Version:

Create and sync Discord slash commands!

83 lines (82 loc) 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FastifyServer = void 0; const server_1 = require("../server"); const multipartData_1 = require("../util/multipartData"); let fastify; let symbols; try { fastify = require('fastify'); symbols = require('fastify/lib/symbols'); } catch { } /** * A server for Fastify applications. * @see https://fastify.io */ class FastifyServer extends server_1.Server { /** * @param app The fastify application, or the options for initialization * @param opts The server options */ constructor(app, opts) { super(opts); if (!fastify) throw new Error('You must have the `fastify` package installed before using this server.'); if (!app) { app = fastify.default(); } else if (!(symbols.kState in app)) { app = fastify.default(app); } this.app = app; } /** * Adds middleware to the Fastify server. * <warn>This requires you to have the 'middie' module registered to the server before using.</warn> * @param middleware The middleware to add. * @see https://www.fastify.io/docs/latest/Middleware/ */ addMiddleware(middleware) { // @ts-ignore if ('use' in this.app) this.app.use(middleware); else throw new Error("In order to use Express-like middleware, you must initialize the server and register the 'middie' module."); return this; } /** Alias for {@link FastifyServer#addMiddleware} */ use(middleware) { return this.addMiddleware(middleware); } /** @private */ createEndpoint(path, handler) { this.app.post(path, (req, res) => handler({ headers: req.headers, body: req.body, request: req, response: res }, async (response) => { res.status(response.status || 200); if (response.headers) res.headers(response.headers); if (response.files) { const data = new multipartData_1.MultipartData(); 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)); res.send(Buffer.concat(data.finish())); } else res.send(response.body); })); } /** @private */ async listen(port = 8030, host = 'localhost') { if (this.alreadyListening) return; await this.app.listen(port, host); } } exports.FastifyServer = FastifyServer;