feat(http): add basic http service
This implementation only works in JavaScript, while the Observable transpilation story gets worked out. Right now, the service just makes a simple request, and returns an Observable of Response. Additional functionality will be captured in separate issues. Fixes #2028
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import {Response as IResponse, ResponseOptions} from './interfaces';
|
||||
import {ResponseTypes} from './enums';
|
||||
import {baseResponseOptions} from './base_response_options';
|
||||
import {BaseException, isJsObject, isString, global} from 'angular2/src/facade/lang';
|
||||
import {Headers} from './headers';
|
||||
|
||||
// TODO: make this injectable so baseResponseOptions can be overridden
|
||||
export class Response implements IResponse {
|
||||
type: ResponseTypes;
|
||||
ok: boolean;
|
||||
url: string;
|
||||
status: number;
|
||||
statusText: string;
|
||||
bytesLoaded: number;
|
||||
totalBytes: number;
|
||||
headers: Headers;
|
||||
constructor(private body?: string | Object | ArrayBuffer | JSON | FormData | Blob,
|
||||
{status, statusText, headers, type, url}: ResponseOptions = baseResponseOptions) {
|
||||
if (isJsObject(headers)) {
|
||||
headers = new Headers(headers);
|
||||
}
|
||||
this.body = body;
|
||||
this.status = status;
|
||||
this.statusText = statusText;
|
||||
this.headers = <Headers>headers;
|
||||
this.type = type;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
blob(): Blob {
|
||||
throw new BaseException('"blob()" method not implemented on Response superclass');
|
||||
}
|
||||
|
||||
json(): JSON {
|
||||
if (isJsObject(this.body)) {
|
||||
return <JSON>this.body;
|
||||
} else if (isString(this.body)) {
|
||||
return global.JSON.parse(<string>this.body);
|
||||
}
|
||||
}
|
||||
|
||||
text(): string { return this.body.toString(); }
|
||||
|
||||
arrayBuffer(): ArrayBuffer {
|
||||
throw new BaseException('"arrayBuffer()" method not implemented on Response superclass');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user