Skip to content

Commit

Permalink
Add server setup to proper SDK gen (#1667)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinsos authored Jan 24, 2024
1 parent 54a45eb commit f6d1461
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
"./server/dbClient": "./dist/server/dbClient.js",
{=! Used by users and by our code, documented. =}
"./server/config": "./dist/server/config.js",
{=! Used by users and by our code, documented. =}
"./server/types": "./dist/server/types/index.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. =}
Expand Down
12 changes: 12 additions & 0 deletions waspc/data/Generator/templates/sdk/server/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type Application } from 'express'
import { Server } from 'http'

export type ServerSetupFn = (context: ServerSetupFnContext) => Promise<void>

export type ServerSetupFnContext = {
app: Application,
server: Server,
}

export type { Application } from 'express'
export type { Server } from 'http'
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import config from 'wasp/server/config'

{=# setupFn.isDefined =}
{=& setupFn.importStatement =}
import { ServerSetupFn, ServerSetupFnContext } from './types'
import { ServerSetupFn, ServerSetupFnContext } from 'wasp/server/types'
{=/ setupFn.isDefined =}

{=# isPgBossJobExecutorUsed =}
Expand Down
13 changes: 0 additions & 13 deletions waspc/data/Generator/templates/server/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
{{={= =}=}}

import { type Application } from 'express'
import { Server } from 'http'

export type ServerSetupFn = (context: ServerSetupFnContext) => Promise<void>

export type ServerSetupFnContext = {
app: Application,
server: Server,
}

export type { Application } from 'express'
export type { Server } from 'http'

{=# isEmailAuthEnabled =}
export type { GetVerificationEmailContentFn, GetPasswordResetEmailContentFn } from '../auth/providers/email/types';
{=/ isEmailAuthEnabled =}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"./universal/validators": "./dist/universal/validators.js",
"./server/dbClient": "./dist/server/dbClient.js",
"./server/config": "./dist/server/config.js",
"./server/types": "./dist/server/types/index.js",
"./server/utils": "./dist/server/utils.js",
"./server/actions": "./dist/server/actions/index.js",
"./server/queries": "./dist/server/queries/index.js",
Expand Down
1 change: 1 addition & 0 deletions waspc/examples/todo-typescript/main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ app TodoTypescript {
system: PostgreSQL
},
server: {
setupFn: import { serverSetup } from "@src/serverSetup.js",
middlewareConfigFn: import { serverMiddlewareFn } from "@src/serverSetup.js"
}
}
Expand Down
8 changes: 8 additions & 0 deletions waspc/examples/todo-typescript/src/serverSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express'
import cors from 'cors'
import type { MiddlewareConfigFn } from 'wasp/server/middleware'
import config from 'wasp/server/config'
import type { Application, ServerSetupFn } from 'wasp/server/types'

export const serverMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => {
// Example of adding an extra domains to CORS.
Expand All @@ -21,3 +22,10 @@ export const fooBarNamespace: MiddlewareConfigFn = (middlewareConfig) => {

return middlewareConfig
}

export const serverSetup: ServerSetupFn = async ({ app }: { app: Application}) => {
app.get('/customRoute', (_req, res) => {
res.send('I am a custom route')
})
console.log("I am a server setup function!");
}
5 changes: 5 additions & 0 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ genSdkReal spec =
<++> genEntitiesAndServerTypesDirs spec
<++> genApis spec
<++> genMiddleware spec
<++> genExportedTypesDir spec
where
genFileCopy = return . C.mkTmplFd

Expand Down Expand Up @@ -288,6 +289,10 @@ genServerUtils spec = return $ C.mkTmplFdWithData [relfile|server/utils.ts|] tmp
where
tmplData = object ["isAuthEnabled" .= (isAuthEnabled spec :: Bool)]

genExportedTypesDir :: AppSpec -> Generator [FileDraft]
genExportedTypesDir _spec =
return [C.mkTmplFd [relfile|server/types/index.ts|]]

genMiddleware :: AppSpec -> Generator [FileDraft]
genMiddleware _spec =
sequence
Expand Down
4 changes: 1 addition & 3 deletions waspc/src/Wasp/Generator/ServerGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,8 @@ genExportedTypesDir spec =
where
tmplData =
object
[ "isExternalAuthEnabled" .= isExternalAuthEnabled,
"isEmailAuthEnabled" .= isEmailAuthEnabled
[ "isEmailAuthEnabled" .= isEmailAuthEnabled
]
isExternalAuthEnabled = AS.App.Auth.isExternalAuthEnabled <$> maybeAuth
isEmailAuthEnabled = AS.App.Auth.isEmailAuthEnabled <$> maybeAuth
maybeAuth = AS.App.auth $ snd $ getApp spec

Expand Down

0 comments on commit f6d1461

Please sign in to comment.