From 197c906df183bd1cade2fc0ea73a7b3e577a2cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C5=A0o=C5=A1i=C4=87?= Date: Tue, 30 Jan 2024 21:03:26 +0100 Subject: [PATCH] [New SDK]: { HttpError, AuthError, DbSeedFn, MiddlewareConfigFn } from 'wasp/server'. (#1699) --- waspc/data/Generator/templates/sdk/auth/utils.ts | 4 +--- .../Generator/templates/sdk/auth/validation.ts | 2 +- waspc/data/Generator/templates/sdk/dbSeed/types.ts | 3 --- waspc/data/Generator/templates/sdk/package.json | 14 ++++---------- .../templates/sdk/{core => server}/AuthError.ts | 6 ++---- .../templates/sdk/{core => server}/HttpError.ts | 6 ++---- waspc/data/Generator/templates/sdk/server/index.ts | 11 +++++++++++ .../sdk/server/middleware/globalMiddleware.ts | 6 ++++-- waspc/data/Generator/templates/server/src/app.js | 2 +- .../auth/providers/email/requestPasswordReset.ts | 2 +- .../src/auth/providers/email/resetPassword.ts | 2 +- .../server/src/auth/providers/email/signup.ts | 2 +- .../server/src/auth/providers/email/verifyEmail.ts | 2 +- .../Generator/templates/server/src/auth/utils.ts | 4 +--- .../data/Generator/templates/server/src/dbSeed.ts | 3 +-- waspc/examples/todo-typescript/src/db/seeds.ts | 2 +- .../todo-typescript/src/setup/serverSetup.ts | 2 +- waspc/examples/todo-typescript/src/task/actions.ts | 2 +- waspc/examples/todo-typescript/src/task/queries.ts | 3 +-- waspc/src/Wasp/Generator/SdkGenerator.hs | 5 ++--- 20 files changed, 38 insertions(+), 45 deletions(-) delete mode 100644 waspc/data/Generator/templates/sdk/dbSeed/types.ts rename waspc/data/Generator/templates/sdk/{core => server}/AuthError.ts (64%) rename waspc/data/Generator/templates/sdk/{core => server}/HttpError.ts (90%) diff --git a/waspc/data/Generator/templates/sdk/auth/utils.ts b/waspc/data/Generator/templates/sdk/auth/utils.ts index fd3c87feeb..2f4524edca 100644 --- a/waspc/data/Generator/templates/sdk/auth/utils.ts +++ b/waspc/data/Generator/templates/sdk/auth/utils.ts @@ -1,9 +1,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' +import { prisma, HttpError, AuthError } from 'wasp/server' import { sleep } from 'wasp/server/utils' import { type {= userEntityUpper =}, diff --git a/waspc/data/Generator/templates/sdk/auth/validation.ts b/waspc/data/Generator/templates/sdk/auth/validation.ts index d7cda4e29f..637f4203fd 100644 --- a/waspc/data/Generator/templates/sdk/auth/validation.ts +++ b/waspc/data/Generator/templates/sdk/auth/validation.ts @@ -1,4 +1,4 @@ -import HttpError from 'wasp/core/HttpError'; +import { HttpError } from 'wasp/server'; export const PASSWORD_FIELD = 'password'; const USERNAME_FIELD = 'username'; diff --git a/waspc/data/Generator/templates/sdk/dbSeed/types.ts b/waspc/data/Generator/templates/sdk/dbSeed/types.ts deleted file mode 100644 index 7a72bca7ef..0000000000 --- a/waspc/data/Generator/templates/sdk/dbSeed/types.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { PrismaClient } from '@prisma/client' - -export type DbSeedFn = (prisma: PrismaClient) => Promise diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index e785457aba..92c05b42fc 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -12,10 +12,6 @@ {=! todo(filip): Check all exports when done with SDK generation =} {=! Some of the statements in the comments might become incorrect. =} {=! "our code" means: "web-app", "server" or "SDK", or "some combination of the three". =} - {=! Used by users, documented. =} - "./core/HttpError": "./dist/core/HttpError.js", - {=! Used by users, documented. =} - "./core/AuthError": "./dist/core/AuthError.js", {=! Used by our code, uncodumented (but accessible) for users. =} "./core/config": "./dist/core/config.js", {=! Used by our code, uncodumented (but accessible) for users. =} @@ -70,8 +66,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/middleware": "./dist/server/middleware/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. =} @@ -81,8 +75,6 @@ {=! Used by our code, uncodumented (but accessible) for users. =} "./server/auth/email": "./dist/server/auth/email/index.js", {=! Used by users, documented. =} - "./dbSeed/types": "./dist/dbSeed/types.js", - {=! Used by users, documented. =} "./test": "./dist/test/index.js", {=! Used by our code, uncodumented (but accessible) for users. =} "./test/*": "./dist/test/*.js", @@ -108,11 +100,13 @@ {=! Used by our code, uncodumented (but accessible) for users. =} "./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx", - {=! Still needed, reconsider during refactoring =} + {=! Still needed, reconsider during refactoring. =} "./server/types": "./dist/server/types/index.js", + {=! Still used by the server code, reconsider during refactoring. =} + "./server/middleware": "./dist/server/middleware/index.js", {=! ================= NEW API HERE =================== =} - {=! Public: { config, prisma, type ServerSetupFn } =} + {=! Public: { config, prisma, type ServerSetupFn, HttpError, AuthError, type DbSeedFn, type MiddlewareConfigFn } =} {=! Private: [] =} "./server": "./dist/server/index.js", {=! Public: { type MyApiRoute1, type MyApiRoute2, ... } =} diff --git a/waspc/data/Generator/templates/sdk/core/AuthError.ts b/waspc/data/Generator/templates/sdk/server/AuthError.ts similarity index 64% rename from waspc/data/Generator/templates/sdk/core/AuthError.ts rename to waspc/data/Generator/templates/sdk/server/AuthError.ts index 3ea4b536ea..4b44d8c30b 100644 --- a/waspc/data/Generator/templates/sdk/core/AuthError.ts +++ b/waspc/data/Generator/templates/sdk/server/AuthError.ts @@ -1,7 +1,7 @@ -class AuthError extends Error { +export class AuthError extends Error { public data: unknown - constructor (message: string, data?: unknown, ...params: unknown[]) { + constructor(message: string, data?: unknown, ...params: unknown[]) { super(message, ...params) if (Error.captureStackTrace) { @@ -15,5 +15,3 @@ class AuthError extends Error { } } } - -export default AuthError diff --git a/waspc/data/Generator/templates/sdk/core/HttpError.ts b/waspc/data/Generator/templates/sdk/server/HttpError.ts similarity index 90% rename from waspc/data/Generator/templates/sdk/core/HttpError.ts rename to waspc/data/Generator/templates/sdk/server/HttpError.ts index 4f229882e4..3e8d3f5a0d 100644 --- a/waspc/data/Generator/templates/sdk/core/HttpError.ts +++ b/waspc/data/Generator/templates/sdk/server/HttpError.ts @@ -1,7 +1,7 @@ -class HttpError extends Error { +export class HttpError extends Error { public statusCode: number public data: unknown - + constructor (statusCode: number, message?: string, data?: Record, ...params: unknown[]) { super(message, ...params) @@ -21,5 +21,3 @@ class HttpError extends Error { } } } - -export default HttpError diff --git a/waspc/data/Generator/templates/sdk/server/index.ts b/waspc/data/Generator/templates/sdk/server/index.ts index 8d527a331b..89b5889298 100644 --- a/waspc/data/Generator/templates/sdk/server/index.ts +++ b/waspc/data/Generator/templates/sdk/server/index.ts @@ -1,7 +1,18 @@ +import type { PrismaClient } from '@prisma/client' + // 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' +// PUBLIC API +export { HttpError } from './HttpError.js' +// PUBLIC API +export { AuthError } from './AuthError.js' +// PUBLIC API +export { MiddlewareConfigFn } from './middleware/index.js' + +// PUBLIC API +export type DbSeedFn = (prisma: PrismaClient) => Promise diff --git a/waspc/data/Generator/templates/sdk/server/middleware/globalMiddleware.ts b/waspc/data/Generator/templates/sdk/server/middleware/globalMiddleware.ts index 28c2631149..8bab5e4709 100644 --- a/waspc/data/Generator/templates/sdk/server/middleware/globalMiddleware.ts +++ b/waspc/data/Generator/templates/sdk/server/middleware/globalMiddleware.ts @@ -1,6 +1,8 @@ import { type RequestHandler } from 'express' -export type MiddlewareConfig = Map - +// PUBLIC API export type MiddlewareConfigFn = (middlewareConfig: MiddlewareConfig) => MiddlewareConfig +// PRIVATE API +export type MiddlewareConfig = Map + diff --git a/waspc/data/Generator/templates/server/src/app.js b/waspc/data/Generator/templates/server/src/app.js index 9db0e1b75d..025a3c4c14 100644 --- a/waspc/data/Generator/templates/server/src/app.js +++ b/waspc/data/Generator/templates/server/src/app.js @@ -1,6 +1,6 @@ import express from 'express' -import HttpError from 'wasp/core/HttpError' +import { HttpError } from 'wasp/server' import indexRouter from './routes/index.js' // TODO: Consider extracting most of this logic into createApp(routes, path) function so that diff --git a/waspc/data/Generator/templates/server/src/auth/providers/email/requestPasswordReset.ts b/waspc/data/Generator/templates/server/src/auth/providers/email/requestPasswordReset.ts index 92d3dea0fb..be0c685335 100644 --- a/waspc/data/Generator/templates/server/src/auth/providers/email/requestPasswordReset.ts +++ b/waspc/data/Generator/templates/server/src/auth/providers/email/requestPasswordReset.ts @@ -13,7 +13,7 @@ import { import { ensureValidEmail } from 'wasp/auth/validation'; import type { EmailFromField } from 'wasp/email/core/types'; import { GetPasswordResetEmailContentFn } from 'wasp/server/auth/email'; -import HttpError from 'wasp/core/HttpError'; +import { HttpError } from 'wasp/server'; export function getRequestPasswordResetRoute({ fromField, diff --git a/waspc/data/Generator/templates/server/src/auth/providers/email/resetPassword.ts b/waspc/data/Generator/templates/server/src/auth/providers/email/resetPassword.ts index ba00a4ae9f..8f5681a994 100644 --- a/waspc/data/Generator/templates/server/src/auth/providers/email/resetPassword.ts +++ b/waspc/data/Generator/templates/server/src/auth/providers/email/resetPassword.ts @@ -8,7 +8,7 @@ import { } from 'wasp/auth/utils'; import { ensureTokenIsPresent, ensurePasswordIsPresent, ensureValidPassword } from 'wasp/auth/validation'; import { tokenVerificationErrors } from "./types.js"; -import HttpError from 'wasp/core/HttpError'; +import { HttpError } from 'wasp/server'; export async function resetPassword( req: Request<{ token: string; password: string; }>, diff --git a/waspc/data/Generator/templates/server/src/auth/providers/email/signup.ts b/waspc/data/Generator/templates/server/src/auth/providers/email/signup.ts index 3039f44746..8aab2e3019 100644 --- a/waspc/data/Generator/templates/server/src/auth/providers/email/signup.ts +++ b/waspc/data/Generator/templates/server/src/auth/providers/email/signup.ts @@ -18,7 +18,7 @@ import { import { ensureValidEmail, ensureValidPassword, ensurePasswordIsPresent } from 'wasp/auth/validation'; import { GetVerificationEmailContentFn } from 'wasp/server/auth/email'; import { validateAndGetUserFields } from 'wasp/auth/utils' -import HttpError from 'wasp/core/HttpError'; +import { HttpError } from 'wasp/server'; import { type UserSignupFields } from 'wasp/auth/providers/types'; export function getSignupRoute({ diff --git a/waspc/data/Generator/templates/server/src/auth/providers/email/verifyEmail.ts b/waspc/data/Generator/templates/server/src/auth/providers/email/verifyEmail.ts index c54963e411..a337b6e616 100644 --- a/waspc/data/Generator/templates/server/src/auth/providers/email/verifyEmail.ts +++ b/waspc/data/Generator/templates/server/src/auth/providers/email/verifyEmail.ts @@ -7,7 +7,7 @@ import { deserializeAndSanitizeProviderData, } from 'wasp/auth/utils'; import { tokenVerificationErrors } from './types.js'; -import HttpError from 'wasp/core/HttpError'; +import { HttpError } from 'wasp/server'; export async function verifyEmail( diff --git a/waspc/data/Generator/templates/server/src/auth/utils.ts b/waspc/data/Generator/templates/server/src/auth/utils.ts index 887997b5cb..2efc01d671 100644 --- a/waspc/data/Generator/templates/server/src/auth/utils.ts +++ b/waspc/data/Generator/templates/server/src/auth/utils.ts @@ -1,9 +1,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' +import { prisma, HttpError, AuthError } from 'wasp/server' import { sleep } from 'wasp/server/utils' import { type {= userEntityUpper =}, diff --git a/waspc/data/Generator/templates/server/src/dbSeed.ts b/waspc/data/Generator/templates/server/src/dbSeed.ts index 51378ed8f9..d7e1d77f88 100644 --- a/waspc/data/Generator/templates/server/src/dbSeed.ts +++ b/waspc/data/Generator/templates/server/src/dbSeed.ts @@ -6,8 +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 { prisma } from 'wasp/server' -import type { DbSeedFn } from 'wasp/dbSeed/types' +import { prisma, DbSeedFn } from 'wasp/server' {=# dbSeeds =} {=& importStatement =} diff --git a/waspc/examples/todo-typescript/src/db/seeds.ts b/waspc/examples/todo-typescript/src/db/seeds.ts index 854a53c313..fc7c0a362e 100644 --- a/waspc/examples/todo-typescript/src/db/seeds.ts +++ b/waspc/examples/todo-typescript/src/db/seeds.ts @@ -1,4 +1,4 @@ -import { DbSeedFn } from "wasp/dbSeed/types"; +import { DbSeedFn } from 'wasp/server'; export const seedMyDb: DbSeedFn = async (prisma) => { const user = await prisma.user.findFirst({}); diff --git a/waspc/examples/todo-typescript/src/setup/serverSetup.ts b/waspc/examples/todo-typescript/src/setup/serverSetup.ts index 1b49dcf081..ae49219b08 100644 --- a/waspc/examples/todo-typescript/src/setup/serverSetup.ts +++ b/waspc/examples/todo-typescript/src/setup/serverSetup.ts @@ -1,6 +1,6 @@ import express, { Application } from 'express' import cors from 'cors' -import type { MiddlewareConfigFn } from 'wasp/server/middleware' +import { type MiddlewareConfigFn } from 'wasp/server' import { config, ServerSetupFn, prisma } from 'wasp/server' export const serverMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => { diff --git a/waspc/examples/todo-typescript/src/task/actions.ts b/waspc/examples/todo-typescript/src/task/actions.ts index 5428ee34df..98a56e925a 100644 --- a/waspc/examples/todo-typescript/src/task/actions.ts +++ b/waspc/examples/todo-typescript/src/task/actions.ts @@ -1,4 +1,4 @@ -import HttpError from 'wasp/core/HttpError' +import { HttpError } from 'wasp/server' import type { CreateTask, UpdateTask, diff --git a/waspc/examples/todo-typescript/src/task/queries.ts b/waspc/examples/todo-typescript/src/task/queries.ts index fbc2e858bd..1b635d17c7 100644 --- a/waspc/examples/todo-typescript/src/task/queries.ts +++ b/waspc/examples/todo-typescript/src/task/queries.ts @@ -1,5 +1,4 @@ -import HttpError from 'wasp/core/HttpError' -import AuthError from 'wasp/core/AuthError' +import { HttpError, AuthError } from 'wasp/server' import type { GetTasks } from 'wasp/server/queries/types' import type { Task } from 'wasp/entities' import { ensureValidEmail, createProviderId } from 'wasp/server/auth' diff --git a/waspc/src/Wasp/Generator/SdkGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator.hs index ddc7b97495..043875c0b9 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator.hs @@ -82,8 +82,6 @@ genSdkReal spec = genFileCopy [relfile|core/auth.ts|], genFileCopy [relfile|core/storage.ts|], genFileCopy [relfile|core/stitches.config.ts|], - genFileCopy [relfile|core/AuthError.ts|], - genFileCopy [relfile|core/HttpError.ts|], -- Not migrated to TS yet genFileCopy [relfile|operations/resources.js|], genFileCopy [relfile|operations/index.ts|], @@ -91,8 +89,9 @@ genSdkReal spec = genFileCopy [relfile|operations/updateHandlersMap.js|], genFileCopy [relfile|server/index.ts|], genFileCopy [relfile|server/dbClient.ts|], + genFileCopy [relfile|server/HttpError.ts|], + genFileCopy [relfile|server/AuthError.ts|], genFileCopy [relfile|types/index.ts|], - genFileCopy [relfile|dbSeed/types.ts|], genFileCopy [relfile|test/vitest/helpers.tsx|], genFileCopy [relfile|test/index.ts|], genFileCopy [relfile|jobs/pgBoss/types.ts|],