Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
feat: load limits from server
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 committed Jul 19, 2024
1 parent ce84111 commit 6aeb7f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
19 changes: 3 additions & 16 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<Launch
throw new Error('Failed to validate API key');
}

const [environment, waterMark] = await Promise.all([
const [environment, waterMark, limits] = await Promise.all([
ApiService.fetchConfig(apiUrl, apiKey),
ApiService.fetchWaterMark(apiUrl, apiKey),
ApiService.fetchLimits(apiUrl, apiKey),
]).catch(() => {
throw new Error('Failed to load configuration from server');
});
Expand All @@ -129,21 +130,7 @@ const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<Launch
environment: (options.environment as EnvironmentTypes) ?? EnvironmentTypes.PROD,
roomId,
debug: options.debug,
limits: {
presence: {
canUse: true,
maxParticipants: 50,
},
realtime: {
canUse: true,
maxParticipants: 200,
},
videoConference: {
canUse: true,
maxParticipants: 255,
canUseTranscript: true,
},
},
limits,
waterMark,
colors: options.customColors,
features,
Expand Down
20 changes: 11 additions & 9 deletions src/services/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const INVALID_API_KEY = 'unit-test-invalid-api-key';
const MOCK_ABLY_KEY = 'unit-test-ably-key';

const CHECK_LIMITS_MOCK = {
usage: LIMITS_MOCK,
limits: LIMITS_MOCK,
};

const FETCH_PARTICIPANTS_BY_GROUP_MOCK = [
{
id: "any_user_id",
name: "any_name",
id: 'any_user_id',
name: 'any_name',
avatar: null,
email: 'any_email',
}
]
},
];

jest.mock('../../common/utils', () => {
return {
Expand Down Expand Up @@ -209,26 +209,28 @@ describe('ApiService', () => {
const baseUrl = 'https://dev.nodeapi.superviz.com';
const response = await ApiService.fetchLimits(baseUrl, VALID_API_KEY);

expect(response).toEqual(CHECK_LIMITS_MOCK.usage);
expect(response).toEqual(CHECK_LIMITS_MOCK.limits);
});
});

describe('fetchParticipants', () => {
test('should return the participants', async () => {
const response = await ApiService.fetchParticipantsByGroup('any_group_id');

expect(response).toEqual([{"avatar": null, "id": "any_user_id", "name": "any_name", "email": "any_email"}]);
expect(response).toEqual([
{ avatar: null, id: 'any_user_id', name: 'any_name', email: 'any_email' },
]);
});
});

describe('Mentions', () => {
test('should create a mention', async () => {
const response = await ApiService.createMentions({
commentsId: 'any_comment_id',
participants: []
participants: [],
});

expect(response).toEqual({});
});
})
});
});
18 changes: 7 additions & 11 deletions src/services/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { SuperVizSdkOptions } from '../../common/types/sdk-options.types';
import { doRequest } from '../../common/utils';
import { Annotation } from '../../components/comments/types';
import config from '../config';

import { ComponentLimits } from '../limits/types';

import {
AnnotationParams,
CommentParams,
CreateOrUpdateParticipantParams,
FetchAnnotationsParams,
MentionParams
MentionParams,
} from './types';

export default class ApiService {
Expand All @@ -32,11 +32,11 @@ export default class ApiService {
return doRequest(url, 'POST', { apiKey });
}

static async fetchLimits(baseUrl: string, apikey: string) {
const path: string = '/user/check_limits';
static async fetchLimits(baseUrl: string, apikey: string): Promise<ComponentLimits> {
const path: string = '/user/check_limits_v2';
const url: string = this.createUrl(baseUrl, path);
const result = await doRequest(url, 'GET', '', { apikey });
return result.usage;
return result.limits;
}

static async fetchWaterMark(baseUrl: string, apiKey: string) {
Expand Down Expand Up @@ -130,18 +130,14 @@ export default class ApiService {
return doRequest(url, 'POST', body, { apikey });
}

static async fetchParticipantsByGroup(
groupId: string,
) {
static async fetchParticipantsByGroup(groupId: string) {
const path = `/groups/participants/${groupId}`;
const baseUrl = config.get<string>('apiUrl');
const url = this.createUrl(baseUrl, path, { take: 10000 });
return doRequest(url, 'GET', undefined, { apikey: config.get('apiKey') });
}

static async createMentions(
mentionParams: MentionParams
) {
static async createMentions(mentionParams: MentionParams) {
const path = '/mentions';
const baseUrl = config.get<string>('apiUrl');
const url = this.createUrl(baseUrl, path);
Expand Down

0 comments on commit 6aeb7f0

Please sign in to comment.