Skip to content

Commit

Permalink
feat: add title config
Browse files Browse the repository at this point in the history
  • Loading branch information
wqcstrong committed Aug 8, 2023
1 parent 5eec946 commit 36b1b54
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
32 changes: 19 additions & 13 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { psLog } from 'src/utils';
import { combineName, parseUserAgent } from 'src/utils/ua';
import { InitConfig } from 'types';

interface TResponse<T> {
code: string;
Expand Down Expand Up @@ -30,6 +31,14 @@ const resolvedProtocol = (() => {
return ['http://', 'ws://'];
})();

const joinQuery = (args: Record<string, unknown>) => {
const params = new URLSearchParams();
Object.entries(args).forEach(([k, v]) => {
params.append(k, String(v));
});
return params.toString();
};

export default class Request {
constructor(public base: string = '') {
/* c8 ignore next 3 */
Expand All @@ -38,11 +47,16 @@ export default class Request {
}
}

createRoom(project: string): Promise<TResponse<TCreateRoom>> {
createRoom(config: Required<InitConfig>): Promise<TResponse<TCreateRoom>> {
const device = parseUserAgent();
const name = combineName(device);
const query = joinQuery({
name,
group: config.project,
title: config.title,
});
return fetch(
`${resolvedProtocol[0]}${this.base}/api/v1/room/create?name=${name}&group=${project}`,
`${resolvedProtocol[0]}${this.base}/api/v1/room/create?${query}`,
{
method: 'POST',
},
Expand All @@ -55,16 +69,8 @@ export default class Request {
}

getRoomUrl(args: Record<string, string | number> = {}) {
const params = Object.keys(args).reduce((acc, cur, index, arr) => {
const val = args[cur];
/* c8 ignore next */
if (val == null) return acc;
let kv = `${cur}=${val}`;
if (index < arr.length - 1) {
kv += '&';
}
return acc + kv;
}, '');
return `${resolvedProtocol[1]}${this.base}/api/v1/ws/room/join?${params}`;
return `${resolvedProtocol[1]}${this.base}/api/v1/ws/room/join?${joinQuery(
args,
)}`;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default class PageSpy {
psLog.error('Cannot get the Request / config info');
return;
}
const { data } = await this.request.createRoom(this.config.project);
const { data } = await this.request.createRoom(this.config);
const roomUrl = this.request.getRoomUrl({
address: data.address,
name: `client:${getRandomId()}`,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const defaultConfig = {
clientOrigin: '',
project: 'default',
autoRender: true,
title: '',
};

const resolveConfig = () => {
Expand All @@ -20,6 +21,7 @@ const resolveConfig = () => {
clientOrigin: origin,
project: 'default',
autoRender: true,
title: '',
};
};

Expand Down
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface InitConfig {
* @default true
*/
autoRender?: boolean;
/**
* Custom title
*/
title?: string;
}

export * as SpyDevice from './lib/device';
Expand Down

0 comments on commit 36b1b54

Please sign in to comment.