3 constructor(backendUrl) {
4 this.baseUrl = backendUrl;
8 return this.request('GET', path, query);
12 return this.request('POST', path, query);
15 request(method, path, query) {
16 return new Promise((fulfill, reject) => {
17 let xhr = new XMLHttpRequest();
18 xhr.open(method, this.buildUrl(path, query));
20 'load', () => fulfill(JSON.parse(xhr.responseText))
26 buildUrl(path, query) {
27 return this.baseUrl + '/' + path + this.encodeQuery(query)
31 return query ? '?' + Object.keys(query).map((key) => {
32 return encodeURIComponent(key) + '=' +
33 encodeURIComponent(query[key]);
38 export default class {
39 constructor(backendUrl) {
41 this.backend = new Backend(backendUrl);
42 // this.state = { method: 'GET', uri: 'http://en.wikipedia.org/wiki/Philosophy' };
45 method: 'GET', uri: 'http://localhost:3000/stuff.json'
48 this.updateInfo().then((info) => this.updateState({ connection: info }));
52 this.listeners.push(listener);
56 return new Promise((fulfill, request) => {
57 let connection = new HTTP.Connection()
61 delete this.state.response;
62 return this.backend.post('send', this.state).
63 then((response) => this.updateState({ response: response }));
67 this.updateState(changes);
68 this.updateInfo().then((info) => this.updateState({ connection: info }));
72 let uri = this.parseURI(this.state.request.uri);
74 return this.backend.get('uri-info', uri).then((info) => {
75 console.log('info', info);
79 return new Promise((f, _) => f({}));
84 if(typeof(values.response) === 'object') {
85 let response = values.response;
86 if(typeof(response.body) === 'string') {
87 response.body = this.castBody(
89 response.headers ? response.headers['content-type'] : ''
93 this.state = this.recursiveMerge(this.state, values);
94 this.listeners.forEach((listener) => listener(this.state));
98 castBody(body, type) {
99 if(type && type.match(/^application\/json/)) {
101 type: 'application/json',
102 data: JSON.parse(body)
105 return new Blob([body], { type: type });
110 return this.backend.get('uri-info', uri);
111 // return new Promise((f, _) => f(uri));
115 let match = uri.match(/^([^\:]+)\:(.+)$/), parsed = {};
116 if(! match) return parsed;
117 parsed.scheme = match[1];
118 switch(parsed.scheme) {
122 match = match[2].match(/^\/\/([^\:\/]+)(?:\:(\d+)|)(.*)$/);
124 parsed.host = match[1];
125 parsed.port = parseInt(
126 match[2] || (parsed.scheme === 'https' ? 443 : 80)
128 parsed.path = match[3];
135 recursiveMerge(a, b) {
138 result[key] = a[key];
141 if(typeof(result[key]) === 'object' &&
142 typeof(b[key]) === 'object' &&
143 ((! result[key] instanceof Array) ||
144 result[key] === null || b[key] === null)) {
145 result[key] = this.recursiveMerge(a, b);
147 result[key] = b[key];