Skip to content

Commit

Permalink
Update imports and API for server/operations (#1711)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Jan 31, 2024
1 parent dd16fb9 commit 26141f9
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 44 deletions.
9 changes: 5 additions & 4 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
"./universal/validators": "./dist/universal/validators.js",
{=! Parts are used by users, documented. Parts are probably used by our code, undocumented (but accessible). =}
"./server/utils": "./dist/server/utils.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/actions": "./dist/server/actions/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/queries": "./dist/server/queries/index.js",
{=! Used by our code (SDK for full-stack type safety), uncodumented (but accessible) for users. =}
"./server/opeations/actions": "./dist/server/actions/index.js",
{=! Used by our code (SDK for full-stack type safety), uncodumented (but accessible) for users. =}
"./server/operations/queries": "./dist/server/queries/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/auth/email": "./dist/server/auth/email/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
Expand Down Expand Up @@ -107,6 +107,7 @@
"./client/crud": "./dist/client/crud/index.js",
"./server/email": "./dist/server/email/index.js",
"./server/jobs": "./dist/server/jobs/index.js",
"./server/operations": "./dist/server/operations/index.js",
"./client/router": "./dist/client/router/index.js",
"./client/test": "./dist/client/test/index.js"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { prisma } from 'wasp/server'
{=/ operations =}
{=# operations =}

// PRIVATE API
export type {= operationTypeName =} = typeof {= jsFn.importIdentifier =}

// PUBLIC API
export const {= operationName =} = async (args, context) => {
return ({= jsFn.importIdentifier =} as any)(args, {
...context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'wasp/server/_types'

{=# operations =}
// PUBLIC API
export type {= typeName =}<Input extends Payload = never, Output extends Payload = Payload> =
{=# usesAuth =}
AuthenticatedAction<
Expand Down
4 changes: 4 additions & 0 deletions waspc/data/Generator/templates/sdk/server/operations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// PUBLIC API
export * from './queries/types'
// PUBLIC API
export * from './actions/types'
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { prisma } from 'wasp/server'
{=/ operations =}
{=# operations =}

// PRIVATE API
export type {= operationTypeName =} = typeof {= jsFn.importIdentifier =}

// PUBLIC API
export const {= operationName =} = async (args, context) => {
return ({= jsFn.importIdentifier =} as any)(args, {
...context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from 'wasp/server/_types'

{=# operations =}
// PUBLIC API
export type {= typeName =}<Input extends Payload = never, Output extends Payload = Payload> =
{=# usesAuth =}
AuthenticatedQuery<
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/task/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
CreateTask,
UpdateTask,
DeleteTasks,
} from 'wasp/server/actions/types'
} from 'wasp/server/operations'
import type { Task } from 'wasp/entities'
import { emailSender } from 'wasp/server/email'

Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/task/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpError, AuthError } from 'wasp/server'
import type { GetTasks } from 'wasp/server/queries/types'
import type { GetTasks } from 'wasp/server/operations'
import type { Task } from 'wasp/entities'
import { ensureValidEmail, createProviderId } from 'wasp/server/auth'

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 @@ -47,8 +47,8 @@ import Wasp.Generator.SdkGenerator.Server.AuthG (genNewServerApi)
import Wasp.Generator.SdkGenerator.Server.CrudG (genNewServerCrudApi)
import Wasp.Generator.SdkGenerator.Server.EmailSenderG (depsRequiredByEmail, genNewEmailSenderApi)
import Wasp.Generator.SdkGenerator.Server.JobGenerator (genNewJobsApi)
import qualified Wasp.Generator.SdkGenerator.Server.OperationsGenerator as ServerOpsGen
import Wasp.Generator.SdkGenerator.ServerApiG (genServerApi)
import qualified Wasp.Generator.SdkGenerator.ServerOpsGenerator as ServerOpsGen
import Wasp.Generator.SdkGenerator.WebSocketGenerator (depsRequiredByWebSockets, genWebSockets)
import qualified Wasp.Generator.ServerGenerator.AuthG as ServerAuthG
import qualified Wasp.Generator.WebAppGenerator.Common as WebApp
Expand Down Expand Up @@ -96,9 +96,9 @@ genSdkReal spec =
genServerUtils spec,
genPackageJson spec
]
<++> ServerOpsGen.genOperations spec
<++> ClientOpsGen.genOperations spec
<++> genAuth spec
<++> ServerOpsGen.genOperations spec
<++> genUniversalDir
<++> genExternalCodeDir (AS.externalCodeFiles spec)
<++> genEntitiesAndServerTypesDirs spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Wasp.Generator.FileDraft (FileDraft)
import Wasp.Generator.Monad (Generator)
import Wasp.Generator.SdkGenerator.Common (SdkTemplatesDir, clientTemplatesDirInSdkTemplatesDir, makeSdkImportPath, relDirToRelFileP)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (serverOperationsDirInSdkRootDir)
import Wasp.Generator.SdkGenerator.Server.OperationsGenerator (serverOperationsDirInSdkRootDir)
import qualified Wasp.Generator.ServerGenerator as ServerGenerator
import qualified Wasp.Generator.ServerGenerator.OperationsRoutesG as ServerOperationsRoutesG
import Wasp.JsImport (JsImportName (JsImportField), JsImportPath (ModuleImportPath), getJsImportStmtAndIdentifier, makeJsImport)
Expand Down
5 changes: 5 additions & 0 deletions waspc/src/Wasp/Generator/SdkGenerator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ data SdkTemplatesDir

data ClientTemplatesDir

data ServerTemplatesDir

asTmplFile :: Path' (Rel d) File' -> Path' (Rel SdkTemplatesDir) File'
asTmplFile = SP.castRel

Expand Down Expand Up @@ -65,3 +67,6 @@ extCodeDirInSdkRootDir = [reldir|ext-src|]

clientTemplatesDirInSdkTemplatesDir :: Path' (Rel SdkTemplatesDir) (Dir ClientTemplatesDir)
clientTemplatesDirInSdkTemplatesDir = [reldir|client|]

serverTemplatesDirInSdkTemplatesDir :: Path' (Rel SdkTemplatesDir) (Dir ServerTemplatesDir)
serverTemplatesDirInSdkTemplatesDir = [reldir|server|]
2 changes: 1 addition & 1 deletion waspc/src/Wasp/Generator/SdkGenerator/CrudG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Wasp.Generator.FileDraft (FileDraft)
import qualified Wasp.Generator.JsImport as GJI
import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (extImportToJsImport)
import Wasp.Generator.SdkGenerator.Server.OperationsGenerator (extImportToJsImport)

genCrud :: AppSpec -> Generator [FileDraft]
genCrud spec =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{-# LANGUAGE TypeApplications #-}

module Wasp.Generator.SdkGenerator.ServerOpsGenerator where
module Wasp.Generator.SdkGenerator.Server.OperationsGenerator where

import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson
import Data.List (nub)
import Data.Maybe (fromJust, fromMaybe)
import StrongPath (Dir', File', Path', Rel, reldir, relfile, (</>))
import StrongPath (Dir, Dir', File', Path', Rel, reldir, relfile, (</>))
import qualified StrongPath as SP
import Wasp.AppSpec (AppSpec)
import qualified Wasp.AppSpec as AS
Expand All @@ -20,25 +20,34 @@ import Wasp.Generator.Common (makeJsonWithEntityData)
import Wasp.Generator.FileDraft (FileDraft)
import qualified Wasp.Generator.JsImport as GJI
import Wasp.Generator.Monad (Generator)
import Wasp.Generator.SdkGenerator.Common (mkTmplFdWithData)
import Wasp.Generator.SdkGenerator.Common (SdkTemplatesDir, mkTmplFdWithData, serverTemplatesDirInSdkTemplatesDir)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.JsImport (JsImport (..), JsImportPath (..))
import qualified Wasp.JsImport as JI
import Wasp.Util (toUpperFirst)

data ServerOpsTemplatesDir

serverOpsDirInSdkTemplatesDir :: Path' (Rel SdkTemplatesDir) (Dir ServerOpsTemplatesDir)
serverOpsDirInSdkTemplatesDir = serverTemplatesDirInSdkTemplatesDir </> [reldir|operations|]

genServerOpsFileCopy :: Path' (Rel ServerOpsTemplatesDir) File' -> Generator FileDraft
genServerOpsFileCopy path = return $ C.mkTmplFd $ serverOpsDirInSdkTemplatesDir </> path

genOperations :: AppSpec -> Generator [FileDraft]
genOperations spec =
sequence
[ genQueryTypesFile spec,
genActionTypesFile spec,
genQueriesIndex spec,
genActionsIndex spec
genActionsIndex spec,
genServerOpsFileCopy [relfile|index.ts|]
]

genQueriesIndex :: AppSpec -> Generator FileDraft
genQueriesIndex spec = return $ mkTmplFdWithData relPath tmplData
where
relPath = [relfile|server/queries/index.ts|]
relPath = serverOpsDirInSdkTemplatesDir </> [relfile|queries/index.ts|]
tmplData =
object
[ "operations" .= map getQueryData (AS.getQueries spec)
Expand All @@ -47,25 +56,23 @@ genQueriesIndex spec = return $ mkTmplFdWithData relPath tmplData
genActionsIndex :: AppSpec -> Generator FileDraft
genActionsIndex spec = return $ mkTmplFdWithData relPath tmplData
where
relPath = [relfile|server/actions/index.ts|]
relPath = serverOpsDirInSdkTemplatesDir </> [relfile|actions/index.ts|]
tmplData =
object
[ "operations" .= map getActionData (AS.getActions spec)
]

genQueryTypesFile :: AppSpec -> Generator FileDraft
genQueryTypesFile spec = genOperationTypesFile tmplFile dstFile operations isAuthEnabledGlobally
genQueryTypesFile spec = genOperationTypesFile relPath operations isAuthEnabledGlobally
where
tmplFile = [relfile|server/queries/types.ts|]
dstFile = [relfile|server/queries/types.ts|]
relPath = serverOpsDirInSdkTemplatesDir </> [relfile|queries/types.ts|]
operations = map (uncurry AS.Operation.QueryOp) $ AS.getQueries spec
isAuthEnabledGlobally = isAuthEnabled spec

genActionTypesFile :: AppSpec -> Generator FileDraft
genActionTypesFile spec = genOperationTypesFile tmplFile dstFile operations isAuthEnabledGlobally
genActionTypesFile spec = genOperationTypesFile relPath operations isAuthEnabledGlobally
where
tmplFile = [relfile|server/actions/types.ts|]
dstFile = [relfile|server/actions/types.ts|]
relPath = serverOpsDirInSdkTemplatesDir </> [relfile|actions/types.ts|]
operations = map (uncurry AS.Operation.ActionOp) $ AS.getActions spec
isAuthEnabledGlobally = isAuthEnabled spec

Expand All @@ -84,12 +91,11 @@ getActionData (actionName, action) = getOperationTmplData operation

genOperationTypesFile ::
Path' (Rel C.SdkTemplatesDir) File' ->
Path' (Rel C.SdkRootDir) File' ->
[AS.Operation.Operation] ->
Bool ->
Generator FileDraft
genOperationTypesFile tmplFile dstFile operations isAuthEnabledGlobally =
return $ C.mkTmplFdWithDstAndData tmplFile dstFile (Just tmplData)
genOperationTypesFile tmplFile operations isAuthEnabledGlobally =
return $ C.mkTmplFdWithData tmplFile tmplData
where
tmplData =
object
Expand All @@ -108,8 +114,10 @@ genOperationTypesFile tmplFile dstFile operations isAuthEnabledGlobally =
usesAuth = fromMaybe isAuthEnabledGlobally . AS.Operation.getAuth

serverOperationsDirInSdkRootDir :: AS.Operation.Operation -> Path' (Rel C.SdkRootDir) Dir'
serverOperationsDirInSdkRootDir (AS.Operation.QueryOp _ _) = [reldir|server/queries|]
serverOperationsDirInSdkRootDir (AS.Operation.ActionOp _ _) = [reldir|server/actions|]
serverOperationsDirInSdkRootDir =
SP.castRel . (serverOpsDirInSdkTemplatesDir </>) . \case
(AS.Operation.QueryOp _ _) -> [reldir|queries|]
(AS.Operation.ActionOp _ _) -> [reldir|actions|]

getOperationTmplData :: AS.Operation.Operation -> Aeson.Value
getOperationTmplData operation =
Expand Down Expand Up @@ -143,18 +151,3 @@ extImportToJsImport extImport@(EI.ExtImport extImportName extImportPath) =
importPath = C.makeSdkImportPath $ extCodeDirP </> SP.castRel extImportPath
extCodeDirP = fromJust $ SP.relDirToPosix C.extCodeDirInSdkRootDir
importName = GJI.extImportNameToJsImportName extImportName

-- extImportToImportJson :: EI.ExtImport -> Aeson.Value
-- extImportToImportJson extImport@(EI.ExtImport importName importPath) =
-- object
-- [ "isDefined" .= True,
-- "importStatement" .= Debug.trace jsImportStmt jsImportStmt,
-- "importIdentifier" .= importAlias
-- ]
-- where
-- jsImportStmt = case importName of
-- EI.ExtImportModule n -> "import " ++ n ++ " from '" ++ importPathStr ++ "'"
-- EI.ExtImportField n -> "import { " ++ n ++ " as " ++ importAlias ++ " } from '" ++ importPathStr ++ "'"
-- importPathStr = C.makeSdkImportPath $ extCodeDirP </> SP.castRel importPath
-- extCodeDirP = fromJust $ SP.relDirToPosix C.extCodeDirInSdkRootDir
-- importAlias = EI.importIdentifier extImport ++ "User"
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Wasp.Generator.FileDraft (FileDraft)
import qualified Wasp.Generator.JsImport as GJI
import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (extImportToJsImport)
import Wasp.Generator.SdkGenerator.Server.OperationsGenerator (extImportToJsImport)
import qualified Wasp.Generator.WebSocket as AS.WS

genWebSockets :: AppSpec -> Generator [FileDraft]
Expand Down
2 changes: 1 addition & 1 deletion waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ library
Wasp.Generator.SdkGenerator.Server.CrudG
Wasp.Generator.SdkGenerator.Server.EmailSenderG
Wasp.Generator.SdkGenerator.Server.JobGenerator
Wasp.Generator.SdkGenerator.Server.OperationsGenerator
Wasp.Generator.SdkGenerator.ServerApiG
Wasp.Generator.SdkGenerator.ServerOpsGenerator
Wasp.Generator.SdkGenerator.WebSocketGenerator
Wasp.Generator.ServerGenerator
Wasp.Generator.ServerGenerator.JsImport
Expand Down

0 comments on commit 26141f9

Please sign in to comment.