Skip to content

Commit 2517a13

Browse files
committed
feat: bff query supports obj
1 parent c4be382 commit 2517a13

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

.changeset/ten-tires-yawn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/create-request': patch
3+
---
4+
5+
feat: bff query supports obj
6+
feat: bff query 支持对象类型

packages/server/create-request/src/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
Sender,
99
UploadCreator,
1010
} from './types';
11-
import { getUploadPayload } from './utiles';
11+
import { getUploadPayload, processQueryValues } from './utiles';
1212

1313
const realRequest: Map<string, typeof fetch> = new Map();
1414

@@ -100,7 +100,7 @@ export const createRequest: RequestCreator = ({
100100
const finalPath = getFinalPath(payload.params);
101101

102102
finalURL = payload.query
103-
? `${finalPath}?${stringify(payload.query)}`
103+
? `${finalPath}?${stringify(processQueryValues(payload.query))}`
104104
: finalPath;
105105
headers = payload.headers || {};
106106
body =

packages/server/create-request/src/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
Sender,
1212
UploadCreator,
1313
} from './types';
14-
import { getUploadPayload } from './utiles';
14+
import { getUploadPayload, processQueryValues } from './utiles';
1515

1616
type Fetch = typeof nodeFetch;
1717

@@ -104,7 +104,7 @@ export const createRequest: RequestCreator<typeof nodeFetch> = ({
104104

105105
const plainPath = getFinalPath(payload.params);
106106
const finalPath = payload.query
107-
? `${plainPath}?${stringify(payload.query)}`
107+
? `${plainPath}?${stringify(processQueryValues(payload.query))}`
108108
: plainPath;
109109
headers = payload.headers || {};
110110

packages/server/create-request/src/utiles.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ export const getUploadPayload = (args: any) => {
2424

2525
return { body, headers: payload.headers, params: payload.params };
2626
};
27+
28+
export const processQueryValues = (
29+
query: Record<string, any>,
30+
): Record<string, string | string[]> => {
31+
const processedQuery: Record<string, string | string[]> = {};
32+
33+
Object.keys(query).forEach(key => {
34+
const value = query[key];
35+
processedQuery[key] =
36+
typeof value === 'string' ||
37+
(Array.isArray(value) && value.every(item => typeof item === 'string'))
38+
? value
39+
: JSON.stringify(value);
40+
});
41+
42+
return processedQuery;
43+
};

tests/integration/bff-hono/api/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,27 @@ export const post = async ({ formUrlencoded }: { formUrlencoded: any }) => {
2424
};
2525
};
2626

27+
const ArrayObjectSchema = z.array(z.object({ from: z.string() }));
28+
29+
const StringOrArrayObject = z
30+
.union([
31+
ArrayObjectSchema,
32+
z.string().refine(str => {
33+
try {
34+
const parsed = JSON.parse(str);
35+
return ArrayObjectSchema.safeParse(parsed).success;
36+
} catch (error) {
37+
return false;
38+
}
39+
}),
40+
])
41+
.transform(value => {
42+
return typeof value === 'string' ? JSON.parse(value) : value;
43+
});
44+
2745
const QuerySchema = z.object({
2846
user: z.string().email(),
47+
ext: StringOrArrayObject,
2948
});
3049

3150
const DataSchema = z.object({
@@ -63,6 +82,7 @@ export const postHello = Api(
6382
return input;
6483
}),
6584
async ({ query, data, params, headers }) => {
85+
console.log('query:>>', query.ext[0]);
6686
const c = useHonoContext();
6787
c.res.headers.set('x-bff-api', c.req.path);
6888
return {
@@ -75,9 +95,13 @@ export const postHello = Api(
7595
},
7696
);
7797

98+
const GetQuerySchema = z.object({
99+
user: z.string().email(),
100+
});
101+
78102
export const getHello = Api(
79103
Get('/hello/get'),
80-
Query(QuerySchema),
104+
Query(GetQuerySchema),
81105
async ({ query }) => {
82106
try {
83107
const c = useHonoContext();

tests/integration/bff-hono/src/routes/base/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Page = () => {
2727
},
2828
query: {
2929
30+
ext: [{ from: '123' }],
3031
},
3132
data: {
3233
message: '3333',

0 commit comments

Comments
 (0)