mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
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.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import fastifyEnv from '@fastify/env'
|
||||
import fastifyHelmet from '@fastify/helmet'
|
||||
import fastifyMultipart from '@fastify/multipart'
|
||||
import fastify, { type FastifyInstance } from 'fastify'
|
||||
import fastifyEnv from '@fastify/env';
|
||||
import fastifyHelmet from '@fastify/helmet';
|
||||
import fastifyMultipart from '@fastify/multipart';
|
||||
import fastify, { type FastifyInstance } from 'fastify';
|
||||
|
||||
export const buildFastifyApp = async (): Promise<FastifyInstance> => {
|
||||
const app = await fastify()
|
||||
const app = await fastify();
|
||||
|
||||
await app.register(fastifyEnv, {
|
||||
schema: {
|
||||
@@ -25,28 +25,28 @@ export const buildFastifyApp = async (): Promise<FastifyInstance> => {
|
||||
},
|
||||
required: ['DISCORD_TOKEN', 'AUTHORIZATION_TOKEN'],
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
await app.register(fastifyHelmet)
|
||||
app.register(fastifyMultipart, { attachFieldsToBody: true })
|
||||
await app.register(fastifyHelmet);
|
||||
app.register(fastifyMultipart, { attachFieldsToBody: true });
|
||||
|
||||
app.addHook('onRequest', async (request, reply) => {
|
||||
if (request.headers.authorization !== request.server.config.AUTHORIZATION_TOKEN) {
|
||||
reply.status(401).send({
|
||||
message: 'Credentials not valid.',
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return app
|
||||
}
|
||||
return app;
|
||||
};
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
config: {
|
||||
HOST: string
|
||||
DISCORD_TOKEN: string
|
||||
AUTHORIZATION_TOKEN: string
|
||||
}
|
||||
HOST: string;
|
||||
DISCORD_TOKEN: string;
|
||||
AUTHORIZATION_TOKEN: string;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,79 +1,79 @@
|
||||
import { createRestManager, type RequestMethods } from '@discordeno/rest'
|
||||
import type { MultipartFile, MultipartValue } from '@fastify/multipart'
|
||||
import { buildFastifyApp } from './fastify.js'
|
||||
import { createRestManager, type RequestMethods } from '@discordeno/rest';
|
||||
import type { MultipartFile, MultipartValue } from '@fastify/multipart';
|
||||
import { buildFastifyApp } from './fastify.js';
|
||||
|
||||
const app = await buildFastifyApp()
|
||||
const app = await buildFastifyApp();
|
||||
|
||||
if (!app.config.DISCORD_TOKEN || !app.config.AUTHORIZATION_TOKEN) {
|
||||
console.error('Missing environment variables. Both DISCORD_TOKEN and AUTHORIZATION_TOKEN are required.')
|
||||
process.exit(1)
|
||||
console.error('Missing environment variables. Both DISCORD_TOKEN and AUTHORIZATION_TOKEN are required.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const discordRestManager = createRestManager({
|
||||
token: app.config.DISCORD_TOKEN,
|
||||
})
|
||||
});
|
||||
|
||||
app.get('/timecheck', async (_request, reply) => {
|
||||
reply.status(200).send({
|
||||
message: Date.now(),
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
app.all('/*', async (request, reply) => {
|
||||
let url = request.originalUrl
|
||||
let url = request.originalUrl;
|
||||
|
||||
if (url.startsWith('/v')) {
|
||||
url = url.slice(url.indexOf('/', 2))
|
||||
url = url.slice(url.indexOf('/', 2));
|
||||
}
|
||||
|
||||
const isMultipart = request.headers['content-type']?.startsWith('multipart/form-data')
|
||||
const body = request.method !== 'GET' && request.method !== 'DELETE' ? request.body : undefined
|
||||
const isMultipart = request.headers['content-type']?.startsWith('multipart/form-data');
|
||||
const body = request.method !== 'GET' && request.method !== 'DELETE' ? request.body : undefined;
|
||||
|
||||
try {
|
||||
const result = await discordRestManager.makeRequest(request.method as RequestMethods, url, {
|
||||
body: isMultipart && body ? await parseMultiformBody(body) : body,
|
||||
})
|
||||
});
|
||||
|
||||
if (result) {
|
||||
reply.status(200).send(result)
|
||||
reply.status(200).send(result);
|
||||
} else {
|
||||
reply.status(204).send({})
|
||||
reply.status(204).send({});
|
||||
}
|
||||
} catch (error) {
|
||||
app.log.error(error)
|
||||
app.log.error(error);
|
||||
|
||||
reply.status(500).send({
|
||||
message: error,
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
try {
|
||||
await app.listen({
|
||||
host: app.config.HOST,
|
||||
port: 8000,
|
||||
})
|
||||
console.log(`Proxy listening on port 8000`)
|
||||
});
|
||||
console.log(`Proxy listening on port 8000`);
|
||||
} catch (error) {
|
||||
app.log.error(error)
|
||||
process.exit(1)
|
||||
app.log.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function parseMultiformBody(body: unknown): Promise<FormData> {
|
||||
const form = new FormData()
|
||||
const form = new FormData();
|
||||
|
||||
if (typeof body !== 'object' || !body) return form
|
||||
if (typeof body !== 'object' || !body) return form;
|
||||
|
||||
for (const objectValue of Object.values(body)) {
|
||||
const value = objectValue as MultipartFile | MultipartValue
|
||||
const value = objectValue as MultipartFile | MultipartValue;
|
||||
|
||||
if (value.type === 'file') {
|
||||
form.append(value.fieldname, new Blob([Uint8Array.from(await value.toBuffer())]), value.filename)
|
||||
form.append(value.fieldname, new Blob([Uint8Array.from(await value.toBuffer())]), value.filename);
|
||||
}
|
||||
if (value.type === 'field' && typeof value.value === 'string') {
|
||||
form.append(value.fieldname, value.value)
|
||||
form.append(value.fieldname, value.value);
|
||||
}
|
||||
}
|
||||
|
||||
return form
|
||||
return form;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user