From cad358d6efdc73c09c4ffe47029a59c376998ad0 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Wed, 31 Jan 2024 12:14:50 +0100 Subject: [PATCH] Implement the wasp/client/router API (#1703) --- .../Generator/templates/react-app/src/router.tsx | 2 +- .../templates/sdk/{ => client}/router/Link.tsx | 1 + .../templates/sdk/{ => client}/router/index.ts | 3 +++ .../sdk/{ => client}/router/linkHelpers.ts | 1 + .../templates/sdk/{ => client}/router/types.ts | 5 +++++ waspc/data/Generator/templates/sdk/package.json | 5 ++--- waspc/examples/todo-typescript/src/user/auth.tsx | 2 +- waspc/src/Wasp/Generator/SdkGenerator.hs | 4 ++-- .../SdkGenerator/{ => Client}/RouterGenerator.hs | 16 ++++++++-------- waspc/waspc.cabal | 2 +- 10 files changed, 25 insertions(+), 16 deletions(-) rename waspc/data/Generator/templates/sdk/{ => client}/router/Link.tsx (97%) rename waspc/data/Generator/templates/sdk/{ => client}/router/index.ts (95%) rename waspc/data/Generator/templates/sdk/{ => client}/router/linkHelpers.ts (98%) rename waspc/data/Generator/templates/sdk/{ => client}/router/types.ts (91%) rename waspc/src/Wasp/Generator/SdkGenerator/{ => Client}/RouterGenerator.hs (74%) diff --git a/waspc/data/Generator/templates/react-app/src/router.tsx b/waspc/data/Generator/templates/react-app/src/router.tsx index 8379bfa3e1..1113b1e6f1 100644 --- a/waspc/data/Generator/templates/react-app/src/router.tsx +++ b/waspc/data/Generator/templates/react-app/src/router.tsx @@ -17,7 +17,7 @@ import createAuthRequiredPage from "./auth/pages/createAuthRequiredPage" import OAuthCodeExchange from "./auth/pages/OAuthCodeExchange" {=/ isExternalAuthEnabled =} -import { routes } from 'wasp/router' +import { routes } from 'wasp/client/router' export const routeNameToRouteComponent = { {=# routes =} diff --git a/waspc/data/Generator/templates/sdk/router/Link.tsx b/waspc/data/Generator/templates/sdk/client/router/Link.tsx similarity index 97% rename from waspc/data/Generator/templates/sdk/router/Link.tsx rename to waspc/data/Generator/templates/sdk/client/router/Link.tsx index 571262cd57..2215ddc639 100644 --- a/waspc/data/Generator/templates/sdk/router/Link.tsx +++ b/waspc/data/Generator/templates/sdk/client/router/Link.tsx @@ -5,6 +5,7 @@ import { type Routes } from './index' type RouterLinkProps = Parameters[0] +// PUBLIC API export function Link( { to, params, search, hash, ...restOfProps }: Omit & { diff --git a/waspc/data/Generator/templates/sdk/router/index.ts b/waspc/data/Generator/templates/sdk/client/router/index.ts similarity index 95% rename from waspc/data/Generator/templates/sdk/router/index.ts rename to waspc/data/Generator/templates/sdk/client/router/index.ts index b1dfa68ab8..91e2ccb954 100644 --- a/waspc/data/Generator/templates/sdk/router/index.ts +++ b/waspc/data/Generator/templates/sdk/client/router/index.ts @@ -6,6 +6,7 @@ import type { ParamValue, } from './types' +// PUBLIC API export const routes = { {=# routes =} {= name =}: { @@ -26,6 +27,8 @@ export const routes = { {=/ routes =} } as const; +// PRIVATE API export type Routes = RouteDefinitionsToRoutes +// PUBLIC API export { Link } from './Link' diff --git a/waspc/data/Generator/templates/sdk/router/linkHelpers.ts b/waspc/data/Generator/templates/sdk/client/router/linkHelpers.ts similarity index 98% rename from waspc/data/Generator/templates/sdk/router/linkHelpers.ts rename to waspc/data/Generator/templates/sdk/client/router/linkHelpers.ts index 291d8238a3..e10cba9281 100644 --- a/waspc/data/Generator/templates/sdk/router/linkHelpers.ts +++ b/waspc/data/Generator/templates/sdk/client/router/linkHelpers.ts @@ -1,5 +1,6 @@ import type { Params, Search } from "./types"; +// PRIVATE API export function interpolatePath( path: string, params?: Params, diff --git a/waspc/data/Generator/templates/sdk/router/types.ts b/waspc/data/Generator/templates/sdk/client/router/types.ts similarity index 91% rename from waspc/data/Generator/templates/sdk/router/types.ts rename to waspc/data/Generator/templates/sdk/client/router/types.ts index 0688d0bbba..ffaeb4fe01 100644 --- a/waspc/data/Generator/templates/sdk/router/types.ts +++ b/waspc/data/Generator/templates/sdk/client/router/types.ts @@ -1,13 +1,18 @@ +// PRIVATE API export type RouteDefinitionsToRoutes = RouteDefinitionsToRoutesObj[keyof RouteDefinitionsToRoutesObj] + // PRIVATE API export type OptionalRouteOptions = { search?: Search hash?: string } +// PRIVATE API export type ParamValue = string | number +// PRIVATE API export type Params = { [name: string]: ParamValue } +// PRIVATE API export type Search = | string[][] | Record diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index c7477e7a26..b3f7feeea7 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -90,8 +90,6 @@ {=! Todo(filip): This export becomes problematic once we start supporting different executors =} "./server/jobs/pgBoss/types": "./dist/server/jobs/pgBoss/types.js", {=! Used by users, documented. =} - "./router": "./dist/router/index.js", - {=! Used by users, documented. =} "./server/webSocket": "./dist/server/webSocket/index.js", {=! Used by users, documented. =} "./webSocket": "./dist/webSocket/index.js", @@ -119,7 +117,8 @@ "./server/crud": "./dist/server/crud/index.js", "./client/crud": "./dist/client/crud/index.js", "./server/email": "./dist/server/email/index.js", - "./server/jobs": "./dist/server/jobs/index.js" + "./server/jobs": "./dist/server/jobs/index.js", + "./client/router": "./dist/client/router/index.js" }, {=! TypeScript doesn't care about the redirects we define above in "exports" field; those diff --git a/waspc/examples/todo-typescript/src/user/auth.tsx b/waspc/examples/todo-typescript/src/user/auth.tsx index 9a15825461..272a16fbdb 100644 --- a/waspc/examples/todo-typescript/src/user/auth.tsx +++ b/waspc/examples/todo-typescript/src/user/auth.tsx @@ -19,7 +19,7 @@ import { // signInUrl as googleSignInUrl, // } from "wasp/auth/helpers/Google"; -import { Link, routes } from 'wasp/router' +import { Link, routes } from 'wasp/client/router' export function SignupPage() { return ( diff --git a/waspc/src/Wasp/Generator/SdkGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator.hs index d0556bb7cc..d5221ab229 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator.hs @@ -39,9 +39,9 @@ import qualified Wasp.Generator.NpmDependencies as N import Wasp.Generator.SdkGenerator.AuthG (genAuth) import Wasp.Generator.SdkGenerator.Client.AuthG (genNewClientAuth) import Wasp.Generator.SdkGenerator.Client.CrudG (genNewClientCrudApi) +import Wasp.Generator.SdkGenerator.Client.RouterGenerator (genNewClientRouterApi) import qualified Wasp.Generator.SdkGenerator.Common as C import Wasp.Generator.SdkGenerator.CrudG (genCrud) -import Wasp.Generator.SdkGenerator.RouterGenerator (genRouter) import Wasp.Generator.SdkGenerator.RpcGenerator (genRpc) import Wasp.Generator.SdkGenerator.Server.AuthG (genNewServerApi) import Wasp.Generator.SdkGenerator.Server.CrudG (genNewServerCrudApi) @@ -110,7 +110,6 @@ genSdkReal spec = <++> genCrud spec <++> genServerApi spec <++> genWebSockets spec - <++> genRouter spec <++> genMiddleware spec <++> genExportedTypesDir spec -- New API @@ -120,6 +119,7 @@ genSdkReal spec = <++> genNewClientCrudApi spec <++> genNewEmailSenderApi spec <++> genNewJobsApi spec + <++> genNewClientRouterApi spec where genFileCopy = return . C.mkTmplFd diff --git a/waspc/src/Wasp/Generator/SdkGenerator/RouterGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator/Client/RouterGenerator.hs similarity index 74% rename from waspc/src/Wasp/Generator/SdkGenerator/RouterGenerator.hs rename to waspc/src/Wasp/Generator/SdkGenerator/Client/RouterGenerator.hs index c5f89fae90..57875a355f 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/RouterGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/Client/RouterGenerator.hs @@ -1,5 +1,5 @@ -module Wasp.Generator.SdkGenerator.RouterGenerator - ( genRouter, +module Wasp.Generator.SdkGenerator.Client.RouterGenerator + ( genNewClientRouterApi, ) where @@ -14,19 +14,19 @@ import Wasp.Generator.Monad (Generator) import qualified Wasp.Generator.SdkGenerator.Common as C import Wasp.Util.WebRouterPath (Param (Optional, Required), extractPathParams) -genRouter :: AppSpec -> Generator [FileDraft] -genRouter spec = +genNewClientRouterApi :: AppSpec -> Generator [FileDraft] +genNewClientRouterApi spec = sequence [ genRouterTsx spec, - genFileCopy [relfile|router/types.ts|], - genFileCopy [relfile|router/linkHelpers.ts|], - genFileCopy [relfile|router/Link.tsx|] + genFileCopy [relfile|client/router/types.ts|], + genFileCopy [relfile|client/router/linkHelpers.ts|], + genFileCopy [relfile|client/router/Link.tsx|] ] where genFileCopy = return . C.mkTmplFd genRouterTsx :: AppSpec -> Generator FileDraft -genRouterTsx spec = return $ C.mkTmplFdWithData [relfile|router/index.ts|] tmplData +genRouterTsx spec = return $ C.mkTmplFdWithData [relfile|client/router/index.ts|] tmplData where tmplData = object ["routes" .= map createRouteTemplateData (AS.getRoutes spec)] diff --git a/waspc/waspc.cabal b/waspc/waspc.cabal index 03063900a8..00f9282f18 100644 --- a/waspc/waspc.cabal +++ b/waspc/waspc.cabal @@ -300,10 +300,10 @@ library Wasp.Generator.SdkGenerator.AuthG Wasp.Generator.SdkGenerator.Client.AuthG Wasp.Generator.SdkGenerator.Client.CrudG + Wasp.Generator.SdkGenerator.Client.RouterGenerator Wasp.Generator.SdkGenerator.Common Wasp.Generator.SdkGenerator.CrudG Wasp.Generator.SdkGenerator.EmailSender.Providers - Wasp.Generator.SdkGenerator.RouterGenerator Wasp.Generator.SdkGenerator.RpcGenerator Wasp.Generator.SdkGenerator.Server.AuthG Wasp.Generator.SdkGenerator.Server.CrudG