Skip to content

Commit

Permalink
fix(backend): request.body may be undefined (#9356)
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Dec 19, 2022
1 parent 72e7909 commit c3cb218
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/backend/src/server/api/ApiCallService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ export class ApiCallService implements OnApplicationShutdown {
@bindThis
public handleRequest(
endpoint: IEndpoint & { exec: any },
request: FastifyRequest<{ Body: Record<string, unknown>, Querystring: Record<string, unknown> }>,
request: FastifyRequest<{ Body: Record<string, unknown> | undefined, Querystring: Record<string, unknown> }>,
reply: FastifyReply,
) {
const body = request.method === 'GET'
? request.query
: request.body;

const token = body['i'];
const token = body?.['i'];
if (token != null && typeof token !== 'string') {
reply.code(400);
return;
}
this.authenticateService.authenticate(token).then(([user, app]) => {
this.call(endpoint, user, app, body, null, request).then((res) => {
if (request.method === 'GET' && endpoint.meta.cacheSec && !body['i'] && !user) {
if (request.method === 'GET' && endpoint.meta.cacheSec && !body?.['i'] && !user) {
reply.header('Cache-Control', `public, max-age=${endpoint.meta.cacheSec}`);
}
this.send(reply, res);
Expand Down Expand Up @@ -111,7 +111,7 @@ export class ApiCallService implements OnApplicationShutdown {
for (const [k, v] of Object.entries(multipartData.fields)) {
fields[k] = v.value;
}

const token = fields['i'];
if (token != null && typeof token !== 'string') {
reply.code(400);
Expand Down Expand Up @@ -199,7 +199,7 @@ export class ApiCallService implements OnApplicationShutdown {
name: string;
path: string;
} | null,
request: FastifyRequest<{ Body: Record<string, unknown>, Querystring: Record<string, unknown> }>,
request: FastifyRequest<{ Body: Record<string, unknown> | undefined, Querystring: Record<string, unknown> }>,
) {
const isSecure = user != null && token == null;
const isModerator = user != null && (user.isModerator || user.isAdmin);
Expand Down

0 comments on commit c3cb218

Please sign in to comment.