]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93
C
1import * as express from 'express'
2
6fcd19ba
C
3import { pseudoRandomBytesPromise } from './core-utils'
4import { ResultList } from '../../shared'
cbe2f7c3 5
69818c93 6function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
a6fd2b30
C
7 res.type('json').status(400).end()
8}
9
6fcd19ba
C
10function generateRandomString (size: number) {
11 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
e4c87ec2
C
12}
13
154898b0 14interface FormatableToJSON {
9e580054 15 toFormatedJSON ()
154898b0
C
16}
17
18function getFormatedObjects<U, T extends FormatableToJSON> (objects: T[], objectsTotal: number) {
19 const formatedObjects: U[] = []
55fa55a9
C
20
21 objects.forEach(function (object) {
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
35export {
36 badRequest,
65fcc311 37 generateRandomString,
1840c2f7 38 getFormatedObjects
65fcc311 39}