- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
3.4.1. Integrate Third-Party Brand System in a Service
1. Create Service#
Start by creating the file src/modules/brand/services/client.ts
with the following content:
1import { Logger, ConfigModule } from "@medusajs/framework/types"2import { BRAND_MODULE } from ".."3 4export type BrandClientOptions = {5 apiKey: string6}7 8type InjectedDependencies = {9 logger: Logger10 configModule: ConfigModule11}12 13export class BrandClient {14 private options_: BrandClientOptions15 private logger_: Logger16 17 constructor({ logger, configModule }: InjectedDependencies) {18 this.logger_ = logger19 20 const moduleDef = configModule.modules[BRAND_MODULE]21 if (typeof moduleDef !== "boolean") {22 this.options_ = moduleDef.options as BrandClientOptions23 }24 }25}
This creates a BrandClient
service. Using dependency injection, you resolve the logger
and configModule
from the Module's container.
logger
is useful to log messages, and configModule
has configurations exported in medusa-config.ts
.
You also define an options_
property in your service to store the module's options.
The configModule
's modules
property is an object whose keys are registered module names and values are the module's configuration.
If the module's configuration isn't a boolean, it has an options
property that holds the module's options. You use it to set the options_
property's value.
Integration Methods#
Next, add the following methods to simulate sending requests to the third-party system:
1// other imports...2import { InferTypeOf } from "@medusajs/framework/types"3import { Brand } from "../models/brand"4 5export class BrandClient {6 // ...7 8 // a dummy method to simulate sending a request,9 // in a realistic scenario, you'd use an SDK, fetch, or axios clients10 private async sendRequest(url: string, method: string, data?: any) {11 this.logger_.info(`Sending a ${12 method13 } request to ${url}. data: ${JSON.stringify(data, null, 2)}`)14 this.logger_.info(`Client Options: ${15 JSON.stringify(this.options_, null, 2)16 }`)17 }18 19 async createBrand(brand: InferTypeOf<typeof Brand>) {20 await this.sendRequest("/brands", "POST", brand)21 }22 23 async deleteBrand(id: string) {24 await this.sendRequest(`/brands/${id}`, "DELETE")25 }26 27 async retrieveBrands() {28 await this.sendRequest("/brands", "GET")29 30 return []31 }32}
The