improve request service interface

This commit is contained in:
Joao Moreno
2016-08-16 18:16:55 +02:00
parent 492dc4a162
commit ae1d545716
13 changed files with 37 additions and 338 deletions

View File

@@ -108,6 +108,23 @@ export function download(filePath: string, context: IRequestContext): TPromise<v
});
}
export function text(context: IRequestContext): TPromise<string> {
return new Promise((c, e) => {
if (!isSuccess(context)) {
return e('Server returned ' + context.res.statusCode);
}
if (hasNoContent(context)) {
return c(null);
}
let buffer: string[] = [];
context.stream.on('data', d => buffer.push(d));
context.stream.on('end', () => c(buffer.join('')));
context.stream.on('error', e);
});
}
export function json<T>(context: IRequestContext): TPromise<T> {
return new Promise((c, e) => {
if (!isSuccess(context)) {