]>
Commit | Line | Data |
---|---|---|
69818c93 C |
1 | import * as express from 'express' |
2 | ||
6fcd19ba C |
3 | import { pseudoRandomBytesPromise } from './core-utils' |
4 | import { ResultList } from '../../shared' | |
cbe2f7c3 | 5 | |
69818c93 | 6 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { |
a6fd2b30 C |
7 | res.type('json').status(400).end() |
8 | } | |
9 | ||
6fcd19ba C |
10 | function generateRandomString (size: number) { |
11 | return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex')) | |
e4c87ec2 C |
12 | } |
13 | ||
154898b0 | 14 | interface FormatableToJSON { |
9e580054 | 15 | toFormatedJSON () |
154898b0 C |
16 | } |
17 | ||
18 | function getFormatedObjects<U, T extends FormatableToJSON> (objects: T[], objectsTotal: number) { | |
19 | const formatedObjects: U[] = [] | |
55fa55a9 | 20 | |
075f16ca | 21 | objects.forEach(object => { |
55fa55a9 C |
22 | formatedObjects.push(object.toFormatedJSON()) |
23 | }) | |
24 | ||
6fcd19ba | 25 | const res: ResultList<U> = { |
55fa55a9 C |
26 | total: objectsTotal, |
27 | data: formatedObjects | |
28 | } | |
6fcd19ba C |
29 | |
30 | return res | |
55fa55a9 C |
31 | } |
32 | ||
9f10b292 | 33 | // --------------------------------------------------------------------------- |
c45f7f84 | 34 | |
65fcc311 C |
35 | export { |
36 | badRequest, | |
65fcc311 | 37 | generateRandomString, |
1840c2f7 | 38 | getFormatedObjects |
65fcc311 | 39 | } |