Skip to content

Commit c33a139

Browse files
committed
docs: add live playground demos to getting-started and request guides
1 parent aee52d0 commit c33a139

6 files changed

Lines changed: 264 additions & 33 deletions

File tree

‎docs/guide/getting-started.md‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎docs/guide/getting-started.mdx‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Getting Started
2+
3+
`@cc-heart/utils` is a library of JavaScript common tools — the environment-agnostic core of the utils family.
4+
5+
## Install
6+
7+
```shell
8+
npm install @cc-heart/utils
9+
# or
10+
pnpm add @cc-heart/utils
11+
```
12+
13+
## Usage
14+
15+
```js
16+
import { capitalize } from '@cc-heart/utils'
17+
18+
capitalize('string') // String
19+
```
20+
21+
## What's inside
22+
23+
- **string** — `capitalize`, `unCapitalize`, ...
24+
- **date** — date formatting helpers
25+
- **random** — random number / string generators
26+
- **validate** — type & value validators
27+
- **promise** — async helpers (`retry`, `withTimeout`, ...)
28+
- **request** — fetch-based HTTP client, see the [Request guide](/guide/request)
29+
- **sse** — Server-Sent Events, see the [SSE guide](/guide/sse)
30+
31+
## Try it online
32+
33+
Edit the code below and see results instantly — the demo runs against the published `@cc-heart/utils` package:
34+
35+
```tsx playground
36+
import { useState } from 'react'
37+
import { capitalize, unCapitalize, underlineToHump, random } from '@cc-heart/utils'
38+
39+
export default function App() {
40+
const [text, setText] = useState('hello_world')
41+
const [num, setNum] = useState(() => random(1, 100))
42+
43+
return (
44+
<div style={{ fontFamily: 'monospace', lineHeight: 2 }}>
45+
<input value={text} onChange={(e) => setText(e.target.value)} />
46+
<div>capitalize: <b>{capitalize(text)}</b></div>
47+
<div>unCapitalize: <b>{unCapitalize(text)}</b></div>
48+
<div>underlineToHump: <b>{underlineToHump(text)}</b></div>
49+
<div>
50+
random(1, 100): <b>{num}</b>{' '}
51+
<button onClick={() => setNum(random(1, 100))}>re-roll</button>
52+
</div>
53+
</div>
54+
)
55+
}
56+
```
57+
58+
## API Reference
59+
60+
See the auto-generated [API documentation](/api/).
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,44 @@ const data2 = await cachedApi.get('/users', {}, { cache: { ttl: 5000 } }) // cac
114114

115115
const otherApi = new Request('/api') // isolated cache
116116
```
117+
118+
## Try it online
119+
120+
A live `Request` demo against the public JSONPlaceholder API:
121+
122+
```tsx playground
123+
import { useState } from 'react'
124+
import { Request } from '@cc-heart/utils'
125+
126+
const api = new Request('https://jsonplaceholder.typicode.com')
127+
128+
export default function App() {
129+
const [user, setUser] = useState(null)
130+
const [loading, setLoading] = useState(false)
131+
const [error, setError] = useState('')
132+
133+
const load = async (id) => {
134+
setLoading(true)
135+
setError('')
136+
try {
137+
setUser(await api.get(`/users/${id}`))
138+
} catch (e) {
139+
setError(String(e))
140+
} finally {
141+
setLoading(false)
142+
}
143+
}
144+
145+
return (
146+
<div style={{ fontFamily: 'monospace' }}>
147+
<button onClick={() => load(1)}>GET /users/1</button>{' '}
148+
<button onClick={() => load(999)}>GET /users/999 (error)</button>
149+
{loading && <p>loading...</p>}
150+
{error && <p style={{ color: 'red' }}>{error}</p>}
151+
{user && !loading && (
152+
<pre>{JSON.stringify(user, null, 2)}</pre>
153+
)}
154+
</div>
155+
)
156+
}
157+
```

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@rslib/core": "^1.0.2",
5252
"@rslint/core": "^0.9.3",
5353
"@rspress/core": "^2.0.22",
54+
"@rspress/plugin-playground": "^2.0.22",
5455
"@rspress/plugin-typedoc": "^2.0.22",
5556
"@rstest/core": "^0.12.2",
5657
"@types/node": "^18.19.44",

0 commit comments

Comments
 (0)