1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Improve types (#3175)

* Improve types

* Add test
This commit is contained in:
Michael Telatynski
2023-02-22 17:39:37 +00:00
committed by GitHub
parent 21cc9c3d8a
commit d70ffdbc02
4 changed files with 34 additions and 22 deletions

View File

@ -1336,18 +1336,25 @@ describe("MatrixClient", function () {
it.each([ it.each([
{ {
userId: "alice@localhost", userId: "alice@localhost",
powerLevel: 100,
expectation: { expectation: {
"alice@localhost": 100, "alice@localhost": 100,
}, },
}, },
{ {
userId: ["alice@localhost", "bob@localhost"], userId: ["alice@localhost", "bob@localhost"],
powerLevel: 100,
expectation: { expectation: {
"alice@localhost": 100, "alice@localhost": 100,
"bob@localhost": 100, "bob@localhost": 100,
}, },
}, },
])("should modify power levels of $userId correctly", async ({ userId, expectation }) => { {
userId: "alice@localhost",
powerLevel: undefined,
expectation: {},
},
])("should modify power levels of $userId correctly", async ({ userId, powerLevel, expectation }) => {
const event = { const event = {
getType: () => "m.room.power_levels", getType: () => "m.room.power_levels",
getContent: () => ({ getContent: () => ({
@ -1364,7 +1371,7 @@ describe("MatrixClient", function () {
}) })
.respond(200, {}); .respond(200, {});
const prom = client!.setPowerLevel("!room_id:server", userId, 100, event); const prom = client!.setPowerLevel("!room_id:server", userId, powerLevel, event);
await httpBackend!.flushAllExpected(); await httpBackend!.flushAllExpected();
await prom; await prom;
}); });

View File

@ -20,5 +20,5 @@ export interface IIdentityServerProvider {
* for the associated client. * for the associated client.
* @returns Promise which resolves to the access token. * @returns Promise which resolves to the access token.
*/ */
getAccessToken(): Promise<string>; getAccessToken(): Promise<string | null>;
} }

View File

@ -4130,7 +4130,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public setPowerLevel( public setPowerLevel(
roomId: string, roomId: string,
userId: string | string[], userId: string | string[],
powerLevel: number, powerLevel: number | undefined,
event: MatrixEvent | null, event: MatrixEvent | null,
): Promise<ISendEventResponse> { ): Promise<ISendEventResponse> {
let content = { let content = {
@ -4141,13 +4141,16 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// existing client state with a failed power level change // existing client state with a failed power level change
content = utils.deepCopy(event.getContent()); content = utils.deepCopy(event.getContent());
} }
if (Array.isArray(userId)) {
for (const user of userId) { const users = Array.isArray(userId) ? userId : [userId];
for (const user of users) {
if (powerLevel == null) {
delete content.users[user];
} else {
content.users[user] = powerLevel; content.users[user] = powerLevel;
} }
} else {
content.users[userId] = powerLevel;
} }
const path = utils.encodeUri("/rooms/$roomId/state/m.room.power_levels", { const path = utils.encodeUri("/rooms/$roomId/state/m.room.power_levels", {
$roomId: roomId, $roomId: roomId,
}); });
@ -8746,18 +8749,19 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
email: string, email: string,
clientSecret: string, clientSecret: string,
sendAttempt: number, sendAttempt: number,
nextLink: string, nextLink?: string,
identityAccessToken?: string, identityAccessToken?: string,
): Promise<any> { ): Promise<IRequestTokenResponse> {
// TODO: Types const params: Record<string, string> = {
const params = {
client_secret: clientSecret, client_secret: clientSecret,
email: email, email: email,
send_attempt: sendAttempt?.toString(), send_attempt: sendAttempt?.toString(),
next_link: nextLink,
}; };
if (nextLink) {
params.next_link = nextLink;
}
return this.http.idServerRequest( return this.http.idServerRequest<IRequestTokenResponse>(
Method.Post, Method.Post,
"/validate/email/requestToken", "/validate/email/requestToken",
params, params,
@ -8788,7 +8792,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param identityAccessToken - The `access_token` field of the Identity * @param identityAccessToken - The `access_token` field of the Identity
* Server `/account/register` response (see {@link registerWithIdentityServer}). * Server `/account/register` response (see {@link registerWithIdentityServer}).
* *
* @returns Promise which resolves: TODO * @returns Promise which resolves to an object with a sid string
* @returns Rejects: with an error response. * @returns Rejects: with an error response.
* @throws Error if no identity server is set * @throws Error if no identity server is set
*/ */
@ -8797,19 +8801,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
phoneNumber: string, phoneNumber: string,
clientSecret: string, clientSecret: string,
sendAttempt: number, sendAttempt: number,
nextLink: string, nextLink?: string,
identityAccessToken?: string, identityAccessToken?: string,
): Promise<any> { ): Promise<IRequestMsisdnTokenResponse> {
// TODO: Types const params: Record<string, string> = {
const params = {
client_secret: clientSecret, client_secret: clientSecret,
country: phoneCountry, country: phoneCountry,
phone_number: phoneNumber, phone_number: phoneNumber,
send_attempt: sendAttempt?.toString(), send_attempt: sendAttempt?.toString(),
next_link: nextLink,
}; };
if (nextLink) {
params.next_link = nextLink;
}
return this.http.idServerRequest( return this.http.idServerRequest<IRequestMsisdnTokenResponse>(
Method.Post, Method.Post,
"/validate/msisdn/requestToken", "/validate/msisdn/requestToken",
params, params,

View File

@ -70,7 +70,7 @@ export class FetchHttpApi<O extends IHttpOpts> {
this.opts.idBaseUrl = url; this.opts.idBaseUrl = url;
} }
public idServerRequest<T extends Record<string, unknown>>( public idServerRequest<T extends {} = Record<string, unknown>>(
method: Method, method: Method,
path: string, path: string,
params: Record<string, string | string[]> | undefined, params: Record<string, string | string[]> | undefined,