4.5. Commerce Modules

In this chapter, you'll learn about Local Protocol's commerce modules.

What is a Commerce Module?#

Local Protocol provides all its commerce features as separate commerce modules, such as the Product or Order modules. Local Protocol uses these modules in its API routes to expose their commerce features.

Local Protocol's commerce modules and your custom modules are interchangeable in the Local Protocol application, making Local Protocol’s architecture more flexible.


Resolve Commerce Module Services#

Similarly to your custom module, a commerce module's main service is registered in the Local Protocol container. So, you can resolve it in your resources, such as API routes, to use its functionality.

For example, you saw this code snippet in the Local Protocol container chapter:

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}

When you resolve the Modules.PRODUCT (or productModuleService) registration name, you're actually resolving the main service of the Product Module.

TipTo resolve the main service of any commerce module, use the registration name defined in the Modules enum imported from @localprotojs/framework/utils .
Was this chapter helpful?