2016-01-05 01:13:48 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
|
|
|
(C) 2016 GNUnet e.V.
|
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2016-07-07 17:59:29 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2016-01-05 01:13:48 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-05 15:42:46 +01:00
|
|
|
/**
|
|
|
|
* Helpers for doing XMLHttpRequest-s that are based on ES6 promises.
|
2017-05-24 16:14:23 +02:00
|
|
|
* Allows for easy mocking for test cases.
|
2016-01-05 15:42:46 +01:00
|
|
|
*/
|
|
|
|
|
2017-05-24 16:14:23 +02:00
|
|
|
/**
|
|
|
|
* An HTTP response that is returned by all request methods of this library.
|
|
|
|
*/
|
2016-01-10 20:07:42 +01:00
|
|
|
export interface HttpResponse {
|
2016-01-05 01:13:48 +01:00
|
|
|
status: number;
|
2019-07-31 01:33:56 +02:00
|
|
|
responseJson: object & any;
|
2016-01-05 01:13:48 +01:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:14:23 +02:00
|
|
|
/**
|
|
|
|
* The request library is bundled into an interface to make mocking easy.
|
|
|
|
*/
|
2016-11-13 08:16:12 +01:00
|
|
|
export interface HttpRequestLibrary {
|
2017-04-20 03:09:25 +02:00
|
|
|
get(url: string): Promise<HttpResponse>;
|
2016-11-13 08:16:12 +01:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postJson(url: string, body: any): Promise<HttpResponse>;
|
2016-11-13 08:16:12 +01:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:14:23 +02:00
|
|
|
/**
|
|
|
|
* An implementation of the [[HttpRequestLibrary]] using the
|
|
|
|
* browser's XMLHttpRequest.
|
|
|
|
*/
|
2019-07-21 23:50:10 +02:00
|
|
|
export class BrowserHttpLib implements HttpRequestLibrary {
|
2019-08-29 23:12:55 +02:00
|
|
|
private req(
|
|
|
|
method: string,
|
|
|
|
url: string,
|
|
|
|
options?: any,
|
|
|
|
): Promise<HttpResponse> {
|
2017-04-20 03:09:25 +02:00
|
|
|
return new Promise<HttpResponse>((resolve, reject) => {
|
2017-05-28 01:10:54 +02:00
|
|
|
const myRequest = new XMLHttpRequest();
|
2017-04-20 03:09:25 +02:00
|
|
|
myRequest.open(method, url);
|
2016-01-06 15:58:18 +01:00
|
|
|
if (options && options.req) {
|
|
|
|
myRequest.send(options.req);
|
|
|
|
} else {
|
|
|
|
myRequest.send();
|
2016-01-05 01:13:48 +01:00
|
|
|
}
|
2019-08-29 23:12:55 +02:00
|
|
|
|
|
|
|
myRequest.onerror = e => {
|
|
|
|
console.error("http request error");
|
|
|
|
reject(Error("could not make XMLHttpRequest"));
|
|
|
|
};
|
|
|
|
|
|
|
|
myRequest.addEventListener("readystatechange", e => {
|
2017-05-28 01:10:54 +02:00
|
|
|
if (myRequest.readyState === XMLHttpRequest.DONE) {
|
2019-08-29 23:12:55 +02:00
|
|
|
if (myRequest.status === 0) {
|
|
|
|
reject(Error("HTTP Request failed (status code 0, maybe URI scheme is wrong?)"))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (myRequest.status != 200) {
|
|
|
|
reject(
|
|
|
|
Error(
|
|
|
|
`HTTP Response with unexpected status code ${myRequest.status}: ${myRequest.statusText}`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let responseJson;
|
|
|
|
try {
|
|
|
|
responseJson = JSON.parse(myRequest.responseText);
|
|
|
|
} catch (e) {
|
|
|
|
reject(Error("Invalid JSON from HTTP response"));
|
|
|
|
return;
|
|
|
|
}
|
2019-07-31 01:33:56 +02:00
|
|
|
if (responseJson === null || typeof responseJson !== "object") {
|
|
|
|
reject(Error("Invalid JSON from HTTP response"));
|
2019-08-29 23:12:55 +02:00
|
|
|
return;
|
2019-07-31 01:33:56 +02:00
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const resp = {
|
2019-07-31 01:33:56 +02:00
|
|
|
responseJson: responseJson,
|
2016-01-06 15:58:18 +01:00
|
|
|
status: myRequest.status,
|
|
|
|
};
|
|
|
|
resolve(resp);
|
|
|
|
}
|
|
|
|
});
|
2016-01-05 01:13:48 +01:00
|
|
|
});
|
2016-01-06 15:58:18 +01:00
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
get(url: string) {
|
2016-01-06 15:58:18 +01:00
|
|
|
return this.req("get", url);
|
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postJson(url: string, body: any) {
|
2019-08-29 23:12:55 +02:00
|
|
|
return this.req("post", url, { req: JSON.stringify(body) });
|
2016-01-06 15:58:18 +01:00
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postForm(url: string, form: any) {
|
2019-08-29 23:12:55 +02:00
|
|
|
return this.req("post", url, { req: form });
|
2016-01-06 15:58:18 +01:00
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
}
|