H
Hono7mo ago
theGagne

Difficulty using OpenAPI registry parameters with createRoute

My setup is index.ts -> has hono app, imports routes schema.ts -> has zod schemas defined routes.ts -> uses createRoute from defined schemas I want to define parameters like user_id, etc and register them and use $ref to refer to them. This would clean up the openapi and reduces repetition. I'm having difficulty getting this to work. Does anyone have an example of this working? It's also a little clunky, it seems like you end up with a zod string which must get registered, and then that used inside a zod object that goes in the param? In any case the DX on this is not great and I've wasted a few hours already trying to get it to work.
1 Reply
Chicha
Chicha7mo ago
app.openapi( createRoute({ method: "get", path: "/openai/models", responses: { 200: { description: "Respond a message" } }, }), async (c) => { const openai = c.get("OpenAIClient"); const list = await openai.models.list(); return c.json(list); } ); app.openapi( createRoute({ method: "post", path: "/openai/content-generator/aboutUs", requestBody: { content: { "application/json": { schema: { type: "object", properties: { company: { type: "string" }, companySpec: { type: "string" }, }, required: ["company", "companySpec"], }, }, }, }, responses: { 200: { description: "Respond a message" }, }, }), async (c) => { const { company, companySpec } = await c.req.json(); websiteContent.company = company; const openai = c.get("OpenAIClient"); const aboutUs = await openai.chat.completions.create({ model: "gpt-4-0125-preview", messages: [ { role: "system", content: generate me a brief description of ${company} company to put on the website in about us section. the company specialties are ${companySpec}, }, ], }); websiteContent.aboutUs = aboutUs.choices[0].message.content; return c.json(aboutUs.choices[0].message.content); } ); this is an example
Want results from more Discord servers?
Add your server