Skip to content

Commit

Permalink
Merge pull request #742 from wazo-platform/WP-1265-ts-cleanup
Browse files Browse the repository at this point in the history
[WP-1265] Typescript cleanup
  • Loading branch information
manuquentin authored Jan 11, 2024
2 parents ae34360 + bfb837c commit dc29e39
Show file tree
Hide file tree
Showing 63 changed files with 801 additions and 1,083 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist/*
docs/*
esm/*
lib/*
lib/*
./index.js
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Alternatively, you may load the Wazo SDK from a CDN. Use one of the following Co
### Importing the Simple API
```js
// For Node / packaged app
import Wazo from '@wazo/sdk/lib/simple';
import Wazo from '@wazo/sdk';

// Browser
// You can access the `Wazo` object directly on the browser, simply include it in the html :
Expand Down Expand Up @@ -747,7 +747,7 @@ client.auth.listPolicies();
**`Log`**

```
import Wazo from '@wazo/sdk/lib/simple';
import Wazo from '@wazo/sdk';
Wazo.IssueReporter.enable();
Wazo.IssueReporter.configureRemoteClient({
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/index');
21 changes: 4 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@wazo/sdk",
"version": "0.39.15",
"description": "Wazo's JavaScript Software Development Kit.",
"main": "dist/wazo-sdk.js",
"main": "index.js",
"types": "lib/types/index.d.ts",
"author": "Wazo (http://wazo.io)",
"maintainers": [
"Wazo Team <dev@wazo.io> (https://github.com/wazo-platform)"
Expand All @@ -13,6 +14,7 @@
"license": "MIT",
"homepage": "https://github.com/wazo-platform/wazo-js-sdk",
"files": [
"index.js",
"dist/*.js",
"src/**/*",
"esm/**/*",
Expand All @@ -39,21 +41,6 @@
"typecheck": "./node_modules/.bin/tsc --noEmit",
"prepublishOnly": "yarn build"
},
"exports": {
".": {
"import": {
"types": "./esm/types/index.d.ts",
"default": "./esm/index.mjs"
},
"require": {
"types": "./lib/types/index.d.ts",
"default": "./lib/index.js"
}
},
"./lib/": "./lib/",
"./lib/simple": "./lib/simple/index.js",
"./dist/wazo-sdk": "./dist/wazo-sdk.js"
},
"dependencies": {
"events": "^3.3.0",
"fstream": "^1.0.12",
Expand Down Expand Up @@ -84,7 +71,7 @@
"@types/jest": "^29.5.2",
"@types/jsrsasign": "^10.5.8",
"@types/node": "^20.4.0",
"@types/sdp-transform": "^2.4.6",
"@types/sdp-transform": "^2.4.9",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"babel-core": "^7.0.0-bridge.0",
Expand Down
29 changes: 10 additions & 19 deletions src/__tests__/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ client.setToken(token);
describe('With correct API results', () => {
beforeEach(() => {
jest.resetModules();
// @ts-expect-error
global.fetch = jest.fn(() => Promise.resolve(mockedJson));
global.fetch = jest.fn(() => Promise.resolve(mockedJson)) as any;
});

describe('logIn test', () => {
Expand All @@ -102,13 +101,12 @@ describe('With correct API results', () => {
Authorization: `Basic ${Base64.encode(`${username}:${password}`)}`,
'Content-Type': 'application/json',
};
// @ts-expect-error

const result = await client.auth.logIn({
username,
password,
});
} as any) as any;
expect(result).toBeInstanceOf(Session);
// @ts-expect-error
expect(result.token).toBe(1);
expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token`, {
method: 'post',
Expand All @@ -123,8 +121,7 @@ describe('With correct API results', () => {
describe('logOut test', () => {
it('should delete the specified token', async () => {
const oldToken = 123;
// @ts-expect-error
await client.auth.logOut(oldToken);
await client.auth.logOut(oldToken as any);
expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token/${oldToken}`, {
method: 'delete',
body: null,
Expand All @@ -139,8 +136,7 @@ describe('With correct API results', () => {
describe('With unAuthorized API results', () => {
beforeEach(() => {
jest.resetModules();
// @ts-expect-error
global.fetch = jest.fn(() => Promise.resolve(mockedUnAuthorized));
global.fetch = jest.fn(() => Promise.resolve(mockedUnAuthorized)) as any;
});

describe('checkLogin test', () => {
Expand All @@ -162,8 +158,7 @@ describe('With unAuthorized API results', () => {
describe('With not found API results', () => {
beforeEach(() => {
jest.resetModules();
// @ts-expect-error
global.fetch = jest.fn(() => Promise.resolve(mockedNotFound));
global.fetch = jest.fn(() => Promise.resolve(mockedNotFound)) as any;
});

describe('fetchVoicemail test', () => {
Expand Down Expand Up @@ -198,19 +193,17 @@ describe('With not found API results', () => {
describe('With erroneous text API results', () => {
beforeEach(() => {
jest.resetModules();
// @ts-expect-error
global.fetch = jest.fn(() => Promise.resolve(mockedTextError));
global.fetch = jest.fn(() => Promise.resolve(mockedTextError)) as any;
});

it('throw an exception when the response is >= 500', async () => {
let error = null;

try {
// @ts-expect-error
await client.auth.logIn({
username,
password,
});
} as any);
} catch (e: any) {
error = e;
}
Expand All @@ -225,19 +218,17 @@ describe('With erroneous text API results', () => {
describe('With erroneous json API results', () => {
beforeEach(() => {
jest.resetModules();
// @ts-expect-error
global.fetch = jest.fn(() => Promise.resolve(mockedJsonError));
global.fetch = jest.fn(() => Promise.resolve(mockedJsonError)) as any;
});

it('throw an exception when the response is >= 500', async () => {
let error = null;

try {
// @ts-expect-error
await client.auth.logIn({
username,
password,
});
} as any);
} catch (e: any) {
error = e;
}
Expand Down
38 changes: 13 additions & 25 deletions src/__tests__/web-rtc-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ jest.mock('sip.js/lib/platform/web/transport');
jest.mock('sip.js/lib/api/user-agent', () => ({
start: () => {},
UserAgent: class UserAgent {
// @ts-ignore
static makeURI = () => {};
static makeURI: () => null = () => null;

// @ts-ignore
start = () => {};

transport = {};
},
}));

// @ts-expect-error
const client = new WebRTCClient({});
const client = new WebRTCClient({} as any, null, undefined);

describe('WebRTC client', () => {
it('should compute muted/unmuted state', async () => {
Expand Down Expand Up @@ -65,14 +62,11 @@ describe('WebRTC client', () => {
},
},
};
// @ts-expect-error
expect(client.isAudioMuted(mutedSession)).toBeTruthy();
// @ts-expect-error
expect(client.isAudioMuted(oldKindMuted)).toBeTruthy();
// @ts-expect-error
expect(client.isAudioMuted(unMutedSession)).toBeFalsy();
// @ts-expect-error
expect(client.isAudioMuted(oldKindUnmuted)).toBeFalsy();

expect(client.isAudioMuted(mutedSession as any)).toBeTruthy();
expect(client.isAudioMuted(oldKindMuted as any)).toBeTruthy();
expect(client.isAudioMuted(unMutedSession as any)).toBeFalsy();
expect(client.isAudioMuted(oldKindUnmuted as any)).toBeFalsy();
});
});
describe('changeAudioInputDevice', () => {
Expand Down Expand Up @@ -131,27 +125,21 @@ describe('changeAudioInputDevice', () => {
},
});
it('should change the audio input track if the provided id is different', async () => {
// @ts-expect-error
client.setMediaConstraints(constraints);
client.setMediaConstraints(constraints as any);
expect(client.getAudioDeviceId()).toBe(defaultId);
// @ts-expect-error
const result = await client.changeAudioInputDevice(deviceId, session);
const result = await client.changeAudioInputDevice(deviceId, session as any);
expect(result).toBeTruthy();
});
it('should NOT change the audio input track if the provided id is the same', async () => {
// @ts-expect-error
client.setMediaConstraints(constraints);
client.setMediaConstraints(constraints as any);
expect(client.getAudioDeviceId()).toBe(defaultId);
// @ts-expect-error
const result = await client.changeAudioInputDevice(defaultId, session);
const result = await client.changeAudioInputDevice(defaultId, session as any);
expect(result).toBeFalsy();
});
it('should change the audio input track if the provided id is the same and force param is TRUE', async () => {
// @ts-expect-error
client.setMediaConstraints(constraints);
client.setMediaConstraints(constraints as any);
expect(client.getAudioDeviceId()).toBe(defaultId);
// @ts-expect-error
const result = await client.changeAudioInputDevice(defaultId, session, true);
const result = await client.changeAudioInputDevice(defaultId, session as any, true);
expect(result).toBeTruthy();
});
describe('setVideoInputDevice', () => {
Expand Down
7 changes: 2 additions & 5 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type LoginParams = {
};

type SendResetPasswordArgs = {
username: string,
email: string,
username?: string,
email?: string,
};

export interface AuthD {
Expand Down Expand Up @@ -166,9 +166,6 @@ export default ((client: ApiRequester, baseUrl: string): AuthD => ({
sendResetPasswordEmail: ({
username,
email,
}: {
username: string | null | undefined;
email: string | null | undefined;
}) => {
const body: any = {};

Expand Down
6 changes: 3 additions & 3 deletions src/api/call-logd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import ApiRequester from '../utils/api-requester';
import CallLog from '../domain/CallLog';

export interface CallLogD {
search: (search: string, limit: number) => Promise<Array<CallLog>>;
searchBy: (field: string, value: string, limit: number) => Promise<Array<CallLog>>;
search: (search: string, limit?: number) => Promise<Array<CallLog>>;
searchBy: (field: string, value: string, limit?: number) => Promise<Array<CallLog>>;
listCallLogs: (offset?: number, limit?: number) => Promise<Array<CallLog>>;
listDistinctCallLogs: (offset: number, limit: number, distinct?: string) => Promise<Array<CallLog>>;
listDistinctCallLogs: (offset: number, limit?: number, distinct?: string) => Promise<Array<CallLog>>;
listCallLogsFromDate: (from: Date, number: string) => Promise<Array<CallLog>>;
}

Expand Down
34 changes: 10 additions & 24 deletions src/api/chatd.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
import ApiRequester from '../utils/api-requester';
import type { UUID } from '../domain/types';
import type { PresenceResponse, UUID } from '../domain/types';
import Profile from '../domain/Profile';
import ChatRoom from '../domain/ChatRoom';
import type { ChatUser, ChatMessageListResponse } from '../domain/ChatMessage';
import ChatMessage from '../domain/ChatMessage';

export type PresenceResponse = {
lines: Array<{
id: number;
state: string;
}>;
sessions: Array<{
mobile: boolean;
uuid: string;
}>;
state: string;
status: string;
user_uuid: string;
};
type PresenceListResponse = {
filtered: number;
total: number;
items: Array<PresenceResponse>;
};
type GetMessagesOptions = {
direction: string | null | undefined;
limit: number | null | undefined;
order: string | null | undefined;
offset: string | null | undefined;
search: string | null | undefined;
distinct: string | null | undefined;
direction?: string;
limit?: number;
order?: string;
offset?: string;
search?: string;
distinct?: string;
};

export interface ChatD {
Expand All @@ -43,7 +30,7 @@ export interface ChatD {
createRoom: (name: string, users: Array<ChatUser>) => Promise<ChatRoom>;
getRoomMessages: (roomUuid: string, params?: GetMessagesOptions) => Promise<Array<ChatMessage>>;
sendRoomMessage: (roomUuid: string, message: ChatMessage) => Promise<ChatMessage>;
getMessages: (options: GetMessagesOptions) => Promise<ChatMessage>;
getMessages: (options: GetMessagesOptions) => Promise<ChatMessageListResponse>;
}

// split contact status retrieval to avoid `414 Request-URI Too Large`.
Expand All @@ -68,9 +55,8 @@ export default ((client: ApiRequester, baseUrl: string): ChatD => ({
const uuids: Array<UUID> = contactUuids || [];

if (uuids.length > MAX_PRESENCE_FETCHED) {
const requests = uuids.reduce((acc, _, i) => {
const requests = uuids.reduce((acc: Promise<PresenceResponse[]>[], _, i) => {
if (i % MAX_PRESENCE_FETCHED === 0) {
// @ts-ignore
acc.push(this.getMultipleLineState(uuids.slice(i, i + MAX_PRESENCE_FETCHED)));
}
return acc;
Expand All @@ -97,5 +83,5 @@ export default ((client: ApiRequester, baseUrl: string): ChatD => ({
return client.get(`${baseUrl}/users/me/rooms/${roomUuid}/messages${qs.length ? `?${qs}` : ''}`).then((response: ChatMessageListResponse) => ChatMessage.parseMany(response));
},
sendRoomMessage: async (roomUuid: string, message: ChatMessage): Promise<ChatMessage> => client.post(`${baseUrl}/users/me/rooms/${roomUuid}/messages`, message).then(ChatMessage.parse),
getMessages: async (options: GetMessagesOptions): Promise<ChatMessage> => client.get(`${baseUrl}/users/me/rooms/messages`, options),
getMessages: async (options: GetMessagesOptions): Promise<ChatMessageListResponse> => client.get(`${baseUrl}/users/me/rooms/messages`, options),
}));
Loading

0 comments on commit dc29e39

Please sign in to comment.