openwire 0.0.0-stage → 1.2.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/LICENSE +21 -0
- package/README.md +281 -2
- package/dist/config.d.ts +31 -0
- package/dist/config.js +97 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +22 -0
- package/dist/errors.js.map +1 -0
- package/dist/http/executor.d.ts +2 -0
- package/dist/http/executor.js +53 -0
- package/dist/http/executor.js.map +1 -0
- package/dist/http/index.d.ts +2 -0
- package/dist/http/index.js +3 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/request.d.ts +2 -0
- package/dist/http/request.js +75 -0
- package/dist/http/request.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +111 -0
- package/dist/index.js.map +1 -0
- package/dist/loader/index.d.ts +2 -0
- package/dist/loader/index.js +2 -0
- package/dist/loader/index.js.map +1 -0
- package/dist/loader/specLoader.d.ts +17 -0
- package/dist/loader/specLoader.js +78 -0
- package/dist/loader/specLoader.js.map +1 -0
- package/dist/logger.d.ts +16 -0
- package/dist/logger.js +65 -0
- package/dist/logger.js.map +1 -0
- package/dist/metadata.d.ts +4 -0
- package/dist/metadata.js +4 -0
- package/dist/metadata.js.map +1 -0
- package/dist/parser/index.d.ts +1 -0
- package/dist/parser/index.js +2 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/parser.d.ts +12 -0
- package/dist/parser/parser.js +423 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/pipeline.d.ts +4 -0
- package/dist/pipeline.js +29 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/sanitizer/index.d.ts +2 -0
- package/dist/sanitizer/index.js +2 -0
- package/dist/sanitizer/index.js.map +1 -0
- package/dist/sanitizer/sanitizer.d.ts +12 -0
- package/dist/sanitizer/sanitizer.js +88 -0
- package/dist/sanitizer/sanitizer.js.map +1 -0
- package/dist/schema/builder.d.ts +10 -0
- package/dist/schema/builder.js +36 -0
- package/dist/schema/builder.js.map +1 -0
- package/dist/schema/index.d.ts +2 -0
- package/dist/schema/index.js +3 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/normalize.d.ts +14 -0
- package/dist/schema/normalize.js +79 -0
- package/dist/schema/normalize.js.map +1 -0
- package/dist/server/handler.d.ts +11 -0
- package/dist/server/handler.js +56 -0
- package/dist/server/handler.js.map +1 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +3 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/server.d.ts +7 -0
- package/dist/server/server.js +50 -0
- package/dist/server/server.js.map +1 -0
- package/dist/types.d.ts +87 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validator/index.d.ts +2 -0
- package/dist/validator/index.js +2 -0
- package/dist/validator/index.js.map +1 -0
- package/dist/validator/validator.d.ts +9 -0
- package/dist/validator/validator.js +46 -0
- package/dist/validator/validator.js.map +1 -0
- package/package.json +74 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Moad Tellami
|
|
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,3 +1,282 @@
|
|
|
1
|
-
#
|
|
1
|
+
# openwire
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> A Model Context Protocol (MCP) server that loads OpenAPI 3.0/3.1 specifications (YAML or JSON) at runtime and dynamically exposes every endpoint as a fully validated MCP tool for LLMs.
|
|
4
|
+
|
|
5
|
+
Instead of hand-writing tool wrappers for each REST endpoint, give the bridge a local OpenAPI file path or a remote URL. It automatically turns each operation into a typed, validated MCP tool.
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Features](#features)
|
|
10
|
+
- [How It Works](#how-it-works)
|
|
11
|
+
- [Requirements](#requirements)
|
|
12
|
+
- [Migrating from api-mcp-bridge](#migrating-from-api-mcp-bridge)
|
|
13
|
+
- [Installation](#installation)
|
|
14
|
+
- [CLI Usage](#cli-usage)
|
|
15
|
+
- [MCP Client Configuration](#mcp-client-configuration)
|
|
16
|
+
- [Configuration & Environment Variables](#configuration--environment-variables)
|
|
17
|
+
- [Authentication](#authentication)
|
|
18
|
+
- [Compatibility and limitations](#compatibility-and-limitations)
|
|
19
|
+
- [Tool Mapping](#tool-mapping)
|
|
20
|
+
- [Security & Limits](#security--limits)
|
|
21
|
+
- [License](#license)
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Dynamic spec ingestion** — loads OpenAPI 3.0.x and 3.1.x specs (JSON or YAML) from a local file path or a remote `http(s)://` URL at startup.
|
|
26
|
+
- **Zero glue code** — every operation in the spec becomes an MCP tool automatically; there is nothing to code per endpoint.
|
|
27
|
+
- **Type-safe input schemas** — path, query, header, and cookie parameters become top-level tool properties; the request body is exposed as a `body` property. Required fields are enforced.
|
|
28
|
+
- **Draft-07 compatible schema normalization** — OpenAPI 3.0 constructs such as `nullable: true` and boolean `exclusiveMinimum`/`exclusiveMaximum` are converted to JSON Schema the MCP/LLM layer understands.
|
|
29
|
+
- **`$ref` resolution** — local component references (`#/components/schemas/...`) are dereferenced recursively, with cycle and depth guards.
|
|
30
|
+
- **Input validation** — LLM-supplied arguments are validated against the generated JSON Schema with AJV (formats included) _before_ any network call; validation failures are returned as readable tool errors.
|
|
31
|
+
- **Automatic HTTP execution** — path parameters are URL-encoded and substituted, query/header/cookie parameters are serialized, and request bodies are sent as JSON with the correct `Content-Type`.
|
|
32
|
+
- **Header-based authentication** — inject static credentials via `API_AUTH_HEADERS`/`API_HEADERS` (or `-H`) when fetching the spec and calling API endpoints. Configured headers are not added to tool schemas.
|
|
33
|
+
- **Base URL override** — point the bridge at a different environment (staging, a local mock, an authenticated proxy) without editing the spec.
|
|
34
|
+
- **Optional response limits** — configure depth, array length, string length, and serialized character budgets. Limits are unlimited by default; tool responses flag truncation.
|
|
35
|
+
- **Built-in `health` tool** — a no-op bootstrap tool to verify the server is responding.
|
|
36
|
+
- **Failure handling** — invalid specifications prevent startup. Invalid tool arguments, network failures, and HTTP 4xx/5xx responses produce MCP tool errors.
|
|
37
|
+
- **Insecure HTTP allowed** — optional flag to permit plain `http://` targets.
|
|
38
|
+
|
|
39
|
+
## How It Works
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
OpenAPI spec (file or URL)
|
|
43
|
+
│
|
|
44
|
+
▼
|
|
45
|
+
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐
|
|
46
|
+
│ Spec Loader │───▶│ Parser │───▶│ Schema Builder │
|
|
47
|
+
│ (YAML/JSON, $ref │ │ (3.0/3.1, ops → │ │ (OpenAPI schema → │
|
|
48
|
+
│ dereference) │ │ tool metadata) │ │ MCP JSON Schema) │
|
|
49
|
+
└───────────────────┘ └───────────────────┘ └───────────────────┘
|
|
50
|
+
│
|
|
51
|
+
▼
|
|
52
|
+
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐
|
|
53
|
+
│ MCP Server │ │ Validator (AJV) │ │ HTTP Executor │
|
|
54
|
+
│ (stdio, one tool │───▶│ (reject bad args) │───▶│ (call the API) │
|
|
55
|
+
│ per operation) │ └───────────────────┘ └─────────┬─────────┘
|
|
56
|
+
└───────────────────┘ ▼
|
|
57
|
+
┌───────────────────┐
|
|
58
|
+
│ Sanitizer │
|
|
59
|
+
│ (bounded response│
|
|
60
|
+
│ for LLM context) │
|
|
61
|
+
└───────────────────┘
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
1. **Load** — the spec is read from a file or fetched over HTTP(S) and parsed as JSON or YAML.
|
|
65
|
+
2. **Parse** — each path/verb pair becomes a `ParsedOperation`; `$ref`s are resolved, parameters merged, and tool names derived from `operationId`.
|
|
66
|
+
3. **Build schemas** — operation schemas are normalized and converted into the JSON Schema input contract for each MCP tool.
|
|
67
|
+
4. **Serve** — an MCP stdio server registers a `health` tool plus one tool per operation.
|
|
68
|
+
5. **Validate** — on each tool call, arguments are checked against the input schema with AJV before anything hits the network.
|
|
69
|
+
6. **Execute** — a real HTTP request is built (URL, headers, query, cookies, JSON body) and dispatched with a timeout.
|
|
70
|
+
7. **Sanitize** — the response body is compacted/truncated under configurable budgets and returned to the model, along with the HTTP status.
|
|
71
|
+
|
|
72
|
+
## Requirements
|
|
73
|
+
|
|
74
|
+
- Node.js `>= 20.19` (ESM, built-in `fetch`)
|
|
75
|
+
- An OpenAPI 3.0.x or 3.1.x specification, as JSON or YAML
|
|
76
|
+
|
|
77
|
+
## Migrating from api-mcp-bridge
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm uninstall -g api-mcp-bridge
|
|
81
|
+
npm install -g openwire
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Change MCP client commands from `api-mcp-bridge` to `openwire`; for npx use `npx --yes --package openwire openwire --spec ./openapi.yaml`. Environment variables and CLI arguments remain compatible. The old command is also included as an alias in Openwire 1.2.0. The default MCP server identity is now `openwire`; `SERVER_NAME` remains available for overrides.
|
|
85
|
+
|
|
86
|
+
Source repository: [mtellami/openwire](https://github.com/mtellami/openwire). See [release notes](CHANGELOG.md) and the [optimization roadmap](ROADMAP.md).
|
|
87
|
+
|
|
88
|
+
## Installation
|
|
89
|
+
|
|
90
|
+
The npm package ships a compiled CLI. This project is an OpenAPI-to-MCP server, unrelated to the Apache ActiveMQ OpenWire protocol.
|
|
91
|
+
|
|
92
|
+
### Global install
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm install -g openwire
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Run-on-demand with npx
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
npx openwire --spec ./openapi.yaml
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### From source
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
npm install
|
|
108
|
+
npm run build
|
|
109
|
+
npm run start -- --spec ./openapi.yaml
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## CLI Usage
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
openwire [options] [source]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
| Argument | Description |
|
|
119
|
+
| ---------------------- | ------------------------------------------------------------------------------------------- |
|
|
120
|
+
| `-s, --spec <src>` | Path to the spec file or an `http(s)://` URL. |
|
|
121
|
+
| `-H, --headers <json>` | JSON object of headers injected into every request (e.g. `{"Authorization":"Bearer ..."}`). |
|
|
122
|
+
| `<src>` (positional) | Same as `--spec`. |
|
|
123
|
+
| `--help` | Show usage. |
|
|
124
|
+
|
|
125
|
+
The spec source can also be provided via the `OPENAPI_SOURCE` environment variable. The CLI flag (or positional argument) takes precedence.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# Local file spec
|
|
129
|
+
openwire --spec ./openapi.yaml
|
|
130
|
+
|
|
131
|
+
# Remote spec URL
|
|
132
|
+
openwire --spec https://api.example.com/openapi.json
|
|
133
|
+
|
|
134
|
+
# Env-driven
|
|
135
|
+
OPENAPI_SOURCE=./openapi.yaml openwire
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## MCP Client Configuration
|
|
139
|
+
|
|
140
|
+
`openwire` is a **stdio** MCP server, so it plugs into MCP-compatible clients (Claude Desktop, Cursor, VS Code Copilot, homegrown agents, etc.) as a stdio subprocess.
|
|
141
|
+
|
|
142
|
+
### Claude Desktop
|
|
143
|
+
|
|
144
|
+
Open `claude_desktop_config.json` (macOS: `~/Library/Application Support/Claude/`; Windows: `%APPDATA%\Claude\`):
|
|
145
|
+
|
|
146
|
+
```json
|
|
147
|
+
{
|
|
148
|
+
"mcpServers": {
|
|
149
|
+
"openwire": {
|
|
150
|
+
"command": "openwire",
|
|
151
|
+
"args": ["--spec", "/absolute/path/to/openapi.yaml"],
|
|
152
|
+
"env": {
|
|
153
|
+
"API_BASE_URL": "https://api.example.com"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Generic / JSON MCP client
|
|
161
|
+
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"mcpServers": {
|
|
165
|
+
"openwire": {
|
|
166
|
+
"command": "openwire",
|
|
167
|
+
"args": ["--spec", "https://api.example.com/openapi.json"]
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### If the binary is not globally installed
|
|
174
|
+
|
|
175
|
+
Use plain `node`:
|
|
176
|
+
|
|
177
|
+
```json
|
|
178
|
+
{
|
|
179
|
+
"mcpServers": {
|
|
180
|
+
"openwire": {
|
|
181
|
+
"command": "node",
|
|
182
|
+
"args": ["/path/to/openwire/dist/index.js", "--spec", "./openapi.yaml"]
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
> Note: the tool list is fixed at startup — restart the MCP client whenever the upstream spec changes.
|
|
189
|
+
|
|
190
|
+
## Configuration & Environment Variables
|
|
191
|
+
|
|
192
|
+
The server is configured once at startup through environment variables (and the CLI `source` argument).
|
|
193
|
+
|
|
194
|
+
| Variable | Default | Description |
|
|
195
|
+
| ---------------------------- | --------------- | -------------------------------------------------------------------------------------------------- |
|
|
196
|
+
| `OPENAPI_SOURCE` | _(required)_ | Spec path or `http(s)://` URL to load at startup. |
|
|
197
|
+
| `OPENAPI_MAX_TOOLS` | `100` | Maximum number of MCP tools generated from the spec. |
|
|
198
|
+
| `API_BASE_URL` | from spec | Overrides the selected operation, path, or document server URL for every request. |
|
|
199
|
+
| `API_TIMEOUT_MS` | `30000` | Per-request timeout for calls to the target API. |
|
|
200
|
+
| `API_MAX_RESPONSE_CHARS` | unlimited | Total character budget for sanitized responses returned to the model. |
|
|
201
|
+
| `SANITIZE_MAX_DEPTH` | unlimited | Max nesting depth kept when compacting JSON responses. |
|
|
202
|
+
| `SANITIZE_MAX_ARRAY_LENGTH` | unlimited | Max number of array elements kept per array. |
|
|
203
|
+
| `SANITIZE_MAX_STRING_LENGTH` | unlimited | Max length of an individual string value. |
|
|
204
|
+
| `LOG_LEVEL` | `info` | `debug`, `info`, `warn`, or `error`. |
|
|
205
|
+
| `SERVER_NAME` | `openwire` | MCP server name reported to the client. |
|
|
206
|
+
| `SERVER_VERSION` | package version | MCP server version reported to the client. |
|
|
207
|
+
| `ALLOW_INSECURE_HTTP` | `false` | When `true`, permits `http://` spec sources and target APIs; otherwise those requests are refused. |
|
|
208
|
+
| `API_AUTH_HEADERS` | _(none)_ | JSON object of headers injected into every request (e.g. `{"Authorization":"Bearer ..."}`). |
|
|
209
|
+
| `API_HEADERS` | _(none)_ | Same as `API_AUTH_HEADERS`; takes precedence when both are set. |
|
|
210
|
+
|
|
211
|
+
Example:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
export OPENAPI_SOURCE=https://petstore3.swagger.io/api/v3/openapi.json
|
|
215
|
+
export API_BASE_URL=https://localhost:8080
|
|
216
|
+
export API_TIMEOUT_MS=15000
|
|
217
|
+
export API_MAX_RESPONSE_CHARS=50000
|
|
218
|
+
export LOG_LEVEL=debug
|
|
219
|
+
openwire
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
> When `API_BASE_URL` is set, every tool call is routed to that base URL regardless of what the spec declares, which is handy for mocking, staging, or authenticated gateways.
|
|
223
|
+
|
|
224
|
+
## Authentication
|
|
225
|
+
|
|
226
|
+
Openwire injects configured credentials as HTTP headers on spec downloads and API requests. Configured headers override tool-supplied header parameters of the same name. Configure them with `API_AUTH_HEADERS` (env), `API_HEADERS` (env, takes precedence), or the `-H/--headers` CLI flag (takes precedence over both).
|
|
227
|
+
|
|
228
|
+
The value is a JSON object of header name/value pairs:
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
export OPENAPI_SOURCE=https://api.example.com/openapi.json
|
|
232
|
+
export API_AUTH_HEADERS='{"Authorization":"Bearer eyJhbGciOi...","X-API-Key":"abc123"}'
|
|
233
|
+
openwire
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Or via the CLI flag:
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
openwire --spec ./openapi.yaml -H '{"Authorization":"Bearer eyJhbGciOi..."}'
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Supply precomputed Bearer tokens, Basic authorization values, API-key headers, or proxy headers. Security schemes in the spec are metadata: Openwire does not perform OAuth/OpenID flows or automatically inject query/cookie API keys. Query/cookie parameters declared by the spec remain ordinary tool arguments.
|
|
243
|
+
|
|
244
|
+
Static tokens only — if your API needs a fresh token per call (client-credentials exchange, short-lived JWTs), point `API_BASE_URL` at a proxy that acquires tokens, or rotate the header value before restarting the bridge.
|
|
245
|
+
|
|
246
|
+
> When the _spec file itself_ is served from a protected URL, the same headers are attached when fetching `OPENAPI_SOURCE` over HTTP(S).
|
|
247
|
+
|
|
248
|
+
## Compatibility and limitations
|
|
249
|
+
|
|
250
|
+
- Local references are supported; external references and recursive schema cycles are rejected.
|
|
251
|
+
- OpenAPI 3.0 schemas are normalized for draft-07 validation. OpenAPI 3.1 documents are accepted, but full JSON Schema 2020-12 semantics are not implemented.
|
|
252
|
+
- JSON request bodies (`application/json` and `application/*+json`) are supported and preferred. Other body encodings return a clear execution error when a body is supplied.
|
|
253
|
+
- Parameter serialization uses simple scalar/JSON values; OpenAPI `style`/`explode` rules are not fully implemented.
|
|
254
|
+
- Parameter names share one tool namespace; duplicate names across locations or a parameter named `body` can collide with the body argument.
|
|
255
|
+
- Server selection uses operation, path, then document precedence. Server-variable expansion is not implemented.
|
|
256
|
+
|
|
257
|
+
## Tool Mapping
|
|
258
|
+
|
|
259
|
+
- **Tool name** — derived from the operation's `operationId` (normalized to camelCase); falls back to `<method>_<path>` when no `operationId` exists. The built-in `health` name is reserved. Collisions get a numeric suffix (`getUser`, `getUser2`, …).
|
|
260
|
+
- **Tool description** — `HTTP <METHOD> <path>`, plus the operation `summary` and `description`.
|
|
261
|
+
- **Input schema** — one property per path/query/header/cookie parameter (required ones listed under `required`), and a single `body` property for operations with a request body (required when the spec marks it so). `additionalProperties` is disabled.
|
|
262
|
+
- **Built-in tools** — a `health` tool with an optional `echo` string argument lets clients probe connectivity.
|
|
263
|
+
|
|
264
|
+
Example effect for a hypothetical `GET /pets/{petId}` with an `order` query param:
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
MCP tool: getPetById
|
|
268
|
+
Input: { petId: string (required), order?: string }
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Security & Limits
|
|
272
|
+
|
|
273
|
+
- Spec loading supports both local files and remote URLs; remote fetches honor `API_TIMEOUT_MS`, and `http://` sources are refused unless `ALLOW_INSECURE_HTTP=true`.
|
|
274
|
+
- Requests to the target API are made with the built-in `fetch` and a timeout; slow or failing upstreams surface as structured tool errors. Plain `http://` targets are refused unless `ALLOW_INSECURE_HTTP=true`.
|
|
275
|
+
- Configured credentials are injected server-side and are not added to tool schemas. Logs omit request headers, but upstream responses may echo secrets; configure and trust your upstream accordingly.
|
|
276
|
+
- Response limits are opt-in. `API_MAX_RESPONSE_CHARS` bounds the serialized response body, including escaping and embedded markers; HTTP status lines and the separate `[response truncated]` notice are outside that budget. JSON stays valid when a value fits; otherwise the body is empty. Downloads are still buffered before sanitization.
|
|
277
|
+
- Argument validation happens before any external call, rejecting malformed input early.
|
|
278
|
+
- To talk to `http://` endpoints (e.g., local mocks), set `ALLOW_INSECURE_HTTP=true`.
|
|
279
|
+
|
|
280
|
+
## License
|
|
281
|
+
|
|
282
|
+
[MIT](LICENSE)
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type LogLevel } from "./logger.js";
|
|
2
|
+
export interface BridgeConfig {
|
|
3
|
+
/** Raw spec source: a filesystem path or an http(s) URL. */
|
|
4
|
+
source: string;
|
|
5
|
+
baseUrlOverride?: string;
|
|
6
|
+
timeoutMs: number;
|
|
7
|
+
maxTools: number;
|
|
8
|
+
/** Response character budget. Unlimited when not set. */
|
|
9
|
+
maxResponseChars?: number;
|
|
10
|
+
/** Max JSON depth kept. Unlimited when not set. */
|
|
11
|
+
maxDepth?: number;
|
|
12
|
+
/** Max array elements kept per array. Unlimited when not set. */
|
|
13
|
+
maxArrayLength?: number;
|
|
14
|
+
/** Max length of an individual string value. Unlimited when not set. */
|
|
15
|
+
maxStringLength?: number;
|
|
16
|
+
logLevel: LogLevel;
|
|
17
|
+
serverName: string;
|
|
18
|
+
serverVersion: string;
|
|
19
|
+
allowInsecureHttp: boolean;
|
|
20
|
+
/** Raw headers injected into every request. */
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
export declare const DEFAULT_CONFIG: {
|
|
24
|
+
readonly timeoutMs: 30000;
|
|
25
|
+
readonly maxTools: 100;
|
|
26
|
+
readonly serverName: string;
|
|
27
|
+
readonly serverVersion: string;
|
|
28
|
+
};
|
|
29
|
+
type RawEnv = Record<string, string | undefined>;
|
|
30
|
+
export declare function loadConfig(env: RawEnv, cliSource?: string, cliHeaders?: string): BridgeConfig;
|
|
31
|
+
export {};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { packageMetadata } from "./metadata.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ConfigError } from "./errors.js";
|
|
4
|
+
import { parseLogLevel } from "./logger.js";
|
|
5
|
+
export const DEFAULT_CONFIG = {
|
|
6
|
+
timeoutMs: 30_000,
|
|
7
|
+
maxTools: 100,
|
|
8
|
+
serverName: packageMetadata.name,
|
|
9
|
+
serverVersion: packageMetadata.version,
|
|
10
|
+
};
|
|
11
|
+
const numberOrEmpty = (def) => z.preprocess((value) => {
|
|
12
|
+
if (value === undefined || value === null || value === "") {
|
|
13
|
+
return def;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === "number") {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
const n = Number(value);
|
|
19
|
+
return Number.isFinite(n) ? n : def;
|
|
20
|
+
}, z.number().min(0));
|
|
21
|
+
const optionalNumber = z.preprocess((value) => {
|
|
22
|
+
if (value === undefined || value === null || value === "") {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (typeof value === "number") {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
const n = Number(value);
|
|
29
|
+
return Number.isFinite(n) ? n : undefined;
|
|
30
|
+
}, z.number().min(0).optional());
|
|
31
|
+
const EnvSchema = z.object({
|
|
32
|
+
OPENAPI_SOURCE: z.string().min(1).optional(),
|
|
33
|
+
API_BASE_URL: z.string().min(1).optional(),
|
|
34
|
+
API_TIMEOUT_MS: numberOrEmpty(DEFAULT_CONFIG.timeoutMs),
|
|
35
|
+
OPENAPI_MAX_TOOLS: numberOrEmpty(DEFAULT_CONFIG.maxTools),
|
|
36
|
+
API_MAX_RESPONSE_CHARS: optionalNumber,
|
|
37
|
+
SANITIZE_MAX_DEPTH: optionalNumber,
|
|
38
|
+
SANITIZE_MAX_ARRAY_LENGTH: optionalNumber,
|
|
39
|
+
SANITIZE_MAX_STRING_LENGTH: optionalNumber,
|
|
40
|
+
LOG_LEVEL: z.string().optional(),
|
|
41
|
+
SERVER_NAME: z.string().min(1).default(DEFAULT_CONFIG.serverName),
|
|
42
|
+
SERVER_VERSION: z.string().min(1).default(DEFAULT_CONFIG.serverVersion),
|
|
43
|
+
ALLOW_INSECURE_HTTP: z
|
|
44
|
+
.enum(["true", "false", "1", "0"])
|
|
45
|
+
.default("false")
|
|
46
|
+
.transform((value) => value === "true" || value === "1"),
|
|
47
|
+
API_AUTH_HEADERS: z.string().optional(),
|
|
48
|
+
API_HEADERS: z.string().optional(),
|
|
49
|
+
});
|
|
50
|
+
export function loadConfig(env, cliSource, cliHeaders) {
|
|
51
|
+
const result = EnvSchema.safeParse(env);
|
|
52
|
+
if (!result.success) {
|
|
53
|
+
const first = result.error.issues[0];
|
|
54
|
+
throw new ConfigError(first
|
|
55
|
+
? `Invalid configuration: ${first.message} (${first.path.join(".")})`
|
|
56
|
+
: "Invalid configuration");
|
|
57
|
+
}
|
|
58
|
+
const parsed = result.data;
|
|
59
|
+
const source = cliSource ?? parsed.OPENAPI_SOURCE;
|
|
60
|
+
if (!source) {
|
|
61
|
+
throw new ConfigError("No spec source provided: use the --spec flag or set OPENAPI_SOURCE");
|
|
62
|
+
}
|
|
63
|
+
const headersRaw = cliHeaders ?? parsed.API_HEADERS ?? parsed.API_AUTH_HEADERS;
|
|
64
|
+
return {
|
|
65
|
+
source,
|
|
66
|
+
baseUrlOverride: parsed.API_BASE_URL,
|
|
67
|
+
timeoutMs: parsed.API_TIMEOUT_MS,
|
|
68
|
+
maxTools: parsed.OPENAPI_MAX_TOOLS,
|
|
69
|
+
maxResponseChars: parsed.API_MAX_RESPONSE_CHARS,
|
|
70
|
+
maxDepth: parsed.SANITIZE_MAX_DEPTH,
|
|
71
|
+
maxArrayLength: parsed.SANITIZE_MAX_ARRAY_LENGTH,
|
|
72
|
+
maxStringLength: parsed.SANITIZE_MAX_STRING_LENGTH,
|
|
73
|
+
logLevel: parseLogLevel(parsed.LOG_LEVEL),
|
|
74
|
+
serverName: parsed.SERVER_NAME,
|
|
75
|
+
serverVersion: parsed.SERVER_VERSION,
|
|
76
|
+
allowInsecureHttp: parsed.ALLOW_INSECURE_HTTP,
|
|
77
|
+
headers: headersRaw !== undefined ? parseHeaders(headersRaw) : undefined,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function parseHeaders(raw) {
|
|
81
|
+
let parsed;
|
|
82
|
+
try {
|
|
83
|
+
parsed = JSON.parse(raw);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
throw new ConfigError("Invalid API_HEADERS: expected a JSON object of header name/value pairs");
|
|
87
|
+
}
|
|
88
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
89
|
+
throw new ConfigError("Invalid API_HEADERS: expected a JSON object of header name/value pairs");
|
|
90
|
+
}
|
|
91
|
+
const headers = {};
|
|
92
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
93
|
+
headers[key] = String(value);
|
|
94
|
+
}
|
|
95
|
+
return headers;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,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AAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS;KACzE,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,GAAW;IAC/B,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,WAAW,CAAC,wEAAwE,CAAC,CAAC;IAClG,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3E,MAAM,IAAI,WAAW,CAAC,wEAAwE,CAAC,CAAC;IAClG,CAAC;IACD,MAAM,OAAO,GAA2B,EAAE,CAAC;IAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class BridgeError extends Error {
|
|
2
|
+
constructor(message: string, options?: {
|
|
3
|
+
cause?: unknown;
|
|
4
|
+
});
|
|
5
|
+
}
|
|
6
|
+
export declare class ConfigError extends BridgeError {
|
|
7
|
+
}
|
|
8
|
+
export declare class SpecLoadError extends BridgeError {
|
|
9
|
+
}
|
|
10
|
+
export declare class SpecParseError extends BridgeError {
|
|
11
|
+
}
|
|
12
|
+
export declare class ValidationError extends BridgeError {
|
|
13
|
+
readonly details?: unknown[];
|
|
14
|
+
constructor(message: string, details?: unknown[]);
|
|
15
|
+
}
|
|
16
|
+
export declare class HttpExecutionError extends BridgeError {
|
|
17
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class BridgeError extends Error {
|
|
2
|
+
constructor(message, options) {
|
|
3
|
+
super(message, options);
|
|
4
|
+
this.name = new.target.name;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class ConfigError extends BridgeError {
|
|
8
|
+
}
|
|
9
|
+
export class SpecLoadError extends BridgeError {
|
|
10
|
+
}
|
|
11
|
+
export class SpecParseError extends BridgeError {
|
|
12
|
+
}
|
|
13
|
+
export class ValidationError extends BridgeError {
|
|
14
|
+
details;
|
|
15
|
+
constructor(message, details) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.details = details;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class HttpExecutionError extends BridgeError {
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,WAAY,SAAQ,KAAK;IACpC,YAAY,OAAe,EAAE,OAA6B;QACxD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;IAC9B,CAAC;CACF;AAED,MAAM,OAAO,WAAY,SAAQ,WAAW;CAAG;AAE/C,MAAM,OAAO,aAAc,SAAQ,WAAW;CAAG;AAEjD,MAAM,OAAO,cAAe,SAAQ,WAAW;CAAG;AAElD,MAAM,OAAO,eAAgB,SAAQ,WAAW;IACrC,OAAO,CAAa;IAE7B,YAAY,OAAe,EAAE,OAAmB;QAC9C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,WAAW;CAAG"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { HttpExecutionError } from "../errors.js";
|
|
2
|
+
import { prepareRequest } from "./request.js";
|
|
3
|
+
export async function executeOperation(runtime, operation, args) {
|
|
4
|
+
const config = runtime.config;
|
|
5
|
+
const baseUrl = config.baseUrlOverride ?? operation.baseUrl;
|
|
6
|
+
if (!baseUrl) {
|
|
7
|
+
throw new HttpExecutionError(`No server URL available for operation "${operation.toolName}"`);
|
|
8
|
+
}
|
|
9
|
+
const request = prepareRequest(operation, args, baseUrl, config.timeoutMs);
|
|
10
|
+
if (config.headers) {
|
|
11
|
+
for (const [name, value] of Object.entries(config.headers)) {
|
|
12
|
+
request.headers[name.toLowerCase()] = value;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (!config.allowInsecureHttp && new URL(request.url).protocol === "http:") {
|
|
16
|
+
throw new HttpExecutionError(`Refusing insecure HTTP request to "${request.url}"; set ALLOW_INSECURE_HTTP=true to allow plain http targets`);
|
|
17
|
+
}
|
|
18
|
+
const controller = new AbortController();
|
|
19
|
+
const timer = setTimeout(() => controller.abort(), request.timeoutMs);
|
|
20
|
+
try {
|
|
21
|
+
const init = {
|
|
22
|
+
method: request.method.toUpperCase(),
|
|
23
|
+
headers: request.headers,
|
|
24
|
+
signal: controller.signal,
|
|
25
|
+
};
|
|
26
|
+
if (request.body !== undefined) {
|
|
27
|
+
init.body = JSON.stringify(request.body);
|
|
28
|
+
}
|
|
29
|
+
const response = await runtime.fetchImpl(request.url, init);
|
|
30
|
+
const bodyText = await response.text();
|
|
31
|
+
return {
|
|
32
|
+
url: request.url,
|
|
33
|
+
method: request.method,
|
|
34
|
+
status: response.status,
|
|
35
|
+
statusText: response.statusText,
|
|
36
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
37
|
+
bodyText,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
42
|
+
throw new HttpExecutionError(`Request to ${request.url} timed out after ${request.timeoutMs}ms`);
|
|
43
|
+
}
|
|
44
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
45
|
+
throw new HttpExecutionError(`HTTP request to ${request.url} failed: ${message}`, {
|
|
46
|
+
cause: err,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../src/http/executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,OAAuB,EACvB,SAA0B,EAC1B,IAA6B;IAE7B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,MAAM,OAAO,GAAG,MAAM,CAAC,eAAe,IAAI,SAAS,CAAC,OAAO,CAAC;IAC5D,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,kBAAkB,CAAC,0CAA0C,SAAS,CAAC,QAAQ,GAAG,CAAC,CAAC;IAChG,CAAC;IAED,MAAM,OAAO,GAAG,cAAc,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;IAC3E,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC3D,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,GAAG,KAAK,CAAC;QAC9C,CAAC;IACH,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,iBAAiB,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QA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,CAAC;QACJ,CAAC;QACD,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjE,MAAM,IAAI,kBAAkB,CAAC,mBAAmB,OAAO,CAAC,GAAG,YAAY,OAAO,EAAE,EAAE;YAChF,KAAK,EAAE,GAAG;SACX,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,YAAY,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ValidationError } from "../errors.js";
|
|
2
|
+
export function prepareRequest(operation, args, baseUrl, timeoutMs) {
|
|
3
|
+
const path = substitutePathParams(operation.path, operation, args);
|
|
4
|
+
const headers = { accept: "application/json" };
|
|
5
|
+
const query = [];
|
|
6
|
+
const cookies = [];
|
|
7
|
+
for (const parameter of operation.parameters) {
|
|
8
|
+
const value = args[parameter.name];
|
|
9
|
+
if (value === undefined || value === null) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
switch (parameter.in) {
|
|
13
|
+
case "query":
|
|
14
|
+
query.push([parameter.name, serializeValue(value)]);
|
|
15
|
+
break;
|
|
16
|
+
case "header":
|
|
17
|
+
headers[parameter.name.toLowerCase()] = serializeValue(value);
|
|
18
|
+
break;
|
|
19
|
+
case "cookie":
|
|
20
|
+
cookies.push(`${parameter.name}=${encodeURIComponent(serializeValue(value))}`);
|
|
21
|
+
break;
|
|
22
|
+
case "path":
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
let body;
|
|
27
|
+
if (operation.requestBody) {
|
|
28
|
+
const value = args.body;
|
|
29
|
+
if (value !== undefined) {
|
|
30
|
+
if (!/^application\/(?:json|[^;]+\+json)(?:;|$)/i.test(operation.requestBody.contentType)) {
|
|
31
|
+
throw new ValidationError(`Unsupported request body content type "${operation.requestBody.contentType}": only JSON bodies are supported`);
|
|
32
|
+
}
|
|
33
|
+
body = value;
|
|
34
|
+
headers["content-type"] = operation.requestBody.contentType;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (cookies.length > 0) {
|
|
38
|
+
headers.cookie = cookies.join("; ");
|
|
39
|
+
}
|
|
40
|
+
const url = buildUrl(baseUrl, path, query);
|
|
41
|
+
return { url, method: operation.method, headers, body, timeoutMs };
|
|
42
|
+
}
|
|
43
|
+
function buildUrl(baseUrl, path, query) {
|
|
44
|
+
const joined = baseUrl.replace(/\/+$/, "") + (path.startsWith("/") ? path : `/${path}`);
|
|
45
|
+
const url = new URL(joined);
|
|
46
|
+
for (const [key, value] of query) {
|
|
47
|
+
url.searchParams.append(key, value);
|
|
48
|
+
}
|
|
49
|
+
return url.toString();
|
|
50
|
+
}
|
|
51
|
+
function substitutePathParams(path, operation, args) {
|
|
52
|
+
return path.replace(/\{([^}]+)\}/g, (match, rawName) => {
|
|
53
|
+
const parameter = operation.parameters.find((parameter) => parameter.in === "path" && parameter.name === rawName);
|
|
54
|
+
const value = parameter ? args[parameter.name] : undefined;
|
|
55
|
+
if (value === undefined || value === null) {
|
|
56
|
+
return match;
|
|
57
|
+
}
|
|
58
|
+
return encodeURIComponent(serializeValue(value));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function serializeValue(value) {
|
|
62
|
+
if (typeof value === "string") {
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
66
|
+
return String(value);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(value);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return String(value);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=request.js.map
|