javascripttypescriptdomain-driven-designddd-repositories

How to call Interface class in the Route when creating Rest Api


so i have a interface like this

export interface AuthenticationService {
    signUp(email: string, password: string): Promise<void>;
    // login(email: string, password: string): Promise<string>;
  }

so i implement the service in like this,

import {AuthenticationService} from "../application/authentication_repository";

import * as admin from "firebase-admin";


export class AuthenticationServiceImpl implements AuthenticationService {
  async signUp(email: string, password: string): Promise<void> {
    const auth = admin.auth();
    const existingUser = await auth.createUser({
      email: email,
      password: password,
      emailVerified: false,
      disabled: false,
    });
    if (existingUser) {
      throw new Error("User already exists");
    }
  }

  // async login(email: string, password: string): Promise<string> {
  // }
}

my i want to use the interface in the route but i keep getting error like this error

how can i use the interface in the route?


Solution

  • i solve this by using inversify

    app.post("/signup", async (req, res) => {
        try {
          const {email, password}: LoginRequest = req.body;
          if (password.toLowerCase() || !email) {
            return res.status(400).send({message: "Missing fields"});
          }
          // eslint-disable-next-line max-len
          const authService = container.get<AuthenticationService>("AuthenticationService");
          const uid= await authService.signUp(email, password);
          return res.status(200).send({uid});
        } catch (error) {
          return res.status(400).send(error);
        }
      }