[New SDK]: { HttpError, AuthError, DbSeedFn, MiddlewareConfigFn } from 'wasp/server'. (#1699)

This commit is contained in:
Martin Šošić 2024-01-30 21:03:26 +01:00 committed by Filip Sodić
parent 72f8437108
commit 129d06480c
20 changed files with 38 additions and 45 deletions

View File

@ -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 =},

View File

@ -1,4 +1,4 @@
import HttpError from 'wasp/core/HttpError';
import { HttpError } from 'wasp/server';
export const PASSWORD_FIELD = 'password';
const USERNAME_FIELD = 'username';

View File

@ -1,3 +0,0 @@
import type { PrismaClient } from '@prisma/client'
export type DbSeedFn = (prisma: PrismaClient) => Promise<void>

View File

@ -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, ... } =}

View File

@ -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

View File

@ -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<string, unknown>, ...params: unknown[]) {
super(message, ...params)
@ -21,5 +21,3 @@ class HttpError extends Error {
}
}
}
export default HttpError

View File

@ -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<void>

View File

@ -1,6 +1,8 @@
import { type RequestHandler } from 'express'
export type MiddlewareConfig = Map<string, RequestHandler>
// PUBLIC API
export type MiddlewareConfigFn = (middlewareConfig: MiddlewareConfig) => MiddlewareConfig
// PRIVATE API
export type MiddlewareConfig = Map<string, RequestHandler>

View File

@ -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

View File

@ -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,

View File

@ -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; }>,

View File

@ -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({

View File

@ -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(

View File

@ -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 =},

View File

@ -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 =}

View File

@ -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({});

View File

@ -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) => {

View File

@ -1,4 +1,4 @@
import HttpError from 'wasp/core/HttpError'
import { HttpError } from 'wasp/server'
import type {
CreateTask,
UpdateTask,

View File

@ -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'

View File

@ -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|],