Skip to content

Commit

Permalink
Implement wasp/client/crud API (#1700)
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho authored Jan 31, 2024
1 parent 197c906 commit 7c93340
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ function createCrud() {
}
}

// PUBLIC API
export const {= name =} = createCrud();
4 changes: 4 additions & 0 deletions waspc/data/Generator/templates/sdk/client/crud/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{={= =}=}}
{=# cruds =}
export { {= name =} } from './{= name =}';
{=/ cruds =}
4 changes: 2 additions & 2 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"./test": "./dist/test/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./test/*": "./dist/test/*.js",
"./crud/*": "./dist/crud/*.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/crud/*": "./dist/server/crud/*",
"./email": "./dist/email/index.js",
Expand Down Expand Up @@ -118,7 +117,8 @@
"./auth": "./dist/auth/index.js",
"./client/auth": "./dist/client/auth/index.js",
"./server/auth": "./dist/server/auth/index.js",
"./server/crud": "./dist/server/crud/index.js"
"./server/crud": "./dist/server/crud/index.js",
"./client/crud": "./dist/client/crud/index.js"
},
{=!
TypeScript doesn't care about the redirects we define above in "exports" field; those
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"./dbSeed/types": "./dist/dbSeed/types.js",
"./test": "./dist/test/index.js",
"./test/*": "./dist/test/*.js",
"./crud/*": "./dist/crud/*.js",
"./server/crud/*": "./dist/server/crud/*",
"./email": "./dist/email/index.js",
"./email/core/types": "./dist/email/core/types.js",
Expand All @@ -64,7 +63,8 @@
"./auth": "./dist/auth/index.js",
"./client/auth": "./dist/client/auth/index.js",
"./server/auth": "./dist/server/auth/index.js",
"./server/crud": "./dist/server/crud/index.js"
"./server/crud": "./dist/server/crud/index.js",
"./client/crud": "./dist/client/crud/index.js"
},
"typesVersions": {
"*": {
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import waspLogo from './waspLogo.png'
import type { Task } from 'wasp/entities'
import { AuthUser, getFirstProviderUserId } from 'wasp/auth'
import { Link } from 'react-router-dom'
import { Tasks } from 'wasp/crud/Tasks'
import { Tasks } from 'wasp/client/crud'
// import login from 'wasp/auth/login'
// import signup from 'wasp/auth/signup'
import { Todo } from './Todo'
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/Todo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Todo, areThereAnyTasks } from './Todo'
import { MainPage } from './MainPage'
import type { AuthUser } from 'wasp/auth'
import { getMe } from 'wasp/client/auth'
import { Tasks } from 'wasp/crud/Tasks'
import { Tasks } from 'wasp/client/crud'

const mockTasks = [
{
Expand Down
2 changes: 2 additions & 0 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Wasp.Generator.Monad (Generator)
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 (genNewClientrudApi)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.CrudG (genCrud)
import Wasp.Generator.SdkGenerator.EmailSenderG (depsRequiredByEmail, genEmailSender)
Expand Down Expand Up @@ -118,6 +119,7 @@ genSdkReal spec =
<++> genNewClientAuth spec
<++> genNewServerApi spec
<++> genNewServerCrudApi spec
<++> genNewClientrudApi spec
where
genFileCopy = return . C.mkTmplFd

Expand Down
51 changes: 51 additions & 0 deletions waspc/src/Wasp/Generator/SdkGenerator/Client/CrudG.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Wasp.Generator.SdkGenerator.Client.CrudG
( genNewClientrudApi,
)
where

import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson
import Data.Maybe (fromJust)
import StrongPath (reldir, relfile, (</>))
import qualified StrongPath as SP
import Wasp.AppSpec (AppSpec, getCruds)
import qualified Wasp.AppSpec.Crud as AS.Crud
import Wasp.AppSpec.Valid (getIdFieldFromCrudEntity)
import Wasp.Generator.Crud (getCrudOperationJson)
import Wasp.Generator.FileDraft (FileDraft)
import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Util ((<++>))

genNewClientrudApi :: AppSpec -> Generator [FileDraft]
genNewClientrudApi spec =
if areThereAnyCruds
then
sequence
[ genCrudIndex spec cruds
]
<++> genCrudOperations spec cruds
else return []
where
cruds = getCruds spec
areThereAnyCruds = not $ null cruds

genCrudIndex :: AppSpec -> [(String, AS.Crud.Crud)] -> Generator FileDraft
genCrudIndex spec cruds = return $ C.mkTmplFdWithData [relfile|client/crud/index.ts|] tmplData
where
tmplData = object ["cruds" .= map getCrudOperationJsonFromCrud cruds]
getCrudOperationJsonFromCrud :: (String, AS.Crud.Crud) -> Aeson.Value
getCrudOperationJsonFromCrud (name, crud) = getCrudOperationJson name crud idField
where
idField = getIdFieldFromCrudEntity spec crud

genCrudOperations :: AppSpec -> [(String, AS.Crud.Crud)] -> Generator [FileDraft]
genCrudOperations spec cruds = return $ map genCrudOperation cruds
where
genCrudOperation :: (String, AS.Crud.Crud) -> FileDraft
genCrudOperation (name, crud) = C.mkTmplFdWithDstAndData tmplPath destPath (Just tmplData)
where
tmplPath = [relfile|client/crud/_crud.ts|]
destPath = [reldir|client/crud|] </> fromJust (SP.parseRelFile (name ++ ".ts"))
tmplData = getCrudOperationJson name crud idField
idField = getIdFieldFromCrudEntity spec crud
30 changes: 2 additions & 28 deletions waspc/src/Wasp/Generator/SdkGenerator/CrudG.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
module Wasp.Generator.SdkGenerator.CrudG
( genCrud,
getCrudTypesImportPathForName,
)
where

import Data.Aeson (KeyValue ((.=)), object)
import qualified Data.Aeson.Types as Aeson.Types
import Data.Maybe (fromJust)
import StrongPath
( File',
Path,
Posix,
Rel,
reldir,
reldirP,
( reldir,
relfile,
(</>),
)
import qualified StrongPath as SP
import Wasp.AppSpec (AppSpec, getCruds)
import qualified Wasp.AppSpec as AS
import qualified Wasp.AppSpec.App as AS.App
Expand All @@ -28,33 +20,18 @@ import Wasp.Generator.Crud (crudDeclarationToOperationsList, getCrudFilePath, ge
import Wasp.Generator.FileDraft (FileDraft)
import qualified Wasp.Generator.JsImport as GJI
import Wasp.Generator.Monad (Generator)
import Wasp.Generator.SdkGenerator.Common (makeSdkImportPath)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (extImportToJsImport)
import Wasp.Util ((<++>))

genCrud :: AppSpec -> Generator [FileDraft]
genCrud spec =
if areThereAnyCruds
then
genCrudOperations spec cruds
<++> genCrudServerOperations spec cruds
then genCrudServerOperations spec cruds
else return []
where
cruds = getCruds spec
areThereAnyCruds = not $ null cruds

genCrudOperations :: AppSpec -> [(String, AS.Crud.Crud)] -> Generator [FileDraft]
genCrudOperations spec cruds = return $ map genCrudOperation cruds
where
genCrudOperation :: (String, AS.Crud.Crud) -> FileDraft
genCrudOperation (name, crud) = C.mkTmplFdWithDstAndData tmplPath destPath (Just tmplData)
where
tmplPath = [relfile|crud/_crud.ts|]
destPath = [reldir|crud|] </> fromJust (SP.parseRelFile (name ++ ".ts"))
tmplData = getCrudOperationJson name crud idField
idField = getIdFieldFromCrudEntity spec crud

genCrudServerOperations :: AppSpec -> [(String, AS.Crud.Crud)] -> Generator [FileDraft]
genCrudServerOperations spec cruds = return $ map genCrudOperation cruds
where
Expand Down Expand Up @@ -91,6 +68,3 @@ genCrudServerOperations spec cruds = return $ map genCrudOperation cruds
operationToOverrideImport (operation, options) = makeCrudOperationKeyAndJsonPair operation importJson
where
importJson = GJI.jsImportToImportJson $ extImportToJsImport <$> AS.Crud.overrideFn options

getCrudTypesImportPathForName :: String -> Path Posix (Rel r) File'
getCrudTypesImportPathForName crudName = makeSdkImportPath $ [reldirP|server/crud|] </> fromJust (SP.parseRelFileP crudName)
4 changes: 1 addition & 3 deletions waspc/src/Wasp/Generator/ServerGenerator/CrudG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Wasp.Generator.Crud
import qualified Wasp.Generator.Crud.Routes as Routes
import Wasp.Generator.FileDraft (FileDraft)
import Wasp.Generator.Monad (Generator)
import Wasp.Generator.SdkGenerator.CrudG (getCrudTypesImportPathForName)
import qualified Wasp.Generator.ServerGenerator.Common as C
import Wasp.Generator.ServerGenerator.JsImport (extImportToImportJson)
import Wasp.JsImport (JsImportPath (RelativeImportPath))
Expand Down Expand Up @@ -96,8 +95,7 @@ genCrudOperations spec cruds = return $ map genCrudOperation cruds
"userEntityUpper" .= maybeUserEntity,
"overrides" .= object overrides,
"queryType" .= queryTsType,
"actionType" .= actionTsType,
"crudTypesImportPath" .= SP.fromRelFileP (getCrudTypesImportPathForName name)
"actionType" .= actionTsType
]
idField = getIdFieldFromCrudEntity spec crud
maybeUserEntity = AS.refName . AS.Auth.userEntity <$> maybeAuth
Expand Down
1 change: 1 addition & 0 deletions waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ library
Wasp.Generator.SdkGenerator.Client.AuthG
Wasp.Generator.SdkGenerator.Server.AuthG
Wasp.Generator.SdkGenerator.Server.CrudG
Wasp.Generator.SdkGenerator.Client.CrudG
Wasp.Generator.ServerGenerator
Wasp.Generator.ServerGenerator.JsImport
Wasp.Generator.ServerGenerator.ApiRoutesG
Expand Down

0 comments on commit 7c93340

Please sign in to comment.