Skip to content

Commit

Permalink
Merge pull request #117 from whereby/nandor/pan-502-remove-the-assert…
Browse files Browse the repository at this point in the history
…-lib-from-jslib-media

Remove the `assert` lib
  • Loading branch information
nandito authored Nov 7, 2023
2 parents 5669fab + 7f4b4eb commit 9e349e0
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 49 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
},
"dependencies": {
"@swc/helpers": "^0.3.13",
"@whereby/jslib-media": "whereby/jslib-media.git#1.3.6",
"assert": "^2.0.0",
"@whereby/jslib-media": "whereby/jslib-media.git#1.4.1",
"axios": "^1.2.3",
"btoa": "^1.2.1",
"events": "^3.3.0",
Expand Down
9 changes: 3 additions & 6 deletions src/lib/api/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import nodeBtoa from "btoa";
import assert from "@whereby/jslib-media/src/utils/assert";
import HttpClient, { HttpClientRequestConfig } from "./HttpClient";
import MultipartHttpClient from "./MultipartHttpClient";
import { assertString } from "./parameterAssertUtils";
Expand Down Expand Up @@ -74,9 +74,6 @@ export default class ApiClient {
baseUrl = "https://api.appearin.net",
fetchDeviceCredentials = noCredentials,
}: ApiClientOptions = {}) {
assertString(baseUrl, "baseUrl");
assert.ok(typeof fetchDeviceCredentials === "function", "fetchDeviceCredentials<Function> is required");

this.authenticatedHttpClient = new AuthenticatedHttpClient({
httpClient: new HttpClient({
baseUrl,
Expand All @@ -92,7 +89,7 @@ export default class ApiClient {
*/
request(url: string, options: HttpClientRequestConfig): Promise<Response> {
assertString(url, "url");
assert.equal(url[0], "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(options, "options are required");

return this.authenticatedHttpClient.request(url, options);
Expand All @@ -103,7 +100,7 @@ export default class ApiClient {
*/
requestMultipart(url: string, options: HttpClientRequestConfig): Promise<Response> {
assertString(url, "url");
assert.equal(url[0], "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(options, "options are required");

return this.authenticatedFormDataHttpClient.request(url, options);
Expand Down
5 changes: 2 additions & 3 deletions src/lib/api/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import assert from "@whereby/jslib-media/src/utils/assert";
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
import Response, { ErrorResponseObject } from "./Response";
import { assertString } from "./parameterAssertUtils";
Expand All @@ -10,7 +10,6 @@ export interface IHttpClient {
}

function _getAbsoluteUrl({ baseUrl, url }: { baseUrl?: string; url: string }): string {
assert.ok(typeof url === "string", "url<String> is required");
return baseUrl ? baseUrl + url : url;
}

Expand Down Expand Up @@ -50,7 +49,7 @@ export default class HttpClient implements IHttpClient {
*/
request(url: string, options: HttpClientRequestConfig): Promise<Response> {
assertString(url, "url");
assert.equal(url[0], "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(options, "options are required");

return this._requestAxios(url, options)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/MultipartHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import assert from "@whereby/jslib-media/src/utils/assert";
import { HttpClientRequestConfig, IHttpClient } from "./HttpClient";
import Response from "./Response";

Expand Down
11 changes: 5 additions & 6 deletions src/lib/api/OrganizationApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "@whereby/jslib-media/src/utils/assert";
import ApiClient from "./ApiClient";
import assert from "assert";
import { assertString } from "./parameterAssertUtils";
import { HttpClientRequestConfig } from "./HttpClient";
import Response from "./Response";
Expand All @@ -18,8 +18,9 @@ export default class OrganizationApiClient {
/**
* Create an OrganizationApiClient instance.
*
* @param {ApiClient} [apiClient] - The apiClient to use.
* @param {Function} [fetchOrganization] - function that returns a promise with the organization.
* @param {Object} options - The options for the OrganizationApiClient.
* @param {ApiClient} [options.apiClient] - The apiClient to use.
* @param {Function} [options.fetchOrganization] - function that returns a promise with the organization.
*/
constructor({
apiClient,
Expand All @@ -29,8 +30,6 @@ export default class OrganizationApiClient {
fetchOrganization?: FetchOrganizationFunction;
}) {
this._apiClient = apiClient;
assert.ok(typeof fetchOrganization === "function", "fetchOrganization<Function> is required");

this._fetchOrganization = fetchOrganization;
this._apiClient = apiClient;
}
Expand All @@ -41,7 +40,7 @@ export default class OrganizationApiClient {
options: HttpClientRequestConfig
): Promise<Response> {
assertString(url, "url");
assert.equal(url[0], "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(url[0] === "/", 'url<String> only accepts relative URLs beginning with "/".');
assert.ok(options, "options are required");
return this._fetchOrganization().then((organization) => {
if (!organization) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/models/Room.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import assert from "assert";
import assert from "@whereby/jslib-media/src/utils/assert";

export default class Room {
public readonly isLocked: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/organizationService/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import assert from "@whereby/jslib-media/src/utils/assert";
import Organization, { OrganizationPreferences } from "../models/Organization";
import {
assertInstanceOf,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/api/parameterAssertUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import assert from "@whereby/jslib-media/src/utils/assert";

/**
* Asserts that value is truthy.
Expand Down Expand Up @@ -65,7 +65,7 @@ export function assertInstanceOf<T>(value: unknown, type: new (any: unknown) =>
*/
export function assertRoomName(roomName: unknown, parameterName = "roomName"): string {
assertString(roomName, parameterName);
assert.equal(typeof roomName === "string" && roomName[0], "/", `${parameterName} must begin with a '/'`);
assert.ok(typeof roomName === "string" && roomName[0] === "/", `${parameterName} must begin with a '/'`);
return roomName as string;
}

Expand Down
8 changes: 0 additions & 8 deletions src/lib/api/test/ApiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ describe("ApiClient", () => {
it("should not throw an error if no constructor params are passed through", () => {
expect(() => new ApiClient()).not.toThrowError();
});

//@ts-expect-error
itShouldThrowIfInvalid("baseUrl", () => new ApiClient({ baseUrl: null }));
//@ts-expect-error
itShouldThrowIfInvalid("fetchDeviceCredentials", () => new ApiClient({ fetchDeviceCredentials: null }));
});

describe("request", () => {
Expand Down Expand Up @@ -48,9 +43,6 @@ describe("ApiClient", () => {
expect(() => apiClient.request("some-url", {})).toThrowError();
});

//@ts-expect-error
itShouldThrowIfInvalid("fetchDeviceCredentials", () => new ApiClient({ fetchDeviceCredentials: null }));

it("should run `this.fetchDeviceCredentials`", () => {
const url = "/some/path";
const fetchOptions = {};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/test/extractUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
nullOrTransform,
Transformer,
} from "../extractUtils";
import assert from "assert";
import assert from "@whereby/jslib-media/src/utils/assert";
import { Json } from "../Response";
import { assertString } from "../parameterAssertUtils";
type User = { name: string; eyeColor: string | null; age: number; dob: Date };
Expand Down
3 changes: 0 additions & 3 deletions src/lib/api/test/parameterAssertUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from "assert";
import {
assertBoolean,
assertRoomName,
Expand All @@ -13,8 +12,6 @@ function forEachObject(object: Record<string, unknown>, func: (value: unknown, k
}
describe("parameterAssertUtils", () => {
function itShouldThrowForValues(testValues: Record<string, unknown>, test: (desc: string, value: unknown) => void) {
assert.ok(typeof test === "function", "test must be a function");

forEachObject(testValues, (value, descriptionOfValue) => {
it(`should throw for ${descriptionOfValue}`, () => {
test(descriptionOfValue, value);
Expand Down
7 changes: 0 additions & 7 deletions src/lib/utils/__tests__/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ describe("debounce", () => {
jest.clearAllMocks();
});

it("should throw if no function is provided", () => {
expect(() => {
// @ts-expect-error
debounce();
}).toThrow("fn<function> is required");
});

it("should set the timer with the specified delay", () => {
const delay = Math.floor(Math.random() * 2000);

Expand Down
4 changes: 0 additions & 4 deletions src/lib/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import assert from "assert";

interface Options {
delay?: number;
edges?: boolean;
Expand All @@ -21,8 +19,6 @@ interface DebouncedFunction {
* @returns {Function} Debounced function.
*/
export default function debounce(fn: DebouncedFunction, { delay = 500, edges }: Options = {}): DebouncedFunction {
assert.ok(typeof fn === "function", "fn<function> is required");

let timeout: NodeJS.Timeout | undefined;
let nCalls = 0;

Expand Down
10 changes: 10 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ declare module "@whereby/jslib-media/src/webrtc/RtcManager" {
}
}

declare const assert: {
(value: unknown, message?: string | Error): asserts value;
ok(value: unknown, message?: string | Error): asserts value;
notEqual<T>(actual: T, expected: T, message: string): void;
};

declare module "@whereby/jslib-media/src/utils/assert" {
export = assert;
}

declare module "@whereby/jslib-media/src/utils/urls" {
export function fromLocation({ host }: { host: string }): { subdomain: string };
}
Expand Down
7 changes: 3 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3581,11 +3581,10 @@
"@webassemblyjs/ast" "1.11.6"
"@xtuc/long" "4.2.2"

"@whereby/jslib-media@whereby/jslib-media.git#1.3.6":
version "1.3.6"
resolved "https://codeload.github.com/whereby/jslib-media/tar.gz/bc8f3efd48e9e5b23b886bc8ea181b33f1d62719"
"@whereby/jslib-media@whereby/jslib-media.git#1.4.1":
version "1.4.1"
resolved "https://codeload.github.com/whereby/jslib-media/tar.gz/abc55b7e2d3af9c71cb3f3a2953291bb79725064"
dependencies:
assert "^2.0.0"
events "^3.3.0"
mediasoup-client "3.6.100"
rtcstats "github:whereby/rtcstats#v5.3.0"
Expand Down

0 comments on commit 9e349e0

Please sign in to comment.