-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add apis/types to proper SDK gen (#1661)
- Loading branch information
Showing
13 changed files
with
92 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { FooBar } from "wasp/server/apis/types"; // 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. | ||
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Wasp.Generator.SdkGenerator.ApiRoutesG | ||
( genApis, | ||
) | ||
where | ||
|
||
import Data.Aeson (object, (.=)) | ||
import qualified Data.Aeson as Aeson | ||
import Data.List (nub) | ||
import Data.Maybe (fromMaybe) | ||
import StrongPath (File', Path', Rel, relfile) | ||
import qualified StrongPath as SP | ||
import Wasp.AppSpec (AppSpec, getApis) | ||
import qualified Wasp.AppSpec as AS | ||
import qualified Wasp.AppSpec.Api as Api | ||
import Wasp.AppSpec.Valid (isAuthEnabled) | ||
import Wasp.Generator.FileDraft (FileDraft) | ||
import Wasp.Generator.Monad (Generator) | ||
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 = | ||
if areThereAnyCustomApiRoutes | ||
then | ||
sequence | ||
[ genApiTypes spec | ||
] | ||
else return [] | ||
where | ||
areThereAnyCustomApiRoutes = not . null $ getApis spec | ||
|
||
genApiTypes :: AppSpec -> Generator FileDraft | ||
genApiTypes spec = | ||
return $ C.mkTmplFdWithDstAndData tmplFile dstFile (Just tmplData) | ||
where | ||
namedApis = AS.getApis spec | ||
apis = snd <$> namedApis | ||
tmplData = | ||
object | ||
[ "apiRoutes" .= map getTmplData namedApis, | ||
"shouldImportAuthenticatedApi" .= any usesAuth apis, | ||
"shouldImportNonAuthenticatedApi" .= not (all usesAuth apis), | ||
"allEntities" .= nub (concatMap getApiEntitiesObject apis) | ||
] | ||
usesAuth = fromMaybe (isAuthEnabledGlobally spec) . Api.auth | ||
tmplFile = C.asTmplFile [relfile|server/apis/types.ts|] | ||
dstFile = SP.castRel tmplFile :: Path' (Rel C.SdkRootDir) File' | ||
|
||
getTmplData :: (String, Api.Api) -> Aeson.Value | ||
getTmplData (name, api) = | ||
object | ||
[ "typeName" .= toUpperFirst name, | ||
"entities" .= getApiEntitiesObject api, | ||
"usesAuth" .= isAuthEnabledForApi spec api | ||
] | ||
|
||
isAuthEnabledGlobally :: AppSpec -> Bool | ||
isAuthEnabledGlobally = isAuthEnabled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters