forked from openapi-ts/openapi-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.bench.js
171 lines (144 loc) · 4.47 KB
/
index.bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import axios from "axios";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { Fetcher } from "openapi-typescript-fetch";
import superagent from "superagent";
import { afterAll, bench, describe } from "vitest";
import createClient, { createPathBasedClient } from "../dist/index.js";
import * as openapiTSCodegen from "./fixtures/openapi-typescript-codegen.min.js";
import { createApiFetchClient } from "feature-fetch";
const BASE_URL = "https://api.test.local";
const server = setupServer(
http.get(`${BASE_URL}/url`, () =>
HttpResponse.json({
message: "success",
}),
),
// Used by openapi-typescript-codegen
http.get("https://api.github.com/repos/test1/test2/pulls/test3", () =>
HttpResponse.json({
message: "success",
}),
),
);
// Ensure we are listening early enough so all the requests are intercepted
server.listen({
onUnhandledRequest: (request) => {
throw new Error(`No request handler found for ${request.method} ${request.url}`);
},
});
afterAll(() => {
server.close();
});
describe("setup", () => {
bench("openapi-fetch", async () => {
createClient({ baseUrl: BASE_URL });
});
bench("openapi-fetch (path based)", async () => {
createPathBasedClient({ baseUrl: BASE_URL });
});
bench("openapi-typescript-fetch", async () => {
const fetcher = Fetcher.for();
fetcher.configure({
baseUrl: BASE_URL,
});
fetcher.path("/pet/findByStatus").method("get").create();
});
bench("axios", async () => {
axios.create({
baseURL: BASE_URL,
});
});
bench("feature-fetch", async () => {
createApiFetchClient({ prefixUrl: BASE_URL });
});
// superagent: N/A
});
describe("get (only URL)", () => {
const openapiFetch = createClient({ baseUrl: BASE_URL });
const openapiFetchPath = createPathBasedClient({ baseUrl: BASE_URL });
const openapiTSFetch = Fetcher.for();
openapiTSFetch.configure({
baseUrl: BASE_URL,
});
const openapiTSFetchGET = openapiTSFetch.path("/url").method("get").create();
const axiosInstance = axios.create({
baseURL: BASE_URL,
});
const featureFetch = createApiFetchClient({ prefixUrl: BASE_URL });
bench("openapi-fetch", async () => {
await openapiFetch.GET("/url");
});
bench("openapi-fetch (path based)", async () => {
await openapiFetchPath["/url"].GET();
});
bench("openapi-typescript-fetch", async () => {
await openapiTSFetchGET();
});
bench("openapi-typescript-codegen", async () => {
await openapiTSCodegen.PullsService.pullsGet("test1", "test2", "test3");
});
bench("axios", async () => {
await axiosInstance.get("/url");
});
bench("superagent", async () => {
await superagent.get(`${BASE_URL}/url`);
});
bench("feature-fetch", async () => {
await featureFetch.get("/url");
});
});
describe("get (headers)", () => {
const openapiFetch = createClient({
baseUrl: BASE_URL,
headers: { "x-base-header": 123 },
});
const openapiFetchPath = createPathBasedClient({
baseUrl: BASE_URL,
headers: { "x-base-header": 123 },
});
const openapiTSFetch = Fetcher.for();
openapiTSFetch.configure({
baseUrl: BASE_URL,
init: { headers: { "x-base-header": 123 } },
});
const openapiTSFetchGET = openapiTSFetch.path("/url").method("get").create();
const axiosInstance = axios.create({
baseURL: BASE_URL,
});
const featureFetch = createApiFetchClient({
prefixUrl: BASE_URL,
headers: { "x-base-header": "123" },
});
bench("openapi-fetch", async () => {
await openapiFetch.GET("/url", {
headers: { "x-header-1": 123, "x-header-2": 456 },
});
});
bench("openapi-fetch (path based)", async () => {
await openapiFetchPath["/url"].GET({
headers: { "x-header-1": 123, "x-header-2": 456 },
});
});
bench("openapi-typescript-fetch", async () => {
await openapiTSFetchGET(null, {
headers: { "x-header-1": 123, "x-header-2": 456 },
});
});
bench("openapi-typescript-codegen", async () => {
await openapiTSCodegen.PullsService.pullsGet("test1", "test2", "test3");
});
bench("axios", async () => {
await axiosInstance.get("/url", {
headers: { "x-header-1": 123, "x-header-2": 456 },
});
});
bench("superagent", async () => {
await superagent.get(`${BASE_URL}/url`).set("x-header-1", 123).set("x-header-2", 456);
});
bench("feature-fetch", async () => {
await featureFetch.get("/url", {
headers: { "x-header-1": "123", "x-header-2": "456" },
});
});
});