1
0
Fork 0
peertube/server/helpers/requests.ts

26 lines
872 B
TypeScript
Raw Normal View History

import * as Promise from 'bluebird'
2017-11-10 08:34:45 -05:00
import { createWriteStream } from 'fs'
2017-11-10 11:27:49 -05:00
import * as request from 'request'
2017-11-09 11:51:58 -05:00
function doRequest (requestOptions: request.CoreOptions & request.UriOptions) {
return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
})
}
2016-02-05 12:03:20 -05:00
2017-11-10 08:34:45 -05:00
function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.UriOptions, destPath: string) {
return new Promise<request.RequestResponse>((res, rej) => {
request(requestOptions)
.on('response', response => res(response as request.RequestResponse))
.on('error', err => rej(err))
.pipe(createWriteStream(destPath))
})
}
// ---------------------------------------------------------------------------
2016-02-05 12:03:20 -05:00
2017-05-15 16:22:03 -04:00
export {
2017-11-09 11:51:58 -05:00
doRequest,
2017-11-10 11:27:49 -05:00
doRequestAndSaveToFile
2017-05-15 16:22:03 -04:00
}