Skip to content

Commit

Permalink
refactor(create-pages): mode changed to render for createApi (#1160)
Browse files Browse the repository at this point in the history
mode => render

Co-authored-by: Tyler <[email protected]>
  • Loading branch information
tylersayshi and tylersayshi authored Jan 16, 2025
1 parent ff5e06f commit 23aca04
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
6 changes: 3 additions & 3 deletions e2e/fixtures/create-pages/src/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const pages: ReturnType<typeof createPages> = createPages(

createApi({
path: '/api/hi.txt',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
const hiTxt = await readFile('./private/hi.txt');
Expand All @@ -113,7 +113,7 @@ const pages: ReturnType<typeof createPages> = createPages(

createApi({
path: '/api/hi',
mode: 'dynamic',
render: 'dynamic',
handlers: {
GET: async () => {
return new Response('hello world!');
Expand All @@ -127,7 +127,7 @@ const pages: ReturnType<typeof createPages> = createPages(

createApi({
path: '/api/empty',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
return new Response(null);
Expand Down
6 changes: 3 additions & 3 deletions examples/21_create-pages/src/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const pages = createPages(

createApi({
path: '/api/hi.txt',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
const hiTxt = await readFile('./private/hi.txt');
Expand All @@ -133,7 +133,7 @@ const pages = createPages(

createApi({
path: '/api/hi',
mode: 'dynamic',
render: 'dynamic',
handlers: {
GET: async () => {
return new Response('hello world!');
Expand All @@ -147,7 +147,7 @@ const pages = createPages(

createApi({
path: '/api/empty',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
return new Response(null, {
Expand Down
16 changes: 8 additions & 8 deletions packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ type ApiHandler = (req: Request) => Promise<Response>;
export type CreateApi = <Path extends string>(
params:
| {
mode: 'static';
render: 'static';
path: Path;
method: 'GET';
handler: ApiHandler;
}
| {
mode: 'dynamic';
render: 'dynamic';
path: Path;
handlers: Partial<Record<Method, ApiHandler>>;
},
Expand Down Expand Up @@ -241,7 +241,7 @@ export const createPages = <
const apiPathMap = new Map<
string, // `${method} ${path}`
{
mode: 'static' | 'dynamic';
render: 'static' | 'dynamic';
pathSpec: PathSpec;
handlers: Partial<Record<Method, ApiHandler>>;
}
Expand Down Expand Up @@ -422,15 +422,15 @@ export const createPages = <
throw new Error(`Duplicated api path: ${options.path}`);
}
const pathSpec = parsePathWithSlug(options.path);
if (options.mode === 'static') {
if (options.render === 'static') {
apiPathMap.set(options.path, {
mode: 'static',
render: 'static',
pathSpec,
handlers: { GET: options.handler },
});
} else {
apiPathMap.set(options.path, {
mode: 'dynamic',
render: 'dynamic',
pathSpec,
handlers: options.handlers,
});
Expand Down Expand Up @@ -639,10 +639,10 @@ export const createPages = <
getApiConfig: async () => {
await configure();

return Array.from(apiPathMap.values()).map(({ pathSpec, mode }) => {
return Array.from(apiPathMap.values()).map(({ pathSpec, render }) => {
return {
path: pathSpec,
isStatic: mode === 'static',
isStatic: render === 'static',
};
});
},
Expand Down
4 changes: 2 additions & 2 deletions packages/waku/src/router/fs-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function fsRouter(
}
createApi({
path: pathItems.join('/'),
mode: 'static',
render: 'static',
method: 'GET',
handler: mod.GET,
});
Expand All @@ -108,7 +108,7 @@ export function fsRouter(
);
createApi({
path: pathItems.join('/'),
mode: 'dynamic',
render: 'dynamic',
handlers,
});
}
Expand Down
40 changes: 26 additions & 14 deletions packages/waku/tests/create-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,29 @@ describe('type tests', () => {
describe('createApi', () => {
it('static', () => {
const createApi: CreateApi = vi.fn();
// @ts-expect-error: mode is not valid
createApi({ path: '/', mode: 'foo', method: 'GET', handler: () => null });
createApi({
path: '/',
mode: 'static',
// @ts-expect-error: render is not valid
render: 'foo',
method: 'GET',
// @ts-expect-error: null is not valid Response
handler: () => null,
});
createApi({
path: '/',
render: 'static',
// @ts-expect-error: method is not valid
method: 'foo',
// @ts-expect-error: null is not valid
handler: () => null,
});
// @ts-expect-error: handler is not valid
createApi({ path: '/', mode: 'static', method: 'GET', handler: 123 });
createApi({ path: '/', render: 'static', method: 'GET', handler: 123 });

// good
createApi({
path: '/',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
return new Response('Hello World');
Expand All @@ -293,23 +299,29 @@ describe('type tests', () => {
});
it('dynamic', () => {
const createApi: CreateApi = vi.fn();
// @ts-expect-error: mode & handler are not valid
createApi({ path: '/', mode: 'foo', method: 'GET', handler: () => null });
createApi({
path: '/',
// @ts-expect-error: render not valid
render: 'foo',
method: 'GET',
// @ts-expect-error: handler not valid
handler: () => null,
});
createApi({
path: '/foo',
mode: 'dynamic',
render: 'dynamic',
handlers: {
// @ts-expect-error: null is not valid
GET: () => null,
},
});
// @ts-expect-error: handler is not valid
createApi({ path: '/', mode: 'dynamic', method: 'GET', handler: 123 });
createApi({ path: '/', render: 'dynamic', method: 'GET', handler: 123 });

// good
createApi({
path: '/foo/[slug]',
mode: 'dynamic',
render: 'dynamic',
handlers: {
POST: async (req) => {
return new Response('Hello World ' + new URL(req.url).pathname);
Expand Down Expand Up @@ -546,7 +558,7 @@ describe('createPages pages and layouts', () => {
createPages(async ({ createApi }) => [
createApi({
path: '/test',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
return new Response('Hello World');
Expand Down Expand Up @@ -578,7 +590,7 @@ describe('createPages pages and layouts', () => {
createPages(async ({ createApi }) => [
createApi({
path: '/test/[slug]',
mode: 'dynamic',
render: 'dynamic',
handlers: {
GET: async () => {
return new Response('Hello World');
Expand Down Expand Up @@ -1309,7 +1321,7 @@ describe('createPages api', () => {
createPages(async ({ createApi }) => [
createApi({
path: '/test',
mode: 'static',
render: 'static',
method: 'GET',
handler: async () => {
return new Response('Hello World');
Expand Down Expand Up @@ -1341,7 +1353,7 @@ describe('createPages api', () => {
createPages(async ({ createApi }) => [
createApi({
path: '/test/[slug]',
mode: 'dynamic',
render: 'dynamic',
handlers: {
GET: async (req) => {
return new Response('Hello World ' + req.url.split('/').at(-1)!);
Expand Down

0 comments on commit 23aca04

Please sign in to comment.