Jeff Cross 21568106b1 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
2015-06-09 10:00:04 -07:00

40 lines
1.6 KiB
TypeScript

import {RequestMethods, RequestModesOpts, RequestCredentialsOpts} from './enums';
import {URLSearchParams} from './url_search_params';
import {RequestOptions, Request as IRequest} from './interfaces';
import {Headers} from './headers';
import {BaseException, RegExpWrapper} from 'angular2/src/facade/lang';
// TODO(jeffbcross): implement body accessors
export class Request implements IRequest {
method: RequestMethods;
mode: RequestModesOpts;
credentials: RequestCredentialsOpts;
headers: Headers;
/*
* Non-Standard Properties
*/
// This property deviates from the standard. Body can be set in constructor, but is only
// accessible
// via json(), text(), arrayBuffer(), and blob() accessors, which also change the request's state
// to "used".
private body: URLSearchParams | FormData | Blob | string;
constructor(public url: string, {body, method = RequestMethods.GET, mode = RequestModesOpts.Cors,
credentials = RequestCredentialsOpts.Omit,
headers = new Headers()}: RequestOptions = {}) {
this.body = body;
// Defaults to 'GET', consistent with browser
this.method = method;
// Defaults to 'cors', consistent with browser
// TODO(jeffbcross): implement behavior
this.mode = mode;
// Defaults to 'omit', consistent with browser
// TODO(jeffbcross): implement behavior
this.credentials = credentials;
// Defaults to empty headers object, consistent with browser
this.headers = headers;
}
text(): String { return this.body ? this.body.toString() : ''; }
}