diff --git a/src/generator/ApiParser.test.ts b/src/generator/ApiParser.test.ts new file mode 100644 index 0000000..d9b1086 --- /dev/null +++ b/src/generator/ApiParser.test.ts @@ -0,0 +1,97 @@ +import { describe, expect, it } from "vitest"; +import { ApiParser } from "./ApiParser"; + +describe("ApiParser", () => { + it("parses an OpenAPI 3.1 API-key search endpoint", async () => { + const spec = { + openapi: "3.1.0", + info: { + title: "Xquik API", + version: "1.0.0", + }, + servers: [{ url: "https://xquik.com" }], + paths: { + "/api/v1/x/tweets/search": { + get: { + operationId: "searchTweets", + summary: "Search tweets", + description: "Search public posts with query, limit, and cursor parameters.", + parameters: [ + { + name: "query", + in: "query", + required: true, + schema: { type: "string" }, + description: "Search query.", + }, + { + name: "limit", + in: "query", + required: false, + schema: { type: "integer" }, + description: "Maximum number of results.", + }, + { + name: "cursor", + in: "query", + required: false, + schema: { type: "string" }, + description: "Pagination cursor.", + }, + ], + responses: { + "200": { + description: "Search results.", + }, + }, + }, + }, + }, + components: { + securitySchemes: { + apiKey: { + type: "apiKey", + in: "header", + name: "x-api-key", + }, + }, + }, + security: [{ apiKey: [] }], + }; + + const parsed = await new ApiParser().parseOpenApi(JSON.stringify(spec)); + + expect(parsed.name).toBe("Xquik API"); + expect(parsed.baseUrl).toBe("https://xquik.com"); + expect(parsed.authType).toBe("apiKey"); + expect(parsed.endpoints).toHaveLength(1); + expect(parsed.endpoints?.[0]).toMatchObject({ + path: "/api/v1/x/tweets/search", + method: "GET", + operationId: "searchTweets", + parameters: [ + { + name: "query", + in: "query", + type: "string", + required: true, + description: "Search query.", + }, + { + name: "limit", + in: "query", + type: "integer", + required: false, + description: "Maximum number of results.", + }, + { + name: "cursor", + in: "query", + type: "string", + required: false, + description: "Pagination cursor.", + }, + ], + }); + }); +});