mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-01 13:31:04 -05:00
feat: add request note decorator
This extracts the note inserted with the get note interceptor into the request to be used by the controller service. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
470b32ed5e
commit
5a45ff2d0b
1 changed files with 32 additions and 0 deletions
32
src/api/utils/request-note.decorator.ts
Normal file
32
src/api/utils/request-note.decorator.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
import {
|
||||||
|
createParamDecorator,
|
||||||
|
ExecutionContext,
|
||||||
|
InternalServerErrorException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { Request } from 'express';
|
||||||
|
|
||||||
|
import { Note } from '../../notes/note.entity';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts the {@link Note} object from a request
|
||||||
|
*
|
||||||
|
* Will throw an {@link InternalServerErrorException} if no note is present
|
||||||
|
*/
|
||||||
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
|
export const RequestNote = createParamDecorator(
|
||||||
|
(data: unknown, ctx: ExecutionContext) => {
|
||||||
|
const request: Request & { note: Note } = ctx.switchToHttp().getRequest();
|
||||||
|
if (!request.note) {
|
||||||
|
// We should have a note here, otherwise something is wrong
|
||||||
|
throw new InternalServerErrorException(
|
||||||
|
'Request is missing a note object',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return request.note;
|
||||||
|
},
|
||||||
|
);
|
Loading…
Reference in a new issue