@ragrails/api-playground-react 0.1.0 → 0.1.2
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/LICENSE +21 -0
- package/README.md +109 -21
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ragrails
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,32 +1,120 @@
|
|
|
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
|
+
[](https://www.npmjs.com/package/@ragrails/api-playground-react)
|
|
6
|
+
[](./LICENSE)
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
An embeddable, themeable **API playground** for React.
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
<!-- Add a screenshot/GIF here for the npm page, e.g.:
|
|
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.
|
|
9
14
|
|
|
10
|
-
|
|
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.
|
|
11
21
|
|
|
12
|
-
|
|
22
|
+
## Install
|
|
13
23
|
|
|
14
|
-
|
|
24
|
+
```bash
|
|
25
|
+
npm i @ragrails/api-playground-react
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`react` and `react-dom` (v18 or v19) are peer dependencies.
|
|
15
29
|
|
|
16
|
-
|
|
30
|
+
## Quick start
|
|
17
31
|
|
|
18
|
-
```
|
|
19
|
-
{
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
},
|
|
25
|
-
"rules": {
|
|
26
|
-
"react/rules-of-hooks": "error",
|
|
27
|
-
"react/only-export-components": ["warn", { "allowConstantExport": true }]
|
|
28
|
-
}
|
|
32
|
+
```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'`} />
|
|
29
38
|
}
|
|
30
39
|
```
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
That single `request` drives the snippet view and the interactive console.
|
|
42
|
+
|
|
43
|
+
## Props
|
|
44
|
+
|
|
45
|
+
| Prop | Type | Default | Description |
|
|
46
|
+
| --- | --- | --- | --- |
|
|
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. |
|
|
56
|
+
|
|
57
|
+
## Theming
|
|
58
|
+
|
|
59
|
+
The widget renders its own theme scope, independent of your app's theme.
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
// Light theme with a custom brand color
|
|
63
|
+
<ApiWidget
|
|
64
|
+
request={curl}
|
|
65
|
+
mode="light"
|
|
66
|
+
customization={{ primary: '#0ea5e9' }}
|
|
67
|
+
/>
|
|
68
|
+
|
|
69
|
+
// Follow the operating system
|
|
70
|
+
<ApiWidget request={curl} mode="system" />
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Fonts fall back to the system UI stack. To use a custom typeface, set `--font-heading` / `--font-sans` on a wrapping element.
|
|
74
|
+
|
|
75
|
+
## Examples
|
|
76
|
+
|
|
77
|
+
```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 }`}
|
|
85
|
+
/>
|
|
86
|
+
|
|
87
|
+
// Read-only documentation widget (no editing, no import)
|
|
88
|
+
<ApiWidget request={curl} editable={false} allowImport={false} />
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## How it works
|
|
92
|
+
|
|
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.
|
|
94
|
+
|
|
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.
|
|
96
|
+
|
|
97
|
+
## Additional exports
|
|
98
|
+
|
|
99
|
+
Besides `ApiWidget`, the package exports the building blocks and helpers:
|
|
100
|
+
|
|
101
|
+
```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'
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT
|
package/package.json
CHANGED