From 26141f989e1ba920e94d85b1c5d0b3960cc09096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Sodi=C4=87?= Date: Wed, 31 Jan 2024 17:43:15 +0100 Subject: [PATCH] Update imports and API for `server/operations` (#1711) --- .../data/Generator/templates/sdk/package.json | 9 +-- .../server/{ => operations}/actions/index.ts | 2 + .../server/{ => operations}/actions/types.ts | 1 + .../templates/sdk/server/operations/index.ts | 4 ++ .../server/{ => operations}/queries/index.ts | 2 + .../server/{ => operations}/queries/types.ts | 1 + .../todo-typescript/src/task/actions.ts | 2 +- .../todo-typescript/src/task/queries.ts | 2 +- waspc/src/Wasp/Generator/SdkGenerator.hs | 4 +- .../Client/OperationsGenerator.hs | 2 +- .../src/Wasp/Generator/SdkGenerator/Common.hs | 5 ++ .../src/Wasp/Generator/SdkGenerator/CrudG.hs | 2 +- .../OperationsGenerator.hs} | 57 ++++++++----------- .../SdkGenerator/WebSocketGenerator.hs | 2 +- waspc/waspc.cabal | 2 +- 15 files changed, 53 insertions(+), 44 deletions(-) rename waspc/data/Generator/templates/sdk/server/{ => operations}/actions/index.ts (96%) rename waspc/data/Generator/templates/sdk/server/{ => operations}/actions/types.ts (98%) create mode 100644 waspc/data/Generator/templates/sdk/server/operations/index.ts rename waspc/data/Generator/templates/sdk/server/{ => operations}/queries/index.ts (96%) rename waspc/data/Generator/templates/sdk/server/{ => operations}/queries/types.ts (98%) rename waspc/src/Wasp/Generator/SdkGenerator/{ServerOpsGenerator.hs => Server/OperationsGenerator.hs} (72%) diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index 4d57268bdd..4cf105ab63 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -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. =} @@ -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" }, diff --git a/waspc/data/Generator/templates/sdk/server/actions/index.ts b/waspc/data/Generator/templates/sdk/server/operations/actions/index.ts similarity index 96% rename from waspc/data/Generator/templates/sdk/server/actions/index.ts rename to waspc/data/Generator/templates/sdk/server/operations/actions/index.ts index a00094e8fe..6ef6340f16 100644 --- a/waspc/data/Generator/templates/sdk/server/actions/index.ts +++ b/waspc/data/Generator/templates/sdk/server/operations/actions/index.ts @@ -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, diff --git a/waspc/data/Generator/templates/sdk/server/actions/types.ts b/waspc/data/Generator/templates/sdk/server/operations/actions/types.ts similarity index 98% rename from waspc/data/Generator/templates/sdk/server/actions/types.ts rename to waspc/data/Generator/templates/sdk/server/operations/actions/types.ts index 15a045acdf..aa8f56d0d8 100644 --- a/waspc/data/Generator/templates/sdk/server/actions/types.ts +++ b/waspc/data/Generator/templates/sdk/server/operations/actions/types.ts @@ -15,6 +15,7 @@ import { } from 'wasp/server/_types' {=# operations =} +// PUBLIC API export type {= typeName =} = {=# usesAuth =} AuthenticatedAction< diff --git a/waspc/data/Generator/templates/sdk/server/operations/index.ts b/waspc/data/Generator/templates/sdk/server/operations/index.ts new file mode 100644 index 0000000000..021333fdf2 --- /dev/null +++ b/waspc/data/Generator/templates/sdk/server/operations/index.ts @@ -0,0 +1,4 @@ +// PUBLIC API +export * from './queries/types' +// PUBLIC API +export * from './actions/types' diff --git a/waspc/data/Generator/templates/sdk/server/queries/index.ts b/waspc/data/Generator/templates/sdk/server/operations/queries/index.ts similarity index 96% rename from waspc/data/Generator/templates/sdk/server/queries/index.ts rename to waspc/data/Generator/templates/sdk/server/operations/queries/index.ts index 1c8cf33b05..dd7d537eea 100644 --- a/waspc/data/Generator/templates/sdk/server/queries/index.ts +++ b/waspc/data/Generator/templates/sdk/server/operations/queries/index.ts @@ -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, diff --git a/waspc/data/Generator/templates/sdk/server/queries/types.ts b/waspc/data/Generator/templates/sdk/server/operations/queries/types.ts similarity index 98% rename from waspc/data/Generator/templates/sdk/server/queries/types.ts rename to waspc/data/Generator/templates/sdk/server/operations/queries/types.ts index 3ae08913a3..7243631993 100644 --- a/waspc/data/Generator/templates/sdk/server/queries/types.ts +++ b/waspc/data/Generator/templates/sdk/server/operations/queries/types.ts @@ -16,6 +16,7 @@ import { } from 'wasp/server/_types' {=# operations =} +// PUBLIC API export type {= typeName =} = {=# usesAuth =} AuthenticatedQuery< diff --git a/waspc/examples/todo-typescript/src/task/actions.ts b/waspc/examples/todo-typescript/src/task/actions.ts index fbeba40687..0abaeb68d5 100644 --- a/waspc/examples/todo-typescript/src/task/actions.ts +++ b/waspc/examples/todo-typescript/src/task/actions.ts @@ -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' diff --git a/waspc/examples/todo-typescript/src/task/queries.ts b/waspc/examples/todo-typescript/src/task/queries.ts index 1b635d17c7..91e0d9fa11 100644 --- a/waspc/examples/todo-typescript/src/task/queries.ts +++ b/waspc/examples/todo-typescript/src/task/queries.ts @@ -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' diff --git a/waspc/src/Wasp/Generator/SdkGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator.hs index ca3332ef1e..31cc543c1b 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator.hs @@ -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 @@ -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 diff --git a/waspc/src/Wasp/Generator/SdkGenerator/Client/OperationsGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator/Client/OperationsGenerator.hs index 2d2213c7c7..6780c72e2c 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/Client/OperationsGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/Client/OperationsGenerator.hs @@ -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) diff --git a/waspc/src/Wasp/Generator/SdkGenerator/Common.hs b/waspc/src/Wasp/Generator/SdkGenerator/Common.hs index 189e1db167..3fe9336e06 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/Common.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/Common.hs @@ -15,6 +15,8 @@ data SdkTemplatesDir data ClientTemplatesDir +data ServerTemplatesDir + asTmplFile :: Path' (Rel d) File' -> Path' (Rel SdkTemplatesDir) File' asTmplFile = SP.castRel @@ -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|] diff --git a/waspc/src/Wasp/Generator/SdkGenerator/CrudG.hs b/waspc/src/Wasp/Generator/SdkGenerator/CrudG.hs index 30bc7ccdf9..1d8a145fb8 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/CrudG.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/CrudG.hs @@ -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 = diff --git a/waspc/src/Wasp/Generator/SdkGenerator/ServerOpsGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator/Server/OperationsGenerator.hs similarity index 72% rename from waspc/src/Wasp/Generator/SdkGenerator/ServerOpsGenerator.hs rename to waspc/src/Wasp/Generator/SdkGenerator/Server/OperationsGenerator.hs index 63e82fb20e..b6c82e634d 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/ServerOpsGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/Server/OperationsGenerator.hs @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 = @@ -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" diff --git a/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs index ae6758780e..d2b089ec42 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs @@ -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] diff --git a/waspc/waspc.cabal b/waspc/waspc.cabal index 27f6283397..4d55cd6dad 100644 --- a/waspc/waspc.cabal +++ b/waspc/waspc.cabal @@ -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