Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion packages/react-client/src/hooks/useSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,36 @@ export const useSandbox = (props: UseSandboxProps) => {
[sandboxApiUrl],
);

return { getSandboxPeerToken, getSandboxViewerToken, getSandboxLivestream };
const fetchMoqToken = useCallback(
async (streamName: string, type: "subscriber" | "publisher") => {
if (!sandboxApiUrl) throw new MissingSandboxApiUrlError();

const urlEncodedStreamName = encodeURIComponent(streamName);

const res = await fetch(`${sandboxApiUrl}/moq/${urlEncodedStreamName}/${type}`);
if (!res.ok) throw new Error(`Failed to retrieve MoQ ${type} token for stream '${streamName}'.`);

const data: { token: string } = await res.json();
return data.token;
},
[sandboxApiUrl],
);

const getSandboxMoqPublisherToken = useCallback(
async (streamName: string) => fetchMoqToken(streamName, "publisher"),
[fetchMoqToken],
);

const getSandboxMoqSubscriberToken = useCallback(
async (streamName: string) => fetchMoqToken(streamName, "subscriber"),
[fetchMoqToken],
);

return {
getSandboxPeerToken,
getSandboxViewerToken,
getSandboxLivestream,
getSandboxMoqPublisherToken,
getSandboxMoqSubscriberToken,
};
};
Loading