Skip to content

Commit 2753ee6

Browse files
committed
Micro perf improvement
1 parent 2bbeb92 commit 2753ee6

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

docs/openapi-fetch/api.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,19 @@ Sometimes your backend doesn’t use one of the standard serialization methods,
7474

7575
```ts
7676
const client = createClient({
77-
querySerializer(queryParam) {
78-
let s = [];
79-
for (const [k, v] of Object.entries(queryParam)) {
80-
if (Array.isArray(v)) {
81-
for (const i of v) {
82-
s.push(`${k}[]=${encodeURIComponent(i)}`);
77+
querySerializer(queryParams) {
78+
const search = [];
79+
for (const name in queryParams) {
80+
const value = queryParams[name];
81+
if (Array.isArray(value)) {
82+
for (const item of value) {
83+
s.push(`${name}[]=${encodeURIComponent(item)}`);
8384
}
8485
} else {
85-
s.push(`${k}=${encodeURLComponent(v)}`);
86+
s.push(`${name}=${encodeURLComponent(value)}`);
8687
}
8788
}
88-
return encodeURI(s.join(",")); // ?tags[]=food,tags[]=california,tags[]=healthy
89+
return search.join(","); // ?tags[]=food,tags[]=california,tags[]=healthy
8990
},
9091
});
9192
```
@@ -126,8 +127,8 @@ const { data, error } = await PUT("/submit", {
126127
},
127128
bodySerializer(body) {
128129
const fd = new FormData();
129-
for (const [k, v] of Object.entries(body)) {
130-
fd.append(k, v);
130+
for (const name in body) {
131+
fd.append(name, body[name]);
131132
}
132133
return fd;
133134
},

packages/openapi-fetch/src/index.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const DEFAULT_HEADERS = {
33
"Content-Type": "application/json",
44
};
55

6-
const LEADING_QUESTION_RE = /^\?+/;
76
const PATH_PARAM_RE = /\{[^{}]+\}/g;
87

98
/**
@@ -19,7 +18,7 @@ export default function createClient(clientOptions) {
1918
} = clientOptions ?? {};
2019
let baseUrl = baseOptions.baseUrl ?? "";
2120
if (baseUrl.endsWith("/")) {
22-
baseUrl = baseUrl.slice(0, -1); // remove trailing slash
21+
baseUrl = baseUrl.substring(0, baseUrl.length - 1);
2322
}
2423

2524
/**
@@ -331,12 +330,8 @@ export function createQuerySerializer(options) {
331330
* @see https://swagger.io/docs/specification/serialization/#path
332331
*/
333332
export function defaultPathSerializer(pathname, pathParams) {
334-
const matches = pathname.match(PATH_PARAM_RE);
335-
if (!matches || !matches.length) {
336-
return undefined;
337-
}
338333
let nextURL = pathname;
339-
for (const match of matches) {
334+
for (const match of pathname.match(PATH_PARAM_RE) ?? []) {
340335
let paramName = match.substring(1, match.length - 1);
341336
let explode = false;
342337
let style = "simple";
@@ -411,9 +406,10 @@ export function createFinalURL(pathname, options) {
411406
if (options.params?.path) {
412407
finalURL = defaultPathSerializer(finalURL, options.params.path);
413408
}
414-
const search = options
415-
.querySerializer(options.params.query ?? {})
416-
.replace(LEADING_QUESTION_RE, "");
409+
let search = options.querySerializer(options.params.query ?? {});
410+
if (search.startsWith("?")) {
411+
search = search.substring(1);
412+
}
417413
if (search) {
418414
finalURL += `?${search}`;
419415
}

packages/openapi-fetch/test/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ describe("client", () => {
684684
},
685685
bodySerializer(body) {
686686
const fd = new FormData();
687-
for (const [k, v] of Object.entries(body)) {
688-
fd.append(k, v);
687+
for (const name in body) {
688+
fd.append(name, body[name as keyof typeof body]);
689689
}
690690
return fd;
691691
},

packages/openapi-fetch/test/v7-beta.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,8 @@ describe("client", () => {
693693
},
694694
bodySerializer(body) {
695695
const fd = new FormData();
696-
for (const [k, v] of Object.entries(body)) {
697-
fd.append(k, v);
696+
for (const name in body) {
697+
fd.append(name, body[name as keyof typeof body]);
698698
}
699699
return fd;
700700
},

0 commit comments

Comments
 (0)