Quickstart
This quickstart covers the three most common paths: the curlToOpenApi helper, the postmanToOpenApi helper, and the XToOpenApi class for custom wiring.
Prerequisite: install the package.
注記
All conversion functions are async and return a ConvertResult. Always await them.
1. Convert a curl command
Pass a single curl command to curlToOpenApi. The result's document is the generated OpenAPI 3.2 object.
import { curlToOpenApi } from "@powerduck/x-to-openapi";
const result = await curlToOpenApi(
`curl -X POST https://api.example.com/users \
-H 'Content-Type: application/json' \
--data '{"name":"Ada","age":36}'`,
);
console.log(JSON.stringify(result.document, null, 2));
The generated document looks like this (trimmed):
{
"openapi": "3.2.0",
"info": { "title": "Generated API", "version": "1.0.0" },
"servers": [{ "url": "https://api.example.com" }],
"paths": {
"/users": {
"post": {
"operationId": "postUsers",
"tags": ["users"],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name", "age"]
}
}
}
},
"responses": { "default": { "description": "Successful response" } }
}
}
}
}
Check whether conversion succeeded and inspect diagnostics:
if (!result.ok) {
for (const d of result.diagnostics) {
console.error(`[${d.severity}] ${d.code}: ${d.message}`);
}
}
console.log("Document valid:", result.documentValid);