Skip to content

Commit

Permalink
[New SDK]: 'wasp/server'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinsos committed Jan 29, 2024
1 parent 14dde97 commit 86a4035
Show file tree
Hide file tree
Showing 34 changed files with 78 additions and 69 deletions.
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/auth/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken'
import util from 'util'

import config from 'wasp/server/config'
import { config } from 'wasp/server'

const jwtSign = util.promisify(jwt.sign)
const jwtVerify = util.promisify(jwt.verify)
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/auth/lucia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{={= =}=}}
import { Lucia } from "lucia";
import { PrismaAdapter } from "@lucia-auth/adapter-prisma";
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
import { type {= userEntityUpper =} } from "wasp/entities"

const prismaAdapter = new PrismaAdapter(
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
deserializeAndSanitizeProviderData,
} from "./utils.js";

import prisma from 'wasp/server/dbClient';
import { prisma } from 'wasp/server';

// Creates a new session for the `authId` in the database
export async function createSession(authId: string): Promise<Session> {
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hashPassword } from './password.js'
import { verify } from './jwt.js'
import AuthError from 'wasp/core/AuthError'
import HttpError from 'wasp/core/HttpError'
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
import { sleep } from 'wasp/server/utils'
import {
type {= userEntityUpper =},
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/dbSeed/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { PrismaClient } from '@prisma/client'

export type DbSeedFn = (prismaClient: PrismaClient) => Promise<void>
export type DbSeedFn = (prisma: PrismaClient) => Promise<void>
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
import type { JSONValue, JSONObject } from 'wasp/server/_types/serialization'
import { type JobFn } from '{= jobExecutorTypesImportPath =}'

Expand Down
12 changes: 6 additions & 6 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@
"./universal/types": "./dist/universal/types.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./universal/validators": "./dist/universal/validators.js",
{=! Used by users and by our code, documented =}
"./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",
{=! Used by users and by our code, documented. =}
"./server/middleware": "./dist/server/middleware/index.js",
{=! Parts are used by users, documented. Parts are probably used by our code, undocumented (but accessible). =}
Expand Down Expand Up @@ -144,7 +138,13 @@
{=! Used by our code, uncodumented (but accessible) for users. =}
"./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx",

{=! Still needed, reconsider during refactoring =}
"./server/types": "./dist/server/types/index.js",

{=! ================= NEW API HERE =================== =}
{=! Public: { config, prisma, type ServerSetupFn } =}
{=! Private: [] =}
"./server": "./dist/server/index.js",
{=! Public: { type MyEntity1, type MyEntity2, ... } =}
{=! Private: [] =}
"./server/api": "./dist/server/api/index.js",
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/server/_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { type Expand } from 'wasp/universal/types';
import { type Request, type Response } from 'express'
import { type ParamsDictionary as ExpressParams, type Query as ExpressQuery } from 'express-serve-static-core'
import prisma from "wasp/server/dbClient"
import { prisma } from 'wasp/server'
{=# isAuthEnabled =}
import {
type {= userEntityName =},
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/server/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
{=! TODO: This template is exactly the same at the moment as one for queries,
consider in the future if it is worth removing this duplication. =}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
deserializeAndSanitizeProviderData,
type EmailProviderData,
} from 'wasp/auth/utils';
import waspServerConfig from 'wasp/server/config';
import { config as waspServerConfig } from 'wasp/server';
import { type {= userEntityUpper =}, type {= authEntityUpper =} } from 'wasp/entities'

export async function createEmailVerificationLink(
Expand Down
1 change: 1 addition & 0 deletions waspc/data/Generator/templates/sdk/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const config: {
}

const resolvedConfig: Config = merge(config.all, config[env])
// PUBLIC API
export default resolvedConfig

function getDevelopmentConfig(): EnvConfig {
Expand Down
5 changes: 3 additions & 2 deletions waspc/data/Generator/templates/sdk/server/dbClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Prisma from '@prisma/client'


const createDbClient = () => {
const prismaClient = new Prisma.PrismaClient()
const prisma = new Prisma.PrismaClient()

return prismaClient
return prisma
}

const dbClient = createDbClient()

// PUBLIC API
export default dbClient
7 changes: 7 additions & 0 deletions waspc/data/Generator/templates/sdk/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// PUBLIC API
export { default as config } from './config.js'
// PUBLIC API
export { default as prisma } from './dbClient.js'
// PUBLIC API
export { type ServerSetupFn } from './types/index.js'

2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/server/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
{=! TODO: This template is exactly the same at the moment as one for actions,
consider in the future if it is worth removing this duplication. =}

Expand Down
5 changes: 2 additions & 3 deletions waspc/data/Generator/templates/sdk/server/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { type Application } from 'express'
import { Server } from 'http'

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

// PRIVATE API (server)
export type ServerSetupFnContext = {
app: Application,
server: Server,
}

export type { Application } from 'express'
export type { Server } from 'http'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Server } from 'socket.io'
import { EventsMap, DefaultEventsMap } from '@socket.io/component-emitter'

import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
{=# isAuthEnabled =}
import { type SanitizedUser } from 'wasp/server/_types/index.js'
{=/ isAuthEnabled =}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'

{=& jsFn.importStatement =}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import { Router } from "express"
import passport from "passport"

import prisma from 'wasp/server/dbClient'
import waspServerConfig from 'wasp/server/config'
import { prisma, config as waspServerConfig } from 'wasp/server'
import {
type ProviderName,
type ProviderId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import passport from "passport";

import waspServerConfig from 'wasp/server/config';
import { config as waspServerConfig } from 'wasp/server';

import type { InitData, ProviderConfig, RequestWithWasp, UserSignupFields } from "wasp/auth/providers/types";
import type { OAuthConfig, UserDefinedConfigFn } from "./types.js";
Expand Down
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/server/src/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hashPassword } from 'wasp/auth/password'
import { verify } from 'wasp/auth/jwt'
import AuthError from 'wasp/core/AuthError'
import HttpError from 'wasp/core/HttpError'
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
import { sleep } from 'wasp/server/utils'
import {
type {= userEntityUpper =},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import prisma from "wasp/server/dbClient";
import { prisma } from 'wasp/server';

import type { Prisma } from "@prisma/client";
import type {
Expand Down
8 changes: 4 additions & 4 deletions waspc/data/Generator/templates/server/src/dbSeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// TODO: Consider in the future moving it into a a separate project (maybe db/ ?), while still
// maintaining access to logic from the server/ .

import prismaClient from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
import type { DbSeedFn } from 'wasp/dbSeed/types'

{=# dbSeeds =}
Expand All @@ -26,13 +26,13 @@ async function main() {
} else {
console.error('Name of the seed to run not specified!')
}
await (seeds[nameOfSeedToRun] satisfies DbSeedFn)(prismaClient)
await (seeds[nameOfSeedToRun] satisfies DbSeedFn)(prisma)
}

main()
.then(async () => { await prismaClient.$disconnect() })
.then(async () => { await prisma.$disconnect() })
.catch(async (e) => {
console.error(e)
await prismaClient.$disconnect()
await prisma.$disconnect()
process.exit(1)
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PgBoss from 'pg-boss'
import config from 'wasp/server/config'
import { config } from 'wasp/server'

const boss = createPgBoss()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import logger from 'morgan'
import cors from 'cors'
import helmet from 'helmet'

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'

{=& jsFn.importStatement =}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{={= =}=}}
import express from 'express'
import prisma from 'wasp/server/dbClient'
import { prisma } from 'wasp/server'
import { handleRejection } from 'wasp/server/utils'
import { MiddlewareConfigFn, globalMiddlewareConfigForExpress } from '../../middleware/index.js'
{=# isAuthEnabled =}
Expand Down
5 changes: 3 additions & 2 deletions waspc/data/Generator/templates/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import http from 'http'

import app from './app.js'
import config from 'wasp/server/config'
import { config } from 'wasp/server'

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

{=# isPgBossJobExecutorUsed =}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import http from 'http'
import { Server, Socket } from 'socket.io'
import type { ServerType } from 'wasp/server/webSocket'

import config from 'wasp/server/config'
import prisma from 'wasp/server/dbClient'
import { config, prisma } from 'wasp/server'

{=# isAuthEnabled =}
import { getSessionAndUserFromSessionId } from 'wasp/auth/session'
Expand Down
8 changes: 3 additions & 5 deletions waspc/examples/todo-typescript/src/setup/serverSetup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import express from 'express'
import express, { Application } 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'
import prismaClient from 'wasp/server/dbClient'
import { config, ServerSetupFn, prisma } from 'wasp/server'

export const serverMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => {
// Example of adding an extra domains to CORS.
Expand All @@ -29,5 +27,5 @@ export const serverSetup: ServerSetupFn = async ({ app }: { app: Application}) =
res.send('I am a custom route')
})
console.log("I am a server setup function!");
console.log("Executed raw prisma client call: ", await prismaClient.$executeRaw``);
console.log("Executed raw prisma client call: ", await prisma.$executeRaw``);
}
18 changes: 10 additions & 8 deletions waspc/examples/todo-typescript/src/user/customEmailSending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ import {
} from 'wasp/server/auth/email/utils'

export async function send() {
const userEmail = '[email protected]'

const link = await createPasswordResetLink(
'[email protected]',
userEmail,
'/password-reset'
)
const secondLink = await createEmailVerificationLink(
'[email protected]',
userEmail,
'/email-verify'
)

// Send email verification email.
await sendEmailVerificationEmail('[email protected]', {
await sendEmailVerificationEmail(userEmail, {
from: {
name: 'Wasp',
email: '[email protected]',
email: userEmail,
},
to: '[email protected]',
to: userEmail,
subject: 'Email verification',
text: 'Click on the link to verify your email. ' + secondLink,
html: `<a href="${secondLink}">Click here to verify your email.</a>`,
})

// Send password reset email.
await sendPasswordResetEmail('[email protected]', {
await sendPasswordResetEmail(userEmail, {
from: {
name: 'Wasp',
email: '[email protected]',
email: userEmail,
},
to: '[email protected]',
to: userEmail,
subject: 'Password reset',
text: 'Click on the link to reset your password.' + link,
html: `<a href="${link}">Click here to reset your password.</a>`,
Expand Down
1 change: 1 addition & 0 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ genSdkReal spec =
genFileCopy [relfile|operations/index.ts|],
-- Not migrated to TS yet
genFileCopy [relfile|operations/updateHandlersMap.js|],
genFileCopy [relfile|server/index.ts|],
genFileCopy [relfile|server/dbClient.ts|],
genFileCopy [relfile|types/index.ts|],
genFileCopy [relfile|dbSeed/types.ts|],
Expand Down
Loading

0 comments on commit 86a4035

Please sign in to comment.