Skip to content

Commit fc3654e

Browse files
committed
enhance(fets/internal): use the same EMPTY_OBJECT for proxies
1 parent d69840c commit fc3654e

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

.changeset/shiny-planets-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fets": patch
3+
---
4+
5+
Use the same EMPTY_OBJECT for proxies

packages/fets/src/client/createClient.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { stringify as qsStringify, type IStringifyOptions } from 'qs';
22
import { fetch, FormData } from '@whatwg-node/fetch';
33
import { iterateAsyncVoid } from '@whatwg-node/server';
4+
import { EMPTY_OBJECT } from '../plugins/utils.js';
45
import { HTTPMethod } from '../typed-fetch.js';
56
import { OpenAPIDocument, Router, SecurityScheme } from '../types.js';
67
import { createClientTypedResponsePromise } from './clientResponse.js';
@@ -54,8 +55,6 @@ function useValidationErrors(): ClientPlugin {
5455
};
5556
}
5657

57-
const EMPTY_OBJECT = {};
58-
5958
/**
6059
* Create a client for an OpenAPI document
6160
* You need to pass the imported OpenAPI document as a generic

packages/fets/src/createRouter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import landingPageRaw from './landing-page.js';
55
import { useDefineRoutes } from './plugins/define-routes.js';
66
import { useOpenAPI } from './plugins/openapi.js';
77
import { useTypeBox } from './plugins/typebox.js';
8+
import { EMPTY_OBJECT } from './plugins/utils.js';
89
import { HTTPMethod, TypedRequest, TypedResponse } from './typed-fetch.js';
910
import type {
1011
OnRouteHandleHook,
@@ -23,8 +24,6 @@ import type {
2324
} from './types.js';
2425
import { asyncIterationUntilReturn } from './utils.js';
2526

26-
const EMPTY_OBJECT = {};
27-
2827
export function createRouterBase(
2928
{
3029
fetchAPI: givenFetchAPI,

packages/fets/src/plugins/utils.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1+
export const EMPTY_OBJECT = Object.freeze({});
2+
13
export function getHeadersObj(headers: Headers): Record<string, string> {
2-
return new Proxy(
3-
{},
4-
{
5-
get(_target, prop: string) {
6-
return headers.get(prop) || undefined;
7-
},
8-
set(_target, prop: string, value) {
9-
headers.set(prop, value);
10-
return true;
11-
},
12-
has(_target, prop: string) {
13-
return headers.has(prop);
14-
},
15-
deleteProperty(_target, prop: string) {
16-
headers.delete(prop);
17-
return true;
18-
},
19-
ownKeys() {
20-
return [...headers.keys()];
21-
},
22-
getOwnPropertyDescriptor() {
23-
return {
24-
enumerable: true,
25-
configurable: true,
26-
};
27-
},
4+
return new Proxy(EMPTY_OBJECT, {
5+
get(_target, prop: string) {
6+
return headers.get(prop) || undefined;
287
},
29-
);
8+
set(_target, prop: string, value) {
9+
headers.set(prop, value);
10+
return true;
11+
},
12+
has(_target, prop: string) {
13+
return headers.has(prop);
14+
},
15+
deleteProperty(_target, prop: string) {
16+
headers.delete(prop);
17+
return true;
18+
},
19+
ownKeys() {
20+
return [...headers.keys()];
21+
},
22+
getOwnPropertyDescriptor() {
23+
return {
24+
enumerable: true,
25+
configurable: true,
26+
};
27+
},
28+
});
3029
}

0 commit comments

Comments
 (0)