أمثلة
تستخدم أمثلة التكامل هذه واجهة API العامة. وفّر مستند OpenAPI 3.2 محمّلًا في spec يتضمن معرّفات العمليات والمسارات المستخدمة في كل مثال. استبدل عناوين URL والرموز ومسارات الملفات المحلية بقيمك الخاصة. تتطلب أمثلة الشبكة خدمة يمكن الوصول إليها؛ وليست خوادم محاكاة مستقلة.
1. طلب HTTP باستخدام SendOptions
تستقبل كل من prepare() وsend() كائن SendOptions يتضمن spec وtarget وvalues.
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