Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/plain-rivers-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/create-request': patch
---

feat: bff query supports obj
feat: bff query 支持对象类型
4 changes: 2 additions & 2 deletions packages/server/create-request/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
Sender,
UploadCreator,
} from './types';
import { getUploadPayload } from './utiles';
import { getUploadPayload, processQueryValues } from './utiles';

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

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

finalURL = payload.query
? `${finalPath}?${stringify(payload.query)}`
? `${finalPath}?${stringify(processQueryValues(payload.query))}`
: finalPath;
headers = payload.headers || {};
body =
Expand Down
4 changes: 2 additions & 2 deletions packages/server/create-request/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
Sender,
UploadCreator,
} from './types';
import { getUploadPayload } from './utiles';
import { getUploadPayload, processQueryValues } from './utiles';

type Fetch = typeof nodeFetch;

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

const plainPath = getFinalPath(payload.params);
const finalPath = payload.query
? `${plainPath}?${stringify(payload.query)}`
? `${plainPath}?${stringify(processQueryValues(payload.query))}`
: plainPath;
headers = payload.headers || {};

Expand Down
17 changes: 17 additions & 0 deletions packages/server/create-request/src/utiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ export const getUploadPayload = (args: any) => {

return { body, headers: payload.headers, params: payload.params };
};

export const processQueryValues = (
query: Record<string, any>,
): Record<string, string | string[]> => {
const processedQuery: Record<string, string | string[]> = {};

Object.keys(query).forEach(key => {
const value = query[key];
processedQuery[key] =
typeof value === 'string' ||
(Array.isArray(value) && value.every(item => typeof item === 'string'))
? value
: JSON.stringify(value);
});

return processedQuery;
};
26 changes: 25 additions & 1 deletion tests/integration/bff-hono/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,27 @@ export const post = async ({ formUrlencoded }: { formUrlencoded: any }) => {
};
};

const ArrayObjectSchema = z.array(z.object({ from: z.string() }));

const StringOrArrayObject = z
.union([
ArrayObjectSchema,
z.string().refine(str => {
try {
const parsed = JSON.parse(str);
return ArrayObjectSchema.safeParse(parsed).success;
} catch (error) {
return false;
}
}),
])
.transform(value => {
return typeof value === 'string' ? JSON.parse(value) : value;
});

const QuerySchema = z.object({
user: z.string().email(),
ext: StringOrArrayObject,
});

const DataSchema = z.object({
Expand Down Expand Up @@ -63,6 +82,7 @@ export const postHello = Api(
return input;
}),
async ({ query, data, params, headers }) => {
console.log('query:>>', query.ext[0]);
const c = useHonoContext();
c.res.headers.set('x-bff-api', c.req.path);
return {
Expand All @@ -75,9 +95,13 @@ export const postHello = Api(
},
);

const GetQuerySchema = z.object({
user: z.string().email(),
});

export const getHello = Api(
Get('/hello/get'),
Query(QuerySchema),
Query(GetQuerySchema),
async ({ query }) => {
try {
const c = useHonoContext();
Expand Down
1 change: 1 addition & 0 deletions tests/integration/bff-hono/src/routes/base/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Page = () => {
},
query: {
user: '[email protected]',
ext: [{ from: '123' }],
},
data: {
message: '3333',
Expand Down