Files
discordeno/examples/bigbot/src/rest/fastify.ts
Fleny 27c261fee2 formatter: Use semicolons (#4686)
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.
2026-01-17 21:54:15 +01:00

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;
}