Convert common.js to ts

This commit is contained in:
RMidhunSuresh 2021-11-21 20:50:07 +05:30
parent 9688a561b3
commit 3a24019d96
2 changed files with 13 additions and 11 deletions

View File

@ -15,15 +15,15 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import type {BlobHandle} from "../../platform/web/dom/BlobHandle.js"; import {BlobHandle} from "../../platform/web/dom/BlobHandle.js";
export interface BlobBody { export interface IEncodedBody {
mimeType: string; mimeType: string;
body: BlobHandle; body: BlobHandle | string;
length: number; length: number;
} }
export function encodeQueryParams(queryParams) { export function encodeQueryParams(queryParams: object): string {
return Object.entries(queryParams || {}) return Object.entries(queryParams || {})
.filter(([, value]) => value !== undefined) .filter(([, value]) => value !== undefined)
.map(([name, value]) => { .map(([name, value]) => {
@ -35,9 +35,10 @@ export function encodeQueryParams(queryParams) {
.join("&"); .join("&");
} }
export function encodeBody(body) { export function encodeBody(body: {}): IEncodedBody {
if (body.nativeBlob && body.mimeType) { // todo: code change here
const blob = body; if (body instanceof BlobHandle) {
const blob = body as BlobHandle;
return { return {
mimeType: blob.mimeType, mimeType: blob.mimeType,
body: blob, // will be unwrapped in request fn body: blob, // will be unwrapped in request fn
@ -48,8 +49,9 @@ export function encodeBody(body) {
return { return {
mimeType: "application/json", mimeType: "application/json",
body: json, body: json,
length: body.length // todo: code change here; body.length is a mistake?
}; length: json.length
}
} else { } else {
throw new Error("Unknown body type: " + body); throw new Error("Unknown body type: " + body);
} }

View File

@ -15,12 +15,12 @@ limitations under the License.
*/ */
import type {RequestResult} from "../web/dom/request/fetch.js"; import type {RequestResult} from "../web/dom/request/fetch.js";
import type {BlobBody} from "../../matrix/net/common"; import type {IEncodedBody} from "../../matrix/net/common";
interface IRequestOptions { interface IRequestOptions {
uploadProgress?: (loadedBytes: number) => void; uploadProgress?: (loadedBytes: number) => void;
timeout?: number; timeout?: number;
body?: string | BlobBody; body?: IEncodedBody;
headers?: { [key: string]: number | string }; headers?: { [key: string]: number | string };
cache?: boolean; cache?: boolean;
method: string; method: string;