4.2. Local Protocol container

In this chapter, you’ll learn about the Local Protocol container and how to use it.

What is the Local Protocol container?#

The Local Protocol container holds all resources registered in the Local Protocol application, such as services.

In your customizations, you use the Local Protocol container to resolve these resources and use their functionalities.

For example, in a custom API route you can resolve any service registered in the Local Protocol application using the scope.resolve method of the LocalProtoRequest parameter:

Code
1import type { LocalProtoRequest, LocalProtoResponse } from "@localprotojs/framework/http"2import { IProductModuleService } from "@localprotojs/framework/types"3import { Modules } from "@localprotojs/framework/utils"4
5export const GET = async (6  req: LocalProtoRequest, 7  res: LocalProtoResponse8) => {9  const productModuleService: IProductModuleService = req.scope.resolve(10    Modules.PRODUCT11  )12
13  const [, count] = await productModuleService14    .listAndCountProducts()15
16  res.json({17    count,18  })19}
Was this chapter helpful?