fixes(eslint): fixing everything that eslint complained about

This commit is contained in:
Maieul BOYER 2025-09-29 11:50:53 +02:00
parent d11e4a4516
commit 404735fe22
17 changed files with 1566 additions and 69 deletions

View file

@ -26,11 +26,16 @@ declare module 'fastify' {
export interface FastifyContextConfig { export interface FastifyContextConfig {
requireAuth?: boolean; requireAuth?: boolean;
} }
export interface RouteOptions {
[kRouteAuthDone]: boolean;
}
} }
export const Otp = OTP; export const Otp = OTP;
let jwtAdded = false; let jwtAdded = false;
export const jwtPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => { export const jwtPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
void _opts;
if (jwtAdded) return; if (jwtAdded) return;
jwtAdded = true; jwtAdded = true;
const env = process.env.JWT_SECRET; const env = process.env.JWT_SECRET;
@ -65,16 +70,18 @@ export type JwtType = Static<typeof JwtType>;
let authAdded = false; let authAdded = false;
export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => { export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
void _opts;
if (authAdded) return void console.log('skipping'); if (authAdded) return void console.log('skipping');
authAdded = true; authAdded = true;
await fastify.register(useDatabase as any, {}); await fastify.register(useDatabase as FastifyPluginAsync, {});
await fastify.register(jwtPlugin as any, {}); await fastify.register(jwtPlugin as FastifyPluginAsync, {});
await fastify.register(cookie); await fastify.register(cookie);
if (!fastify.hasRequestDecorator('authUser')) {fastify.decorateRequest('authUser', undefined);} if (!fastify.hasRequestDecorator('authUser')) { fastify.decorateRequest('authUser', undefined); }
fastify.addHook('onRoute', (routeOpts) => { fastify.addHook('onRoute', (routeOpts) => {
if ( if (
routeOpts.config?.requireAuth && routeOpts.config?.requireAuth &&
!(routeOpts as any)[kRouteAuthDone] !routeOpts[kRouteAuthDone]
) { ) {
const f: preValidationAsyncHookHandler = async function(req, res) { const f: preValidationAsyncHookHandler = async function(req, res) {
try { try {
@ -119,7 +126,7 @@ export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
routeOpts.preValidation = [routeOpts.preValidation, f]; routeOpts.preValidation = [routeOpts.preValidation, f];
} }
(routeOpts as any)[kRouteAuthDone] = true; routeOpts[kRouteAuthDone] = true;
} }
}); });
}); });

View file

@ -21,14 +21,15 @@ let dbAdded = false;
export const useDatabase = fp<FastifyPluginAsync>(async function( export const useDatabase = fp<FastifyPluginAsync>(async function(
f: FastifyInstance, f: FastifyInstance,
_options: {}) { _options: object) {
if (dbAdded) {return;} void _options;
if (dbAdded) { return; }
dbAdded = true; dbAdded = true;
const path = process.env.DATABASE_DIR; const path = process.env.DATABASE_DIR;
if (isNullish(path)) {throw 'env `DATABASE_DIR` not defined';} if (isNullish(path)) { throw 'env `DATABASE_DIR` not defined'; }
f.log.info(`Opening database with path: ${path}/database.db`); f.log.info(`Opening database with path: ${path}/database.db`);
const db: Database = new DbImpl(`${path}/database.db`) as Database; const db: Database = new DbImpl(`${path}/database.db`) as Database;
if (!f.hasDecorator('db')) {f.decorate('db', db);} if (!f.hasDecorator('db')) { f.decorate('db', db); }
}); });
export default useDatabase; export default useDatabase;

View file

@ -1,6 +1,6 @@
import sqlite from 'better-sqlite3'; import sqlite from 'better-sqlite3';
// @ts-ignore: this file is included using vite, typescript doesn't know how to include this... // @ts-expect-error: this file is included using vite, typescript doesn't know how to include this...
import initSql from '../init.sql?raw'; import initSql from '../init.sql?raw';
export type SqliteReturn = object | undefined; export type SqliteReturn = object | undefined;

View file

