mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-21 02:40:08 +00:00
I prefer semicolors, they also help avoiding certain pitfalls in JavaScript/TypeScript, such as the following code sample: ```js const xyz = "test" (something.else as string) = "another" ``` This results in a TypeError: "test" is not a function, this is because js thinks we are trying to call the string "test" as a function. To fix this it requires a `;` somewhere before the `(`, such as `;(something ... ` which in my opinion is ugly and less clean overall.
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import fastifyMultipart, { type MultipartFile, type MultipartValue } from '@fastify/multipart';
|
|
import fastify, { type FastifyInstance } from 'fastify';
|
|
import { REST_AUTHORIZATION } from '../config.js';
|
|
|
|
export function buildFastifyApp(): FastifyInstance {
|
|
const app = fastify();
|
|
|
|
app.register(fastifyMultipart, { attachFieldsToBody: true });
|
|
|
|
// Authorization check
|
|
app.addHook('onRequest', async (req, res) => {
|
|
if (req.headers.authorization !== REST_AUTHORIZATION) {
|
|
res.status(401).send({
|
|
message: 'Credentials not valid.',
|
|
});
|
|
}
|
|
});
|
|
|
|
return app;
|
|
}
|
|
|
|
export async function parseMultiformBody(body: unknown): Promise<FormData> {
|
|
const form = new FormData();
|
|
|
|
if (typeof body !== 'object' || !body) return form;
|
|
|
|
for (const objectValue of Object.values(body)) {
|
|
const value = objectValue as MultipartFile | MultipartValue;
|
|
|
|
if (value.type === 'file') {
|
|
form.append(value.fieldname, new Blob([await value.toBuffer()]), value.filename);
|
|
}
|
|
if (value.type === 'field' && typeof value.value === 'string') {
|
|
form.append(value.fieldname, value.value);
|
|
}
|
|
}
|
|
|
|
return form;
|
|
}
|