Skip to content

Commit

Permalink
[New SDK]: 'wasp/server/api'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinsos committed Jan 29, 2024
1 parent c2d5bcd commit 48eddd3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
6 changes: 5 additions & 1 deletion waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@
{=! Used by users, documented. =}
"./webSocket": "./dist/webSocket/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx"
"./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx",

{=! ================= NEW API HERE =================== =}
{=! Public: { type MyEntity1, type MyEntity2, ... } =}
"./server/api": "./dist/server/api/index.js"
},
"license": "ISC",
"include": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import { type ParamsDictionary as ExpressParams, type Query as ExpressQuery } from 'express-serve-static-core'

export type { ParamsDictionary as ExpressParams, Query as ExpressQuery } from 'express-serve-static-core'

import {
{=# allEntities =}
type {= internalTypeName =},
Expand All @@ -16,6 +14,8 @@ import {
{=/ shouldImportAuthenticatedApi =}
} from '../_types'

// PUBLIC API

{=# apiRoutes =}
export type {= typeName =}<
P extends ExpressParams = ExpressParams,
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FooBar } from "wasp/server/apis/types"; // This type is generated by Wasp based on the `api` declaration above.
import type { FooBar } from 'wasp/server/api'; // This type is generated by Wasp based on the `api` declaration above.

export const fooBar: FooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", "*"); // Example of modifying headers to override Wasp default CORS middleware.
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 @@ -36,14 +36,14 @@ import Wasp.Generator.Job.IO (readJobMessagesAndPrintThemPrefixed)
import Wasp.Generator.Job.Process (runNodeCommandAsJob)
import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.NpmDependencies as N
import Wasp.Generator.SdkGenerator.ApiRoutesG (genApis)
import Wasp.Generator.SdkGenerator.AuthG (genAuth)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.CrudG (genCrud)
import Wasp.Generator.SdkGenerator.EmailSenderG (depsRequiredByEmail, genEmailSender)
import Wasp.Generator.SdkGenerator.JobGenerator (genJobTypes)
import Wasp.Generator.SdkGenerator.RouterGenerator (genRouter)
import Wasp.Generator.SdkGenerator.RpcGenerator (genRpc)
import Wasp.Generator.SdkGenerator.ServerApiG (genServerApi)
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (genOperations)
import Wasp.Generator.SdkGenerator.WebSocketGenerator (depsRequiredByWebSockets, genWebSockets)
import qualified Wasp.Generator.ServerGenerator.AuthG as ServerAuthG
Expand Down Expand Up @@ -105,7 +105,7 @@ genSdkReal spec =
<++> genEntitiesAndServerTypesDirs spec
<++> genCrud spec
<++> genJobTypes spec
<++> genApis spec
<++> genServerApi spec
<++> genWebSockets spec
<++> genRouter spec
<++> genMiddleware spec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Wasp.Generator.SdkGenerator.ApiRoutesG
( genApis,
module Wasp.Generator.SdkGenerator.ServerApiG
( genServerApi,
)
where

Expand All @@ -19,19 +19,19 @@ import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.ServerGenerator.ApiRoutesG (getApiEntitiesObject, isAuthEnabledForApi)
import Wasp.Util (toUpperFirst)

genApis :: AppSpec -> Generator [FileDraft]
genApis spec =
genServerApi :: AppSpec -> Generator [FileDraft]
genServerApi spec =
if areThereAnyCustomApiRoutes
then
sequence
[ genApiTypes spec
[ genIndexTsWithApiRoutes spec
]
else return []
where
areThereAnyCustomApiRoutes = not . null $ getApis spec

genApiTypes :: AppSpec -> Generator FileDraft
genApiTypes spec =
genIndexTsWithApiRoutes :: AppSpec -> Generator FileDraft
genIndexTsWithApiRoutes spec =
return $ C.mkTmplFdWithDstAndData tmplFile dstFile (Just tmplData)
where
namedApis = AS.getApis spec
Expand All @@ -44,7 +44,7 @@ genApiTypes spec =
"allEntities" .= nub (concatMap getApiEntitiesObject apis)
]
usesAuth = fromMaybe (isAuthEnabledGlobally spec) . Api.auth
tmplFile = C.asTmplFile [relfile|server/apis/types.ts|]
tmplFile = C.asTmplFile [relfile|server/api/index.ts|]
dstFile = SP.castRel tmplFile :: Path' (Rel C.SdkRootDir) File'

getTmplData :: (String, Api.Api) -> Aeson.Value
Expand Down
2 changes: 1 addition & 1 deletion waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ library
Wasp.Generator.NpmInstall.Common
Wasp.Generator.NpmInstall.InstalledNpmDepsLog
Wasp.Generator.SdkGenerator
Wasp.Generator.SdkGenerator.ApiRoutesG
Wasp.Generator.SdkGenerator.ServerApiG
Wasp.Generator.SdkGenerator.Auth.AuthFormsG
Wasp.Generator.SdkGenerator.Auth.EmailAuthG
Wasp.Generator.SdkGenerator.CrudG
Expand Down

0 comments on commit 48eddd3

Please sign in to comment.