Skip to content

Commit

Permalink
ready fs-router for createApi changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersayshi committed Jan 13, 2025
1 parent 7aa00b5 commit 6a613db
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/11_fs-router/src/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare global {

export default fsRouter(
import.meta.url,
(file: string) => import.meta.glob('./pages/**/*.tsx')[`./pages/${file}`]?.(),
(file: string) =>
import.meta.glob('./pages/**/*.{tsx,ts}')[`./pages/${file}`]?.(),
'pages',
);
7 changes: 7 additions & 0 deletions examples/11_fs-router/src/pages/api/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function GET() {
return new Response('Hello from API!');
}

export async function POST(req: Request) {
return new Response(`Hello from API! ${new URL(req.url).pathname}`);
}
2 changes: 2 additions & 0 deletions packages/waku/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
export const SRC_MAIN = 'main';
export const SRC_ENTRIES = 'entries';
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods partial
export const METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'] as const;
3 changes: 3 additions & 0 deletions packages/waku/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ReactNode } from 'react';

import type { Config } from '../config.js';
import type { PathSpec } from '../lib/utils/path.js';
import type { METHODS } from './constants.js';

type Elements = Record<string, ReactNode>;

Expand Down Expand Up @@ -100,3 +101,5 @@ export type HandlerRes = {
headers?: Record<string, string | string[]>;
status?: number;
};

export type Method = (typeof METHODS)[number];
3 changes: 1 addition & 2 deletions packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
} from './create-pages-utils/inferred-path-types.js';
import { Children, Slot } from '../minimal/client.js';
import { ErrorBoundary } from '../router/client.js';
import type { Method } from 'waku/lib/types';

const sanitizeSlug = (slug: string) =>
slug.replace(/\./g, '').replace(/ /g, '-');
Expand Down Expand Up @@ -148,8 +149,6 @@ export type CreateLayout = <Path extends string>(
},
) => void;

type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';

type ApiHandler = (req: Request) => Promise<Response>;

export type CreateApi = <Path extends string>(
Expand Down
45 changes: 37 additions & 8 deletions packages/waku/src/router/fs-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { unstable_getPlatformObject } from '../server.js';
import { createPages } from './create-pages.js';

import { EXTENSIONS } from '../lib/constants.js';
import { EXTENSIONS, METHODS } from '../lib/constants.js';
import type { Method } from '../lib/types.js';

const DO_NOT_BUNDLE = '';

Expand Down Expand Up @@ -76,13 +77,41 @@ export function fsRouter(
'Page file cannot be named [path]. This will conflict with the path prop of the page component.',
);
} else if (pathItems.at(0) === 'api') {
createApi({
path: pathItems.slice(1).join('/'),
mode: 'dynamic',
method: 'GET',
handler: mod.default,
...config,
});
if (config?.render === 'static') {
if (Object.keys(mod).length !== 2 || !mod.GET) {
console.warn(
`API ${path} is invalid. For static API routes, only a single GET handler is supported.`,
);
}
createApi({
path: pathItems.join('/'),
mode: 'static',
method: 'GET',
handler: mod.GET,
});
} else {
const validMethods = new Set(METHODS);
const handlers = Object.fromEntries(
Object.entries(mod).filter(([exportName]) => {
const isValidExport =
exportName === 'getConfig' ||
validMethods.has(exportName as Method);
if (!isValidExport) {
console.warn(
`API ${path} has an invalid export: ${exportName}. Valid exports are: ${METHODS.join(
', ',
)}`,
);
}
return isValidExport && exportName !== 'getConfig';
}),
);
createApi({
path: pathItems.join('/'),
mode: 'dynamic',
handlers,
});
}
} else if (pathItems.at(-1) === '_layout') {
createLayout({
path,
Expand Down

0 comments on commit 6a613db

Please sign in to comment.