Skip to content

Commit 480c90a

Browse files
tylersayshidai-shi
andauthored
fix(create-pages): pass request url for createApi handler with search (#1300)
fixes #1296 --------- Co-authored-by: Tyler <[email protected]> Co-authored-by: Daishi Kato <[email protected]>
1 parent a122193 commit 480c90a

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

e2e/create-pages.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ for (const mode of ['DEV', 'PRD'] as const) {
146146
expect(await res.text()).toBe('hello world!');
147147
});
148148

149+
test('api url with search params', async () => {
150+
const res = await fetch(`http://localhost:${port}/api/url?foo=bar`);
151+
expect(res.status).toBe(200);
152+
expect(await res.text()).toBe(
153+
`url http://localhost:${port}/api/url?foo=bar`,
154+
);
155+
});
156+
149157
test('api hi.txt', async () => {
150158
const res = await fetch(`http://localhost:${port}/api/hi.txt`);
151159
expect(res.status).toBe(200);

e2e/fixtures/create-pages/src/entries.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ const pages: ReturnType<typeof createPages> = createPages(
145145
},
146146
}),
147147

148+
createApi({
149+
path: '/api/url',
150+
render: 'dynamic',
151+
handlers: {
152+
GET: async (req) => {
153+
return new Response('url ' + req.url);
154+
},
155+
},
156+
}),
157+
148158
createApi({
149159
path: '/api/empty',
150160
render: 'static',

packages/waku/src/router/create-pages.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -699,22 +699,14 @@ export const createPages = <
699699
};
700700
});
701701
},
702-
handleApi: async (path, options) => {
702+
handleApi: async (path, { url, ...options }) => {
703703
await configure();
704704
const routePath = getApiRoutePath(path, options.method);
705705
if (!routePath) {
706706
throw new Error('API Route not found: ' + path);
707707
}
708708
const { handlers } = apiPathMap.get(routePath)!;
709-
710-
const req = new Request(
711-
new URL(
712-
path,
713-
// TODO consider if we should apply `Forwarded` header here
714-
'http://localhost',
715-
),
716-
options,
717-
);
709+
const req = new Request(url, options);
718710
const handler = handlers[options.method as Method];
719711
if (!handler) {
720712
throw new Error(

0 commit comments

Comments
 (0)