Skip to content

Commit 75d8f9c

Browse files
authored
FCE-3471 add sandbox moq methods (#549)
## Description Adds sandbox api MoQ methods to the `useSandbox` hook. ## Documentation impact - [ ] Documentation update required - [ ] Documentation updated [in another PR](_) - [x] No documentation update required ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
1 parent ae87f14 commit 75d8f9c

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

packages/react-client/src/hooks/useSandbox.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,36 @@ export const useSandbox = (props: UseSandboxProps) => {
7979
[sandboxApiUrl],
8080
);
8181

82-
return { getSandboxPeerToken, getSandboxViewerToken, getSandboxLivestream };
82+
const fetchMoqToken = useCallback(
83+
async (streamName: string, type: "subscriber" | "publisher") => {
84+
if (!sandboxApiUrl) throw new MissingSandboxApiUrlError();
85+
86+
const urlEncodedStreamName = encodeURIComponent(streamName);
87+
88+
const res = await fetch(`${sandboxApiUrl}/moq/${urlEncodedStreamName}/${type}`);
89+
if (!res.ok) throw new Error(`Failed to retrieve MoQ ${type} token for stream '${streamName}'.`);
90+
91+
const data: { token: string } = await res.json();
92+
return data.token;
93+
},
94+
[sandboxApiUrl],
95+
);
96+
97+
const getSandboxMoqPublisherToken = useCallback(
98+
async (streamName: string) => fetchMoqToken(streamName, "publisher"),
99+
[fetchMoqToken],
100+
);
101+
102+
const getSandboxMoqSubscriberToken = useCallback(
103+
async (streamName: string) => fetchMoqToken(streamName, "subscriber"),
104+
[fetchMoqToken],
105+
);
106+
107+
return {
108+
getSandboxPeerToken,
109+
getSandboxViewerToken,
110+
getSandboxLivestream,
111+
getSandboxMoqPublisherToken,
112+
getSandboxMoqSubscriberToken,
113+
};
83114
};

0 commit comments

Comments
 (0)