@ragrails/api-playground-react 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +159 -68
- package/dist/index.cjs +12 -11
- package/dist/index.js +552 -1305
- package/dist/src/features/api-widget/{ApiWidget.d.ts → ApiPlayground.d.ts} +7 -7
- package/dist/src/features/api-widget/index.d.ts +2 -2
- package/dist/src/index.d.ts +1 -1
- package/dist/src/lib/highlight.d.ts +1 -5
- package/dist/styles.css +1 -1
- package/package.json +39 -9
package/README.md
CHANGED
|
@@ -1,23 +1,44 @@
|
|
|
1
1
|
# @ragrails/api-playground-react
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@ragrails/api-playground-react)
|
|
4
|
-
[](https://bundlephobia.com/package/@ragrails/api-playground-react)
|
|
5
5
|
[](https://www.npmjs.com/package/@ragrails/api-playground-react)
|
|
6
6
|
[](./LICENSE)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
A lightweight, embeddable React API playground for interactive API docs, developer portals, SDK docs, and internal tools.
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-

|
|
12
|
-
-->
|
|
13
|
-
Drop in a cURL command and get a polished docs widget: language-tabbed code snippets (cURL · JavaScript · Python · Go) and an interactive **Try it out** console that sends real requests, with headers/body/auth editing, response viewing, and history.
|
|
10
|
+
Give developers a fast way to understand and test your API without leaving your docs. Add one component and get cURL-powered code snippets, a **Try it out** console, request editing, auth, cURL import, live API responses, and history.
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Embeddable API tester**: add an interactive playground to docs, portals, and internal tools.
|
|
15
|
+
- **Multi-language examples**: show cURL, JavaScript, Python, and Go snippets.
|
|
16
|
+
- **Try-it-out console**: let developers edit requests and see responses immediately.
|
|
17
|
+
- **cURL import**: paste an existing cURL command and start testing from it.
|
|
18
|
+
- **Headers, body, and auth**: support common request controls out of the box.
|
|
19
|
+
- **Sample responses**: show expected output alongside the request.
|
|
20
|
+
- **Request history**: help users move between previous API calls.
|
|
21
|
+
- **Brandable design**: match your product with theme and color options.
|
|
22
|
+
- **Light, dark, and auto mode**: choose a fixed theme or follow the user's system preference.
|
|
23
|
+
- **Lightweight setup**: no Tailwind setup and no required stylesheet import.
|
|
24
|
+
|
|
25
|
+
## Benefits
|
|
26
|
+
|
|
27
|
+
- **Reduce friction**: users can test an endpoint at the moment they read about it.
|
|
28
|
+
- **Improve comprehension**: examples become interactive instead of static.
|
|
29
|
+
- **Speed up onboarding**: new developers can see inputs, auth, and responses in context.
|
|
30
|
+
- **Keep docs consistent**: one cURL request powers the experience.
|
|
31
|
+
- **Fit existing products**: embed it where your users already learn and work.
|
|
32
|
+
|
|
33
|
+
## Use Cases
|
|
34
|
+
|
|
35
|
+
- Interactive API documentation
|
|
36
|
+
- Developer portals
|
|
37
|
+
- SDK documentation
|
|
38
|
+
- API onboarding flows
|
|
39
|
+
- Internal API tools
|
|
40
|
+
- Support and debugging pages
|
|
41
|
+
- REST API testing inside React apps
|
|
21
42
|
|
|
22
43
|
## Install
|
|
23
44
|
|
|
@@ -25,96 +46,166 @@ An embeddable, themeable **API playground** for React.
|
|
|
25
46
|
npm i @ragrails/api-playground-react
|
|
26
47
|
```
|
|
27
48
|
|
|
28
|
-
`react` and `react-dom`
|
|
49
|
+
`react` and `react-dom` are peer dependencies. React 18 and 19 are supported.
|
|
29
50
|
|
|
30
|
-
## Quick
|
|
51
|
+
## Quick Start
|
|
31
52
|
|
|
32
53
|
```tsx
|
|
33
|
-
import {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
54
|
+
import { ApiPlayground } from '@ragrails/api-playground-react'
|
|
55
|
+
|
|
56
|
+
export function ApiDocs() {
|
|
57
|
+
return (
|
|
58
|
+
<ApiPlayground
|
|
59
|
+
request={`curl -X GET 'https://jsonplaceholder.typicode.com/users/1'`}
|
|
60
|
+
/>
|
|
61
|
+
)
|
|
38
62
|
}
|
|
39
63
|
```
|
|
40
64
|
|
|
41
|
-
That
|
|
65
|
+
That is enough to render a snippet card with a **Try it out** console.
|
|
66
|
+
|
|
67
|
+
Styles are included automatically. An optional CSS export is available if your app prefers explicit stylesheet loading:
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import '@ragrails/api-playground-react/styles.css'
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## A Complete Example
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import { useState } from 'react'
|
|
77
|
+
import { ApiPlayground } from '@ragrails/api-playground-react'
|
|
78
|
+
|
|
79
|
+
const initialRequest = `curl -X POST 'https://jsonplaceholder.typicode.com/posts' \\
|
|
80
|
+
-H 'Content-Type: application/json' \\
|
|
81
|
+
-d '{ "title": "API client", "body": "Live request from the widget", "userId": 1 }'`
|
|
82
|
+
|
|
83
|
+
export function CreatePostDocs() {
|
|
84
|
+
const [request, setRequest] = useState(initialRequest)
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<ApiPlayground
|
|
88
|
+
request={request}
|
|
89
|
+
onUpdateRequest={setRequest}
|
|
90
|
+
title="Create Post"
|
|
91
|
+
sampleResponse={`{
|
|
92
|
+
"id": 101,
|
|
93
|
+
"title": "API client",
|
|
94
|
+
"body": "Live request from the widget",
|
|
95
|
+
"userId": 1
|
|
96
|
+
}`}
|
|
97
|
+
customization={{
|
|
98
|
+
primary: '#7855ff',
|
|
99
|
+
background: '#16171d',
|
|
100
|
+
}}
|
|
101
|
+
/>
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
```
|
|
42
105
|
|
|
43
106
|
## Props
|
|
44
107
|
|
|
45
108
|
| Prop | Type | Default | Description |
|
|
46
109
|
| --- | --- | --- | --- |
|
|
47
|
-
| `request` | `string` |
|
|
48
|
-
| `
|
|
49
|
-
| `
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
110
|
+
| `request` | `string` | Required | The cURL request shown and tested by the widget. |
|
|
111
|
+
| `onUpdateRequest` | `(request: string) => void` | `undefined` | Receives the latest cURL request after edits or imports. |
|
|
112
|
+
| `title` | `string` | inferred | Label shown above the snippet. Defaults to the request method and path. |
|
|
113
|
+
| `sampleResponse` | `string` | `undefined` | Example response body shown below the snippet. JSON strings are syntax-highlighted. |
|
|
114
|
+
| `editable` | `boolean` | `true` | Enables or disables editing in the console. |
|
|
115
|
+
| `allowImport` | `boolean` | `true` | Shows or hides the cURL import action. |
|
|
116
|
+
| `customization` | `{ primary?: string; background?: string }` | `undefined` | Overrides the widget's primary color and background. Accepts any CSS color. |
|
|
117
|
+
| `mode` | `'dark' \| 'light' \| 'system'` | `'dark'` | Controls the widget theme. `system` follows the user's OS preference. |
|
|
118
|
+
| `syncSnippet` | `boolean` | `false` | When `true`, the snippet follows console edits. |
|
|
56
119
|
|
|
57
|
-
##
|
|
120
|
+
## Common Patterns
|
|
58
121
|
|
|
59
|
-
|
|
122
|
+
### Read-only documentation
|
|
123
|
+
|
|
124
|
+
Use this when the page should explain an endpoint but not allow mutation.
|
|
60
125
|
|
|
61
126
|
```tsx
|
|
62
|
-
|
|
63
|
-
<ApiWidget
|
|
127
|
+
<ApiPlayground
|
|
64
128
|
request={curl}
|
|
65
|
-
|
|
66
|
-
|
|
129
|
+
editable={false}
|
|
130
|
+
allowImport={false}
|
|
67
131
|
/>
|
|
68
|
-
|
|
69
|
-
// Follow the operating system
|
|
70
|
-
<ApiWidget request={curl} mode="system" />
|
|
71
132
|
```
|
|
72
133
|
|
|
73
|
-
|
|
134
|
+
### Controlled request state
|
|
74
135
|
|
|
75
|
-
|
|
136
|
+
Use `onUpdateRequest` when the parent app should own the latest cURL string.
|
|
76
137
|
|
|
77
138
|
```tsx
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
title="Create Post"
|
|
84
|
-
sampleResponse={`{ "id": 101, "title": "Hello", "userId": 1 }`}
|
|
139
|
+
const [request, setRequest] = useState(curl)
|
|
140
|
+
|
|
141
|
+
<ApiPlayground
|
|
142
|
+
request={request}
|
|
143
|
+
onUpdateRequest={setRequest}
|
|
85
144
|
/>
|
|
145
|
+
```
|
|
86
146
|
|
|
87
|
-
|
|
88
|
-
|
|
147
|
+
### Brand customization
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
<ApiPlayground
|
|
151
|
+
request={curl}
|
|
152
|
+
customization={{
|
|
153
|
+
primary: '#52b788',
|
|
154
|
+
background: '#101114',
|
|
155
|
+
}}
|
|
156
|
+
/>
|
|
89
157
|
```
|
|
90
158
|
|
|
91
|
-
|
|
159
|
+
### Light, dark, and auto mode
|
|
160
|
+
|
|
161
|
+
Use a fixed theme, or let the widget automatically match the user's system preference.
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
<ApiPlayground request={curl} mode="light" />
|
|
165
|
+
<ApiPlayground request={curl} mode="dark" />
|
|
166
|
+
<ApiPlayground request={curl} mode="system" />
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Live Testing
|
|
170
|
+
|
|
171
|
+
The console sends real browser requests and displays the response status, timing, headers, and body.
|
|
172
|
+
|
|
173
|
+
For protected APIs, users can configure bearer tokens or API key headers directly in the console.
|
|
92
174
|
|
|
93
|
-
|
|
175
|
+
## cURL Import
|
|
94
176
|
|
|
95
|
-
|
|
177
|
+
When import is enabled, users can paste a cURL request and continue testing from it. This is useful for docs, support flows, onboarding, and internal tools where developers already share API examples as cURL.
|
|
96
178
|
|
|
97
|
-
##
|
|
179
|
+
## Styling
|
|
98
180
|
|
|
99
|
-
|
|
181
|
+
The widget includes its styles automatically. Consumers do not need Tailwind, a Tailwind config, or a required stylesheet import.
|
|
182
|
+
|
|
183
|
+
The optional stylesheet export remains available:
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
import '@ragrails/api-playground-react/styles.css'
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Use that path if your app prefers explicit CSS loading.
|
|
190
|
+
|
|
191
|
+
The widget is themeable with `mode` and `customization`.
|
|
192
|
+
|
|
193
|
+
## TypeScript
|
|
194
|
+
|
|
195
|
+
Types are included. No separate `@types` package is required.
|
|
100
196
|
|
|
101
197
|
```ts
|
|
102
|
-
import {
|
|
103
|
-
ApiWidget,
|
|
104
|
-
RequestSnippet, // snippet-only view
|
|
105
|
-
ApiConsole, // interactive console
|
|
106
|
-
ImportCard, // cURL import card
|
|
107
|
-
parseCurl, // cURL string -> WidgetRequest
|
|
108
|
-
generateSnippet, // WidgetRequest -> code string (curl/js/python/go)
|
|
109
|
-
executeWidgetRequest // run a WidgetRequest with fetch
|
|
110
|
-
} from '@ragrails/api-playground-react'
|
|
111
|
-
|
|
112
|
-
import type {
|
|
113
|
-
ApiWidgetProps, ApiWidgetMode, ApiWidgetCustomization,
|
|
114
|
-
WidgetRequest, WidgetResponse, WidgetHeader, WidgetAuth, SnippetLanguage,
|
|
115
|
-
} from '@ragrails/api-playground-react'
|
|
198
|
+
import type { ApiPlaygroundProps } from '@ragrails/api-playground-react'
|
|
116
199
|
```
|
|
117
200
|
|
|
201
|
+
## Notes
|
|
202
|
+
|
|
203
|
+
- The browser enforces CORS for real requests.
|
|
204
|
+
- Keep secrets out of checked-in docs examples. Let users paste tokens in the console when possible.
|
|
205
|
+
- Use `editable={false}` for destructive endpoints if you only want to document the request.
|
|
206
|
+
- Use `syncSnippet={false}` when your docs should preserve the canonical example while users experiment.
|
|
207
|
+
|
|
118
208
|
## License
|
|
119
209
|
|
|
120
210
|
MIT
|
|
211
|
+
# api-playground
|