@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 CHANGED
@@ -1,23 +1,44 @@
1
1
  # @ragrails/api-playground-react
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/@ragrails/api-playground-react.svg)](https://www.npmjs.com/package/@ragrails/api-playground-react)
4
- [![bundle size](https://img.shields.io/bundlephobia/minzip/@ragrails/api-playground-react.svg)](https://bundlephobia.com/package/@ragrails/api-playground-react)
4
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/%40ragrails%2Fapi-playground-react.svg)](https://bundlephobia.com/package/@ragrails/api-playground-react)
5
5
  [![types](https://img.shields.io/npm/types/@ragrails/api-playground-react.svg)](https://www.npmjs.com/package/@ragrails/api-playground-react)
6
6
  [![license](https://img.shields.io/npm/l/@ragrails/api-playground-react.svg)](./LICENSE)
7
7
 
8
- An embeddable, themeable **API playground** for React.
8
+ A lightweight, embeddable React API playground for interactive API docs, developer portals, SDK docs, and internal tools.
9
9
 
10
- <!-- Add a screenshot/GIF here for the npm page, e.g.:
11
- ![API playground widget](https://your-cdn-or-repo/screenshot.png)
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
- - 🎯 **One prop in** — pass a cURL string, everything else is derived.
16
- - 🧪 **Live console** — edit method, URL, headers, body, and auth; **Send** runs a real `fetch`.
17
- - 🧾 **Snippets** cURL, JavaScript, Python, Go, generated from the same request.
18
- - 🌓 **Theming** `dark` / `light` / `system`, plus primary color & background overrides.
19
- - 📦 **Zero Tailwind for consumers** ships a single pre-compiled stylesheet.
20
- - 🔒 **Self-contained** its own theme scope; won't leak styles into your app.
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` (v18 or v19) are peer dependencies.
49
+ `react` and `react-dom` are peer dependencies. React 18 and 19 are supported.
29
50
 
30
- ## Quick start
51
+ ## Quick Start
31
52
 
32
53
  ```tsx
33
- import { ApiWidget } from '@ragrails/api-playground-react'
34
- import '@ragrails/api-playground-react/styles.css' // import once, anywhere in your app
35
-
36
- export function Docs() {
37
- return <ApiWidget request={`curl -X GET 'https://jsonplaceholder.typicode.com/users/1'`} />
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 single `request` drives the snippet view and the interactive console.
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` | | **Required.** The request as a cURL command the source of truth. |
48
- | `title` | `string` | derived | Caption shown above the snippet (e.g. `"Get Users"`). |
49
- | `sampleResponse` | `string` | | Example response (JSON) displayed under the snippet. |
50
- | `mode` | `'dark' \| 'light' \| 'system'` | `'dark'` | Color theme. `'system'` follows the OS and updates live. |
51
- | `customization` | `{ primary?: string; background?: string }` | | Override the brand color / background (any CSS color). |
52
- | `editable` | `boolean` | `true` | When `false`, the console is read-only. |
53
- | `allowImport` | `boolean` | `true` | Show the **Import** action (paste a cURL command). |
54
- | `syncSnippet` | `boolean` | `false` | When `true`, console edits update the start snippet; otherwise it stays the documented request. |
55
- | `onUpdateRequest` | `(curl: string) => void` | | Notified whenever the live request changes. |
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
- ## Theming
120
+ ## Common Patterns
58
121
 
59
- The widget renders its own theme scope, independent of your app's theme.
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
- // Light theme with a custom brand color
63
- <ApiWidget
127
+ <ApiPlayground
64
128
  request={curl}
65
- mode="light"
66
- customization={{ primary: '#0ea5e9' }}
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
- Fonts fall back to the system UI stack. To use a custom typeface, set `--font-heading` / `--font-sans` on a wrapping element.
134
+ ### Controlled request state
74
135
 
75
- ## Examples
136
+ Use `onUpdateRequest` when the parent app should own the latest cURL string.
76
137
 
77
138
  ```tsx
78
- // POST with headers, body, and a sample response
79
- <ApiWidget
80
- request={`curl -X POST 'https://api.example.com/posts' \\
81
- -H 'Content-Type: application/json' \\
82
- -d '{ "title": "Hello", "userId": 1 }'`}
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
- // Read-only documentation widget (no editing, no import)
88
- <ApiWidget request={curl} editable={false} allowImport={false} />
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
- ## How it works
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
- The cURL `request` is the single source of truth. The **snippet** is generated from it, and the **console** parses it into editable fields. Editing in the console updates an internal live request; with `syncSnippet={false}` (default) the documented snippet stays put, so the widget doubles as both reference docs and a live tester.
175
+ ## cURL Import
94
176
 
95
- **Send performs a real `fetch`** to the target URL, so it is subject to CORS the API must allow the browser's origin. Network/CORS failures are shown as an error response.
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
- ## Additional exports
179
+ ## Styling
98
180
 
99
- Besides `ApiWidget`, the package exports the building blocks and helpers:
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