diff options
Diffstat (limited to 'server/helpers/express-utils.ts')
-rw-r--r-- | server/helpers/express-utils.ts | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 010c6961a..e3ff93cdd 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts | |||
@@ -8,6 +8,7 @@ import { isArray } from './custom-validators/misc' | |||
8 | import { logger } from './logger' | 8 | import { logger } from './logger' |
9 | import { deleteFileAndCatch, generateRandomString } from './utils' | 9 | import { deleteFileAndCatch, generateRandomString } from './utils' |
10 | import { getExtFromMimetype } from './video' | 10 | import { getExtFromMimetype } from './video' |
11 | import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details' | ||
11 | 12 | ||
12 | function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { | 13 | function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { |
13 | if (paramNSFW === 'true') return true | 14 | if (paramNSFW === 'true') return true |
@@ -125,6 +126,34 @@ function getCountVideos (req: express.Request) { | |||
125 | return req.query.skipCount !== true | 126 | return req.query.skipCount !== true |
126 | } | 127 | } |
127 | 128 | ||
129 | // helpers added in server.ts and used in subsequent controllers used | ||
130 | const apiResponseHelpers = (req, res: express.Response, next = null) => { | ||
131 | res.fail = (options) => { | ||
132 | const { data, status, message, title, type, docs, instance } = { | ||
133 | data: null, | ||
134 | status: HttpStatusCode.BAD_REQUEST_400, | ||
135 | ...options | ||
136 | } | ||
137 | |||
138 | const extension = new ProblemDocumentExtension({ | ||
139 | ...data, | ||
140 | docs: docs || res.docs | ||
141 | }) | ||
142 | |||
143 | res.status(status) | ||
144 | res.setHeader('Content-Type', 'application/problem+json') | ||
145 | res.json(new ProblemDocument({ | ||
146 | status, | ||
147 | title, | ||
148 | instance, | ||
149 | type: type && '' + type, | ||
150 | detail: message | ||
151 | }, extension)) | ||
152 | } | ||
153 | |||
154 | if (next !== null) next() | ||
155 | } | ||
156 | |||
128 | // --------------------------------------------------------------------------- | 157 | // --------------------------------------------------------------------------- |
129 | 158 | ||
130 | export { | 159 | export { |
@@ -134,5 +163,6 @@ export { | |||
134 | badRequest, | 163 | badRequest, |
135 | createReqFiles, | 164 | createReqFiles, |
136 | cleanUpReqFiles, | 165 | cleanUpReqFiles, |
137 | getCountVideos | 166 | getCountVideos, |
167 | apiResponseHelpers | ||
138 | } | 168 | } |