Skip to content

Commit

Permalink
add composite root for DI
Browse files Browse the repository at this point in the history
  • Loading branch information
AsgrimS committed Sep 5, 2024
1 parent 483788b commit 5450d0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
15 changes: 15 additions & 0 deletions src/lib/server/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { CreateUserDTO, UserDTO } from "$lib/dtos/user"
import { appContainer } from "./dependencyInjection/inversify.config"
import { TYPES } from "./dependencyInjection/types"
import type { ICommand } from "./shared/command"
import type { IQuery } from "./shared/query"

// This file acts as composition root for the application.

// Commands
export const createUserCommand = appContainer.get<ICommand<CreateUserDTO>>(TYPES.CreateUserCommand)

// Queries
export const findUserByUsernameQuery = appContainer.get<IQuery<string, UserDTO>>(
TYPES.FindUserByUsernameQuery
)
8 changes: 1 addition & 7 deletions src/routes/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import type { UserDTO } from "$lib/dtos/user"
import { loginSchema } from "$lib/forms"
import { findUserByUsernameQuery } from "$lib/server/app"
import { lucia } from "$lib/server/auth"
import { appContainer } from "$lib/server/dependencyInjection/inversify.config"
import { TYPES } from "$lib/server/dependencyInjection/types"
import { getLimiter } from "$lib/server/limiter"
import { AuthService } from "$lib/server/services/authService"
import type { IQuery } from "$lib/server/shared/query"
import type { Actions, PageServerLoad } from "./$types"
import { fail, redirect, error } from "@sveltejs/kit"
import { setError, superValidate } from "sveltekit-superforms"
import { typebox } from "sveltekit-superforms/adapters"

const limiter = getLimiter("login")
const authService = new AuthService()
const findUserByUsernameQuery = appContainer.get<IQuery<string, UserDTO>>(
TYPES.FindUserByUsernameQuery
)

export const load: PageServerLoad = async (event) => {
if (event.locals.user) redirect(302, "/")
Expand Down
7 changes: 1 addition & 6 deletions src/routes/signup/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { CreateUserDTO } from "$lib/dtos/user"
import { signUpSchema } from "$lib/forms"
import { createUserCommand } from "$lib/server/app"
import { lucia } from "$lib/server/auth"
import { appContainer } from "$lib/server/dependencyInjection/inversify.config"
import { TYPES } from "$lib/server/dependencyInjection/types"
import { getLimiter } from "$lib/server/limiter"
import { AuthService } from "$lib/server/services/authService"
import type { ICommand } from "$lib/server/shared/command"
import type { Actions, PageServerLoad } from "./$types"
import { error, fail, redirect } from "@sveltejs/kit"
import { superValidate, setError } from "sveltekit-superforms"
Expand All @@ -14,8 +11,6 @@ import { typebox } from "sveltekit-superforms/adapters"
const limiter = getLimiter("signup", [5, "m"])
const authService = new AuthService()

const createUserCommand = appContainer.get<ICommand<CreateUserDTO>>(TYPES.CreateUserCommand)

export const load: PageServerLoad = async (event) => {
const form = await superValidate(typebox(signUpSchema))
await limiter.cookieLimiter?.preflight(event)
Expand Down

0 comments on commit 5450d0f

Please sign in to comment.