Skip to content

Commit

Permalink
Moved middleware configuration to SDK + fixed sdk/server/config.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinsos committed Jan 23, 2024
1 parent ca3c11c commit 54a45eb
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 10 deletions.
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"./universal/validators": "./dist/universal/validators.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/dbClient": "./dist/server/dbClient.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
{=! Used by users and by our code, documented. =}
"./server/config": "./dist/server/config.js",
{=! Parts are used by users, documented. Parts are probably used by our code, undocumented (but accessible). =}
"./server/utils": "./dist/server/utils.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { type RequestHandler } from 'express'

export type MiddlewareConfig = Map<string, RequestHandler>

export type MiddlewareConfigFn = (middlewareConfig: MiddlewareConfig) => MiddlewareConfig

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './globalMiddleware.js'
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import cors from 'cors'
import helmet from 'helmet'

import config from 'wasp/server/config'
import type { MiddlewareConfig, MiddlewareConfigFn } from 'wasp/server/middleware'
export type { MiddlewareConfig, MiddlewareConfigFn } from 'wasp/server/middleware'

{=# globalMiddlewareConfigFn.isDefined =}
{=& globalMiddlewareConfigFn.importStatement =}
Expand All @@ -14,10 +16,6 @@ import config from 'wasp/server/config'
const {=& globalMiddlewareConfigFn.importAlias =} = (mc: MiddlewareConfig) => mc
{=/ globalMiddlewareConfigFn.isDefined =}

export type MiddlewareConfig = Map<string, express.RequestHandler>

export type MiddlewareConfigFn = (middlewareConfig: MiddlewareConfig) => MiddlewareConfig

// This is the set of middleware Wasp supplies by default.
// NOTE: Remember to update the docs of these change.
const defaultGlobalMiddlewareConfig: MiddlewareConfig = new Map([
Expand Down
8 changes: 8 additions & 0 deletions waspc/examples/todo-typescript/main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ app TodoTypescript {
],
system: PostgreSQL
},
server: {
middlewareConfigFn: import { serverMiddlewareFn } from "@src/serverSetup.js"
}
}

// Use Prisma Schema Language (PSL) to define our entities: https://www.prisma.io/docs/concepts/components/prisma-schema
Expand Down Expand Up @@ -121,3 +124,8 @@ api fooBar {
fn: import { fooBar } from "@src/api.js",
httpRoute: (GET, "/foo/bar")
}

apiNamespace fooBarNamespace {
middlewareConfigFn: import { fooBarNamespace } from "@src/serverSetup.js",
path: "/foo/bar"
}
72 changes: 72 additions & 0 deletions waspc/examples/todo-typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions waspc/examples/todo-typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "prototype",
"dependencies": {
"wasp": "file:.wasp/out/sdk/wasp",
"react": "^18.2.0"
"cors": "^2.8.5",
"react": "^18.2.0",
"wasp": "file:.wasp/out/sdk/wasp"
},
"devDependencies": {
"typescript": "^5.1.0",
"vite": "^4.3.9",
"@types/cors": "^2.8.5",
"@types/express": "^4.17.13",
"@types/react": "^18.0.37",
"prisma": "4.16.2"
"prisma": "4.16.2",
"typescript": "^5.1.0",
"vite": "^4.3.9"
}
}
23 changes: 23 additions & 0 deletions waspc/examples/todo-typescript/src/serverSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express from 'express'
import cors from 'cors'
import type { MiddlewareConfigFn } from 'wasp/server/middleware'
import config from 'wasp/server/config'

export const serverMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => {
// Example of adding an extra domains to CORS.
middlewareConfig.set('cors', cors({
origin: [config.frontendUrl, 'https://example1.com', 'https://example2.com']
}))
return middlewareConfig
}

export const fooBarNamespace: MiddlewareConfigFn = (middlewareConfig) => {
const customMiddleware: express.RequestHandler = (_req, _res, next) => {
console.log('fooBarNamespaceMiddlewareFn: custom middleware')
next()
}

middlewareConfig.set('custom.middleware', customMiddleware)

return middlewareConfig
}
8 changes: 8 additions & 0 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ genSdkReal spec =
<++> genExternalCodeDir (AS.externalCodeFiles spec)
<++> genEntitiesAndServerTypesDirs spec
<++> genApis spec
<++> genMiddleware spec
where
genFileCopy = return . C.mkTmplFd

Expand Down Expand Up @@ -286,3 +287,10 @@ genServerUtils :: AppSpec -> Generator FileDraft
genServerUtils spec = return $ C.mkTmplFdWithData [relfile|server/utils.ts|] tmplData
where
tmplData = object ["isAuthEnabled" .= (isAuthEnabled spec :: Bool)]

genMiddleware :: AppSpec -> Generator [FileDraft]
genMiddleware _spec =
sequence
[ return $ C.mkTmplFd [relfile|server/middleware/index.ts|],
return $ C.mkTmplFd [relfile|server/middleware/globalMiddleware.ts|]
]

0 comments on commit 54a45eb

Please sign in to comment.