Skip to content

Commit

Permalink
Merge pull request #185 from SuperViz/feat/new-room-oackage-params
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 authored Dec 13, 2024
2 parents cd920e5 + c8d2042 commit 0de1c75
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
2 changes: 2 additions & 0 deletions apps/playground/src/pages/superviz-room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function SuperVizRoom() {
id: uuid,
},
roomId: `${SUPERVIZ_ROOM_PREFIX}-${componentName}`,
debug: true,
environment: 'dev',
});

room.current = newRoom;
Expand Down
66 changes: 66 additions & 0 deletions packages/room/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Room } from './core';
import config from './services/config';

import { createRoom } from '.';

Expand Down Expand Up @@ -83,4 +84,69 @@ describe('createRoom', () => {
const createRoomPromise2 = createRoom(params2);
await expect(createRoomPromise2).rejects.toThrow('[SuperViz | Room] Developer Key is required');
});

test('sets up the environment correctly', async () => {
const params = {
developerKey: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
name: 'John Doe',
},
};

await createRoom(params);

expect(config.get('apiKey')).toBe('abc123');
expect(config.get('roomId')).toBe('abc123');
expect(config.get('environment')).toBe('prod');
expect(config.get('debug')).toBe(false);

const paramsWithOptionalFields = {
developerKey: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
name: 'John Doe',
},
debug: true,
environment: 'dev' as 'dev',
};

await createRoom(paramsWithOptionalFields);

expect(config.get('apiKey')).toBe('abc123');
expect(config.get('roomId')).toBe('abc123');
expect(config.get('environment')).toBe('dev');
expect(config.get('debug')).toBe(true);
});

test('set up the environment with the correct API URL', async () => {
const params = {
developerKey: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
name: 'John Doe',
},
};

await createRoom(params);

expect(config.get('apiUrl')).toBe('https://api.superviz.com');

const paramsWithProdEnvironment = {
developerKey: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
name: 'John Doe',
},
environment: 'dev' as 'dev',
};

await createRoom(paramsWithProdEnvironment);

expect(config.get('apiUrl')).toBe('https://dev.nodeapi.superviz.com');
});
});
17 changes: 11 additions & 6 deletions packages/room/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ import { InitializeRoomParams, InitializeRoomSchema } from './types';
* Will throw an error if the configuration fails to load
* from the server or if the API key is invalid.
*/
async function setUpEnvironment(developerKey: string, roomId: string) {
config.set('apiUrl', 'https://dev.nodeapi.superviz.com');
async function setUpEnvironment({
developerKey,
roomId,
environment,
debug: enableDebug,
}: InitializeRoomParams): Promise<void> {
config.set('apiKey', developerKey);
config.set('debug', true);
config.set('debug', !!enableDebug);
config.set('roomId', roomId);
config.set('environment', 'dev');
config.set('environment', environment ?? 'prod');
config.set('apiUrl', config.get('environment') === 'prod' ? 'https://api.superviz.com' : 'https://dev.nodeapi.superviz.com');

if (config.get('debug')) {
if (enableDebug) {
console.log('[SuperViz | Room] Debug mode enabled');
debug.enable('@superviz/*');
} else {
Expand Down Expand Up @@ -63,7 +68,7 @@ export async function createRoom(params: InitializeRoomParams): Promise<Room> {
try {
const { developerKey, participant, roomId } = InitializeRoomSchema.parse(params);

await setUpEnvironment(developerKey, roomId);
await setUpEnvironment(params);

return new Room({ participant: participant as Participant });
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions packages/room/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const InitializeRoomSchema = z.object({
id: z.string().regex(pattern, { message: '[SuperViz | Room] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».|\'"' }),
name: z.string(),
}),
debug: z.boolean().optional(),
environment: z.enum(['dev', 'prod'], { message: '[SuperViz | Room] Environment must be either "dev" or "prod"' }).optional(),
});

export type InitializeRoomParams = z.infer<typeof InitializeRoomSchema>;

0 comments on commit 0de1c75

Please sign in to comment.