Skip to content

Commit

Permalink
Improve types (#3175)
Browse files Browse the repository at this point in the history
* Improve types

* Add test
  • Loading branch information
t3chguy authored Feb 22, 2023
1 parent 21cc9c3 commit d70ffdb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
11 changes: 9 additions & 2 deletions spec/integ/matrix-client-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,18 +1336,25 @@ describe("MatrixClient", function () {
it.each([
{
userId: "alice@localhost",
powerLevel: 100,
expectation: {
"alice@localhost": 100,
},
},
{
userId: ["alice@localhost", "bob@localhost"],
powerLevel: 100,
expectation: {
"alice@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 = {
getType: () => "m.room.power_levels",
getContent: () => ({
Expand All @@ -1364,7 +1371,7 @@ describe("MatrixClient", function () {
})
.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 prom;
});
Expand Down
2 changes: 1 addition & 1 deletion src/@types/IIdentityServerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export interface IIdentityServerProvider {
* for the associated client.
* @returns Promise which resolves to the access token.
*/
getAccessToken(): Promise<string>;
getAccessToken(): Promise<string | null>;
}
41 changes: 23 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4130,7 +4130,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public setPowerLevel(
roomId: string,
userId: string | string[],
powerLevel: number,
powerLevel: number | undefined,
event: MatrixEvent | null,
): Promise<ISendEventResponse> {
let content = {
Expand All @@ -4141,13 +4141,16 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// existing client state with a failed power level change
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;
}
} else {
content.users[userId] = powerLevel;
}

const path = utils.encodeUri("/rooms/$roomId/state/m.room.power_levels", {
$roomId: roomId,
});
Expand Down Expand Up @@ -8746,18 +8749,19 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
email: string,
clientSecret: string,
sendAttempt: number,
nextLink: string,
nextLink?: string,
identityAccessToken?: string,
): Promise<any> {
// TODO: Types
const params = {
): Promise<IRequestTokenResponse> {
const params: Record<string, string> = {
client_secret: clientSecret,
email: email,
send_attempt: sendAttempt?.toString(),
next_link: nextLink,
};
if (nextLink) {
params.next_link = nextLink;
}

return this.http.idServerRequest(
return this.http.idServerRequest<IRequestTokenResponse>(
Method.Post,
"/validate/email/requestToken",
params,
Expand Down Expand Up @@ -8788,7 +8792,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param identityAccessToken - The `access_token` field of the Identity
* 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.
* @throws Error if no identity server is set
*/
Expand All @@ -8797,19 +8801,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
phoneNumber: string,
clientSecret: string,
sendAttempt: number,
nextLink: string,
nextLink?: string,
identityAccessToken?: string,
): Promise<any> {
// TODO: Types
const params = {
): Promise<IRequestMsisdnTokenResponse> {
const params: Record<string, string> = {
client_secret: clientSecret,
country: phoneCountry,
phone_number: phoneNumber,
send_attempt: sendAttempt?.toString(),
next_link: nextLink,
};
if (nextLink) {
params.next_link = nextLink;
}

return this.http.idServerRequest(
return this.http.idServerRequest<IRequestMsisdnTokenResponse>(
Method.Post,
"/validate/msisdn/requestToken",
params,
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class FetchHttpApi<O extends IHttpOpts> {
this.opts.idBaseUrl = url;
}

public idServerRequest<T extends Record<string, unknown>>(
public idServerRequest<T extends {} = Record<string, unknown>>(
method: Method,
path: string,
params: Record<string, string | string[]> | undefined,
Expand Down

0 comments on commit d70ffdb

Please sign in to comment.