vector-im-hydrogen-web/src/matrix/hs-api.js

112 lines
3.5 KiB
JavaScript
Raw Normal View History

import {
2019-06-26 22:31:36 +02:00
HomeServerError,
} from "./error.js";
2019-02-10 21:25:29 +01:00
2019-02-04 23:26:24 +01:00
class RequestWrapper {
2019-12-23 14:28:27 +01:00
constructor(method, url, requestResult) {
this._requestResult = requestResult;
this._promise = this._requestResult.response().then(response => {
// ok?
if (response.status >= 200 && response.status < 300) {
return response.body;
} else {
switch (response.status) {
default:
throw new HomeServerError(method, url, response.body);
}
}
});
2019-06-26 22:31:36 +02:00
}
2018-12-21 14:35:24 +01:00
2019-06-26 22:31:36 +02:00
abort() {
2019-12-23 14:28:27 +01:00
return this._requestResult.abort();
2019-06-26 22:31:36 +02:00
}
2018-12-21 14:35:24 +01:00
2019-06-26 22:31:36 +02:00
response() {
return this._promise;
}
2018-12-21 14:35:24 +01:00
}
2019-02-07 01:25:12 +01:00
export default class HomeServerApi {
2019-12-23 14:28:27 +01:00
constructor({homeServer, accessToken, request}) {
2019-03-08 20:03:47 +01:00
// 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
2019-12-23 14:28:27 +01:00
this._homeserver = homeServer;
2019-06-26 22:31:36 +02:00
this._accessToken = accessToken;
2019-12-23 14:28:27 +01:00
this._requestFn = request;
2019-06-26 22:31:36 +02:00
}
2018-12-21 14:35:24 +01:00
2019-06-26 22:31:36 +02:00
_url(csPath) {
return `${this._homeserver}/_matrix/client/r0${csPath}`;
}
2018-12-21 14:35:24 +01:00
2019-06-26 22:31:36 +02:00
_request(method, csPath, queryParams = {}, body) {
const queryString = Object.entries(queryParams)
.filter(([, value]) => value !== undefined)
2019-10-12 20:24:09 +02:00
.map(([name, value]) => {
if (typeof value === "object") {
value = JSON.stringify(value);
}
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
})
2019-06-26 22:31:36 +02:00
.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);
}
2019-12-23 14:28:27 +01:00
const requestResult = this._requestFn(url, {
2019-06-26 22:31:36 +02:00
method,
headers,
body: bodyString,
});
2019-12-23 14:28:27 +01:00
return new RequestWrapper(method, url, requestResult);
2019-06-26 22:31:36 +02:00
}
2019-02-04 23:26:24 +01:00
2019-06-26 22:31:36 +02:00
_post(csPath, queryParams, body) {
return this._request("POST", csPath, queryParams, body);
}
2019-02-04 23:26:24 +01:00
2019-07-26 22:03:57 +02:00
_put(csPath, queryParams, body) {
return this._request("PUT", csPath, queryParams, body);
}
2019-06-26 22:31:36 +02:00
_get(csPath, queryParams, body) {
return this._request("GET", csPath, queryParams, body);
}
2018-12-21 14:35:24 +01:00
2019-06-26 22:31:36 +02:00
sync(since, filter, timeout) {
return this._get("/sync", {since, timeout, filter});
}
2019-02-04 23:26:24 +01:00
2019-03-09 00:41:06 +01:00
// params is from, dir and optionally to, limit, filter.
messages(roomId, params) {
2019-10-12 20:24:09 +02:00
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params);
2019-03-09 00:41:06 +01:00
}
2019-07-26 22:03:57 +02:00
send(roomId, eventType, txnId, content) {
2019-10-12 20:24:09 +02:00
return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content);
2019-07-26 22:03:57 +02:00
}
2019-06-26 22:31:36 +02:00
passwordLogin(username, password) {
2019-02-04 23:26:24 +01:00
return this._post("/login", undefined, {
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": username
},
"password": password
});
2019-06-26 22:31:36 +02:00
}
2019-10-12 20:24:09 +02:00
createFilter(userId, filter) {
return this._post(`/user/${encodeURIComponent(userId)}/filter`, undefined, filter);
}
2019-03-08 12:26:59 +01:00
}