|
| 1 | +import axios from "axios"; |
| 2 | +import { Fetcher } from "openapi-typescript-fetch"; |
| 3 | +import superagent from "superagent"; |
| 4 | +import { afterAll, bench, describe, vi } from "vitest"; |
| 5 | +import createFetchMock from "vitest-fetch-mock"; |
| 6 | +import * as openapiTSCodegen from "./openapi-typescript-codegen.min.js"; |
| 7 | +import createClient from "../dist/index.js"; |
| 8 | + |
| 9 | +const BASE_URL = "https://api.test.local"; |
| 10 | + |
| 11 | +const fetchMocker = createFetchMock(vi); |
| 12 | + |
| 13 | +fetchMocker.enableMocks(); |
| 14 | +fetchMocker.mockResponse("{}"); |
| 15 | +afterAll(() => { |
| 16 | + fetchMocker.resetMocks(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("setup", () => { |
| 20 | + bench("openapi-fetch", async () => { |
| 21 | + createClient(); |
| 22 | + }); |
| 23 | + |
| 24 | + bench("openapi-typescript-fetch", async () => { |
| 25 | + const fetcher = Fetcher.for(); |
| 26 | + fetcher.path("/pet/findByStatus").method("get").create(); |
| 27 | + }); |
| 28 | + |
| 29 | + // axios: N/A |
| 30 | + |
| 31 | + // superagent: N/A |
| 32 | +}); |
| 33 | + |
| 34 | +describe("get (only URL)", () => { |
| 35 | + let openapiFetch = createClient({ baseUrl: BASE_URL }); |
| 36 | + let openapiTSFetch = Fetcher.for(); |
| 37 | + openapiTSFetch.configure({ |
| 38 | + init: { baseUrl: BASE_URL }, |
| 39 | + }); |
| 40 | + |
| 41 | + bench("openapi-fetch", async () => { |
| 42 | + await openapiFetch.GET("/url"); |
| 43 | + }); |
| 44 | + |
| 45 | + bench("openapi-typescript-fetch", async () => { |
| 46 | + await openapiTSFetch.path("/url").method("get").create()(); |
| 47 | + }); |
| 48 | + |
| 49 | + bench("openapi-typescript-codegen", async () => { |
| 50 | + await openapiTSCodegen.PullsService.pullsGet(); |
| 51 | + }); |
| 52 | + |
| 53 | + bench("axios", async () => { |
| 54 | + await axios.get("/url", { |
| 55 | + async adapter() { |
| 56 | + return "{}"; |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + bench("superagent", async () => { |
| 62 | + await superagent.get("/url").end(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("get (headers)", () => { |
| 67 | + let openapiFetch = createClient({ baseUrl: BASE_URL, headers: { "x-base-header": 123 } }); |
| 68 | + let openapiTSFetch = Fetcher.for(); |
| 69 | + openapiTSFetch.configure({ |
| 70 | + init: { baseUrl: BASE_URL, headers: { "x-base-header": 123 } }, |
| 71 | + }); |
| 72 | + |
| 73 | + bench("openapi-fetch", async () => { |
| 74 | + await openapiFetch.GET("/url", { headers: { "x-header-1": 123, "x-header-2": 456 } }); |
| 75 | + }); |
| 76 | + |
| 77 | + bench("openapi-typescript-fetch", async () => { |
| 78 | + await openapiTSFetch.path("/url").method("get").create()(null, { headers: { "x-header-1": 123, "x-header-2": 456 } }); |
| 79 | + }); |
| 80 | + |
| 81 | + bench("openapi-typescript-codegen", async () => { |
| 82 | + await openapiTSCodegen.PullsService.pullsGet(); |
| 83 | + }); |
| 84 | + |
| 85 | + bench("axios", async () => { |
| 86 | + await axios.get(`${BASE_URL}/url`, { |
| 87 | + headers: { "x-header-1": 123, "x-header-2": 456 }, |
| 88 | + async adapter() { |
| 89 | + return "{}"; |
| 90 | + }, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + bench("superagent", async () => { |
| 95 | + await superagent.get(`${BASE_URL}/url`).set("x-header-1", 123).set("x-header-2", 456).end(); |
| 96 | + }); |
| 97 | +}); |
0 commit comments