1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Improve types (#3589)

* Improve types

* Improve coverage
This commit is contained in:
Michael Telatynski
2023-07-12 11:39:33 +01:00
committed by GitHub
parent 01226e41d9
commit 1cb5fff5a1
8 changed files with 319 additions and 246 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -71,6 +71,7 @@ import {
UploadResponse,
HTTPError,
IRequestOpts,
Body,
} from "./http-api";
import {
Crypto,
@@ -2012,12 +2013,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* Set whether this client is a guest account. <b>This method is experimental
* and may change without warning.</b>
* @param guest - True if this is a guest account.
* @experimental if the token is a macaroon, it should be encoded in it that it is a 'guest'
* access token, which means that the SDK can determine this entirely without
* the dev manually flipping this flag.
*/
public setGuest(guest: boolean): void {
// EXPERIMENTAL:
// If the token is a macaroon, it should be encoded in it that it is a 'guest'
// access token, which means that the SDK can determine this entirely without
// the dev manually flipping this flag.
this.isGuestAccount = guest;
}
@@ -4405,7 +4405,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
private sendCompleteEvent(
roomId: string,
threadId: string | null,
eventObject: any,
eventObject: Partial<IEvent>,
txnId?: string,
): Promise<ISendEventResponse> {
if (!txnId) {
@@ -4982,7 +4982,12 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns Promise which resolves: to an empty object `{}`
* @returns Rejects: with an error response.
*/
public async sendReceipt(event: MatrixEvent, receiptType: ReceiptType, body: any, unthreaded = false): Promise<{}> {
public async sendReceipt(
event: MatrixEvent,
receiptType: ReceiptType,
body?: Record<string, any>,
unthreaded = false,
): Promise<{}> {
if (this.isGuest()) {
return Promise.resolve({}); // guests cannot send receipts so don't bother.
}
@@ -5140,7 +5145,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
$roomId: roomId,
$userId: this.getUserId()!,
});
const data: any = {
const data: QueryDict = {
typing: isTyping,
};
if (isTyping) {
@@ -5332,7 +5337,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}
const populationResults: { [roomId: string]: Error } = {};
const promises: Promise<any>[] = [];
const promises: Promise<unknown>[] = [];
const doLeave = (roomId: string): Promise<void> => {
return this.leave(roomId)
@@ -5935,7 +5940,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
throw new Error("getLatestTimeline only supports room timelines");
}
let event;
let event: IRoomEvent | undefined;
if (timelineSet.threadListType !== null) {
const res = await this.createThreadListMessagesRequest(
timelineSet.room.roomId,
@@ -6394,7 +6399,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
"",
);
let readPromise: Promise<any> = Promise.resolve<any>(undefined);
let readPromise: Promise<unknown> = Promise.resolve();
if (opts.allowRead) {
readPromise = this.sendStateEvent(
roomId,
@@ -6599,7 +6604,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
*/
private async requestTokenFromEndpoint<T extends IRequestTokenResponse>(
endpoint: string,
params: Record<string, any>,
params: QueryDict,
): Promise<T> {
const postParams = Object.assign({}, params);
@@ -7949,8 +7954,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* to false.
* @returns Promise which resolves: On success, the empty object
*/
public deactivateAccount(auth?: any, erase?: boolean): Promise<{ id_server_unbind_result: IdServerUnbindResult }> {
const body: any = {};
public deactivateAccount(
auth?: AuthDict,
erase?: boolean,
): Promise<{ id_server_unbind_result: IdServerUnbindResult }> {
const body: Body = {};
if (auth) {
body.auth = auth;
}
@@ -8188,7 +8196,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public sendStateEvent(
roomId: string,
eventType: string,
content: any,
content: IContent,
stateKey = "",
opts: IRequestOpts = {},
): Promise<ISendEventResponse> {
@@ -8406,13 +8414,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* it invisible.
* @returns Promise which resolves: result object
* @returns Rejects: with an error response.
* @deprecated missing from the spec
*/
public setRoomDirectoryVisibilityAppService(
networkId: string,
roomId: string,
visibility: "public" | "private",
): Promise<any> {
// TODO: Types
const path = utils.encodeUri("/directory/list/appservice/$networkId/$roomId", {
$networkId: networkId,
$roomId: roomId,
@@ -8428,7 +8436,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns Promise which resolves: an array of results.
*/
public searchUserDirectory({ term, limit }: { term: string; limit?: number }): Promise<IUserDirectoryResponse> {
const body: any = {
const body: Body = {
search_term: term,
};
@@ -8506,14 +8514,12 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* Add a 3PID to your homeserver account and optionally bind it to an identity
* server as well. An identity server is required as part of the `creds` object.
*
* This API is deprecated, and you should instead use `addThreePidOnly`
* for homeservers that support it.
* @deprecated this API is deprecated, and you should instead use `addThreePidOnly` for homeservers that support it.
*
* @returns Promise which resolves: on success
* @returns Rejects: with an error response.
*/
public addThreePid(creds: any, bind: boolean): Promise<any> {
// TODO: Types
public addThreePid(creds: IBindThreePidBody, bind: boolean): Promise<{ submit_url?: string }> {
const path = "/account/3pid";
const data = {
threePidCreds: creds,
@@ -8673,7 +8679,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
$device_id: deviceId,
});
const body: any = {};
const body: Body = {};
if (auth) {
body.auth = auth;
@@ -8691,7 +8697,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns Rejects: with an error response.
*/
public deleteMultipleDevices(devices: string[], auth?: AuthDict): Promise<{}> {
const body: any = { devices };
const body: Body = { devices };
if (auth) {
body.auth = auth;
@@ -8868,7 +8874,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
{ body, next_batch: nextBatch }: { body: ISearchRequestBody; next_batch?: string },
abortSignal?: AbortSignal,
): Promise<ISearchResponse> {
const queryParams: any = {};
const queryParams: QueryDict = {};
if (nextBatch) {
queryParams.next_batch = nextBatch;
}
@@ -9488,7 +9494,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* response to getThirdpartyProtocols()
* @returns Promise which resolves to the result object
*/
public getThirdpartyUser(protocol: string, params: any): Promise<IThirdPartyUser[]> {
public getThirdpartyUser(protocol: string, params?: QueryDict): Promise<IThirdPartyUser[]> {
const path = utils.encodeUri("/thirdparty/user/$protocol", {
$protocol: protocol,
});
@@ -9664,7 +9670,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
proxyBaseUrl?: string,
abortSignal?: AbortSignal,
): Promise<MSC3575SlidingSyncResponse> {
const qps: Record<string, any> = {};
const qps: QueryDict = {};
if (req.pos) {
qps.pos = req.pos;
delete req.pos;

View File

@@ -18,8 +18,7 @@ limitations under the License.
* Cross signing methods
*/
import { PkSigning } from "@matrix-org/olm";
import type { PkSigning } from "@matrix-org/olm";
import { decodeBase64, encodeBase64, IObject, pkSign, pkVerify } from "./olmlib";
import { logger } from "../logger";
import { IndexedDBCryptoStore } from "../crypto/store/indexeddb-crypto-store";
@@ -245,7 +244,7 @@ export class CrossSigningInfo {
* @returns A map from key type (string) to private key (Uint8Array)
*/
public async getCrossSigningKeysFromCache(): Promise<Map<string, Uint8Array>> {
const keys = new Map();
const keys = new Map<string, Uint8Array>();
const cacheCallbacks = this.cacheCallbacks;
if (!cacheCallbacks) return keys;
for (const type of ["master", "self_signing", "user_signing"]) {
@@ -294,8 +293,8 @@ export class CrossSigningInfo {
const privateKeys: Record<string, Uint8Array> = {};
const keys: Record<string, ICrossSigningKey> = {};
let masterSigning;
let masterPub;
let masterSigning: PkSigning | undefined;
let masterPub: string | undefined;
try {
if (level & CrossSigningLevel.MASTER) {

View File

@@ -48,7 +48,7 @@ import { InRoomChannel, InRoomRequests } from "./verification/request/InRoomChan
import { Request, ToDeviceChannel, ToDeviceRequests } from "./verification/request/ToDeviceChannel";
import { IllegalMethod } from "./verification/IllegalMethod";
import { KeySignatureUploadError } from "../errors";
import { calculateKeyCheck, decryptAES, encryptAES } from "./aes";
import { calculateKeyCheck, decryptAES, encryptAES, IEncryptedPayload } from "./aes";
import { DehydrationManager } from "./dehydration";
import { BackupManager } from "./backup";
import { IStore } from "../store";
@@ -1242,8 +1242,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* @returns the key, if any, or null
*/
public async getSessionBackupPrivateKey(): Promise<Uint8Array | null> {
let key = await new Promise<any>((resolve) => {
// TODO types
let key = await new Promise<Uint8Array | IEncryptedPayload | null>((resolve) => {
this.cryptoStore.doTxn("readonly", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
this.cryptoStore.getSecretStorePrivateKey(txn, resolve, "m.megolm_backup.v1");
});
@@ -1254,7 +1253,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
key = new Uint8Array(olmlib.decodeBase64(fixBackupKey(key) || key));
await this.storeSessionBackupPrivateKey(key);
}
if (key && key.ciphertext) {
if (key && typeof key === "object" && "ciphertext" in key) {
const pickleKey = Buffer.from(this.olmDevice.pickleKey);
const decrypted = await decryptAES(key, pickleKey, "m.megolm_backup.v1");
key = olmlib.decodeBase64(decrypted);
@@ -2202,12 +2201,12 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
blocked: boolean | null = null,
known: boolean | null = null,
keys?: Record<string, string>,
): Promise<DeviceInfo | CrossSigningInfo> {
): Promise<DeviceInfo | CrossSigningInfo | ICrossSigningKey | undefined> {
// Check if the 'device' is actually a cross signing key
// The js-sdk's verification treats cross-signing keys as devices
// and so uses this method to mark them verified.
const xsk = this.deviceList.getStoredCrossSigningForUser(userId);
if (xsk && xsk.getId() === deviceId) {
if (xsk?.getId() === deviceId) {
if (blocked !== null || known !== null) {
throw new Error("Cannot set blocked or known for a cross-signing key");
}
@@ -2257,7 +2256,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
// This will emit events when it comes back down the sync
// (we could do local echo to speed things up)
}
return device as any; // TODO types
return device!;
} else {
return xsk;
}

View File

@@ -22,13 +22,11 @@ import { checkObjectHasKeys, encodeParams } from "../utils";
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { Method } from "./method";
import { ConnectionError, MatrixError } from "./errors";
import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, IRequestOpts } from "./interface";
import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, IRequestOpts, Body } from "./interface";
import { anySignal, parseErrorResponse, timeoutSignal } from "./utils";
import { QueryDict } from "../utils";
import { logger } from "../logger";
type Body = Record<string, any> | BodyInit;
interface TypedResponse<T> extends Response {
json(): Promise<T>;
}

View File

@@ -16,6 +16,8 @@ limitations under the License.
import { MatrixError } from "./errors";
export type Body = Record<string, any> | BodyInit;
export interface IHttpOpts {
fetchFn?: typeof global.fetch;

View File

@@ -17,7 +17,12 @@ limitations under the License.
import { UnstableValue } from "matrix-events-sdk";
import { RendezvousChannel, RendezvousFailureListener, RendezvousFailureReason, RendezvousIntent } from ".";
import { IMSC3882GetLoginTokenCapability, MatrixClient, UNSTABLE_MSC3882_CAPABILITY } from "../client";
import {
ICrossSigningKey,
IMSC3882GetLoginTokenCapability,
MatrixClient,
UNSTABLE_MSC3882_CAPABILITY,
} from "../client";
import { CrossSigningInfo } from "../crypto/CrossSigning";
import { DeviceInfo } from "../crypto/deviceinfo";
import { buildFeatureSupportMap, Feature, ServerSupport } from "../feature";
@@ -178,7 +183,9 @@ export class MSC3906Rendezvous {
return deviceId;
}
private async verifyAndCrossSignDevice(deviceInfo: DeviceInfo): Promise<CrossSigningInfo | DeviceInfo> {
private async verifyAndCrossSignDevice(
deviceInfo: DeviceInfo,
): Promise<CrossSigningInfo | DeviceInfo | ICrossSigningKey | undefined> {
if (!this.client.crypto) {
throw new Error("Crypto not available on client");
}
@@ -223,7 +230,7 @@ export class MSC3906Rendezvous {
*/
public async verifyNewDeviceOnExistingDevice(
timeout = 10 * 1000,
): Promise<DeviceInfo | CrossSigningInfo | undefined> {
): Promise<DeviceInfo | CrossSigningInfo | ICrossSigningKey | undefined> {
if (!this.newDeviceId) {
throw new Error("No new device to sign");
}

View File

@@ -23,14 +23,13 @@ export class MediaTrackHandler {
const isNotNullAndKind = (track: MediaStreamTrack | null): boolean => {
return track !== null && track.kind === kind;
};
// @ts-ignore The linter don't get it
return this.pc
.getTransceivers()
.filter((t) => t.currentDirection === "sendonly" || t.currentDirection === "sendrecv")
.filter((t) => t.sender !== null)
.map((t) => t.sender)
.map((s) => s.track)
.filter(isNotNullAndKind);
.filter(isNotNullAndKind) as MediaStreamTrack[];
}
public getTackById(trackId: string): MediaStreamTrack | undefined {