Skip to content

Commit

Permalink
fix: handleApi and related issues (with example defineRouter with api…
Browse files Browse the repository at this point in the history
… call) (#1116)

@dai-shi when I curl `http://localhost:3000/api/hi` I see the log, but
the response is still html.

Am I setting this up wrong, or is there more work to do so that we
response with 'Hello World' here?

---------

Co-authored-by: Tyler <[email protected]>
Co-authored-by: daishi <[email protected]>
  • Loading branch information
3 people authored Jan 6, 2025
1 parent de3a3c6 commit 4ce83b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
20 changes: 20 additions & 0 deletions examples/22_define-router/src/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,24 @@ export default defineRouter({
}
throw new Error('renderRoute: No such path:' + path);
},
getApiConfig: async () => [
{
path: [
{ type: 'literal', name: 'api' },
{ type: 'literal', name: 'hi' },
],
},
{
path: [
{ type: 'literal', name: 'api' },
{ type: 'literal', name: 'hi.txt' },
],
isStatic: true,
},
],
handleApi: async () => {
return {
status: 200,
};
},
});
16 changes: 7 additions & 9 deletions packages/waku/src/lib/middleware/dev-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const createMainViteServer = (
include: ['react-server-dom-webpack/client.edge'],
},
},
appType: 'mpa',
appType: 'custom',
server: { middlewareMode: true },
},
config,
Expand Down Expand Up @@ -201,11 +201,14 @@ const createMainViteServer = (
});
};

// TODO We might be able to elminate this function
// FIXME This function feels like a hack
const willBeHandled = async (pathname: string) => {
const vite = await vitePromise;
try {
const result = await vite.transformRequest(pathname);
if (result?.code === `export default "/@fs${pathname}"`) {
return false;
}
return !!result;
} catch {
return false;
Expand Down Expand Up @@ -417,14 +420,9 @@ export const devServer: Middleware = (options) => {
transformIndexHtml,
};

if (
// HACK depending on `rscBase` is a bad idea
// FIXME This hack should be removed as well as `willBeHandled`
ctx.req.url.pathname.startsWith(config.basePath + config.rscBase + '/') ||
!(await willBeHandled(ctx.req.url.pathname))
) {
if (!(await willBeHandled(ctx.req.url.pathname))) {
await next();
if (ctx.res.body) {
if (ctx.res.body || ctx.res.status) {
return;
}
}
Expand Down
15 changes: 12 additions & 3 deletions packages/waku/src/lib/renderers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export const encodeFuncId = (funcId: string) => {
if (name.includes('/')) {
throw new Error('Function name must not include `/`: ' + name);
}
if (file.startsWith('_')) {
throw new Error('File must not start with `_`: ' + file);
}
if (file.startsWith('/')) {
return FUNC_PREFIX + '_' + file + '/' + name;
}
return FUNC_PREFIX + file + '/' + name;
};

Expand All @@ -50,9 +56,12 @@ export const decodeFuncId = (encoded: string) => {
return null;
}
const index = encoded.lastIndexOf('/');
return (
encoded.slice(FUNC_PREFIX.length, index) + '#' + encoded.slice(index + 1)
);
const file = encoded.slice(FUNC_PREFIX.length, index);
const name = encoded.slice(index + 1);
if (file.startsWith('_')) {
return file.slice(1) + '#' + name;
}
return file + '#' + name;
};

export const hasStatusCode = (x: unknown): x is { statusCode: number } =>
Expand Down
6 changes: 6 additions & 0 deletions packages/waku/src/router/define-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ export function unstable_defineRouter(fns: {
const entriesCache = new Map<string, Record<string, ReactNode>>();
await Promise.all(
pathConfig.map(async ({ pathSpec, pathname, pattern, specs }) => {
if (specs.isApi) {
return;
}
const moduleIds = new Set<string>();
moduleIdsForPrefetch.set(pathSpec, moduleIds);
if (!pathname) {
Expand Down Expand Up @@ -387,6 +390,9 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => {
};`;

for (const { pathSpec, pathname, specs } of pathConfig) {
if (specs.isApi) {
continue;
}
tasks.push(async () => {
const moduleIds = moduleIdsForPrefetch.get(pathSpec)!;
if (pathname) {
Expand Down

0 comments on commit 4ce83b2

Please sign in to comment.