Skip to content

Commit 191e7bb

Browse files
committed
fix up optional context pattern
1 parent f0b5434 commit 191e7bb

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

example/src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function App() {
1212
registryUrl: import.meta.env.VITE_SPEAKEASY_REGISTRY_URL,
1313
});
1414

15+
// You can use the context provider to pass the core SDK instance to the code
16+
// samples viewer
1517
return (
1618
<SpeakeasyCodeSamplesProvider client={coreSdk}>
1719
<h1 className="">Speakeasy Code Samples</h1>
@@ -21,6 +23,7 @@ function App() {
2123
defaultLang={"typescript"}
2224
title={CodeSampleFilenameTitle}
2325
operation={"getPassageText"}
26+
// client={coreSdk}
2427
/>
2528
</div>
2629
</SpeakeasyCodeSamplesProvider>

src/react-components/code-sample.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function CodeSamplesViewer({
6767
style,
6868
copyable,
6969
defaultLang,
70+
client: clientProp,
7071
}: CodeSamplesViewerProps) {
7172
const TitleComponent = title;
7273

@@ -82,7 +83,7 @@ export function CodeSamplesViewer({
8283
return { methoPaths: [operation] };
8384
}, [operation]);
8485

85-
const client = useSafeSpeakeasyCodeSamplesContext();
86+
const client = useSafeSpeakeasyCodeSamplesContext(clientProp);
8687
const { status, data, error } = useHighlightedCodeSamples(client, request);
8788

8889
const { selectedSnippet, selectedLang, setSelectedLang } = useSelectedSnippet(

src/react-components/hooks.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ type SuccessState = {
3232

3333
type CodeSamplesState = LoadingState | ErrorState | SuccessState;
3434

35-
export const useSafeSpeakeasyCodeSamplesContext = (opts?: {
36-
apiKey: string;
37-
registryUrl: string;
38-
}) => {
39-
if (opts) {
40-
return new SpeakeasyCodeSamplesCore(opts);
35+
/** Intended to give the user the option of providing their own client. */
36+
export const useSafeSpeakeasyCodeSamplesContext = (
37+
coreClient?: SpeakeasyCodeSamplesCore,
38+
) => {
39+
if (coreClient) {
40+
return coreClient;
4141
}
4242

4343
try {

0 commit comments

Comments
 (0)