From 77a1fc9e603b1397c6fd01705a65fd155c5f5cad Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Tue, 21 Apr 2020 11:45:54 +0100
Subject: [PATCH] Convert bunch of things to TypeScript
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
---
src/@types/global.d.ts | 25 +++++++++++++++++++++++++
src/{matrix.js => matrix.ts} | 22 ++++++++++++++++++++--
src/utils.ts | 2 +-
3 files changed, 46 insertions(+), 3 deletions(-)
create mode 100644 src/@types/global.d.ts
rename src/{matrix.js => matrix.ts} (89%)
diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts
new file mode 100644
index 000000000..239b74db6
--- /dev/null
+++ b/src/@types/global.d.ts
@@ -0,0 +1,25 @@
+/*
+Copyright 2020 The Matrix.org Foundation C.I.C.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+export {};
+
+declare global {
+ namespace NodeJS {
+ interface Global {
+ localStorage: any;
+ }
+ }
+}
diff --git a/src/matrix.js b/src/matrix.ts
similarity index 89%
rename from src/matrix.js
rename to src/matrix.ts
index f9f71c542..a74b08697 100644
--- a/src/matrix.js
+++ b/src/matrix.ts
@@ -17,7 +17,12 @@ limitations under the License.
*/
import {MemoryCryptoStore} from "./crypto/store/memory-crypto-store";
+import {LocalStorageCryptoStore} from "./crypto/store/localStorage-crypto-store";
+import {IndexedDBCryptoStore} from "./crypto/store/indexeddb-crypto-store";
import {MemoryStore} from "./store/memory";
+import {StubStore} from "./store/stub";
+import {LocalIndexedDBStoreBackend} from "./store/indexeddb-local-backend";
+import {RemoteIndexedDBStoreBackend} from "./store/indexeddb-remote-backend";
import {MatrixScheduler} from "./scheduler";
import {MatrixClient} from "./client";
@@ -89,6 +94,10 @@ export function wrapRequest(wrapper) {
};
}
+type Store =
+ StubStore | MemoryStore | LocalIndexedDBStoreBackend | RemoteIndexedDBStoreBackend;
+
+type CryptoStore = MemoryCryptoStore | LocalStorageCryptoStore | IndexedDBCryptoStore;
let cryptoStoreFactory = () => new MemoryCryptoStore;
@@ -102,6 +111,15 @@ export function setCryptoStoreFactory(fac) {
cryptoStoreFactory = fac;
}
+
+interface ICreateClientOpts {
+ baseUrl: string;
+ store?: Store;
+ cryptoStore?: CryptoStore;
+ scheduler?: MatrixScheduler;
+ request?: any; // TODO
+}
+
/**
* Construct a Matrix Client. Similar to {@link module:client.MatrixClient}
* except that the 'request', 'store' and 'scheduler' dependencies are satisfied.
@@ -125,10 +143,10 @@ export function setCryptoStoreFactory(fac) {
* @see {@link module:client.MatrixClient} for the full list of options for
* opts.
*/
-export function createClient(opts) {
+export function createClient(opts: ICreateClientOpts | string) {
if (typeof opts === "string") {
opts = {
- "baseUrl": opts,
+ "baseUrl": opts as string,
};
}
opts.request = opts.request || requestInstance;
diff --git a/src/utils.ts b/src/utils.ts
index 1e31dff88..c32dedaf8 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -265,7 +265,7 @@ export function checkObjectHasNoAdditionalKeys(obj: object, allowedKeys: string[
* @param {Object} obj The object to deep copy.
* @return {Object} A copy of the object without any references to the original.
*/
-export function deepCopy(obj: object): object {
+export function deepCopy(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}