Skip to content

Commit

Permalink
Implement the wasp/client/router API (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho authored and sodic committed Jan 31, 2024
1 parent 58d22d9 commit cad358d
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/react-app/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 =}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type Routes } from './index'

type RouterLinkProps = Parameters<typeof RouterLink>[0]

// PUBLIC API
export function Link(
{ to, params, search, hash, ...restOfProps }: Omit<RouterLinkProps, "to">
& {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ParamValue,
} from './types'

// PUBLIC API
export const routes = {
{=# routes =}
{= name =}: {
Expand All @@ -26,6 +27,8 @@ export const routes = {
{=/ routes =}
} as const;

// PRIVATE API
export type Routes = RouteDefinitionsToRoutes<typeof routes>

// PUBLIC API
export { Link } from './Link'
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Params, Search } from "./types";

// PRIVATE API
export function interpolatePath(
path: string,
params?: Params,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// PRIVATE API
export type RouteDefinitionsToRoutes<Routes extends RoutesDefinition> =
RouteDefinitionsToRoutesObj<Routes>[keyof RouteDefinitionsToRoutesObj<Routes>]

// 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<string, string>
Expand Down
5 changes: 2 additions & 3 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/user/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -110,7 +110,6 @@ genSdkReal spec =
<++> genCrud spec
<++> genServerApi spec
<++> genWebSockets spec
<++> genRouter spec
<++> genMiddleware spec
<++> genExportedTypesDir spec
-- New API
Expand All @@ -120,6 +119,7 @@ genSdkReal spec =
<++> genNewClientCrudApi spec
<++> genNewEmailSenderApi spec
<++> genNewJobsApi spec
<++> genNewClientRouterApi spec
where
genFileCopy = return . C.mkTmplFd

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Wasp.Generator.SdkGenerator.RouterGenerator
( genRouter,
module Wasp.Generator.SdkGenerator.Client.RouterGenerator
( genNewClientRouterApi,
)
where

Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cad358d

Please sign in to comment.