Handling the API request with an API Controller

Inside of the users/useCases/createUser folder, we find the CreateUserController.

The CreateUserController has a single dependency injected into it. The CreateUserUseCase. It's the Application Service/Use Case that does all the action.

The responsibilities of a controller are to:

  • Invoke the CreateUserUseCase.
  • Respond to the API call with the appropriate HTTP response code based on the result of the executed use case.

See the CreateUserController below.

// CreateUsersController - Handling the request data from 
// the internet and passing it to the dependency injected 
// use case class for execution.

// users/useCases/createUser/CreateUserController.ts

import { CreateUserUseCase } from "./CreateUserUseCase"
import { CreateUserDTO } from "./CreateUserDTO"
import { CreateUserErrors } from "./CreateUserErrors"
import { BaseController } from "../../../../shared/infra/http/models/BaseController"
import { TextUtils } from "../../../../shared/utils/TextUtils"
import { DecodedExpressRequest } from "../../infra/http/models/decodedRequest"
import * as express from "express"

export class CreateUserController extends BaseController {
  private useCase: CreateUserUseCase

  constructor(useCase: CreateUserUseCase) {
    super()
    this.useCase = useCase
  }

  async executeImpl(
    req: DecodedExpressRequest,
    res: express.Response
  ): Promise<any> {
    let dto: CreateUserDTO = req.body as CreateUserDTO

    dto = {
      username: TextUtils.sanitize(dto.username),
      email: TextUtils.sanitize(dto.email),
      password: dto.password,
    }

    try {
      /**
       * Execute the CreateUserUseCase use case.
       */

      const result = await this.useCase.execute(dto)

      if (result.isLeft()) {
        const error = result.value

        /**
         * This is an example of how one can standardize a
         * RESTful API's error messages.
         *
         * We utilize the Use Case Error types.
         */

        switch (error.constructor) {
          case CreateUserErrors.UsernameTakenError:
            return this.conflict(error.errorValue().message)
          case CreateUserErrors.EmailAlreadyExistsError:
            return this.conflict(error.errorValue().message)
          default
            return this.fail(res, error.errorValue().message)
        }
      } else {
        return this.ok(res)
      }
    } catch (err) {
      return this.fail(res, err)
    }
  }
}

Like this?: If you like the way this Express.js controller uses declarative response methods, check out "Clean & Consistent Express.js Controllers | Enterprise Node.js + TypeScript".

Howdy 👋

This is an online wiki and book about the basics of software design and architecture with TypeScript by Khalil Stemmler, Developer Advocate @ Apollo GraphQL .

This book’s mission is to teach developers the essential skills and practices to write testable, flexible, and maintainable code.

You can read more about the learning journey in the "Software Design and Architecture Roadmap 🖼️".

Already bought it?

If you’ve already purchased the book, click here to re-send your link. You can read the online wiki or download a copy of the book in PDF, EPUB, and Kindle versions.

Want access?

You can read the intro to the book for free and visit solidbook.io to buy the book/wiki (it's currently on pre-sale for 33% off)! For recent updates, click here. To get an idea of my writing, read some of my best free content here and here.

Need help?

Something not working? Have a question? You can reach me on Twitter or khalil@khalilstemmler.com.