Skip to content

Commit dc18a7d

Browse files
committed
added documentation for the react component
1 parent d8c7da0 commit dc18a7d

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @speakeasyapi/code-samples
22

3-
Developer-friendly & type-safe Typescript SDK specifically catered to leverage *@speakeasyapi/code-samples* API.
3+
Developer-friendly & type-safe Typescript SDK specifically catered to leverage _@speakeasyapi/code-samples_ API.
44

55
<div align="left">
66
<a href="https://www.speakeasy.com/?utm_source=@speakeasyapi/code-samples&utm_campaign=typescript"><img src="https://custom-icon-badges.demolab.com/badge/-Built%20By%20Speakeasy-212015?style=for-the-badge&logoColor=FBE331&logo=speakeasy&labelColor=545454" /></a>
@@ -9,8 +9,8 @@ Developer-friendly & type-safe Typescript SDK specifically catered to leverage *
99
</a>
1010
</div>
1111

12-
1312
<br /><br />
13+
1414
> [!IMPORTANT]
1515
> This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your [workspace](https://app.speakeasy.com/org/speakeasy-self/speakeasy-self). Delete this section before > publishing to a package manager.
1616
@@ -123,6 +123,49 @@ run();
123123
```
124124
<!-- End SDK Example Usage [usage] -->
125125

126+
### React Component
127+
128+
This library includes a React component that fetches and highlights code
129+
snippets using `highlight.js`. Along with displaying the snippet, it shows a loading state during
130+
fetching and provides a fallback view if an error occurs.
131+
132+
```tsx
133+
import { SpeakeasyCodeSamplesCore } from "@speakeasyapi/code-samples/core";
134+
import {
135+
CodeSample,
136+
SpeakeasyCodeSamplesProvider,
137+
} from "@speakeasyapi/code-samples/react";
138+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
139+
140+
const queryClient = new QueryClient();
141+
142+
const speakeasyCodeSamples = new SpeakeasyCodeSamplesCore({
143+
apiKey: "<YOUR_API_KEY_HERE>",
144+
registryUrl: "https://spec.speakeasy.com/org/ws/my-source",
145+
});
146+
147+
// Retries are handled by the underlying SDK.
148+
queryClient.setQueryDefaults(["@speakeasyapi/code-samples"], { retry: false });
149+
150+
queryClient.setMutationDefaults(["@speakeasyapi/code-samples"], {
151+
retry: false,
152+
});
153+
154+
function App() {
155+
return (
156+
<QueryClientProvider client={queryClient}>
157+
<SpeakeasyCodeSamplesProvider client={speakeasyCodeSamples}>
158+
<CodeSample operationId="getPetById" language="typescript" />
159+
</SpeakeasyCodeSamplesProvider>
160+
</QueryClientProvider>
161+
);
162+
}
163+
```
164+
165+
> [!NOTE]
166+
> To apply styles to the highlighted code, import a `highlight.js` theme CSS
167+
> file into your project using methods like a `<link>` tag.
168+
126169
<!-- Start Authentication [security] -->
127170
## Authentication
128171

@@ -541,7 +584,7 @@ looking for the latest version.
541584

542585
## Contributions
543586

544-
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation.
545-
We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
587+
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation.
588+
We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
546589

547590
### SDK Created by [Speakeasy](https://www.speakeasy.com/?utm_source=@speakeasyapi/code-samples&utm_campaign=typescript)

src/react-components/code-sample.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
type CodeSamplesGetQueryData,
55
type QueryHookOptions,
66
} from "../react-query/index.js";
7-
import { Highlight, type HighlightLanguage as Language } from "./highlight.js";
7+
import {
8+
Highlight,
9+
type HighlightLanguage as CodeSampleLanguage,
10+
} from "./highlight.js";
811

912
type CodeSampleProps = {
1013
/**
@@ -20,7 +23,7 @@ type CodeSampleProps = {
2023
operationId: string;
2124

2225
/** The language of the code sample that you would like to fetch. */
23-
language: Language;
26+
language: CodeSampleLanguage;
2427

2528
/** The options for the TanStack query that fetches the code snippet. */
2629
queryOptions?: QueryHookOptions<CodeSamplesGetQueryData>;

0 commit comments

Comments
 (0)