@sonatel-os/openapi-runtime 0.1.0

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 ADDED
@@ -0,0 +1,191 @@
1
+ # 🍊 @sonatel-os/openapi-runtime
2
+
3
+ **Stop generating code. Start calling APIs.**
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@sonatel-os/openapi-runtime)](https://www.npmjs.com/package/@sonatel-os/openapi-runtime)
6
+ ![npm downloads](https://img.shields.io/npm/dm/@sonatel-os/openapi-runtime)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+
9
+ A lightweight, dynamic runtime wrapper for OpenAPI (Swagger) v3.
10
+ Consume APIs, validate payloads, and mock responses instantly ; without regenerating thousands of lines of JavaScript SDKs every time your backend changes.
11
+
12
+ ---
13
+
14
+ ## 🚀 Why?
15
+
16
+ **The Old Way (Codegen):**
17
+ 1. Backend updates `swagger.json`.
18
+ 2. You run `openapi-generator`.
19
+ 3. You wait.
20
+ 4. You get 50 new files and a git conflict.
21
+ 5. Repeat.
22
+
23
+ **The Runtime Way:**
24
+ 1. Drop `api.json` into your project.
25
+ 2. Call `runtime.executeAPI('getUsers')`.
26
+ 3. Done.
27
+
28
+ ---
29
+
30
+ ## 📦 Installation
31
+
32
+ ```bash
33
+ npm install @sonatel-os/openapi-runtime
34
+ # or
35
+ yarn add @sonatel-os/openapi-runtime
36
+ ```
37
+
38
+ -----
39
+
40
+ ## ⚡️ Quick Start
41
+
42
+ ### 1\. Load a Spec
43
+
44
+ You can load multiple specs (e.g., `products`, `users`, `payment`).
45
+
46
+ ```javascript
47
+ import * as runtime from '@sonatel-os/openapi-runtime';
48
+ import mySpec from './specs/products.openapi.json'; // or fetch it
49
+
50
+ // Initialize the registry
51
+ await runtime.save({
52
+ name: 'products',
53
+ specName: mySpec,
54
+ autoAuth: true // Magic auth enabled
55
+ });
56
+ ```
57
+
58
+ ### 2\. Execute a Request
59
+
60
+ Use the `operationId` defined in your Swagger file. No need to memorize URLs.
61
+
62
+ ```javascript
63
+ // GET /products/{id}
64
+ const response = await runtime.executeAPI({
65
+ name: 'products',
66
+ api: 'getProductById',
67
+ params: { id: 123 },
68
+ qs: { details: true } // Query string
69
+ });
70
+
71
+ console.log(response.body);
72
+ ```
73
+
74
+ -----
75
+
76
+ ## 🔐 Magic Auto-Authentication
77
+
78
+ Don't hardcode tokens. The runtime automatically injects credentials from `process.env` based on the security scheme names in your Swagger file.
79
+
80
+ **Naming Convention:** `OAR_{SPEC_NAME}_{SCHEME_NAME}`
81
+
82
+ | Security Scheme (in YAML) | Env Variable Required |
83
+ |---------------------------|-----------------------|
84
+ | `bearerAuth` (in `products` spec) | `OAR_PRODUCTS_BEARERAUTH` |
85
+ | `apiKey` (in `legacy` spec) | `OAR_LEGACY_APIKEY` |
86
+ | `oauth2` (ClientId) | `OAUTH_CLIENT_ID_PRODUCTS_OAUTH2` |
87
+
88
+ **Example:**
89
+ If your spec is named `eyone` and uses `BearerAuth`:
90
+
91
+ ```bash
92
+ # .env
93
+ OAR_EYONE_BEARERAUTH=eyJhbGciOiJIUzI1Ni...
94
+ ```
95
+
96
+ *The runtime handles the rest.*
97
+
98
+ -----
99
+
100
+ ## 🛠️ Features for Power Users
101
+
102
+ ### 1\. The "Poke" System (Mocking & Heuristics)
103
+
104
+ Need to build a UI before the backend is ready? Or check if an API is alive?
105
+
106
+ **Mock a Response:**
107
+ Returns a dummy JSON object based on the schema definition.
108
+
109
+ ```javascript
110
+ const mockData = runtime.mockAPI({
111
+ name: 'products',
112
+ api: 'createProduct',
113
+ status: 201
114
+ });
115
+ ```
116
+
117
+ **Auto-Probe (Heuristic Check):**
118
+ The runtime can inspect a contract and generate "safe" dummy data to ping an endpoint.
119
+
120
+ ```javascript
121
+ const contract = runtime.showContract({ name: 'products', api: 'searchProducts' });
122
+ // Returns: { method: 'POST', path: '/search', parameters: [...] }
123
+ ```
124
+
125
+ ### 2\. Validation
126
+
127
+ Validate data against the OpenAPI schema without making a network request. Great for form validation.
128
+
129
+ ```javascript
130
+ const result = runtime.validateResponse({
131
+ name: 'products',
132
+ api: 'createProduct',
133
+ data: { price: "invalid-string" } // Should be number
134
+ });
135
+
136
+ if (!result.valid) console.error(result.errors);
137
+ ```
138
+
139
+ ### 3\. Interceptors
140
+
141
+ Hook into requests before they fly.
142
+
143
+ ```javascript
144
+ runtime.setInterceptors('products', {
145
+ request: async (req) => {
146
+ console.log(`📡 Calling ${req.url}`);
147
+ return req;
148
+ },
149
+ error: (err) => {
150
+ console.error("🔥 API Fire:", err);
151
+ throw err;
152
+ }
153
+ });
154
+ ```
155
+
156
+ -----
157
+
158
+ ## ⚛️ Next.js Best Practices
159
+
160
+ This library uses Node.js `fs` module for some operations.
161
+
162
+ **Server Components (Recommended):**
163
+ Import directly in `page.jsx` or `layout.jsx`.
164
+
165
+ ```javascript
166
+ import 'server-only';
167
+ import * as runtime from '@sonatel-os/openapi-runtime';
168
+ // Works perfectly
169
+ ```
170
+
171
+ **Client Components:**
172
+ Do **not** import the runtime directly in Client Components (`"use client"`).
173
+ Instead, fetch data in a Server Action or API Route and pass the pure JSON data to your client component.
174
+
175
+ -----
176
+
177
+ ## 🤝 Contributing
178
+
179
+ This project is part of the **Sonatel Open Source** initiative.
180
+ PRs are welcome\!
181
+
182
+ 1. Fork it
183
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
184
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
185
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
186
+ 5. Open a Pull Request
187
+
188
+ -----
189
+
190
+ **License**
191
+ MIT © [Sonatel](#)