2019-02-11 02:10:11 +05:30
|
|
|
import {
|
2019-06-27 02:01:36 +05:30
|
|
|
HomeServerError,
|
|
|
|
RequestAbortError,
|
2019-03-08 16:56:59 +05:30
|
|
|
NetworkError
|
2019-02-11 02:10:11 +05:30
|
|
|
} from "./error.js";
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2019-02-05 03:56:24 +05:30
|
|
|
class RequestWrapper {
|
2019-06-27 02:01:36 +05:30
|
|
|
constructor(promise, controller) {
|
2019-06-26 23:19:49 +05:30
|
|
|
if (!controller) {
|
|
|
|
const abortPromise = new Promise((_, reject) => {
|
|
|
|
this._controller = {
|
|
|
|
abort() {
|
|
|
|
const err = new Error("fetch request aborted");
|
|
|
|
err.name = "AbortError";
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
this._promise = Promise.race([promise, abortPromise]);
|
|
|
|
} else {
|
|
|
|
this._promise = promise;
|
|
|
|
this._controller = controller;
|
|
|
|
}
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
abort() {
|
|
|
|
this._controller.abort();
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
response() {
|
|
|
|
return this._promise;
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
}
|
|
|
|
|
2019-07-27 01:33:57 +05:30
|
|
|
// todo: everywhere here, encode params in the url that could have slashes ... mainly event ids?
|
2019-02-07 05:55:12 +05:30
|
|
|
export default class HomeServerApi {
|
2019-06-27 02:01:36 +05:30
|
|
|
constructor(homeserver, accessToken) {
|
2019-03-09 00:33:47 +05:30
|
|
|
// store these both in a closure somehow so it's harder to get at in case of XSS?
|
|
|
|
// one could change the homeserver as well so the token gets sent there, so both must be protected from read/write
|
|
|
|
this._homeserver = homeserver;
|
2019-06-27 02:01:36 +05:30
|
|
|
this._accessToken = accessToken;
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_url(csPath) {
|
|
|
|
return `${this._homeserver}/_matrix/client/r0${csPath}`;
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_request(method, csPath, queryParams = {}, body) {
|
|
|
|
const queryString = Object.entries(queryParams)
|
|
|
|
.filter(([, value]) => value !== undefined)
|
|
|
|
.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`)
|
|
|
|
.join("&");
|
|
|
|
const url = this._url(`${csPath}?${queryString}`);
|
|
|
|
let bodyString;
|
|
|
|
const headers = new Headers();
|
|
|
|
if (this._accessToken) {
|
|
|
|
headers.append("Authorization", `Bearer ${this._accessToken}`);
|
|
|
|
}
|
|
|
|
headers.append("Accept", "application/json");
|
|
|
|
if (body) {
|
|
|
|
headers.append("Content-Type", "application/json");
|
|
|
|
bodyString = JSON.stringify(body);
|
|
|
|
}
|
|
|
|
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
|
|
|
// TODO: set authenticated headers with second arguments, cache them
|
|
|
|
let promise = fetch(url, {
|
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body: bodyString,
|
2019-09-15 15:53:08 +05:30
|
|
|
signal: controller && controller.signal,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "omit",
|
|
|
|
referrer: "no-referrer",
|
|
|
|
cache: "no-cache",
|
2019-06-27 02:01:36 +05:30
|
|
|
});
|
|
|
|
promise = promise.then(async (response) => {
|
|
|
|
if (response.ok) {
|
|
|
|
return await response.json();
|
|
|
|
} else {
|
|
|
|
switch (response.status) {
|
|
|
|
default:
|
|
|
|
throw new HomeServerError(method, url, await response.json())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, err => {
|
2019-03-08 16:56:59 +05:30
|
|
|
if (err.name === "AbortError") {
|
|
|
|
throw new RequestAbortError();
|
|
|
|
} else if (err instanceof TypeError) {
|
|
|
|
// Network errors are reported as TypeErrors, see
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful
|
|
|
|
// this can either mean user is offline, server is offline, or a CORS error (server misconfiguration).
|
|
|
|
//
|
|
|
|
// One could check navigator.onLine to rule out the first
|
|
|
|
// but the 2 later ones are indistinguishable from javascript.
|
2019-09-15 18:01:50 +05:30
|
|
|
throw new NetworkError(`${method} ${url}: ${err.message}`);
|
2019-03-08 16:56:59 +05:30
|
|
|
}
|
2019-06-27 02:01:36 +05:30
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
return new RequestWrapper(promise, controller);
|
|
|
|
}
|
2019-02-05 03:56:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_post(csPath, queryParams, body) {
|
|
|
|
return this._request("POST", csPath, queryParams, body);
|
|
|
|
}
|
2019-02-05 03:56:24 +05:30
|
|
|
|
2019-07-27 01:33:57 +05:30
|
|
|
_put(csPath, queryParams, body) {
|
|
|
|
return this._request("PUT", csPath, queryParams, body);
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_get(csPath, queryParams, body) {
|
|
|
|
return this._request("GET", csPath, queryParams, body);
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
sync(since, filter, timeout) {
|
|
|
|
return this._get("/sync", {since, timeout, filter});
|
|
|
|
}
|
2019-02-05 03:56:24 +05:30
|
|
|
|
2019-03-09 05:11:06 +05:30
|
|
|
// params is from, dir and optionally to, limit, filter.
|
|
|
|
messages(roomId, params) {
|
|
|
|
return this._get(`/rooms/${roomId}/messages`, params);
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:33:57 +05:30
|
|
|
send(roomId, eventType, txnId, content) {
|
|
|
|
return this._put(`/rooms/${roomId}/send/${eventType}/${txnId}`, {}, content);
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
passwordLogin(username, password) {
|
2019-02-05 03:56:24 +05:30
|
|
|
return this._post("/login", undefined, {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"identifier": {
|
|
|
|
"type": "m.id.user",
|
|
|
|
"user": username
|
|
|
|
},
|
|
|
|
"password": password
|
|
|
|
});
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2019-03-08 16:56:59 +05:30
|
|
|
}
|