improve fetching of endpoint arguments

including support for route parameters (e.g. '/v2/note/:noteId' giving us a 'noteId' value)

Co-authored-by: Johann150 <johann.galle@protonmail.com>
This commit is contained in:
Andy 2022-09-25 21:12:50 +02:00 committed by Johann150
parent 8276bd3bdc
commit 7685b92511
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -5,12 +5,23 @@ import authenticate, { AuthenticationError } from './authenticate.js';
import call from './call.js'; import call from './call.js';
import { ApiError } from './error.js'; import { ApiError } from './error.js';
function getRequestArguments(ctx: Koa.Context): Record<string, any> {
const args = {
...(ctx.params || {}),
...ctx.query,
...(ctx.request.body || {}),
};
// For security reasons, we drop the i parameter if it's a GET request
if (ctx.method === 'GET') {
delete args['i'];
}
return args;
}
export async function handler(endpoint: IEndpoint, ctx: Koa.Context): Promise<void> { export async function handler(endpoint: IEndpoint, ctx: Koa.Context): Promise<void> {
const body = ctx.is('multipart/form-data') const body = getRequestArguments(ctx);
? (ctx.request as any).body
: ctx.method === 'GET'
? ctx.query
: ctx.request.body;
// Authentication // Authentication
// for GET requests, do not even pass on the body parameter as it is considered unsafe // for GET requests, do not even pass on the body parameter as it is considered unsafe