Prefer type over interface

This commit is contained in:
RMidhunSuresh 2021-12-01 11:42:24 +05:30
parent b328c54da8
commit 82de3c9867
7 changed files with 30 additions and 30 deletions

View file

@ -19,7 +19,7 @@ import {encodeQueryParams, encodeBody} from "./common";
import {HomeServerRequest} from "./HomeServerRequest"; import {HomeServerRequest} from "./HomeServerRequest";
import type {IHomeServerRequest} from "./HomeServerRequest"; import type {IHomeServerRequest} from "./HomeServerRequest";
import type {Reconnector} from "./Reconnector"; import type {Reconnector} from "./Reconnector";
import type {IEncodedBody} from "./common"; import type {EncodedBody} from "./common";
import type {IRequestOptions, RequestFunction} from "../../platform/types/types"; import type {IRequestOptions, RequestFunction} from "../../platform/types/types";
import type {LogItem} from "../../logging/LogItem"; import type {LogItem} from "../../logging/LogItem";
@ -66,7 +66,7 @@ export class HomeServerApi {
method, method,
}, parent.level.Info); }, parent.level.Info);
} }
let encodedBody: IEncodedBody["body"]; let encodedBody: EncodedBody["body"];
const headers: Map<string, string | number> = new Map(); const headers: Map<string, string | number> = new Map();
if (accessToken) { if (accessToken) {
headers.set("Authorization", `Bearer ${accessToken}`); headers.set("Authorization", `Bearer ${accessToken}`);

View file

@ -18,7 +18,7 @@ import {encodeQueryParams} from "./common";
import {decryptAttachment} from "../e2ee/attachment.js"; import {decryptAttachment} from "../e2ee/attachment.js";
import {Platform} from "../../platform/web/Platform.js"; import {Platform} from "../../platform/web/Platform.js";
import {BlobHandle} from "../../platform/web/dom/BlobHandle.js"; import {BlobHandle} from "../../platform/web/dom/BlobHandle.js";
import type {IAttachment, IEncryptedFile} from "./types/response"; import type {Attachment, EncryptedFile} from "./types/response";
export class MediaRepository { export class MediaRepository {
private readonly _homeserver: string; private readonly _homeserver: string;
@ -58,7 +58,7 @@ export class MediaRepository {
} }
} }
async downloadEncryptedFile(fileEntry: IEncryptedFile, cache: boolean = false): Promise<BlobHandle> { async downloadEncryptedFile(fileEntry: EncryptedFile, cache: boolean = false): Promise<BlobHandle> {
const url = this.mxcUrl(fileEntry.url); const url = this.mxcUrl(fileEntry.url);
const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response(); const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response();
const decryptedBuffer = await decryptAttachment(this._platform, encryptedBuffer, fileEntry); const decryptedBuffer = await decryptAttachment(this._platform, encryptedBuffer, fileEntry);
@ -71,7 +71,7 @@ export class MediaRepository {
return this._platform.createBlob(buffer, mimetype); return this._platform.createBlob(buffer, mimetype);
} }
async downloadAttachment(content: IAttachment, cache: boolean = false): Promise<BlobHandle> { async downloadAttachment(content: Attachment, cache: boolean = false): Promise<BlobHandle> {
if (content.file) { if (content.file) {
return this.downloadEncryptedFile(content.file, cache); return this.downloadEncryptedFile(content.file, cache);
} else { } else {

View file

@ -18,7 +18,7 @@ import {ObservableValue} from "../../observable/ObservableValue";
import type {ExponentialRetryDelay} from "./ExponentialRetryDelay"; import type {ExponentialRetryDelay} from "./ExponentialRetryDelay";
import type {TimeMeasure} from "../../platform/web/dom/Clock.js"; import type {TimeMeasure} from "../../platform/web/dom/Clock.js";
import type {OnlineStatus} from "../../platform/web/dom/OnlineStatus.js"; import type {OnlineStatus} from "../../platform/web/dom/OnlineStatus.js";
import type {IVersionResponse} from "./types/response"; import type {VersionResponse} from "./types/response";
import type {HomeServerApi} from "./HomeServerApi"; import type {HomeServerApi} from "./HomeServerApi";
export enum ConnectionStatus { export enum ConnectionStatus {
@ -39,7 +39,7 @@ export class Reconnector {
private readonly _onlineStatus: OnlineStatus; private readonly _onlineStatus: OnlineStatus;
private readonly _state: ObservableValue<ConnectionStatus>; private readonly _state: ObservableValue<ConnectionStatus>;
private _isReconnecting: boolean; private _isReconnecting: boolean;
private _versionsResponse?: IVersionResponse; private _versionsResponse?: VersionResponse;
private _stateSince: TimeMeasure; private _stateSince: TimeMeasure;
constructor({retryDelay, createMeasure, onlineStatus}: Ctor) { constructor({retryDelay, createMeasure, onlineStatus}: Ctor) {
@ -51,7 +51,7 @@ export class Reconnector {
this._isReconnecting = false; this._isReconnecting = false;
} }
get lastVersionsResponse(): IVersionResponse | undefined { get lastVersionsResponse(): VersionResponse | undefined {
return this._versionsResponse; return this._versionsResponse;
} }

View file

@ -17,7 +17,7 @@ limitations under the License.
import {BlobHandle} from "../../platform/web/dom/BlobHandle.js"; import {BlobHandle} from "../../platform/web/dom/BlobHandle.js";
export interface IEncodedBody { export type EncodedBody = {
mimeType: string; mimeType: string;
body: BlobHandle | string; body: BlobHandle | string;
length: number; length: number;
@ -35,7 +35,7 @@ export function encodeQueryParams(queryParams?: Record<string, any>): string {
.join("&"); .join("&");
} }
export function encodeBody(body: Record<string, any>): IEncodedBody { export function encodeBody(body: Record<string, any>): EncodedBody {
// todo: code change here // todo: code change here
if (body instanceof BlobHandle) { if (body instanceof BlobHandle) {
const blob = body as BlobHandle; const blob = body as BlobHandle;

View file

@ -18,21 +18,21 @@ import {AbortError, ConnectionError} from "../../error.js";
import type {IRequestOptions, RequestFunction} from "../../../platform/types/types.js"; import type {IRequestOptions, RequestFunction} from "../../../platform/types/types.js";
import type {RequestResult} from "../../../platform/web/dom/request/fetch.js"; import type {RequestResult} from "../../../platform/web/dom/request/fetch.js";
interface IOptions extends IRequestOptions { type Options = IRequestOptions & {
method?: any; method?: any;
delay?: boolean; delay?: boolean;
} }
class RequestLogItem { class RequestLogItem {
public readonly url: string; public readonly url: string;
public readonly options: IOptions; public readonly options: Options;
public error: {aborted: boolean, network: boolean, message: string}; public error: {aborted: boolean, network: boolean, message: string};
public status: number; public status: number;
public body: Response["body"]; public body: Response["body"];
public start: number = performance.now(); public start: number = performance.now();
public end: number = 0; public end: number = 0;
constructor(url: string, options: IOptions) { constructor(url: string, options: Options) {
this.url = url; this.url = url;
this.options = options; this.options = options;
} }
@ -62,7 +62,7 @@ export class RecordRequester {
this.request = this.request.bind(this); this.request = this.request.bind(this);
} }
request(url: string, options: IOptions): RequestResult { request(url: string, options: Options): RequestResult {
const requestItem = new RequestLogItem(url, options); const requestItem = new RequestLogItem(url, options);
this._requestLog.push(requestItem); this._requestLog.push(requestItem);
try { try {
@ -84,15 +84,15 @@ export class RecordRequester {
export class ReplayRequester { export class ReplayRequester {
private readonly _log: RequestLogItem[]; private readonly _log: RequestLogItem[];
private readonly _options: IOptions; private readonly _options: Options;
constructor(log: RequestLogItem[], options: IOptions) { constructor(log: RequestLogItem[], options: Options) {
this._log = log.slice(); this._log = log.slice();
this._options = options; this._options = options;
this.request = this.request.bind(this); this.request = this.request.bind(this);
} }
request(url: string, options: IOptions): ReplayRequestResult { request(url: string, options: Options): ReplayRequestResult {
const idx = this._log.findIndex((item) => { const idx = this._log.findIndex((item) => {
return item.url === url && options.method === item.options.method; return item.url === url && options.method === item.options.method;
}); });
@ -107,10 +107,10 @@ export class ReplayRequester {
class ReplayRequestResult { class ReplayRequestResult {
private readonly _item: RequestLogItem; private readonly _item: RequestLogItem;
private readonly _options: IOptions; private readonly _options: Options;
private _aborted: boolean; private _aborted: boolean;
constructor(item: RequestLogItem, options: IOptions) { constructor(item: RequestLogItem, options: Options) {
this._item = item; this._item = item;
this._options = options; this._options = options;
this._aborted = false; this._aborted = false;

View file

@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export interface IAttachment { export type Attachment = {
body: string; body: string;
info: IAttachmentInfo; info: AttachmentInfo;
// todo: what about m.audio? // todo: what about m.audio?
msgtype: "m.image" | "m.file" | "m.video"; msgtype: "m.image" | "m.file" | "m.video";
url?: string; url?: string;
file?: IEncryptedFile; file?: EncryptedFile;
filename?: string; filename?: string;
} }
export interface IEncryptedFile { export type EncryptedFile = {
key: JsonWebKey; key: JsonWebKey;
iv: string; iv: string;
hashes: { hashes: {
@ -35,25 +35,25 @@ export interface IEncryptedFile {
mimetype?: string; mimetype?: string;
} }
interface IAttachmentInfo { type AttachmentInfo = {
h?: number; h?: number;
w?: number; w?: number;
mimetype: string; mimetype: string;
size: number; size: number;
duration?: number; duration?: number;
thumbnail_url?: string; thumbnail_url?: string;
thumbnail_file?: IEncryptedFile; thumbnail_file?: EncryptedFile;
thumbnail_info?: IThumbnailInfo; thumbnail_info?: ThumbnailInfo;
} }
interface IThumbnailInfo { type ThumbnailInfo = {
h: number; h: number;
w: number; w: number;
mimetype: string; mimetype: string;
size: number; size: number;
} }
export interface IVersionResponse { export type VersionResponse = {
versions: string[]; versions: string[];
unstable_features?: Record<string, boolean>; unstable_features?: Record<string, boolean>;
} }

View file

@ -15,13 +15,13 @@ limitations under the License.
*/ */
import type {RequestResult} from "../web/dom/request/fetch.js"; import type {RequestResult} from "../web/dom/request/fetch.js";
import type {IEncodedBody} from "../../matrix/net/common"; import type {EncodedBody} from "../../matrix/net/common";
import type {LogItem} from "../../logging/LogItem"; import type {LogItem} from "../../logging/LogItem";
export interface IRequestOptions { export interface IRequestOptions {
uploadProgress?: (loadedBytes: number) => void; uploadProgress?: (loadedBytes: number) => void;
timeout?: number; timeout?: number;
body?: IEncodedBody; body?: EncodedBody;
headers?: Map<string, string|number>; headers?: Map<string, string|number>;
cache?: boolean; cache?: boolean;
log?: LogItem; log?: LogItem;