@ -94,17 +94,17 @@ export const UserImpl: Omit<IUserDb, keyof Database> = {
}, },
getUserOtpSecret(this: IUserDb, id: UserId): string | undefined { getUserOtpSecret(this: IUserDb, id: UserId): string | undefined {
const otp: any = this.prepare('SELECT otp FROM user WHERE id = @id LIMIT 1').get({ id }) as SqliteReturn; const otp = this.prepare('SELECT otp FROM user WHERE id = @id LIMIT 1').get({ id }) as ({ otp: string } | null | undefined);
if (isNullish(otp?.otp)) return undefined; if (isNullish(otp?.otp)) return undefined;
return otp.otp; return otp.otp;
}, },
ensureUserOtpSecret(this: IUserDb, id: UserId): string | undefined { ensureUserOtpSecret(this: IUserDb, id: UserId): string | undefined {
const otp = this.getUserOtpSecret(id); const otp = this.getUserOtpSecret(id);
if (!isNullish(otp)) {return otp;} if (!isNullish(otp)) { return otp; }
const otpGen = new Otp(); const otpGen = new Otp();
const res: any = this.prepare('UPDATE OR IGNORE user SET otp = @otp WHERE id = @id RETURNING otp') const res = this.prepare('UPDATE OR IGNORE user SET otp = @otp WHERE id = @id RETURNING otp')
.get({ id, otp: otpGen.secret }); .get({ id, otp: otpGen.secret }) as ({ otp: string } | null | undefined);
return res?.otp; return res?.otp;
}, },
@ -154,12 +154,14 @@ async function hashPassword(
* *
* @returns The user if it exists, undefined otherwise * @returns The user if it exists, undefined otherwise
*/ */
function userFromRow(row: any): User | undefined { function userFromRow(row: Partial<User>): User | undefined {
if (isNullish(row)) return undefined; if (isNullish(row)) return undefined;
if (isNullish(row.id)) return undefined;
if (isNullish(row.name)) return undefined;
return { return {
id: row.id as UserId, id: row.id,
name: row.name || undefined, name: row.name,
password: row.password || undefined, password: row.password ?? undefined,
otp: row.otp || undefined, otp: row.otp ?? undefined,
}; };
} }

View file

@ -1,4 +1,4 @@
import { Type } from '@sinclair/typebox'; import { TObject, TProperties, Type } from '@sinclair/typebox';
/** /**
* @description Represent a message key * @description Represent a message key
@ -12,7 +12,7 @@ import { Type } from '@sinclair/typebox';
* @example `pong.you.lost` * @example `pong.you.lost`
*/ */
export type MessageKey = string; export type MessageKey = string;
export type ResponseBase<T = {}> = { export type ResponseBase<T = object> = {
kind: string, kind: string,
msg: MessageKey, msg: MessageKey,
payload?: T, payload?: T,
@ -26,7 +26,7 @@ export type ResponseBase<T = {}> = {
* @example makeResponse("failure", "login.failure.invalid") * @example makeResponse("failure", "login.failure.invalid")
* @example makeResponse("success", "login.success", { token: "supersecrettoken" }) * @example makeResponse("success", "login.success", { token: "supersecrettoken" })
*/ */
export function makeResponse<T = {}>(kind: string, key: MessageKey, payload?: T): ResponseBase<T> { export function makeResponse<T = object>(kind: string, key: MessageKey, payload?: T): ResponseBase<T> {
console.log(`making response {kind: ${JSON.stringify(kind)}; key: ${JSON.stringify(key)}}`); console.log(`making response {kind: ${JSON.stringify(kind)}; key: ${JSON.stringify(key)}}`);
return { kind, msg: key, payload }; return { kind, msg: key, payload };
} }
@ -39,7 +39,7 @@ export function makeResponse<T = {}>(kind: string, key: MessageKey, payload?: T)
* @example typeResponse("otpRequired", "login.otpRequired", { token: Type.String() }) * @example typeResponse("otpRequired", "login.otpRequired", { token: Type.String() })
* @example typeResponse("success", "login.success", { token: Type.String() }) * @example typeResponse("success", "login.success", { token: Type.String() })
*/ */
export function typeResponse(kind: string, key: MessageKey | MessageKey[], payload?: any): any { export function typeResponse(kind: string, key: MessageKey | MessageKey[], payload?: TProperties): TObject<TProperties> {
let tKey; let tKey;
if (key instanceof Array) { if (key instanceof Array) {
tKey = Type.Union(key.map(l => Type.Const(l))); tKey = Type.Union(key.map(l => Type.Const(l)));

View file

@ -1,17 +1,14 @@
import { FastifyPluginAsync } from 'fastify'; import { FastifyPluginAsync } from 'fastify';
import fastifyFormBody from '@fastify/formbody'; import fastifyFormBody from '@fastify/formbody';
import fastifyMultipart from '@fastify/multipart'; import fastifyMultipart from '@fastify/multipart';
import { mkdir } from 'node:fs/promises';
import fp from 'fastify-plugin';
import * as db from '@shared/database'; import * as db from '@shared/database';
import * as auth from '@shared/auth'; import * as auth from '@shared/auth';
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this... // @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true }); const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this... // @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const routes = import.meta.glob('./routes/**/*.ts', { eager: true }); const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
// When using .decorate you have to specify added properties for Typescript // When using .decorate you have to specify added properties for Typescript
declare module 'fastify' { declare module 'fastify' {
export interface FastifyInstance { export interface FastifyInstance {
@ -19,20 +16,18 @@ declare module 'fastify' {
} }
} }
const app: FastifyPluginAsync = async ( const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify, void opts;
opts, await fastify.register(db.useDatabase as FastifyPluginAsync, {});
): Promise<void> => { await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
await fastify.register(db.useDatabase as any, {}); await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
await fastify.register(auth.jwtPlugin as any, {});
await fastify.register(auth.authPlugin as any, {});
// Place here your custom code! // Place here your custom code!
for (const plugin of Object.values(plugins)) { for (const plugin of Object.values(plugins)) {
void fastify.register(plugin as any, {}); void fastify.register(plugin as FastifyPluginAsync, {});
} }
for (const route of Object.values(routes)) { for (const route of Object.values(routes)) {
void fastify.register(route as any, {}); void fastify.register(route as FastifyPluginAsync, {});
} }
void fastify.register(fastifyFormBody, {}); void fastify.register(fastifyFormBody, {});

View file

@ -12,10 +12,12 @@ export const WhoAmIRes = Type.Union([
export type WhoAmIRes = Static<typeof WhoAmIRes>; export type WhoAmIRes = Static<typeof WhoAmIRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.put( fastify.put(
'/api/auth/disableOtp', '/api/auth/disableOtp',
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } }, { schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
async function(req, _res) { async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'disableOtp.failure.generic');} if (isNullish(req.authUser)) {return makeResponse('failure', 'disableOtp.failure.generic');}
this.db.deleteUserOtpSecret(req.authUser.id); this.db.deleteUserOtpSecret(req.authUser.id);
return makeResponse('success', 'disableOtp.success'); return makeResponse('success', 'disableOtp.success');

View file

@ -13,10 +13,12 @@ export const WhoAmIRes = Type.Union([
export type WhoAmIRes = Static<typeof WhoAmIRes>; export type WhoAmIRes = Static<typeof WhoAmIRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.put( fastify.put(
'/api/auth/enableOtp', '/api/auth/enableOtp',
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } }, { schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
async function(req, _res) { async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'enableOtp.failure.noUser');} if (isNullish(req.authUser)) {return makeResponse('failure', 'enableOtp.failure.noUser');}
const otpSecret = this.db.ensureUserOtpSecret(req.authUser!.id); const otpSecret = this.db.ensureUserOtpSecret(req.authUser!.id);
if (isNullish(otpSecret)) {return makeResponse('failure', 'enableOtp.failure.noSecret');} if (isNullish(otpSecret)) {return makeResponse('failure', 'enableOtp.failure.noSecret');}

View file

@ -21,10 +21,12 @@ export const LoginRes = Type.Union([
export type LoginRes = Static<typeof LoginRes>; export type LoginRes = Static<typeof LoginRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: LoginReq; Response: LoginRes }>( fastify.post<{ Body: LoginReq; Response: LoginRes }>(
'/api/auth/login', '/api/auth/login',
{ schema: { body: LoginReq, response: { '2xx': LoginRes } } }, { schema: { body: LoginReq, response: { '2xx': LoginRes } } },
async function(req, _res) { async function(req, _res) {
void _res;
try { try {
const { name, password } = req.body; const { name, password } = req.body;
const user = this.db.getUserFromName(name); const user = this.db.getUserFromName(name);

View file

@ -1,9 +1,11 @@
import { FastifyPluginAsync } from 'fastify'; import { FastifyPluginAsync } from 'fastify';
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post( fastify.post(
'/api/auth/logout', '/api/auth/logout',
async function(_req, res) { async function(_req, res) {
void _req;
return res.clearCookie('token').send('{}'); return res.clearCookie('token').send('{}');
}, },
); );

View file

@ -21,30 +21,35 @@ type OtpRes = Static<typeof OtpRes>;
const OTP_TOKEN_TIMEOUT_SEC = 120; const OTP_TOKEN_TIMEOUT_SEC = 120;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: OtpReq }>( fastify.post<{ Body: OtpReq }>(
'/api/auth/otp', '/api/auth/otp',
{ schema: { body: OtpReq, response: { '2xx': OtpRes } } }, { schema: { body: OtpReq, response: { '2xx': OtpRes } } },
async function(req, _res) { async function(req, _res) {
void _res;
try { try {
const { token, code } = req.body; const { token, code } = req.body;
// lets try to decode+verify the jwt // lets try to decode+verify the jwt
const dJwt = this.jwt.verify<JwtType>(token); const dJwt = this.jwt.verify<JwtType>(token);
// is the jwt a valid `otp` jwt ? // is the jwt a valid `otp` jwt ?
if (dJwt.kind != 'otp') if (dJwt.kind != 'otp') {
// no ? fuck off then // no ? fuck off then
{return makeResponse('failed', 'otp.failed.invalid');} return makeResponse('failed', 'otp.failed.invalid');
}
// is it too old ? // is it too old ?
if (dJwt.createdAt + OTP_TOKEN_TIMEOUT_SEC * 1000 < Date.now()) if (dJwt.createdAt + OTP_TOKEN_TIMEOUT_SEC * 1000 < Date.now()) {
// yes ? fuck off then, redo the password // yes ? fuck off then, redo the password
{return makeResponse('failed', 'otp.failed.timeout');} return makeResponse('failed', 'otp.failed.timeout');
}
// get the Otp sercret from the db // get the Otp sercret from the db
const user = this.db.getUserFromName(dJwt.who); const user = this.db.getUserFromName(dJwt.who);
if (isNullish(user?.otp)) if (isNullish(user?.otp)) {
// oops, either no user, or user without otpSecret // oops, either no user, or user without otpSecret
// fuck off // fuck off
{return makeResponse('failed', 'otp.failed.noSecret');} return makeResponse('failed', 'otp.failed.noSecret');
}
// good lets now verify the token you gave us is the correct one... // good lets now verify the token you gave us is the correct one...
const otpHandle = new Otp({ secret: user.otp }); const otpHandle = new Otp({ secret: user.otp });
@ -58,10 +63,11 @@ const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
]; ];
// checking if any of the array match // checking if any of the array match
if (tokens.some((c) => c === code)) if (tokens.some((c) => c === code)) {
// they do ! // they do !
// gg you are now logged in ! // gg you are now logged in !
{return makeResponse('success', 'otp.success', { token: this.signJwt('auth', dJwt.who) });} return makeResponse('success', 'otp.success', { token: this.signJwt('auth', dJwt.who) });
}
} }
catch { catch {
return makeResponse('failed', 'otp.failed.generic'); return makeResponse('failed', 'otp.failed.generic');

View file

@ -3,7 +3,7 @@ import { FastifyPluginAsync } from 'fastify';
import { Static, Type } from '@sinclair/typebox'; import { Static, Type } from '@sinclair/typebox';
import { typeResponse, makeResponse, isNullish } from '@shared/utils'; import { typeResponse, makeResponse, isNullish } from '@shared/utils';
const USERNAME_CHECK: RegExp = /^[a-zA-Z\_0-9]+$/; const USERNAME_CHECK: RegExp = /^[a-zA-Z_0-9]+$/;
const SignInReq = Type.Object({ const SignInReq = Type.Object({
name: Type.String(), name: Type.String(),
@ -28,11 +28,13 @@ const SignInRes = Type.Union([
type SignInRes = Static<typeof SignInRes>; type SignInRes = Static<typeof SignInRes>;
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: SignInReq }>( fastify.post<{ Body: SignInReq }>(
'/api/auth/signin', '/api/auth/signin',
{ schema: { body: SignInReq, response: { '200': SignInRes, '5xx': Type.Object({}) } } }, { schema: { body: SignInReq, response: { '200': SignInRes, '5xx': Type.Object({}) } } },
async function(req, res) { async function(req, _res) {
void _res;
const { name, password } = req.body; const { name, password } = req.body;
if (name.length < 4) {return makeResponse('failed', 'signin.failed.username.tooshort');} if (name.length < 4) {return makeResponse('failed', 'signin.failed.username.tooshort');}

View file

@ -14,10 +14,12 @@ export const StatusOtpRes = Type.Union([
export type StatusOtpRes = Static<typeof StatusOtpRes>; export type StatusOtpRes = Static<typeof StatusOtpRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.get( fastify.get(
'/api/auth/statusOtp', '/api/auth/statusOtp',
{ schema: { response: { '2xx': StatusOtpRes } }, config: { requireAuth: true } }, { schema: { response: { '2xx': StatusOtpRes } }, config: { requireAuth: true } },
async function(req, _res) { async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'statusOtp.failure.generic');} if (isNullish(req.authUser)) {return makeResponse('failure', 'statusOtp.failure.generic');}
const otpSecret = this.db.getUserOtpSecret(req.authUser.id); const otpSecret = this.db.getUserOtpSecret(req.authUser.id);
if (isNullish(otpSecret)) {return makeResponse('success', 'statusOtp.success.disabled');} if (isNullish(otpSecret)) {return makeResponse('success', 'statusOtp.success.disabled');}

View file

@ -12,10 +12,12 @@ export const WhoAmIRes = Type.Union([
export type WhoAmIRes = Static<typeof WhoAmIRes>; export type WhoAmIRes = Static<typeof WhoAmIRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.get( fastify.get(
'/api/auth/whoami', '/api/auth/whoami',
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } }, { schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
async function(req, _res) { async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'whoami.failure.generic');} if (isNullish(req.authUser)) {return makeResponse('failure', 'whoami.failure.generic');}
return makeResponse('success', 'whoami.success', { name: req.authUser.name }); return makeResponse('success', 'whoami.success', { name: req.authUser.name });
}, },

View file

@ -4,10 +4,11 @@ import fastifyMultipart from '@fastify/multipart';
import { mkdir } from 'node:fs/promises'; import { mkdir } from 'node:fs/promises';
import fp from 'fastify-plugin'; import fp from 'fastify-plugin';
import * as db from '@shared/database'; import * as db from '@shared/database';
import { authPlugin, jwtPlugin } from '@shared/auth';
// @ts-except-error: import.meta.glob is a vite thing. Typescript doesn't know this... // @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true }); const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
// @ts-except-error: import.meta.glob is a vite thing. Typescript doesn't know this... // @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const routes = import.meta.glob('./routes/**/*.ts', { eager: true }); const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
@ -20,20 +21,23 @@ declare module 'fastify' {
const app: FastifyPluginAsync = async ( const app: FastifyPluginAsync = async (
fastify, fastify,
opts, _opts,
): Promise<void> => { ): Promise<void> => {
void _opts;
// Place here your custom code! // Place here your custom code!
for (const plugin of Object.values(plugins)) { for (const plugin of Object.values(plugins)) {
void fastify.register(plugin as any, {}); void fastify.register(plugin as FastifyPluginAsync, {});
} }
for (const route of Object.values(routes)) { for (const route of Object.values(routes)) {
void fastify.register(route as any, {}); void fastify.register(route as FastifyPluginAsync, {});
} }
await fastify.register(db.useDatabase as any, {}); await fastify.register(db.useDatabase as FastifyPluginAsync, {});
await fastify.register(authPlugin as FastifyPluginAsync, {});
await fastify.register(jwtPlugin as FastifyPluginAsync, {});
void fastify.register(fastifyFormBody, {}); void fastify.register(fastifyFormBody, {});
void fastify.register(fastifyMultipart, {}); void fastify.register(fastifyMultipart, {});
console.log(fastify.db.getUser(1));
// The use of fastify-plugin is required to be able // The use of fastify-plugin is required to be able
// to export the decorators to the outer scope // to export the decorators to the outer scope

View file

@ -5,21 +5,22 @@ import sharp from 'sharp';
import rawBody from 'raw-body'; import rawBody from 'raw-body';
import { isNullish } from '@shared/utils'; import { isNullish } from '@shared/utils';
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
// await fastify.register(authMethod, {}); // await fastify.register(authMethod, {});
// here we register plugins that will be active for the current fastify instance (aka everything in this function) // here we register plugins that will be active for the current fastify instance (aka everything in this function)
// we register a route handler for: `/<USERID_HERE>` // we register a route handler for: `/<USERID_HERE>`
// it sets some configuration options, and set the actual function that will handle the request // it sets some configuration options, and set the actual function that will handle the request
fastify.addContentTypeParser('*', function(request, payload, done: any) { fastify.addContentTypeParser('*', function(request, payload, done) {
done(); done(null);
}); });
fastify.post('/:userid', async function(request, reply) { fastify.post<{ Params: { userid: string } }>('/:userid', async function(request, reply) {
const buffer = await rawBody(request.raw); const buffer = await rawBody(request.raw);
// this is how we get the `:userid` part of things // this is how we get the `:userid` part of things
const userid: string | undefined = (request.params as any)['userid']; const userid: string | undefined = (request.params)['userid'];
if (isNullish(userid)) { if (isNullish(userid)) {
return await reply.code(403); return await reply.code(403);
} }
@ -38,10 +39,10 @@ const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
await image_file.write(data); await image_file.write(data);
await image_file.close(); await image_file.close();
} }
catch (e: any) { catch (e) {
fastify.log.error(`Error: ${e}`); fastify.log.error(`Error: ${e}`);
reply.code(400); reply.code(400);
return { status: 'error', message: e.toString() }; return { status: 'error' };
} }
}); });
}; };

1469
src/package-lock.json generated

File diff suppressed because it is too large Load diff