From a073ab5c89beba331c38ec05609ef50b2996f021 Mon Sep 17 00:00:00 2001 From: Tyler <26290074+thegitduck@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:21:39 -0800 Subject: [PATCH] refactor(create-pages): mode changed to render for createApi --- e2e/fixtures/create-pages/src/entries.tsx | 6 ++-- examples/21_create-pages/src/entries.tsx | 6 ++-- packages/waku/src/router/create-pages.ts | 16 ++++----- packages/waku/src/router/fs-router.ts | 4 +-- packages/waku/tests/create-pages.test.ts | 40 +++++++++++++++-------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/e2e/fixtures/create-pages/src/entries.tsx b/e2e/fixtures/create-pages/src/entries.tsx index 4082e4391..1e04f4d81 100644 --- a/e2e/fixtures/create-pages/src/entries.tsx +++ b/e2e/fixtures/create-pages/src/entries.tsx @@ -103,7 +103,7 @@ const pages: ReturnType = createPages( createApi({ path: '/api/hi.txt', - mode: 'static', + render: 'static', method: 'GET', handler: async () => { const hiTxt = await readFile('./private/hi.txt'); @@ -113,7 +113,7 @@ const pages: ReturnType = createPages( createApi({ path: '/api/hi', - mode: 'dynamic', + render: 'dynamic', handlers: { GET: async () => { return new Response('hello world!'); @@ -127,7 +127,7 @@ const pages: ReturnType = createPages( createApi({ path: '/api/empty', - mode: 'static', + render: 'static', method: 'GET', handler: async () => { return new Response(null); diff --git a/examples/21_create-pages/src/entries.tsx b/examples/21_create-pages/src/entries.tsx index ee3994e80..21e05b21a 100644 --- a/examples/21_create-pages/src/entries.tsx +++ b/examples/21_create-pages/src/entries.tsx @@ -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'); @@ -133,7 +133,7 @@ const pages = createPages( createApi({ path: '/api/hi', - mode: 'dynamic', + render: 'dynamic', handlers: { GET: async () => { return new Response('hello world!'); @@ -147,7 +147,7 @@ const pages = createPages( createApi({ path: '/api/empty', - mode: 'static', + render: 'static', method: 'GET', handler: async () => { return new Response(null, { diff --git a/packages/waku/src/router/create-pages.ts b/packages/waku/src/router/create-pages.ts index 9c346f7b5..6a60f9877 100644 --- a/packages/waku/src/router/create-pages.ts +++ b/packages/waku/src/router/create-pages.ts @@ -157,13 +157,13 @@ type ApiHandler = (req: Request) => Promise; export type CreateApi = ( params: | { - mode: 'static'; + render: 'static'; path: Path; method: 'GET'; handler: ApiHandler; } | { - mode: 'dynamic'; + render: 'dynamic'; path: Path; handlers: Partial>; }, @@ -241,7 +241,7 @@ export const createPages = < const apiPathMap = new Map< string, // `${method} ${path}` { - mode: 'static' | 'dynamic'; + render: 'static' | 'dynamic'; pathSpec: PathSpec; handlers: Partial>; } @@ -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, }); @@ -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', }; }); }, diff --git a/packages/waku/src/router/fs-router.ts b/packages/waku/src/router/fs-router.ts index 92ac1f2b4..65b11705f 100644 --- a/packages/waku/src/router/fs-router.ts +++ b/packages/waku/src/router/fs-router.ts @@ -85,7 +85,7 @@ export function fsRouter( } createApi({ path: pathItems.join('/'), - mode: 'static', + render: 'static', method: 'GET', handler: mod.GET, }); @@ -108,7 +108,7 @@ export function fsRouter( ); createApi({ path: pathItems.join('/'), - mode: 'dynamic', + render: 'dynamic', handlers, }); } diff --git a/packages/waku/tests/create-pages.test.ts b/packages/waku/tests/create-pages.test.ts index 529d458ca..6c9dd3a3c 100644 --- a/packages/waku/tests/create-pages.test.ts +++ b/packages/waku/tests/create-pages.test.ts @@ -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'); @@ -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); @@ -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'); @@ -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'); @@ -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'); @@ -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)!);