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