उदाहरण
ये इंटीग्रेशन उदाहरण सार्वजनिक API का उपयोग करते हैं। हर उदाहरण में इस्तेमाल ऑपरेशन ID और पाथ वाला लोड किया गया OpenAPI 3.2 दस्तावेज़ spec के रूप में दें। उदाहरण के URL, टोकन और स्थानीय फ़ाइल पाथ को अपने मानों से बदलें। नेटवर्क उदाहरणों के लिए पहुँच योग्य सेवा आवश्यक है; ये स्वतंत्र मॉक सर्वर नहीं हैं।
1. SendOptions के साथ HTTP अनुरोध
prepare() और send() दोनों spec, target और values वाला SendOptions ऑब्जेक्ट लेते हैं।
import { createClient, createDebugger } from "@powerduck/openapi-request";
// Create client and debugger (options are optional)
const client = createClient();
const dbg = createDebugger();
// Plan a request by operationId
const plan = client.prepare({
spec,
target: { operationId: "getUserById" },
});
console.log(plan.protocol); // "http"
console.log(plan.display.mode); // "response"
console.log(plan.stream.kind); // "none"
// Send the request with concrete values
const result = await dbg.send({
spec,
target: { operationId: "getUserById" },
values: {
path: { id: "42" },
query: { include: "profile" },
},
serverUrl: "https://api.example.com",
auth: { type: "bearer", token: "your-token" },
timeout: 15000,
});
console.log(result.response.status); // e.g. 200
console.log(result.response.body); // body value returned by the adapter
console.log(result.response.headers); // response headers
console.log(result.response.timings.durationMs); // request duration in ms
मेथड और पाथ से लक्ष्य चुनें
const plan2 = client.prepare({
spec,
target: { method: "GET", path: "/users/{id}" },
});
sendMany से बैच निष्पादन
const batch = await dbg.sendMany(
spec,
[
{ target: { operationId: "listUsers" } },
{ target: { operationId: "getUserById" }, values: { path: { id: "1" } } },
{ target: { operationId: "getUserById" }, values: { path: { id: "2" } } },
],
{
serverUrl: "https://api.example.com",
auth: { type: "bearer", token: "your-token" },
},
);
batch.results.forEach((r, i) => {
if ("error" in r) {
console.log(`Request ${i} failed:`, r.error);
} else {
console.log(`Request ${i} status:`, r.response.status);
}
});
2. SSE स्ट्रीमिंग
जब प्रतिक्रिया में text/event-stream घोषित हो, तो क्लाइंट अपने-आप event-list डिस्प्ले मोड की योजना बनाता है।
const plan = client.prepare({
spec,
target: { operationId: "streamEvents" },
});
console.log(plan.stream.kind); // "sse"
console.log(plan.display.mode); // "event-list"
वास्तविक एंडपॉइंट की जाँच करें
import { probeStreamingResponse } from "@powerduck/openapi-request";
const probe = await probeStreamingResponse("https://api.example.com/events");
console.log(probe.kind, probe.status); // "sse" | "ndjson" | "chunked" | "none"
// The probe performs a request; consume or cancel its response body.
await probe.response.body?.cancel();
SSE स्ट्रीम पार्स करें
import { SseParser } from "@powerduck/openapi-request/http";
const response = await fetch("https://api.example.com/events");
if (!response.ok || !response.body) throw new Error("SSE response unavailable");
const parser = new SseParser();
const reader = response.body.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
for (const event of parser.push(value)) console.log(event.event, event.data);
}
for (const event of parser.flush()) console.log(event.event, event.data);
} finally {
await reader.cancel();
reader.releaseLock();
}
3. WebSocket सत्र
import { createManualSession } from "@powerduck/openapi-request";
const session = createManualSession({
kind: "websocket",
url: "wss://api.example.com/ws",
});
// Subscribe before opening so connection events are included.
const subscription = session.onEvent((event) => {
console.log("Event:", event.kind, event.data);
});
try {
await session.open();
await session.send({ type: "subscribe", channel: "updates" });
// Keep the session open for as long as your application needs it.
} finally {
await session.close();
subscription.unsubscribe();
}