aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r--server/helpers/utils.ts24
1 files changed, 7 insertions, 17 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index bbf135fa1..e99a48393 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,25 +1,14 @@
1import * as express from 'express' 1import * as express from 'express'
2 2
3import { pseudoRandomBytes } from 'crypto' 3import { pseudoRandomBytesPromise } from './core-utils'
4 4import { ResultList } from '../../shared'
5import { logger } from './logger'
6 5
7function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { 6function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
8 res.type('json').status(400).end() 7 res.type('json').status(400).end()
9} 8}
10 9
11function generateRandomString (size: number, callback: (err: Error, randomString?: string) => void) { 10function generateRandomString (size: number) {
12 pseudoRandomBytes(size, function (err, raw) { 11 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
13 if (err) return callback(err)
14
15 callback(null, raw.toString('hex'))
16 })
17}
18
19function createEmptyCallback () {
20 return function (err) {
21 if (err) logger.error('Error in empty callback.', { error: err })
22 }
23} 12}
24 13
25interface FormatableToJSON { 14interface FormatableToJSON {
@@ -33,17 +22,18 @@ function getFormatedObjects<U, T extends FormatableToJSON> (objects: T[], object
33 formatedObjects.push(object.toFormatedJSON()) 22 formatedObjects.push(object.toFormatedJSON())
34 }) 23 })
35 24
36 return { 25 const res: ResultList<U> = {
37 total: objectsTotal, 26 total: objectsTotal,
38 data: formatedObjects 27 data: formatedObjects
39 } 28 }
29
30 return res
40} 31}
41 32
42// --------------------------------------------------------------------------- 33// ---------------------------------------------------------------------------
43 34
44export { 35export {
45 badRequest, 36 badRequest,
46 createEmptyCallback,
47 generateRandomString, 37 generateRandomString,
48 getFormatedObjects 38 getFormatedObjects
49} 39}