]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/utils.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
... / ...
CommitLineData
1import * as express from 'express'
2
3import { pseudoRandomBytesPromise } from './core-utils'
4import { ResultList } from '../../shared'
5
6function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
7 res.type('json').status(400).end()
8}
9
10function generateRandomString (size: number) {
11 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
12}
13
14interface FormatableToJSON {
15 toFormatedJSON ()
16}
17
18function 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
35export {
36 badRequest,
37 generateRandomString,
38 getFormatedObjects
39}