chore: move ModuleRegistrationName to utils (#7911)
This commit is contained in:
1 parent
46f15b4909
commit
a7844efd09
339 files changed
+7203
-7620
No files matched your search
@@ -30,15 +30,9 @@ To disable the authentication guard on custom routes under the `/admin` or `/sto
|
||||
For example:
|
||||
|
||||
```ts title="src/api/store/customers/me/custom/route.ts" highlights={[["15"]]} apiTesting testApiUrl="http://localhost:9000/store/customers/me/custom" testApiMethod="GET"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
|
||||
export const GET = async (
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
res.json({
|
||||
message: "Hello",
|
||||
})
|
||||
@@ -62,15 +56,16 @@ import type {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
const customerModuleService: ICustomerModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const customer = await customerModuleService.retrieve(
|
||||
req.auth_context.actor_id
|
||||
@@ -95,7 +90,7 @@ import type {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
|
||||
export const GET = async (
|
||||
@@ -106,9 +101,7 @@ export const GET = async (
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const user = await userService.retrieve(
|
||||
req.auth_context.actor_id
|
||||
)
|
||||
const user = await userService.retrieve(req.auth_context.actor_id)
|
||||
|
||||
// ...
|
||||
}
|
||||
@@ -125,29 +118,30 @@ To protect custom API Routes that don’t start with `/store/customers/me` or `/
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["11", "authenticate", "Only authenticated admin users can access routes starting with `/custom/admin`"],
|
||||
["17", "authenticate", "Only authenticated customers can access routes starting with `/custom/customers`"]
|
||||
[
|
||||
"11",
|
||||
"authenticate",
|
||||
"Only authenticated admin users can access routes starting with `/custom/admin`",
|
||||
],
|
||||
[
|
||||
"17",
|
||||
"authenticate",
|
||||
"Only authenticated customers can access routes starting with `/custom/customers`",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/api/middlewares.ts" highlights={highlights}
|
||||
import {
|
||||
MiddlewaresConfig,
|
||||
authenticate,
|
||||
} from "@medusajs/medusa"
|
||||
import { MiddlewaresConfig, authenticate } from "@medusajs/medusa"
|
||||
|
||||
export const config: MiddlewaresConfig = {
|
||||
routes: [
|
||||
{
|
||||
matcher: "/custom/admin*",
|
||||
middlewares: [
|
||||
authenticate("user", ["session", "bearer", "api-key"]),
|
||||
],
|
||||
middlewares: [authenticate("user", ["session", "bearer", "api-key"])],
|
||||
},
|
||||
{
|
||||
matcher: "/custom/customer*",
|
||||
middlewares: [
|
||||
authenticate("customer", ["session", "bearer"]),
|
||||
],
|
||||
middlewares: [authenticate("customer", ["session", "bearer"])],
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -158,5 +152,5 @@ The `authenticate` middleware function accepts three parameters:
|
||||
1. The type of user authenticating. Use `user` for authenticating admin users, and `customer` for authenticating customers.
|
||||
2. An array of the types of authentication methods allowed. Both `user` and `customer` scopes support `session` and `bearer`. The `admin` scope also supports the `api-key` authentication method.
|
||||
3. An optional object of options having the following properties:
|
||||
1. `allowUnauthenticated`: (default: `false`) A boolean indicating whether authentication is required. For example, you may have an API route where you want to access the logged-in customer if available, but guest customers can still access it too. In that case, enable the `allowUnauthenticated` option.
|
||||
2. `allowUnregistered`: (default: `false`) A boolean indicating whether new users can be authenticated.
|
||||
1. `allowUnauthenticated`: (default: `false`) A boolean indicating whether authentication is required. For example, you may have an API route where you want to access the logged-in customer if available, but guest customers can still access it too. In that case, enable the `allowUnauthenticated` option.
|
||||
2. `allowUnregistered`: (default: `false`) A boolean indicating whether new users can be authenticated.
|
||||
@@ -19,17 +19,13 @@ To create a custom CLI script, create a TypeScript or JavaScript file under the
|
||||
For example, create the file `src/scripts/my-script.ts` with the following content:
|
||||
|
||||
```ts title="src/scripts/my-script.ts"
|
||||
import {
|
||||
ExecArgs,
|
||||
IProductModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ExecArgs, IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function myScript({
|
||||
container,
|
||||
}: ExecArgs) {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export default async function myScript({ container }: ExecArgs) {
|
||||
const productModuleService: IProductModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const [, count] = await productModuleService.listAndCount()
|
||||
|
||||
@@ -60,9 +56,7 @@ For example:
|
||||
```ts
|
||||
import { ExecArgs } from "@medusajs/types"
|
||||
|
||||
export default async function myScript({
|
||||
args,
|
||||
}: ExecArgs) {
|
||||
export default async function myScript({ args }: ExecArgs) {
|
||||
console.log(`The arguments you passed: ${args}`)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -22,9 +22,7 @@ For example, create the file `src/loaders/hello-world.ts` with the following con
|
||||
|
||||
```ts title="src/loaders/hello-world.ts"
|
||||
export default function () {
|
||||
console.log(
|
||||
"[HELLO LOADER] Just started the Medusa application!"
|
||||
)
|
||||
console.log("[HELLO LOADER] Just started the Medusa application!")
|
||||
}
|
||||
```
|
||||
|
||||
@@ -51,11 +49,12 @@ For example:
|
||||
```ts title="src/loaders/hello-world.ts" collapsibleLines="1-5" expandButtonLabel="Show Imports"
|
||||
import { MedusaContainer } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function (container: MedusaContainer) {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
const productModuleService: IProductModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const [, count] = await productModuleService.listAndCount()
|
||||
|
||||
@@ -78,13 +77,11 @@ import { MedusaContainer } from "@medusajs/medusa"
|
||||
import { ConfigModule } from "@medusajs/types"
|
||||
|
||||
export default async function (
|
||||
container: MedusaContainer,
|
||||
container: MedusaContainer,
|
||||
config: ConfigModule
|
||||
) {
|
||||
console.log(`You have ${
|
||||
Object.values(config.modules || {}).length
|
||||
} modules!`)
|
||||
console.log(`You have ${Object.values(config.modules || {}).length} modules!`)
|
||||
}
|
||||
```
|
||||
|
||||
This loader logs on application start-up the number of modules defined in your Medusa configurations.
|
||||
This loader logs on application start-up the number of modules defined in your Medusa configurations.
|
||||
@@ -26,7 +26,6 @@ In the file, add the type of the expected workflow input:
|
||||
import { UpdateProductDTO } from "@medusajs/types"
|
||||
|
||||
export type UpdateProductAndErpWorkflowInput = UpdateProductDTO
|
||||
|
||||
```
|
||||
|
||||
The expected input is the data to update in the product along with the product’s ID.
|
||||
@@ -41,18 +40,23 @@ Create the file `src/workflows/update-product-erp/steps/update-product.ts` with
|
||||
|
||||
export const updateProductHighlights = [
|
||||
["13", "resolve", "Resolve the `ProductService` from the Medusa container."],
|
||||
["16", "previousProductData", "Retrieve the `previousProductData` to pass it to the compensation function."],
|
||||
[
|
||||
"16",
|
||||
"previousProductData",
|
||||
"Retrieve the `previousProductData` to pass it to the compensation function.",
|
||||
],
|
||||
["19", "", "Update the product."],
|
||||
["39", "", "Revert the product’s data using the `previousProductData` passed from the step to the compensation function."]
|
||||
[
|
||||
"39",
|
||||
"",
|
||||
"Revert the product’s data using the `previousProductData` passed from the step to the compensation function.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/update-product-erp/steps/update-product.ts" highlights={updateProductHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { UpdateProductAndErpWorkflowInput } from ".."
|
||||
|
||||
const updateProduct = createStep(
|
||||
@@ -62,8 +66,7 @@ const updateProduct = createStep(
|
||||
context.container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
const { id } = input
|
||||
const previousProductData =
|
||||
await productModuleService.retrieve(id)
|
||||
const previousProductData = await productModuleService.retrieve(id)
|
||||
|
||||
const product = await productModuleService.update(id, input)
|
||||
|
||||
@@ -77,37 +80,28 @@ const updateProduct = createStep(
|
||||
const productModuleService: IProductModuleService =
|
||||
context.container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
const {
|
||||
id,
|
||||
type,
|
||||
options,
|
||||
variants,
|
||||
...previousData
|
||||
} = previousProductData
|
||||
const { id, type, options, variants, ...previousData } = previousProductData
|
||||
|
||||
await productModuleService.update(
|
||||
id,
|
||||
{
|
||||
...previousData,
|
||||
variants: variants.map((variant) => {
|
||||
const variantOptions = {}
|
||||
await productModuleService.update(id, {
|
||||
...previousData,
|
||||
variants: variants.map((variant) => {
|
||||
const variantOptions = {}
|
||||
|
||||
variant.options.forEach((option) => {
|
||||
variantOptions[option.option.title] = option.value
|
||||
})
|
||||
variant.options.forEach((option) => {
|
||||
variantOptions[option.option.title] = option.value
|
||||
})
|
||||
|
||||
return {
|
||||
...variant,
|
||||
options: variantOptions,
|
||||
}
|
||||
}),
|
||||
options: options.map((option) => ({
|
||||
...option,
|
||||
values: option.values.map((value) => value.value),
|
||||
})),
|
||||
type_id: type.id,
|
||||
}
|
||||
)
|
||||
return {
|
||||
...variant,
|
||||
options: variantOptions,
|
||||
}
|
||||
}),
|
||||
options: options.map((option) => ({
|
||||
...option,
|
||||
values: option.values.map((value) => value.value),
|
||||
})),
|
||||
type_id: type.id,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -139,17 +133,30 @@ The `ErpModuleService` used is assumed to be created in a module.
|
||||
Create the file `src/workflows/update-product-erp/steps/update-erp.ts` with the following content:
|
||||
|
||||
export const updateErpHighlights = [
|
||||
["12", "resolve", "Resolve the `erpModuleService` from the Medusa container."],
|
||||
["17", "previousErpData", "Retrieve the `previousErpData` to pass it to the compensation function."],
|
||||
["21", "updateProductErpData", "Update the product’s ERP data and return the data from the ERP system."],
|
||||
["37", "updateProductErpData", "Revert the product's data in the ERP system to its previous state using the `previousErpData`."]
|
||||
[
|
||||
"12",
|
||||
"resolve",
|
||||
"Resolve the `erpModuleService` from the Medusa container.",
|
||||
],
|
||||
[
|
||||
"17",
|
||||
"previousErpData",
|
||||
"Retrieve the `previousErpData` to pass it to the compensation function.",
|
||||
],
|
||||
[
|
||||
"21",
|
||||
"updateProductErpData",
|
||||
"Update the product’s ERP data and return the data from the ERP system.",
|
||||
],
|
||||
[
|
||||
"37",
|
||||
"updateProductErpData",
|
||||
"Revert the product's data in the ERP system to its previous state using the `previousErpData`.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/update-product-erp/steps/update-erp.ts" highlights={updateErpHighlights} collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { UpdateProductAndErpWorkflowInput } from ".."
|
||||
import ErpModuleService from "../../../modules/erp/service"
|
||||
|
||||
@@ -162,14 +169,12 @@ const updateErp = createStep(
|
||||
const { id, ...updatedData } = input
|
||||
|
||||
// get previous ERP data
|
||||
const previousErpData =
|
||||
await erpModuleService.retrieveProductErpDetails(id)
|
||||
const previousErpData = await erpModuleService.retrieveProductErpDetails(id)
|
||||
|
||||
const updatedErpData =
|
||||
await erpModuleService.updateProductErpData(
|
||||
id,
|
||||
updatedData
|
||||
)
|
||||
const updatedErpData = await erpModuleService.updateProductErpData(
|
||||
id,
|
||||
updatedData
|
||||
)
|
||||
|
||||
return new StepResponse(updatedErpData, {
|
||||
// pass to compensation function
|
||||
@@ -179,13 +184,9 @@ const updateErp = createStep(
|
||||
},
|
||||
// compensation function
|
||||
async ({ previousErpData, productId }, context) => {
|
||||
const erpService: ErpModuleService =
|
||||
context.container.resolve("erpService")
|
||||
const erpService: ErpModuleService = context.container.resolve("erpService")
|
||||
|
||||
await erpService.updateProductErpData(
|
||||
productId,
|
||||
previousErpData
|
||||
)
|
||||
await erpService.updateProductErpData(productId, previousErpData)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -253,10 +254,7 @@ import updateProductAndErpWorkflow, {
|
||||
UpdateProductAndErpWorkflowInput,
|
||||
} from "../../../../../workflows/update-product-erp"
|
||||
|
||||
type ProductErpReq = Omit<
|
||||
UpdateProductAndErpWorkflowInput,
|
||||
"id"
|
||||
>
|
||||
type ProductErpReq = Omit<UpdateProductAndErpWorkflowInput, "id">
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<ProductErpReq>,
|
||||
@@ -268,9 +266,7 @@ export const POST = async (
|
||||
...req.body,
|
||||
}
|
||||
|
||||
const { result } = await updateProductAndErpWorkflow(
|
||||
req.scope
|
||||
).run({
|
||||
const { result } = await updateProductAndErpWorkflow(req.scope).run({
|
||||
input: productData,
|
||||
})
|
||||
|
||||
@@ -280,4 +276,4 @@ export const POST = async (
|
||||
|
||||
In this `POST` API route, you retrieve the product’s ID from the path parameter and the data to update from the request body. You then execute the workflow by passing it the retrieved data as an input.
|
||||
|
||||
The route returns the result of the workflow, which is an object holding both the update product’s details and the ERP details.
|
||||
The route returns the result of the workflow, which is an object holding both the update product’s details and the ERP details.
|
||||
@@ -23,10 +23,7 @@ A workflow is considered long-running if at least one step has its `async` confi
|
||||
For example, consider the following workflow and steps:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["13"]]} collapsibleLines="1-10" expandButtonLabel="Show More"
|
||||
import {
|
||||
createStep,
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
|
||||
const step1 = createStep("step-1", async () => {
|
||||
// ...
|
||||
@@ -50,19 +47,18 @@ type WorkflowOutput = {
|
||||
message: string
|
||||
}
|
||||
|
||||
const myWorkflow = createWorkflow<
|
||||
{},
|
||||
WorkflowOutput
|
||||
>({
|
||||
name: "hello-world",
|
||||
}, function () {
|
||||
step1()
|
||||
step2()
|
||||
step3()
|
||||
})
|
||||
const myWorkflow = createWorkflow<{}, WorkflowOutput>(
|
||||
{
|
||||
name: "hello-world",
|
||||
},
|
||||
function () {
|
||||
step1()
|
||||
step2()
|
||||
step3()
|
||||
}
|
||||
)
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
```
|
||||
|
||||
The second step has in its configuration object `async` set to true. This indicates that this step is an asynchronous step.
|
||||
@@ -89,26 +85,15 @@ export const highlights = [
|
||||
]
|
||||
|
||||
```ts title="src/api/store/workflows/route.ts" highlights={highlights} collapsibleLines="1-11" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import myWorkflow from "../../../workflows/hello-world"
|
||||
import {
|
||||
IWorkflowEngineService,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const { transaction, result } = await myWorkflow(req.scope)
|
||||
.run()
|
||||
export async function GET(req: MedusaRequest, res: MedusaResponse) {
|
||||
const { transaction, result } = await myWorkflow(req.scope).run()
|
||||
|
||||
const workflowEngine = req.scope.resolve<
|
||||
IWorkflowEngineService
|
||||
>(
|
||||
const workflowEngine = req.scope.resolve<IWorkflowEngineService>(
|
||||
ModuleRegistrationName.WORKFLOW_ENGINE
|
||||
)
|
||||
|
||||
@@ -137,18 +122,20 @@ The `subscribe` method accepts an object having three properties:
|
||||
{
|
||||
name: "workflowId",
|
||||
type: "`string`",
|
||||
description: "The name of the workflow."
|
||||
description: "The name of the workflow.",
|
||||
},
|
||||
{
|
||||
name: "transactionId",
|
||||
type: "`string`",
|
||||
description: "The ID of the workflow exection's transaction. The transaction's details are returned in the response of the workflow execution."
|
||||
description:
|
||||
"The ID of the workflow exection's transaction. The transaction's details are returned in the response of the workflow execution.",
|
||||
},
|
||||
{
|
||||
name: "subscriber",
|
||||
type: "`string`",
|
||||
description: "The function executed when the workflow execution's status changes. The function receives a data object. It has an `eventType` property, which you use to check the status of the workflow execution."
|
||||
}
|
||||
description:
|
||||
"The function executed when the workflow execution's status changes. The function receives a data object. It has an `eventType` property, which you use to check the status of the workflow execution.",
|
||||
},
|
||||
]}
|
||||
sectionTitle="Access Long-Running Workflow Status and Result"
|
||||
/>
|
||||
|
||||
@@ -23,19 +23,14 @@ Similarly to your custom module, a commerce module's main service is registered
|
||||
For example, you saw this code snippet in the [Medusa container chapter](../medusa-container/page.mdx):
|
||||
|
||||
```ts highlights={[["13"]]}
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const productModuleService: IProductModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const productModuleService: IProductModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const [, count] = await productModuleService.listAndCount()
|
||||
|
||||
@@ -51,4 +46,4 @@ When you resolve the `ModuleRegistrationName.PRODUCT` (or `productModuleService`
|
||||
|
||||
To resolve the main service of any commerce module, use the registration name defined in the `ModuleRegistrationName` enum imported from `@medusajs/modules-sdk`.
|
||||
|
||||
</Note>
|
||||
</Note>
|
||||
@@ -43,7 +43,7 @@ The above subscriber listens to the `product.created` event. Whenever the event
|
||||
|
||||
{/* TODO add when we have the admin dashboard to use with V2 for easy testing. */}
|
||||
|
||||
{/* ### Test Subscriber
|
||||
{/\* ### Test Subscriber
|
||||
|
||||
To test the subscriber, start the Medusa application:
|
||||
|
||||
@@ -58,7 +58,7 @@ info: Processing product.created which has 1 subscribers
|
||||
A product is created
|
||||
```
|
||||
|
||||
The first message indicates that the `product.created` event was emitted, and the second one is the message logged from the subscriber. */}
|
||||
The first message indicates that the `product.created` event was emitted, and the second one is the message logged from the subscriber. \*/}
|
||||
|
||||
---
|
||||
|
||||
@@ -83,23 +83,25 @@ For example:
|
||||
export const highlights = [
|
||||
["10", "container", "Recieve the Medusa Container in the object parameter."],
|
||||
["13", "resolve", "Resolve the Product Module's main service."],
|
||||
["13", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
|
||||
[
|
||||
"13",
|
||||
"ModuleRegistrationName.PRODUCT",
|
||||
"The resource registration name imported from `@medusajs/modules-sdk`.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/subscribers/product-created.ts" highlights={highlights}
|
||||
import {
|
||||
SubscriberArgs,
|
||||
type SubscriberConfig,
|
||||
} from "@medusajs/medusa"
|
||||
import { SubscriberArgs, type SubscriberConfig } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
data,
|
||||
container,
|
||||
}: SubscriberArgs<Record<string, string>>) {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
const productModuleService: IProductModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const product = await productModuleService.retrieve(data.id)
|
||||
|
||||
|
||||
@@ -16,23 +16,22 @@ For example, in a custom API route you can resolve any service registered in the
|
||||
|
||||
export const highlights = [
|
||||
["13", "resolve", "Resolve the Product Module's main service."],
|
||||
["13", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
|
||||
[
|
||||
"13",
|
||||
"ModuleRegistrationName.PRODUCT",
|
||||
"The resource registration name imported from `@medusajs/modules-sdk`.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts highlights={highlights}
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const productModuleService: IProductModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const productModuleService: IProductModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const [, count] = await productModuleService.listAndCount()
|
||||
|
||||
|
||||
@@ -25,10 +25,7 @@ A workflow is made of a series of steps. A step is created using the `createStep
|
||||
Create the file `src/workflows/hello-world.ts` with the following content:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
|
||||
const step1 = createStep("step-1", async () => {
|
||||
return new StepResponse(`Hello from step one!`)
|
||||
@@ -44,12 +41,9 @@ type WorkflowInput = {
|
||||
name: string
|
||||
}
|
||||
|
||||
const step2 = createStep(
|
||||
"step-2",
|
||||
async ({ name }: WorkflowInput) => {
|
||||
return new StepResponse(`Hello ${name} from step two!`)
|
||||
}
|
||||
)
|
||||
const step2 = createStep("step-2", async ({ name }: WorkflowInput) => {
|
||||
return new StepResponse(`Hello ${name} from step two!`)
|
||||
})
|
||||
```
|
||||
|
||||
### 2. Create a Workflow
|
||||
@@ -68,21 +62,20 @@ type WorkflowOutput = {
|
||||
message: string
|
||||
}
|
||||
|
||||
const myWorkflow = createWorkflow<
|
||||
WorkflowInput,
|
||||
WorkflowOutput
|
||||
>("hello-world", function (input) {
|
||||
const str1 = step1()
|
||||
// to pass input
|
||||
const str2 = step2(input)
|
||||
const myWorkflow = createWorkflow<WorkflowInput, WorkflowOutput>(
|
||||
"hello-world",
|
||||
function (input) {
|
||||
const str1 = step1()
|
||||
// to pass input
|
||||
const str2 = step2(input)
|
||||
|
||||
return {
|
||||
message: str1,
|
||||
return {
|
||||
message: str1,
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
```
|
||||
|
||||
This creates a `hello-world` workflow. When you create a workflow, it’s constructed but not executed yet.
|
||||
@@ -226,7 +219,11 @@ For example:
|
||||
|
||||
export const highlights = [
|
||||
["15", "resolve", "Resolve the Product Module's main service."],
|
||||
["15", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
|
||||
[
|
||||
"15",
|
||||
"ModuleRegistrationName.PRODUCT",
|
||||
"The resource registration name imported from `@medusajs/modules-sdk`.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/product-count.ts" highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
|
||||
@@ -236,18 +233,17 @@ import {
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
type WorkflowOutput = {
|
||||
count: number
|
||||
}
|
||||
|
||||
const step1 = createStep("step-1", async (_, context) => {
|
||||
const productModuleService: IProductModuleService =
|
||||
context.container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
return new StepResponse(
|
||||
(await productModuleService.listAndCount())[1]
|
||||
const productModuleService: IProductModuleService = context.container.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
return new StepResponse((await productModuleService.listAndCount())[1])
|
||||
})
|
||||
|
||||
const myWorkflow = createWorkflow<unknown, WorkflowOutput>(
|
||||
@@ -264,4 +260,4 @@ const myWorkflow = createWorkflow<unknown, WorkflowOutput>(
|
||||
export default myWorkflow
|
||||
```
|
||||
|
||||
In the step, you resolve the Product Module's main service and use it to retrieve the product count.
|
||||
In the step, you resolve the Product Module's main service and use it to retrieve the product count.
|
||||
+28
-18
@@ -15,17 +15,26 @@ In your resource, such as a subscriber, resolve the Notification Module's main s
|
||||
export const highlights = [
|
||||
["12", "notificationModuleService", "Resolve the Notification Module."],
|
||||
["17", "create", "Create the notification to be sent."],
|
||||
["19", '"email"', "Use the module provider defined for the `email` channel to send an email."],
|
||||
["20", '"product-created"', "The ID of the template defined in the third-party service, such as SendGrid."],
|
||||
["21", "data", "The data to pass to the template defined in the third-party service."]
|
||||
[
|
||||
"19",
|
||||
'"email"',
|
||||
"Use the module provider defined for the `email` channel to send an email.",
|
||||
],
|
||||
[
|
||||
"20",
|
||||
'"product-created"',
|
||||
"The ID of the template defined in the third-party service, such as SendGrid.",
|
||||
],
|
||||
[
|
||||
"21",
|
||||
"data",
|
||||
"The data to pass to the template defined in the third-party service.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
SubscriberArgs,
|
||||
SubscriberConfig,
|
||||
} from "@medusajs/medusa"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/medusa"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { INotificationModuleService } from "@medusajs/types"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
@@ -33,9 +42,7 @@ export default async function productCreateHandler({
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const notificationModuleService: INotificationModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.NOTIFICATION
|
||||
)
|
||||
container.resolve(ModuleRegistrationName.NOTIFICATION)
|
||||
|
||||
await notificationModuleService.create({
|
||||
to: "shahednasser@gmail.com",
|
||||
@@ -57,26 +64,29 @@ The `create` method accepts an object or an array of objects having the followin
|
||||
{
|
||||
name: "to",
|
||||
type: "`string`",
|
||||
description: "The destination to send the notification to. When sending an email, it'll be the email address. When sending an SMS, it'll be the phone number.",
|
||||
description:
|
||||
"The destination to send the notification to. When sending an email, it'll be the email address. When sending an SMS, it'll be the phone number.",
|
||||
optional: false,
|
||||
},
|
||||
{
|
||||
name: "channel",
|
||||
type: "`string`",
|
||||
description: "The channel to send the notification through. For example, `email` or `sms`. The module provider defined for that channel will be used to send the notification.",
|
||||
optional: false
|
||||
description:
|
||||
"The channel to send the notification through. For example, `email` or `sms`. The module provider defined for that channel will be used to send the notification.",
|
||||
optional: false,
|
||||
},
|
||||
{
|
||||
name: "template",
|
||||
type: "`string`",
|
||||
description: "The ID of the template used for the notification. This is useful for providers like SendGrid, where you define templates within SendGrid and use their IDs here.",
|
||||
optional: false
|
||||
description:
|
||||
"The ID of the template used for the notification. This is useful for providers like SendGrid, where you define templates within SendGrid and use their IDs here.",
|
||||
optional: false,
|
||||
},
|
||||
{
|
||||
name: "data",
|
||||
type: "`Record<string, unknown>`",
|
||||
description: "The data to pass along to the template, if necessary."
|
||||
}
|
||||
description: "The data to pass along to the template, if necessary.",
|
||||
},
|
||||
]}
|
||||
sectionTitle="Use the Create Method"
|
||||
/>
|
||||
|
||||
@@ -86,40 +86,19 @@ SENDGRID_FROM=<YOUR_SENDGRID_FROM>
|
||||
</Table.Header>
|
||||
<Table.Body>
|
||||
<Table.Row>
|
||||
<Table.Cell>`channels`</Table.Cell>
|
||||
<Table.Cell>
|
||||
|
||||
`channels`
|
||||
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
|
||||
The channels this notification module is used to send notifications for. Only one provider can be defined for a channel.
|
||||
|
||||
The channels this notification module is used to send notifications for.
|
||||
Only one provider can be defined for a channel.
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>
|
||||
|
||||
`api_key`
|
||||
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
|
||||
The SendGrid API key.
|
||||
|
||||
</Table.Cell>
|
||||
<Table.Cell>`api_key`</Table.Cell>
|
||||
<Table.Cell>The SendGrid API key.</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>
|
||||
|
||||
`from`
|
||||
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
|
||||
The SendGrid from email.
|
||||
|
||||
</Table.Cell>
|
||||
<Table.Cell>`from`</Table.Cell>
|
||||
<Table.Cell>The SendGrid from email.</Table.Cell>
|
||||
</Table.Row>
|
||||
</Table.Body>
|
||||
</Table>
|
||||
@@ -141,17 +120,18 @@ To test the module out, create a simple subscriber at `src/subscribers/product-c
|
||||
export const highlights = [
|
||||
["12", "notificationModuleService", "Resolve the Notification Module."],
|
||||
["17", "create", "Create the notification to be sent."],
|
||||
["19", '"email"', "By specifying the `email` channel, SendGrid will be used to send the notification."],
|
||||
[
|
||||
"19",
|
||||
'"email"',
|
||||
"By specifying the `email` channel, SendGrid will be used to send the notification.",
|
||||
],
|
||||
["20", '"product-created"', "The ID of the template defined in SendGrid."],
|
||||
["21", "data", "The data to pass to the template defined in SendGrid."]
|
||||
["21", "data", "The data to pass to the template defined in SendGrid."],
|
||||
]
|
||||
|
||||
```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
SubscriberArgs,
|
||||
SubscriberConfig,
|
||||
} from "@medusajs/medusa"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/medusa"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { INotificationModuleService } from "@medusajs/types"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
@@ -159,9 +139,7 @@ export default async function productCreateHandler({
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const notificationModuleService: INotificationModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.NOTIFICATION
|
||||
)
|
||||
container.resolve(ModuleRegistrationName.NOTIFICATION)
|
||||
|
||||
await notificationModuleService.create({
|
||||
to: "test@gmail.com",
|
||||
@@ -178,7 +156,7 @@ export const config: SubscriberConfig = {
|
||||
|
||||
In this subscriber:
|
||||
|
||||
- Resolve the Notification Module's main service.
|
||||
- Resolve the Notification Module's main service.
|
||||
- Use the `create` method of the main service to create a notification to be sent to the specified email.
|
||||
- By specifying the `email` channel, the SendGrid Notification Module Provider is used to send the notification.
|
||||
- The `template` property of the `create` method's parameter specifies the ID of the template defined in SendGrid.
|
||||
|
||||
@@ -13,54 +13,50 @@ In this guide, you’ll find common examples of how you can use the API Key Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const apiKeyModuleService: IApiKeyModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
const apiKey = await apiKeyModuleService.createApiKeys({
|
||||
title: "Publishable API key",
|
||||
type: "publishable",
|
||||
created_by: "user_123",
|
||||
})
|
||||
const apiKey = await apiKeyModuleService.createApiKeys({
|
||||
title: "Publishable API key",
|
||||
type: "publishable",
|
||||
created_by: "user_123",
|
||||
})
|
||||
|
||||
res.json({
|
||||
api_key: apiKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
api_key: apiKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeApiKeyModule,
|
||||
} from "@medusajs/api-key"
|
||||
import { initialize as initializeApiKeyModule } from "@medusajs/api-key"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
export async function POST(request: Request) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
|
||||
const apiKey = await apiKeyModuleService.createApiKeys({
|
||||
title: "Publishable API key",
|
||||
type: "publishable",
|
||||
created_by: "user_123",
|
||||
})
|
||||
const apiKey = await apiKeyModuleService.createApiKeys({
|
||||
title: "Publishable API key",
|
||||
type: "publishable",
|
||||
created_by: "user_123",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
api_key: apiKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
api_key: apiKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -72,42 +68,38 @@ In this guide, you’ll find common examples of how you can use the API Key Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
export async function GET(request: MedusaRequest, res: MedusaResponse) {
|
||||
const apiKeyModuleService: IApiKeyModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
res.json({
|
||||
api_keys: await apiKeyModuleService.listApiKeys(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
api_keys: await apiKeyModuleService.listApiKeys(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeApiKeyModule,
|
||||
} from "@medusajs/api-key"
|
||||
import { initialize as initializeApiKeyModule } from "@medusajs/api-key"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
export async function GET(request: Request) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
|
||||
return NextResponse.json({
|
||||
api_keys: await apiKeyModuleService.listApiKeys(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
api_keys: await apiKeyModuleService.listApiKeys(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -119,71 +111,56 @@ In this guide, you’ll find common examples of how you can use the API Key Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import {
|
||||
ModuleRegistrationName,
|
||||
} from "@medusajs/modules-sdk"
|
||||
```ts collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
export async function POST(
|
||||
request: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
const revokedKey = await apiKeyModuleService.revoke(
|
||||
request.params.id,
|
||||
{
|
||||
revoked_by: request.auth_context.actor_id,
|
||||
}
|
||||
)
|
||||
const revokedKey = await apiKeyModuleService.revoke(request.params.id, {
|
||||
revoked_by: request.auth_context.actor_id,
|
||||
})
|
||||
|
||||
res.json({
|
||||
api_key: revokedKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
api_key: revokedKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeApiKeyModule,
|
||||
} from "@medusajs/api-key"
|
||||
import { initialize as initializeApiKeyModule } from "@medusajs/api-key"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
user_id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
user_id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
|
||||
const revokedKey = await apiKeyModuleService.revoke(
|
||||
params.id,
|
||||
{
|
||||
revoked_by: params.user_id,
|
||||
}
|
||||
)
|
||||
const revokedKey = await apiKeyModuleService.revoke(params.id, {
|
||||
revoked_by: params.user_id,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
api_key: revokedKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
api_key: revokedKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -195,57 +172,52 @@ In this guide, you’ll find common examples of how you can use the API Key Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const apiKeyModuleService: IApiKeyModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
const authenticatedToken =
|
||||
await apiKeyModuleService.authenticate(request.params.token)
|
||||
const authenticatedToken = await apiKeyModuleService.authenticate(
|
||||
request.params.token
|
||||
)
|
||||
|
||||
res.json({
|
||||
is_authenticated: !!authenticatedToken,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
is_authenticated: !!authenticatedToken,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeApiKeyModule,
|
||||
} from "@medusajs/api-key"
|
||||
import { initialize as initializeApiKeyModule } from "@medusajs/api-key"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
token: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
token: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
|
||||
const authenticatedToken =
|
||||
await apiKeyModuleService.authenticate(request.params.token)
|
||||
const authenticatedToken = await apiKeyModuleService.authenticate(
|
||||
request.params.token
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
is_authenticated: !!authenticatedToken,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
is_authenticated: !!authenticatedToken,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -257,78 +229,68 @@ In this guide, you’ll find common examples of how you can use the API Key Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
export async function POST(
|
||||
request: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const apiKeyModuleService: IApiKeyModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
const revokedKey = await apiKeyModuleService.revoke(
|
||||
request.params.id,
|
||||
{
|
||||
revoked_by: request.auth_context.actor_id,
|
||||
}
|
||||
)
|
||||
const revokedKey = await apiKeyModuleService.revoke(request.params.id, {
|
||||
revoked_by: request.auth_context.actor_id,
|
||||
})
|
||||
|
||||
const newKey = await apiKeyModuleService.createApiKeys({
|
||||
title: revokedKey.title,
|
||||
type: revokedKey.type,
|
||||
created_by: revokedKey.created_by,
|
||||
})
|
||||
const newKey = await apiKeyModuleService.createApiKeys({
|
||||
title: revokedKey.title,
|
||||
type: revokedKey.type,
|
||||
created_by: revokedKey.created_by,
|
||||
})
|
||||
|
||||
res.json({
|
||||
api_key: newKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
api_key: newKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeApiKeyModule,
|
||||
} from "@medusajs/api-key"
|
||||
import { initialize as initializeApiKeyModule } from "@medusajs/api-key"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
user_id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
user_id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const apiKeyModuleService = await initializeApiKeyModule()
|
||||
|
||||
const revokedKey = await apiKeyModuleService.revoke(params.id, {
|
||||
revoked_by: params.user_id,
|
||||
})
|
||||
const revokedKey = await apiKeyModuleService.revoke(params.id, {
|
||||
revoked_by: params.user_id,
|
||||
})
|
||||
|
||||
const newKey = await apiKeyModuleService.createApiKeys({
|
||||
title: revokedKey.title,
|
||||
type: revokedKey.type,
|
||||
created_by: revokedKey.created_by,
|
||||
})
|
||||
const newKey = await apiKeyModuleService.createApiKeys({
|
||||
title: revokedKey.title,
|
||||
type: revokedKey.type,
|
||||
created_by: revokedKey.created_by,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
api_key: newKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
api_key: newKey,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -37,8 +37,7 @@ const secretApiKey = await apiKeyModuleService.createApiKeys({
|
||||
Verify tokens of secret API keys to authenticate users or actions, such as verifying a password reset token.
|
||||
|
||||
```ts
|
||||
const authenticatedToken =
|
||||
await apiKeyModuleService.authenticate("sk_123")
|
||||
const authenticatedToken = await apiKeyModuleService.authenticate("sk_123")
|
||||
|
||||
if (!authenticatedToken) {
|
||||
console.error("Couldn't verify token")
|
||||
@@ -84,61 +83,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const apiKeyModuleService: IApiKeyModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
res.json({
|
||||
api_keys: await apiKeyModuleService.listApiKeys(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
api_keys: await apiKeyModuleService.listApiKeys(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
container.resolve(ModuleRegistrationName.API_KEY)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const apiKeyModuleService: IApiKeyModuleService = container.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
const apiKeys = await apiKeyModuleService.listApiKeys()
|
||||
}
|
||||
```
|
||||
const apiKeys = await apiKeyModuleService.listApiKeys()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const apiKeyModuleService: IApiKeyModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const apiKeyModuleService: IApiKeyModuleService = container.resolve(
|
||||
ModuleRegistrationName.API_KEY
|
||||
)
|
||||
|
||||
const apiKeys = await apiKeyModuleService.listApiKeys()
|
||||
})
|
||||
```
|
||||
const apiKeys = await apiKeyModuleService.listApiKeys()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -19,92 +19,90 @@ This example uses the [jsonwebtoken NPM package](https://www.npmjs.com/package/j
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts collapsibleLines="1-10" expandButtonLabel="Show Imports"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import {
|
||||
IAuthModuleService,
|
||||
AuthenticationInput,
|
||||
} from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import jwt from "jsonwebtoken"
|
||||
```ts collapsibleLines="1-10" expandButtonLabel="Show Imports"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService, AuthenticationInput } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import jwt from "jsonwebtoken"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const { success, authIdentity, location, error } =
|
||||
await authModuleService.authenticate("emailpass", {
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
authScope: "admin",
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput)
|
||||
const { success, authIdentity, location, error } =
|
||||
await authModuleService.authenticate("emailpass", {
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
authScope: "admin",
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput)
|
||||
|
||||
if (!success) {
|
||||
throw new MedusaError(MedusaError.Types.UNAUTHORIZED, error)
|
||||
}
|
||||
|
||||
if (location) {
|
||||
res.redirect(location)
|
||||
return
|
||||
}
|
||||
|
||||
const { jwtSecret } =
|
||||
req.scope.resolve("configModule").projectConfig.http
|
||||
const token = jwt.sign(authIdentity, jwtSecret)
|
||||
|
||||
res.status(200).json({ token })
|
||||
if (!success) {
|
||||
throw new MedusaError(MedusaError.Types.UNAUTHORIZED, error)
|
||||
}
|
||||
```
|
||||
|
||||
if (location) {
|
||||
res.redirect(location)
|
||||
return
|
||||
}
|
||||
|
||||
const { jwtSecret } = req.scope.resolve("configModule").projectConfig.http
|
||||
const token = jwt.sign(authIdentity, jwtSecret)
|
||||
|
||||
res.status(200).json({ token })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeAuthModule,
|
||||
} from "@medusajs/auth"
|
||||
import { initialize as initializeAuthModule } from "@medusajs/auth"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
const url = new URL(request.url)
|
||||
export async function POST(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
const url = new URL(request.url)
|
||||
|
||||
const { success, authIdentity, location, error } =
|
||||
await authModuleService.authenticate("emailpass", {
|
||||
url: request.url,
|
||||
headers: Object.fromEntries(request.headers),
|
||||
query: Object.fromEntries(url.searchParams),
|
||||
body: await request.json(),
|
||||
authScope: "admin",
|
||||
protocol: url.protocol,
|
||||
} as AuthenticationInput)
|
||||
const { success, authIdentity, location, error } =
|
||||
await authModuleService.authenticate("emailpass", {
|
||||
url: request.url,
|
||||
headers: Object.fromEntries(request.headers),
|
||||
query: Object.fromEntries(url.searchParams),
|
||||
body: await request.json(),
|
||||
authScope: "admin",
|
||||
protocol: url.protocol,
|
||||
} as AuthenticationInput)
|
||||
|
||||
if (!success) {
|
||||
throw new Error(error)
|
||||
}
|
||||
|
||||
if (location) {
|
||||
return NextResponse.redirect(location)
|
||||
return
|
||||
}
|
||||
|
||||
const token = jwt.sign(authIdentity, "supersecret")
|
||||
|
||||
return NextResponse.json({
|
||||
token,
|
||||
}, {
|
||||
status: 200,
|
||||
})
|
||||
if (!success) {
|
||||
throw new Error(error)
|
||||
}
|
||||
```
|
||||
|
||||
if (location) {
|
||||
return NextResponse.redirect(location)
|
||||
return
|
||||
}
|
||||
|
||||
const token = jwt.sign(authIdentity, "supersecret")
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
token,
|
||||
},
|
||||
{
|
||||
status: 200,
|
||||
}
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -122,96 +120,94 @@ This example uses the [jsonwebtoken NPM package](https://www.npmjs.com/package/j
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts collapsibleLines="1-10" expandButtonLabel="Show Imports"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import {
|
||||
IAuthModuleService,
|
||||
AuthenticationInput,
|
||||
} from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import jwt from "jsonwebtoken"
|
||||
```ts collapsibleLines="1-10" expandButtonLabel="Show Imports"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService, AuthenticationInput } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import jwt from "jsonwebtoken"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const { success, authIdentity, error, successRedirectUrl } =
|
||||
await authModuleService.validateCallback("google", {
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
authScope: "admin",
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput)
|
||||
const { success, authIdentity, error, successRedirectUrl } =
|
||||
await authModuleService.validateCallback("google", {
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
authScope: "admin",
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput)
|
||||
|
||||
if (!success) {
|
||||
throw new MedusaError(MedusaError.Types.UNAUTHORIZED, error)
|
||||
}
|
||||
|
||||
const { jwtSecret } =
|
||||
req.scope.resolve("configModule").projectConfig.http
|
||||
const token = jwt.sign(authIdentity, jwtSecret)
|
||||
|
||||
if (successRedirectUrl) {
|
||||
const url = new URL(successRedirectUrl!)
|
||||
url.searchParams.append("auth_token", token)
|
||||
|
||||
return res.redirect(url.toString())
|
||||
}
|
||||
|
||||
res.status(200).json({ token })
|
||||
if (!success) {
|
||||
throw new MedusaError(MedusaError.Types.UNAUTHORIZED, error)
|
||||
}
|
||||
```
|
||||
|
||||
const { jwtSecret } = req.scope.resolve("configModule").projectConfig.http
|
||||
const token = jwt.sign(authIdentity, jwtSecret)
|
||||
|
||||
if (successRedirectUrl) {
|
||||
const url = new URL(successRedirectUrl!)
|
||||
url.searchParams.append("auth_token", token)
|
||||
|
||||
return res.redirect(url.toString())
|
||||
}
|
||||
|
||||
res.status(200).json({ token })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
import { NextResponse } from "next/server"
|
||||
```ts collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeAuthModule,
|
||||
} from "@medusajs/auth"
|
||||
import { initialize as initializeAuthModule } from "@medusajs/auth"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
const url = new URL(request.url)
|
||||
export async function POST(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
const url = new URL(request.url)
|
||||
|
||||
const { success, authIdentity, location, error } =
|
||||
await authModuleService.authenticate("google", {
|
||||
url: request.url,
|
||||
headers: Object.fromEntries(request.headers),
|
||||
query: Object.fromEntries(url.searchParams),
|
||||
body: await request.json(),
|
||||
authScope: "admin",
|
||||
protocol: url.protocol,
|
||||
} as AuthenticationInput)
|
||||
const { success, authIdentity, location, error } =
|
||||
await authModuleService.authenticate("google", {
|
||||
url: request.url,
|
||||
headers: Object.fromEntries(request.headers),
|
||||
query: Object.fromEntries(url.searchParams),
|
||||
body: await request.json(),
|
||||
authScope: "admin",
|
||||
protocol: url.protocol,
|
||||
} as AuthenticationInput)
|
||||
|
||||
if (!success) {
|
||||
throw new Error(error)
|
||||
}
|
||||
|
||||
const token = jwt.sign(authIdentity, "supersecret")
|
||||
|
||||
if (successRedirectUrl) {
|
||||
const url = new URL(successRedirectUrl!)
|
||||
url.searchParams.append("auth_token", token)
|
||||
|
||||
return NextResponse.redirect(url.toString())
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
token,
|
||||
}, {
|
||||
status: 200,
|
||||
})
|
||||
if (!success) {
|
||||
throw new Error(error)
|
||||
}
|
||||
```
|
||||
|
||||
const token = jwt.sign(authIdentity, "supersecret")
|
||||
|
||||
if (successRedirectUrl) {
|
||||
const url = new URL(successRedirectUrl!)
|
||||
url.searchParams.append("auth_token", token)
|
||||
|
||||
return NextResponse.redirect(url.toString())
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
token,
|
||||
},
|
||||
{
|
||||
status: 200,
|
||||
}
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -223,54 +219,51 @@ This example uses the [jsonwebtoken NPM package](https://www.npmjs.com/package/j
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authIdentity = await authModuleService
|
||||
.createAuthIdentities({
|
||||
provider: "emailpass",
|
||||
entity_id: "user@example.com",
|
||||
scope: "admin",
|
||||
})
|
||||
const authIdentity = await authModuleService.createAuthIdentities({
|
||||
provider: "emailpass",
|
||||
entity_id: "user@example.com",
|
||||
scope: "admin",
|
||||
})
|
||||
|
||||
res.json({ auth_identity: authIdentity })
|
||||
}
|
||||
```
|
||||
res.json({ auth_identity: authIdentity })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeAuthModule,
|
||||
} from "@medusajs/auth"
|
||||
import { initialize as initializeAuthModule } from "@medusajs/auth"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
export async function POST(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
|
||||
const authIdentity = await authModuleService
|
||||
.createAuthIdentities({
|
||||
provider: "emailpass",
|
||||
entity_id: "user@example.com",
|
||||
scope: "admin",
|
||||
})
|
||||
const authIdentity = await authModuleService.createAuthIdentities({
|
||||
provider: "emailpass",
|
||||
entity_id: "user@example.com",
|
||||
scope: "admin",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
auth_identity: authIdentity,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
auth_identity: authIdentity,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -282,44 +275,41 @@ This example uses the [jsonwebtoken NPM package](https://www.npmjs.com/package/j
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
res.json({
|
||||
auth_identitys:
|
||||
await authModuleService.listAuthIdentities(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
auth_identitys: await authModuleService.listAuthIdentities(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeAuthModule,
|
||||
} from "@medusajs/auth"
|
||||
import { initialize as initializeAuthModule } from "@medusajs/auth"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
export async function GET(request: Request) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
|
||||
return NextResponse.json({
|
||||
auth_identities:
|
||||
await authModuleService.listAuthIdentities(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
auth_identities: await authModuleService.listAuthIdentities(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -331,67 +321,61 @@ This example uses the [jsonwebtoken NPM package](https://www.npmjs.com/package/j
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authIdentity = await authModuleService
|
||||
.updateAuthIdentites({
|
||||
id: "authusr_123",
|
||||
provider_metadata: {
|
||||
test: true,
|
||||
},
|
||||
})
|
||||
const authIdentity = await authModuleService.updateAuthIdentites({
|
||||
id: "authusr_123",
|
||||
provider_metadata: {
|
||||
test: true,
|
||||
},
|
||||
})
|
||||
|
||||
res.json({
|
||||
auth_identity: authIdentity,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
auth_identity: authIdentity,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeAuthModule,
|
||||
} from "@medusajs/auth"
|
||||
import { initialize as initializeAuthModule } from "@medusajs/auth"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
|
||||
const authIdentity = await authModuleService
|
||||
.updateAuthIdentites({
|
||||
id: "authusr_123",
|
||||
provider_metadata: {
|
||||
test: true,
|
||||
},
|
||||
})
|
||||
const authIdentity = await authModuleService.updateAuthIdentites({
|
||||
id: "authusr_123",
|
||||
provider_metadata: {
|
||||
test: true,
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
auth_identity: authIdentity,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
auth_identity: authIdentity,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -403,53 +387,45 @@ This example uses the [jsonwebtoken NPM package](https://www.npmjs.com/package/j
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
await authModuleService.deleteAuthIdentities([
|
||||
"authusr_123",
|
||||
])
|
||||
await authModuleService.deleteAuthIdentities(["authusr_123"])
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeAuthModule,
|
||||
} from "@medusajs/auth"
|
||||
import { initialize as initializeAuthModule } from "@medusajs/auth"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
export async function DELETE(request: Request, { params }: ContextType) {
|
||||
const authModuleService = await initializeAuthModule()
|
||||
|
||||
await authModuleService.deleteAuthIdentities([
|
||||
"authusr_123",
|
||||
])
|
||||
}
|
||||
```
|
||||
await authModuleService.deleteAuthIdentities(["authusr_123"])
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -15,15 +15,17 @@ The Auth Module is the `@medusajs/auth` NPM package that provides authentication
|
||||
With the Auth Module, authenticate users using their email and password credentials.
|
||||
|
||||
```ts
|
||||
const { success, authIdentity, error } =
|
||||
await authModuleService.authenticate("emailpass", {
|
||||
const { success, authIdentity, error } = await authModuleService.authenticate(
|
||||
"emailpass",
|
||||
{
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
authScope: "admin",
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput)
|
||||
} as AuthenticationInput
|
||||
)
|
||||
|
||||
if (!success) {
|
||||
// incorrect authentication details
|
||||
@@ -53,15 +55,17 @@ if (!authIdentity && location) {
|
||||
}
|
||||
|
||||
// in callback API route
|
||||
const { success, authIdentity } =
|
||||
await authModuleService.validateCallback("google", {
|
||||
const { success, authIdentity } = await authModuleService.validateCallback(
|
||||
"google",
|
||||
{
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
authScope: "admin",
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput)
|
||||
} as AuthenticationInput
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
@@ -81,72 +85,57 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import {
|
||||
ModuleRegistrationName,
|
||||
} from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.AUTH)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const authModuleService: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
res.json({
|
||||
authIdentitys:
|
||||
await authModuleService.listAuthIdentities(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
authIdentitys: await authModuleService.listAuthIdentities(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import {
|
||||
ModuleRegistrationName,
|
||||
} from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const authModuleService: IAuthModuleService =
|
||||
container.resolve(ModuleRegistrationName.AUTH)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const authModuleService: IAuthModuleService = container.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authIdentitys = await authModuleService
|
||||
.listAuthIdentities()
|
||||
}
|
||||
```
|
||||
const authIdentitys = await authModuleService.listAuthIdentities()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import {
|
||||
ModuleRegistrationName,
|
||||
} from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const authModuleService: IAuthModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
const authIdentitys = await authModuleService
|
||||
.listAuthIdentities()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const authModuleService: IAuthModuleService = container.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
const authIdentitys = await authModuleService.listAuthIdentities()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,70 +13,69 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
address_1: "1512 Barataria Blvd",
|
||||
country_code: "us",
|
||||
const cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
address_1: "1512 Barataria Blvd",
|
||||
country_code: "us",
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Shirt",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Shirt",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
})
|
||||
],
|
||||
})
|
||||
|
||||
res.json({ cart })
|
||||
}
|
||||
```
|
||||
res.json({ cart })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function POST(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
const cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
address_1: "1512 Barataria Blvd",
|
||||
country_code: "us",
|
||||
const cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
address_1: "1512 Barataria Blvd",
|
||||
country_code: "us",
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Shirt",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Shirt",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
})
|
||||
],
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
cart,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
cart,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -88,48 +87,43 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const cart = await cartModuleService.retrieveCart(
|
||||
"cart_123"
|
||||
)
|
||||
const cart = await cartModuleService.retrieveCart("cart_123")
|
||||
|
||||
res.json({ cart })
|
||||
}
|
||||
```
|
||||
res.json({ cart })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function GET(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
const cart = await cartModuleService.retrieveCart(
|
||||
"cart_123"
|
||||
)
|
||||
const cart = await cartModuleService.retrieveCart("cart_123")
|
||||
|
||||
return NextResponse.json({
|
||||
cart,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
cart,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -141,58 +135,55 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const lineItem = await cartModuleService.addLineItems({
|
||||
cart_id: "cart_123",
|
||||
title: "Shirt",
|
||||
quantity: 2,
|
||||
unit_price: 5000,
|
||||
})
|
||||
const lineItem = await cartModuleService.addLineItems({
|
||||
cart_id: "cart_123",
|
||||
title: "Shirt",
|
||||
quantity: 2,
|
||||
unit_price: 5000,
|
||||
})
|
||||
|
||||
res.json({
|
||||
line_item: lineItem,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
line_item: lineItem,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function POST(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
const lineItem = await cartModuleService.addLineItems({
|
||||
cart_id: "cart_123",
|
||||
title: "Shirt",
|
||||
quantity: 2,
|
||||
unit_price: 5000,
|
||||
})
|
||||
const lineItem = await cartModuleService.addLineItems({
|
||||
cart_id: "cart_123",
|
||||
title: "Shirt",
|
||||
quantity: 2,
|
||||
unit_price: 5000,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
line_item: lineItem,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
line_item: lineItem,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -204,58 +195,53 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const shippingMethod =
|
||||
await cartModuleService.addShippingMethods({
|
||||
cart_id: "cart_123",
|
||||
name: "Custom shipping",
|
||||
amount: 1000,
|
||||
})
|
||||
const shippingMethod = await cartModuleService.addShippingMethods({
|
||||
cart_id: "cart_123",
|
||||
name: "Custom shipping",
|
||||
amount: 1000,
|
||||
})
|
||||
|
||||
res.json({
|
||||
shipping_method: shippingMethod,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
shipping_method: shippingMethod,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function POST(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
const shippingMethod =
|
||||
await cartModuleService.addShippingMethods({
|
||||
cart_id: "cart_123",
|
||||
name: "Custom shipping",
|
||||
amount: 1000,
|
||||
})
|
||||
const shippingMethod = await cartModuleService.addShippingMethods({
|
||||
cart_id: "cart_123",
|
||||
name: "Custom shipping",
|
||||
amount: 1000,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
shipping_method: shippingMethod,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
shipping_method: shippingMethod,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -267,58 +253,53 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const itemAdjustment =
|
||||
await cartModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
amount: 500,
|
||||
code: "50%OFF",
|
||||
})
|
||||
const itemAdjustment = await cartModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
amount: 500,
|
||||
code: "50%OFF",
|
||||
})
|
||||
|
||||
res.json({
|
||||
adjustment: itemAdjustment,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
adjustment: itemAdjustment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function POST(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
const itemAdjustment =
|
||||
await cartModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
amount: 500,
|
||||
code: "50%OFF",
|
||||
})
|
||||
const itemAdjustment = await cartModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
amount: 500,
|
||||
code: "50%OFF",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
adjustment: itemAdjustment,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
adjustment: itemAdjustment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -330,58 +311,55 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const shippingMethodAdjustment =
|
||||
await cartModuleService.addShippingMethodAdjustments({
|
||||
shipping_method_id: "casm_123",
|
||||
amount: 500,
|
||||
code: "FREESHIPPING",
|
||||
})
|
||||
|
||||
res.json({
|
||||
adjustment: shippingMethodAdjustment,
|
||||
const shippingMethodAdjustment =
|
||||
await cartModuleService.addShippingMethodAdjustments({
|
||||
shipping_method_id: "casm_123",
|
||||
amount: 500,
|
||||
code: "FREESHIPPING",
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
res.json({
|
||||
adjustment: shippingMethodAdjustment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function POST(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
const shippingMethodAdjustment =
|
||||
await cartModuleService.addShippingMethodAdjustments({
|
||||
shipping_method_id: "casm_123",
|
||||
amount: 500,
|
||||
code: "FREESHIPPING",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
adjustment: shippingMethodAdjustment,
|
||||
const shippingMethodAdjustment =
|
||||
await cartModuleService.addShippingMethodAdjustments({
|
||||
shipping_method_id: "casm_123",
|
||||
amount: 500,
|
||||
code: "FREESHIPPING",
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
return NextResponse.json({
|
||||
adjustment: shippingMethodAdjustment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -393,42 +371,39 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
await cartModuleService.deleteLineItems(["cali_123"])
|
||||
await cartModuleService.deleteLineItems(["cali_123"])
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCartModule,
|
||||
} from "@medusajs/cart"
|
||||
import { initialize as initializeCartModule } from "@medusajs/cart"
|
||||
|
||||
export async function DELETE(
|
||||
request: Request
|
||||
) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
export async function DELETE(request: Request) {
|
||||
const cartModuleService = await initializeCartModule()
|
||||
|
||||
await cartModuleService.deleteLineItems(["cali_123"])
|
||||
}
|
||||
```
|
||||
await cartModuleService.deleteLineItems(["cali_123"])
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -440,44 +415,46 @@ In this guide, you’ll find common examples of how you can use the Cart Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
await cartModuleService.deleteShippingMethods(["casm_123"])
|
||||
await cartModuleService.deleteShippingMethods(["casm_123"])
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
await cartModuleService.deleteShippingMethods(["casm_123"])
|
||||
await cartModuleService.deleteShippingMethods(["casm_123"])
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -36,12 +36,11 @@ const cart = await cartModuleService.createCarts({
|
||||
Apply promotions or discounts to line items and shipping methods by adding adjustment lines that are factored into their subtotals.
|
||||
|
||||
```ts
|
||||
const lineAdjustments =
|
||||
await cartModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
code: "50OFF",
|
||||
amount: 500,
|
||||
})
|
||||
const lineAdjustments = await cartModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
code: "50OFF",
|
||||
amount: 500,
|
||||
})
|
||||
|
||||
const shippingAdjustments =
|
||||
await cartModuleService.addShippingMethodAdjustments({
|
||||
@@ -72,61 +71,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CART)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const cartModuleService: ICartModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
res.json({
|
||||
carts: await cartModuleService.listCarts(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
carts: await cartModuleService.listCarts(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const cartModuleService: ICartModuleService =
|
||||
container.resolve(ModuleRegistrationName.CART)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const cartModuleService: ICartModuleService = container.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const carts = await cartModuleService.listCarts()
|
||||
}
|
||||
```
|
||||
const carts = await cartModuleService.listCarts()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const cartModuleService: ICartModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const carts = await cartModuleService.listCarts()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const cartModuleService: ICartModuleService = container.resolve(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const carts = await cartModuleService.listCarts()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,43 +13,41 @@ In this guide, you’ll find common examples of how you can use the Currency Mod
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const currencyModuleService: ICurrencyModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CURRENCY)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const currencyModuleService: ICurrencyModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CURRENCY
|
||||
)
|
||||
|
||||
res.json({
|
||||
currencies: await currencyModuleService.listCurrencies(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
currencies: await currencyModuleService.listCurrencies(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCurrencyModule,
|
||||
} from "@medusajs/currency"
|
||||
import { initialize as initializeCurrencyModule } from "@medusajs/currency"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const currencyModuleService =
|
||||
await initializeCurrencyModule()
|
||||
export async function GET(request: Request) {
|
||||
const currencyModuleService = await initializeCurrencyModule()
|
||||
|
||||
return NextResponse.json({
|
||||
currencies: await currencyModuleService.listCurrencies(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
currencies: await currencyModuleService.listCurrencies(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -61,48 +59,43 @@ In this guide, you’ll find common examples of how you can use the Currency Mod
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const currencyModuleService: ICurrencyModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CURRENCY)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const currencyModuleService: ICurrencyModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CURRENCY
|
||||
)
|
||||
|
||||
const currency = await currencyModuleService
|
||||
.retrieveCurrency("usd")
|
||||
const currency = await currencyModuleService.retrieveCurrency("usd")
|
||||
|
||||
res.json({
|
||||
currency,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
currency,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCurrencyModule,
|
||||
} from "@medusajs/currency"
|
||||
import { initialize as initializeCurrencyModule } from "@medusajs/currency"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const currencyModuleService = await initializeCurrencyModule()
|
||||
export async function GET(request: Request) {
|
||||
const currencyModuleService = await initializeCurrencyModule()
|
||||
|
||||
const currency = await currencyModuleService
|
||||
.retrieveCurrency("usd")
|
||||
const currency = await currencyModuleService.retrieveCurrency("usd")
|
||||
|
||||
return NextResponse.json({ currency })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ currency })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -15,9 +15,7 @@ The Currency Module is the `@medusajs/currency` NPM package that provides curren
|
||||
List and retrieve currencies stored in your application.
|
||||
|
||||
```ts
|
||||
const currency = await currencyModuleService.retrieveCurrency(
|
||||
"usd"
|
||||
)
|
||||
const currency = await currencyModuleService.retrieveCurrency("usd")
|
||||
```
|
||||
|
||||
### Support Currencies in Modules
|
||||
@@ -27,9 +25,7 @@ Other commerce modules use currency codes in their data models or operations. Yo
|
||||
An example with the Region Module:
|
||||
|
||||
```ts
|
||||
const region = await regionModuleService.retrieveCurrency(
|
||||
"reg_123"
|
||||
)
|
||||
const region = await regionModuleService.retrieveCurrency("reg_123")
|
||||
const currency = await currencyModuleService.retrieveCurrency(
|
||||
region.currency_code
|
||||
)
|
||||
@@ -46,63 +42,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const currencyModuleService: ICurrencyModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.CURRENCY)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const currencyModuleService: ICurrencyModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.CURRENCY
|
||||
)
|
||||
|
||||
res.json({
|
||||
currencies: await currencyModuleService.listCurrencies(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
currencies: await currencyModuleService.listCurrencies(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const currencyModuleService: ICurrencyModuleService =
|
||||
container.resolve(ModuleRegistrationName.CURRENCY)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const currencyModuleService: ICurrencyModuleService = container.resolve(
|
||||
ModuleRegistrationName.CURRENCY
|
||||
)
|
||||
|
||||
const currencies = await currencyModuleService
|
||||
.listCurrencies()
|
||||
}
|
||||
```
|
||||
const currencies = await currencyModuleService.listCurrencies()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const currencyModuleService: ICurrencyModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.CURRENCY
|
||||
)
|
||||
|
||||
const currencies = await currencyModuleService
|
||||
.listCurrencies()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const currencyModuleService: ICurrencyModuleService = container.resolve(
|
||||
ModuleRegistrationName.CURRENCY
|
||||
)
|
||||
|
||||
const currencies = await currencyModuleService.listCurrencies()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,54 +13,50 @@ In this guide, you’ll find common examples of how you can use the Customer Mod
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const customerModuleService: ICustomerModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const customer = await customerModuleService.createCustomers({
|
||||
first_name: "Peter",
|
||||
last_name: "Hayes",
|
||||
email: "peter.hayes@example.com",
|
||||
})
|
||||
const customer = await customerModuleService.createCustomers({
|
||||
first_name: "Peter",
|
||||
last_name: "Hayes",
|
||||
email: "peter.hayes@example.com",
|
||||
})
|
||||
|
||||
res.json({
|
||||
customer,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
customer,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCustomerModule,
|
||||
} from "@medusajs/customer"
|
||||
import { initialize as initializeCustomerModule } from "@medusajs/customer"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
|
||||
const customer = await customerModuleService.createCustomers({
|
||||
first_name: "Peter",
|
||||
last_name: "Hayes",
|
||||
email: "peter.hayes@example.com",
|
||||
})
|
||||
const customer = await customerModuleService.createCustomers({
|
||||
first_name: "Peter",
|
||||
last_name: "Hayes",
|
||||
email: "peter.hayes@example.com",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
customer,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
customer,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -72,52 +68,46 @@ In this guide, you’ll find common examples of how you can use the Customer Mod
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const customerModuleService: ICustomerModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const customerGroup =
|
||||
await customerModuleService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
const customerGroup = await customerModuleService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
|
||||
res.json({
|
||||
customer_group: customerGroup,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
customer_group: customerGroup,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCustomerModule,
|
||||
} from "@medusajs/customer"
|
||||
import { initialize as initializeCustomerModule } from "@medusajs/customer"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
|
||||
const customerGroup =
|
||||
await customerModuleService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
const customerGroup = await customerModuleService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
customer_group: customerGroup,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
customer_group: customerGroup,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -129,48 +119,44 @@ In this guide, you’ll find common examples of how you can use the Customer Mod
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const customerModuleService: ICustomerModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await customerModuleService.addCustomerToGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
await customerModuleService.addCustomerToGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeCustomerModule,
|
||||
} from "@medusajs/customer"
|
||||
import { initialize as initializeCustomerModule } from "@medusajs/customer"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
|
||||
await customerModuleService.addCustomerToGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
await customerModuleService.addCustomerToGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
|
||||
return NextResponse.json({}, { status: 200 })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({}, { status: 200 })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -182,49 +168,45 @@ In this guide, you’ll find common examples of how you can use the Customer Mod
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const customerModuleService: ICustomerModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await customerModuleService.removeCustomerFromGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
await customerModuleService.removeCustomerFromGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
import {
|
||||
initialize as initializeCustomerModule,
|
||||
} from "@medusajs/customer"
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
import { initialize as initializeCustomerModule } from "@medusajs/customer"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
export async function POST(request: Request) {
|
||||
const customerModuleService = await initializeCustomerModule()
|
||||
|
||||
await customerModuleService.removeCustomerFromGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
await customerModuleService.removeCustomerFromGroup({
|
||||
customer_id: "cus_123",
|
||||
customer_group_id: "cusgroup_123",
|
||||
})
|
||||
|
||||
return NextResponse.json({}, { status: 200 })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({}, { status: 200 })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -27,10 +27,9 @@ const customer = await customerModuleService.createCustomers({
|
||||
You can organize customers into groups. This has a lot of benefits and supports more use cases, such as provide discounts for specific customer groups using the Promotion Module.
|
||||
|
||||
```ts
|
||||
const customerGroup =
|
||||
await customerModuleService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
const customerGroup = await customerModuleService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
|
||||
await customerModuleService.addCustomerToGroup({
|
||||
customer_id: "cus_123",
|
||||
@@ -49,59 +48,55 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
export async function GET(request: MedusaRequest, res: MedusaResponse) {
|
||||
const customerModuleService: ICustomerModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
res.json({
|
||||
customers: await customerModuleService.listCustomers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
customers: await customerModuleService.listCustomers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
container.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const customerModuleService: ICustomerModuleService = container.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const customers = await customerModuleService.listCustomers()
|
||||
}
|
||||
```
|
||||
const customers = await customerModuleService.listCustomers()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const customerModuleService: ICustomerModuleService =
|
||||
container.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const customerModuleService: ICustomerModuleService = container.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const customers = await customerModuleService.listCustomers()
|
||||
})
|
||||
```
|
||||
const customers = await customerModuleService.listCustomers()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -15,26 +15,25 @@ The Fulfillment Module is the `@medusajs/fulfillment` NPM package that provides
|
||||
Create fulfillments and keep track of their status, items, and more.
|
||||
|
||||
```ts
|
||||
const fulfillment =
|
||||
await fulfillmentModuleService.createFulfillment({
|
||||
location_id: "loc_123",
|
||||
provider_id: "webshipper",
|
||||
delivery_address: {
|
||||
country_code: "us",
|
||||
city: "Strongsville",
|
||||
address_1: "18290 Royalton Rd",
|
||||
const fulfillment = await fulfillmentModuleService.createFulfillment({
|
||||
location_id: "loc_123",
|
||||
provider_id: "webshipper",
|
||||
delivery_address: {
|
||||
country_code: "us",
|
||||
city: "Strongsville",
|
||||
address_1: "18290 Royalton Rd",
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Shirt",
|
||||
sku: "SHIRT",
|
||||
quantity: 1,
|
||||
barcode: "123456",
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Shirt",
|
||||
sku: "SHIRT",
|
||||
quantity: 1,
|
||||
barcode: "123456",
|
||||
},
|
||||
],
|
||||
labels: [],
|
||||
order: {},
|
||||
})
|
||||
],
|
||||
labels: [],
|
||||
order: {},
|
||||
})
|
||||
```
|
||||
|
||||
### Integrate Third-Party Fulfillment Providers
|
||||
@@ -42,11 +41,10 @@ const fulfillment =
|
||||
Use third-party fulfillment providers to provide customers with shipping options and fulfill their orders.
|
||||
|
||||
```ts
|
||||
const shippingOption =
|
||||
await fulfillmentModuleService.createShippingOptions({
|
||||
// ...
|
||||
provider_id: "webshipper",
|
||||
})
|
||||
const shippingOption = await fulfillmentModuleService.createShippingOptions({
|
||||
// ...
|
||||
provider_id: "webshipper",
|
||||
})
|
||||
```
|
||||
|
||||
### Restrict By Location and Rules
|
||||
@@ -54,33 +52,31 @@ const shippingOption =
|
||||
Shipping options can be restricted to specific geographical locations. You can also specify custom rules to restrict shipping options.
|
||||
|
||||
```ts
|
||||
const serviceZone =
|
||||
await fulfillmentModuleService.createServiceZones({
|
||||
name: "US",
|
||||
fulfillment_set_id: "fset_123",
|
||||
geo_zones: [
|
||||
{
|
||||
type: "country",
|
||||
country_code: "us",
|
||||
},
|
||||
],
|
||||
})
|
||||
const serviceZone = await fulfillmentModuleService.createServiceZones({
|
||||
name: "US",
|
||||
fulfillment_set_id: "fset_123",
|
||||
geo_zones: [
|
||||
{
|
||||
type: "country",
|
||||
country_code: "us",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const shippingOption =
|
||||
await fulfillmentModuleService.createShippingOptions({
|
||||
name: "Express Shipping",
|
||||
// restrict location
|
||||
service_zone_id: serviceZone.id,
|
||||
// restrict by custom rules
|
||||
rules: [
|
||||
{
|
||||
field: "customer_group",
|
||||
operator: "eq",
|
||||
value: "vip",
|
||||
},
|
||||
],
|
||||
// ...
|
||||
})
|
||||
const shippingOption = await fulfillmentModuleService.createShippingOptions({
|
||||
name: "Express Shipping",
|
||||
// restrict location
|
||||
service_zone_id: serviceZone.id,
|
||||
// restrict by custom rules
|
||||
rules: [
|
||||
{
|
||||
field: "customer_group",
|
||||
operator: "eq",
|
||||
value: "vip",
|
||||
},
|
||||
],
|
||||
// ...
|
||||
})
|
||||
```
|
||||
|
||||
### Support Different Fulfillment Forms
|
||||
@@ -88,18 +84,16 @@ const shippingOption =
|
||||
Support various fulfillment forms, such as shipping or pick up.
|
||||
|
||||
```ts
|
||||
const fulfillmentSets = await fulfillmentModuleService.createFulfillmentSets(
|
||||
[
|
||||
{
|
||||
name: "Shipping",
|
||||
type: "shipping",
|
||||
},
|
||||
{
|
||||
name: "Pick-up",
|
||||
type: "pick-up",
|
||||
},
|
||||
]
|
||||
)
|
||||
const fulfillmentSets = await fulfillmentModuleService.createFulfillmentSets([
|
||||
{
|
||||
name: "Shipping",
|
||||
type: "shipping",
|
||||
},
|
||||
{
|
||||
name: "Pick-up",
|
||||
type: "pick-up",
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
---
|
||||
@@ -119,64 +113,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const fulfillmentModuleService: IFulfillmentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.FULFILLMENT)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const fulfillmentModuleService: IFulfillmentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
res.json({
|
||||
fulfillments:
|
||||
await fulfillmentModuleService.listFulfillments(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
fulfillments: await fulfillmentModuleService.listFulfillments(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const fulfillmentModuleService: IFulfillmentModuleService =
|
||||
container.resolve(ModuleRegistrationName.FULFILLMENT)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const fulfillmentModuleService: IFulfillmentModuleService = container.resolve(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
const fulfillments =
|
||||
await fulfillmentModuleService.listFulfillments()
|
||||
}
|
||||
```
|
||||
const fulfillments = await fulfillmentModuleService.listFulfillments()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const fulfillmentModuleService: IFulfillmentModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
const fulfillments =
|
||||
await fulfillmentModuleService.listFulfillments()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const fulfillmentModuleService: IFulfillmentModuleService = container.resolve(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
const fulfillments = await fulfillmentModuleService.listFulfillments()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,54 +13,50 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryItem =
|
||||
await inventoryModuleService.createInventoryItems({
|
||||
sku: request.body.sku,
|
||||
title: request.body.title,
|
||||
requires_shipping: request.body.requires_shipping,
|
||||
})
|
||||
const inventoryItem = await inventoryModuleService.createInventoryItems({
|
||||
sku: request.body.sku,
|
||||
title: request.body.title,
|
||||
requires_shipping: request.body.requires_shipping,
|
||||
})
|
||||
|
||||
res.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
res.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
|
||||
const inventoryItem =
|
||||
await inventoryModuleService.createInventoryItems({
|
||||
sku: body.sku,
|
||||
title: body.title,
|
||||
requires_shipping: body.requires_shipping,
|
||||
})
|
||||
const inventoryItem = await inventoryModuleService.createInventoryItems({
|
||||
sku: body.sku,
|
||||
title: body.title,
|
||||
requires_shipping: body.requires_shipping,
|
||||
})
|
||||
|
||||
return NextResponse.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -72,45 +68,41 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryItems =
|
||||
await inventoryModuleService.listInventoryItems({})
|
||||
const inventoryItems = await inventoryModuleService.listInventoryItems({})
|
||||
|
||||
res.json({ inventory_items: inventoryItems })
|
||||
}
|
||||
```
|
||||
res.json({ inventory_items: inventoryItems })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
export async function GET(request: Request) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
|
||||
const inventoryItems =
|
||||
await inventoryModuleService.listInventoryItems({})
|
||||
const inventoryItems = await inventoryModuleService.listInventoryItems({})
|
||||
|
||||
return NextResponse.json({ inventory_items: inventoryItems })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ inventory_items: inventoryItems })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -122,58 +114,51 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryItem =
|
||||
await inventoryModuleService.retrieveInventoryItem(
|
||||
request.params.id
|
||||
)
|
||||
const inventoryItem = await inventoryModuleService.retrieveInventoryItem(
|
||||
request.params.id
|
||||
)
|
||||
|
||||
res.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
res.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
export async function GET(request: Request, { params }: ContextType) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
|
||||
const inventoryItem =
|
||||
await inventoryModuleService.retrieveInventoryItem(
|
||||
params.id
|
||||
)
|
||||
const inventoryItem = await inventoryModuleService.retrieveInventoryItem(
|
||||
params.id
|
||||
)
|
||||
|
||||
return NextResponse.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ inventory_item: inventoryItem })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -185,54 +170,50 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryLevel =
|
||||
await inventoryModuleService.createInventoryLevels({
|
||||
inventory_item_id: request.body.inventory_item_id,
|
||||
location_id: request.body.location_id,
|
||||
stocked_quantity: request.body.quantity,
|
||||
})
|
||||
const inventoryLevel = await inventoryModuleService.createInventoryLevels({
|
||||
inventory_item_id: request.body.inventory_item_id,
|
||||
location_id: request.body.location_id,
|
||||
stocked_quantity: request.body.quantity,
|
||||
})
|
||||
|
||||
res.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
res.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
|
||||
const inventoryLevel =
|
||||
await inventoryModuleService.createInventoryLevels({
|
||||
inventory_item_id: body.inventory_item_id,
|
||||
location_id: body.location_id,
|
||||
stocked_quantity: body.quantity,
|
||||
})
|
||||
const inventoryLevel = await inventoryModuleService.createInventoryLevels({
|
||||
inventory_item_id: body.inventory_item_id,
|
||||
location_id: body.location_id,
|
||||
stocked_quantity: body.quantity,
|
||||
})
|
||||
|
||||
return NextResponse.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -244,54 +225,50 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryLevel =
|
||||
await inventoryModuleService.adjustInventory(
|
||||
request.body.inventory_item_id,
|
||||
request.body.location_id,
|
||||
request.body.new_quantity
|
||||
)
|
||||
const inventoryLevel = await inventoryModuleService.adjustInventory(
|
||||
request.body.inventory_item_id,
|
||||
request.body.location_id,
|
||||
request.body.new_quantity
|
||||
)
|
||||
|
||||
res.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
res.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
|
||||
const inventoryLevel =
|
||||
await inventoryModuleService.adjustInventory(
|
||||
body.inventory_item_id,
|
||||
body.location_id,
|
||||
body.new_quantity
|
||||
)
|
||||
const inventoryLevel = await inventoryModuleService.adjustInventory(
|
||||
body.inventory_item_id,
|
||||
body.location_id,
|
||||
body.new_quantity
|
||||
)
|
||||
|
||||
return NextResponse.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ inventory_level: inventoryLevel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -303,52 +280,50 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
res.json({
|
||||
is_available: await inventoryModuleService.confirmInventory(
|
||||
request.body.inventory_item_id,
|
||||
[request.body.location_id],
|
||||
request.body.required_quantity
|
||||
),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
is_available: await inventoryModuleService.confirmInventory(
|
||||
request.body.inventory_item_id,
|
||||
[request.body.location_id],
|
||||
request.body.required_quantity
|
||||
),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
|
||||
return NextResponse.json({
|
||||
is_available: await inventoryModuleService.confirmInventory(
|
||||
body.inventory_item_id,
|
||||
[body.location_id],
|
||||
body.required_quantity
|
||||
),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
is_available: await inventoryModuleService.confirmInventory(
|
||||
body.inventory_item_id,
|
||||
[body.location_id],
|
||||
body.required_quantity
|
||||
),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -360,56 +335,52 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const reservationItem =
|
||||
await inventoryModuleService.createReservationItems({
|
||||
inventory_item_id: request.body.inventory_item_id,
|
||||
location_id: request.body.location_id,
|
||||
quantity: request.body.reserved_quantity,
|
||||
})
|
||||
const reservationItem = await inventoryModuleService.createReservationItems({
|
||||
inventory_item_id: request.body.inventory_item_id,
|
||||
location_id: request.body.location_id,
|
||||
quantity: request.body.reserved_quantity,
|
||||
})
|
||||
|
||||
res.json({ reservation_item: reservationItem })
|
||||
}
|
||||
```
|
||||
res.json({ reservation_item: reservationItem })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
const body = await request.json()
|
||||
|
||||
const reservationItem =
|
||||
await inventoryModuleService.createReservationItems({
|
||||
inventory_item_id: body.inventory_item_id,
|
||||
location_id: body.location_id,
|
||||
quantity: body.reserved_quantity,
|
||||
})
|
||||
const reservationItem = await inventoryModuleService.createReservationItems({
|
||||
inventory_item_id: body.inventory_item_id,
|
||||
location_id: body.location_id,
|
||||
quantity: body.reserved_quantity,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
reservation_item: reservationItem,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
reservation_item: reservationItem,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -421,89 +392,82 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const reservedQuantity =
|
||||
await inventoryModuleService.retrieveReservedQuantity(
|
||||
request.params.inventory_item_id,
|
||||
[request.params.location_id]
|
||||
)
|
||||
const stockedQuantity =
|
||||
await inventoryModuleService.retrieveStockedQuantity(
|
||||
request.params.inventory_item_id,
|
||||
[request.params.location_id]
|
||||
)
|
||||
const availableQuantity =
|
||||
await inventoryModuleService.retrieveAvailableQuantity(
|
||||
request.params.inventory_item_id,
|
||||
[request.params.location_id]
|
||||
)
|
||||
const reservedQuantity =
|
||||
await inventoryModuleService.retrieveReservedQuantity(
|
||||
request.params.inventory_item_id,
|
||||
[request.params.location_id]
|
||||
)
|
||||
const stockedQuantity = await inventoryModuleService.retrieveStockedQuantity(
|
||||
request.params.inventory_item_id,
|
||||
[request.params.location_id]
|
||||
)
|
||||
const availableQuantity =
|
||||
await inventoryModuleService.retrieveAvailableQuantity(
|
||||
request.params.inventory_item_id,
|
||||
[request.params.location_id]
|
||||
)
|
||||
|
||||
res.json({
|
||||
reserved_quantity: reservedQuantity,
|
||||
stocked_quantity: stockedQuantity,
|
||||
available_quantity: availableQuantity,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
reserved_quantity: reservedQuantity,
|
||||
stocked_quantity: stockedQuantity,
|
||||
available_quantity: availableQuantity,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
inventory_item_id: string
|
||||
location_id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
inventory_item_id: string
|
||||
location_id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
|
||||
const reservedQuantity =
|
||||
await inventoryModuleService.retrieveReservedQuantity(
|
||||
params.inventory_item_id,
|
||||
[params.location_id]
|
||||
)
|
||||
const stockedQuantity =
|
||||
await inventoryModuleService.retrieveStockedQuantity(
|
||||
params.inventory_item_id,
|
||||
[params.location_id]
|
||||
)
|
||||
const availableQuantity =
|
||||
await inventoryModuleService.retrieveAvailableQuantity(
|
||||
params.inventory_item_id,
|
||||
[params.location_id]
|
||||
)
|
||||
const reservedQuantity =
|
||||
await inventoryModuleService.retrieveReservedQuantity(
|
||||
params.inventory_item_id,
|
||||
[params.location_id]
|
||||
)
|
||||
const stockedQuantity = await inventoryModuleService.retrieveStockedQuantity(
|
||||
params.inventory_item_id,
|
||||
[params.location_id]
|
||||
)
|
||||
const availableQuantity =
|
||||
await inventoryModuleService.retrieveAvailableQuantity(
|
||||
params.inventory_item_id,
|
||||
[params.location_id]
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
reserved_quantity: reservedQuantity,
|
||||
stocked_quantity: stockedQuantity,
|
||||
available_quantity: availableQuantity,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
reserved_quantity: reservedQuantity,
|
||||
stocked_quantity: stockedQuantity,
|
||||
available_quantity: availableQuantity,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -515,52 +479,45 @@ In this document, you’ll find common examples of how you can use the Inventory
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
await inventoryModuleService.deleteReservationItems([
|
||||
request.params.reservation_id,
|
||||
])
|
||||
await inventoryModuleService.deleteReservationItems([
|
||||
request.params.reservation_id,
|
||||
])
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import {
|
||||
initialize as initializeInventoryModule,
|
||||
} from "@medusajs/inventory-next"
|
||||
```ts
|
||||
import { initialize as initializeInventoryModule } from "@medusajs/inventory-next"
|
||||
|
||||
type ContextType = {
|
||||
params: {
|
||||
reservation_id: string
|
||||
}
|
||||
type ContextType = {
|
||||
params: {
|
||||
reservation_id: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const inventoryModuleService =
|
||||
await initializeInventoryModule({})
|
||||
export async function DELETE(request: Request, { params }: ContextType) {
|
||||
const inventoryModuleService = await initializeInventoryModule({})
|
||||
|
||||
await inventoryModuleService.deleteReservationItems([
|
||||
params.reservation_id,
|
||||
])
|
||||
}
|
||||
```
|
||||
await inventoryModuleService.deleteReservationItems([params.reservation_id])
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -17,12 +17,11 @@ Store and manage inventory of any stock-kept item, such as product variants.
|
||||
Inventory items hold details of the underlying stock-kept item, as well as inventory details such as whether the item requires shipping.
|
||||
|
||||
```ts
|
||||
const inventoryItem =
|
||||
await inventoryModuleService.createInventoryItems({
|
||||
sku: "SHIRT",
|
||||
title: "Green Medusa Shirt",
|
||||
requires_shipping: true,
|
||||
})
|
||||
const inventoryItem = await inventoryModuleService.createInventoryItems({
|
||||
sku: "SHIRT",
|
||||
title: "Green Medusa Shirt",
|
||||
requires_shipping: true,
|
||||
})
|
||||
```
|
||||
|
||||
### Inventory Across Locations
|
||||
@@ -30,12 +29,13 @@ const inventoryItem =
|
||||
Inventory items' quantities are set per locations through inventory levels. This gives you more flexibility in managing the quantity of a stock-kept item across different locations, such as different warehouses.
|
||||
|
||||
```ts
|
||||
const inventoryLevel =
|
||||
await inventoryModuleService.createInventoryLevels([{
|
||||
const inventoryLevel = await inventoryModuleService.createInventoryLevels([
|
||||
{
|
||||
inventory_item_id: "iitem_123",
|
||||
location_id: "sloc_123",
|
||||
stocked_quantity: 20,
|
||||
}])
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
### Reservation Management
|
||||
@@ -43,12 +43,13 @@ const inventoryLevel =
|
||||
Reserve quantities of inventory items at specific locations for orders or other purposes. The reserved quantity isn't considered for purchase, but can be deleted to revert the reservation.
|
||||
|
||||
```ts
|
||||
const reservationItem =
|
||||
await inventoryModuleService.createReservationItems([{
|
||||
const reservationItem = await inventoryModuleService.createReservationItems([
|
||||
{
|
||||
inventory_item_id: "iitem_123",
|
||||
location_id: "sloc_123",
|
||||
quantity: 10,
|
||||
}])
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
### Check Inventory Availability
|
||||
@@ -56,12 +57,11 @@ const reservationItem =
|
||||
Check whether an inventory item has the necessary quantity for purchase. Any reserved quantity is considered unavailable.
|
||||
|
||||
```ts
|
||||
const isAvailable =
|
||||
await inventoryModuleService.confirmInventory(
|
||||
inventoryItemId,
|
||||
[locationId],
|
||||
neededQuantity
|
||||
)
|
||||
const isAvailable = await inventoryModuleService.confirmInventory(
|
||||
inventoryItemId,
|
||||
[locationId],
|
||||
neededQuantity
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
@@ -75,62 +75,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
request.scope.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const inventoryModuleService: IInventoryService = request.scope.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
res.json({
|
||||
inventory_items:
|
||||
await inventoryModuleService.listInventoryItems({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
inventory_items: await inventoryModuleService.listInventoryItems({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
container.resolve(ModuleRegistrationName.INVENTORY)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const inventoryModuleService: IInventoryService = container.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryItems =
|
||||
await inventoryModuleService.listInventoryItems({})
|
||||
}
|
||||
```
|
||||
const inventoryItems = await inventoryModuleService.listInventoryItems({})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const inventoryModuleService: IInventoryService =
|
||||
container.resolve(ModuleRegistrationName.INVENTORY)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const inventoryModuleService: IInventoryService = container.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const inventoryItems =
|
||||
await inventoryModuleService.listInventoryItems({})
|
||||
})
|
||||
```
|
||||
const inventoryItems = await inventoryModuleService.listInventoryItems({})
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -50,12 +50,11 @@ const draftOrder = await orderModuleService.createOrders({
|
||||
Apply promotions or discounts to the order's items and shipping methods by adding adjustment lines that are factored into their subtotals.
|
||||
|
||||
```ts
|
||||
const lineAdjustments =
|
||||
await orderModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
code: "50OFF",
|
||||
amount: 500,
|
||||
})
|
||||
const lineAdjustments = await orderModuleService.addLineItemAdjustments({
|
||||
item_id: "cali_123",
|
||||
code: "50OFF",
|
||||
amount: 500,
|
||||
})
|
||||
|
||||
const shippingAdjustments =
|
||||
await orderModuleService.addShippingMethodAdjustments({
|
||||
@@ -78,10 +77,9 @@ Orders can be changed to return items from the customer, exchange an item with a
|
||||
Changes are only applied after confirmation, and order history is preserved through versioning.
|
||||
|
||||
```ts
|
||||
const orderChange =
|
||||
await orderModuleService.createOrderChange({
|
||||
order_id: "ord_123",
|
||||
})
|
||||
const orderChange = await orderModuleService.createOrderChange({
|
||||
order_id: "ord_123",
|
||||
})
|
||||
|
||||
// add item to the order
|
||||
await orderModuleService.addOrderAction({
|
||||
@@ -109,60 +107,57 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const orderModuleService: IOrderModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.ORDER)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const orderModuleService: IOrderModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
res.json({
|
||||
orders: await orderModuleService.listOrders(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
orders: await orderModuleService.listOrders(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const orderModuleService: IOrderModuleService =
|
||||
container.resolve(ModuleRegistrationName.ORDER)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const orderModuleService: IOrderModuleService = container.resolve(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
const orders = await orderModuleService.listOrders()
|
||||
}
|
||||
```
|
||||
const orders = await orderModuleService.listOrders()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const orderModuleService: IOrderModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
const orders = await orderModuleService.listOrders()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const orderModuleService: IOrderModuleService = container.resolve(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
const orders = await orderModuleService.listOrders()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,56 +13,57 @@ In this guide, you’ll find common examples of how you can use the Payment Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const paymentCollection =
|
||||
await paymentModuleService.createPaymentCollections({
|
||||
region_id: "reg_123",
|
||||
currency_code: "usd",
|
||||
amount: 4000,
|
||||
})
|
||||
const paymentCollection = await paymentModuleService.createPaymentCollections(
|
||||
{
|
||||
region_id: "reg_123",
|
||||
currency_code: "usd",
|
||||
amount: 4000,
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
payment_collection: paymentCollection,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payment_collection: paymentCollection,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePaymentModule,
|
||||
} from "@medusajs/payment"
|
||||
import { initialize as initializePaymentModule } from "@medusajs/payment"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
|
||||
const paymentCollection =
|
||||
await paymentModuleService.createPaymentCollections({
|
||||
region_id: "reg_123",
|
||||
currency_code: "usd",
|
||||
amount: 4000,
|
||||
})
|
||||
const paymentCollection = await paymentModuleService.createPaymentCollections(
|
||||
{
|
||||
region_id: "reg_123",
|
||||
currency_code: "usd",
|
||||
amount: 4000,
|
||||
}
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
payment_collection: paymentCollection,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
payment_collection: paymentCollection,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -74,64 +75,61 @@ In this guide, you’ll find common examples of how you can use the Payment Modu
|
||||
<CodeTabs groupId="app-type" isCodeCodeTabs={true}>
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const paymentSession =
|
||||
await paymentModuleService.createPaymentSession(
|
||||
"pay_col_123",
|
||||
{
|
||||
currency_code: "usd",
|
||||
provider_id: "system",
|
||||
amount: 4000,
|
||||
data: {},
|
||||
}
|
||||
)
|
||||
const paymentSession = await paymentModuleService.createPaymentSession(
|
||||
"pay_col_123",
|
||||
{
|
||||
currency_code: "usd",
|
||||
provider_id: "system",
|
||||
amount: 4000,
|
||||
data: {},
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
payment_session: paymentSession,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payment_session: paymentSession,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePaymentModule,
|
||||
} from "@medusajs/payment"
|
||||
import { initialize as initializePaymentModule } from "@medusajs/payment"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
|
||||
const paymentSession =
|
||||
await paymentModuleService.createPaymentSession(
|
||||
"pay_col_123",
|
||||
{
|
||||
currency_code: "usd",
|
||||
provider_id: "system",
|
||||
amount: 4000,
|
||||
data: {},
|
||||
}
|
||||
)
|
||||
const paymentSession = await paymentModuleService.createPaymentSession(
|
||||
"pay_col_123",
|
||||
{
|
||||
currency_code: "usd",
|
||||
provider_id: "system",
|
||||
amount: 4000,
|
||||
data: {},
|
||||
}
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
payment_session: paymentSession,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
payment_session: paymentSession,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -143,54 +141,49 @@ In this guide, you’ll find common examples of how you can use the Payment Modu
|
||||
<CodeTabs groupId="app-type" isCodeCodeTabs={true}>
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const paymentSessions =
|
||||
await paymentModuleService.listPaymentSessions({
|
||||
payment_collection_id: ["pay_col_123"],
|
||||
})
|
||||
const paymentSessions = await paymentModuleService.listPaymentSessions({
|
||||
payment_collection_id: ["pay_col_123"],
|
||||
})
|
||||
|
||||
res.json({
|
||||
payment_sessions: paymentSessions,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payment_sessions: paymentSessions,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePaymentModule,
|
||||
} from "@medusajs/payment"
|
||||
import { initialize as initializePaymentModule } from "@medusajs/payment"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
|
||||
const paymentSessions =
|
||||
await paymentModuleService.listPaymentSessions({
|
||||
payment_collection_id: ["pay_col_123"],
|
||||
})
|
||||
const paymentSessions = await paymentModuleService.listPaymentSessions({
|
||||
payment_collection_id: ["pay_col_123"],
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
payment_sessions: paymentSessions,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
payment_sessions: paymentSessions,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -202,56 +195,51 @@ In this guide, you’ll find common examples of how you can use the Payment Modu
|
||||
<CodeTabs groupId="app-type" isCodeCodeTabs={true}>
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const payment =
|
||||
await paymentModuleService.authorizePaymentSession(
|
||||
"payses_123",
|
||||
{}
|
||||
)
|
||||
const payment = await paymentModuleService.authorizePaymentSession(
|
||||
"payses_123",
|
||||
{}
|
||||
)
|
||||
|
||||
res.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePaymentModule,
|
||||
} from "@medusajs/payment"
|
||||
import { initialize as initializePaymentModule } from "@medusajs/payment"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
|
||||
const payment =
|
||||
await paymentModuleService.authorizePaymentSession(
|
||||
"payses_123",
|
||||
{}
|
||||
)
|
||||
const payment = await paymentModuleService.authorizePaymentSession(
|
||||
"payses_123",
|
||||
{}
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -263,52 +251,49 @@ In this guide, you’ll find common examples of how you can use the Payment Modu
|
||||
<CodeTabs groupId="app-type" isCodeCodeTabs={true}>
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const payments = await paymentModuleService.listPayments({
|
||||
session_id: "payses_123",
|
||||
})
|
||||
const payments = await paymentModuleService.listPayments({
|
||||
session_id: "payses_123",
|
||||
})
|
||||
|
||||
res.json({
|
||||
payments,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payments,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePaymentModule,
|
||||
} from "@medusajs/payment"
|
||||
import { initialize as initializePaymentModule } from "@medusajs/payment"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
export async function GET(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
|
||||
const payments = await paymentModuleService.listPayments({
|
||||
session_id: "payses_123",
|
||||
})
|
||||
const payments = await paymentModuleService.listPayments({
|
||||
session_id: "payses_123",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
payments,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
payments,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -320,52 +305,49 @@ In this guide, you’ll find common examples of how you can use the Payment Modu
|
||||
<CodeTabs groupId="app-type" isCodeCodeTabs={true}>
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const payment = await paymentModuleService.capturePayment({
|
||||
payment_id: "pay_123",
|
||||
})
|
||||
const payment = await paymentModuleService.capturePayment({
|
||||
payment_id: "pay_123",
|
||||
})
|
||||
|
||||
res.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePaymentModule,
|
||||
} from "@medusajs/payment"
|
||||
import { initialize as initializePaymentModule } from "@medusajs/payment"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
export async function POST(request: Request) {
|
||||
const paymentModuleService = await initializePaymentModule()
|
||||
|
||||
const payment = await paymentModuleService.capturePayment({
|
||||
payment_id: "pay_123",
|
||||
})
|
||||
const payment = await paymentModuleService.capturePayment({
|
||||
payment_id: "pay_123",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
payment,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -18,12 +18,11 @@ The Payment Module provides payment functionalities that allow you to process pa
|
||||
All payment processing starts with creating a payment collection.
|
||||
|
||||
```ts
|
||||
const paymentCollection =
|
||||
await paymentModuleService.createPaymentCollections({
|
||||
region_id: "reg_123",
|
||||
currency_code: "usd",
|
||||
amount: 5000,
|
||||
})
|
||||
const paymentCollection = await paymentModuleService.createPaymentCollections({
|
||||
region_id: "reg_123",
|
||||
currency_code: "usd",
|
||||
amount: 5000,
|
||||
})
|
||||
```
|
||||
|
||||
### Authorize, Capture, and Refund Payment
|
||||
@@ -41,18 +40,14 @@ await paymentModuleService.capturePayment({
|
||||
Use payment providers like Stripe to handle and process payments.
|
||||
|
||||
```ts
|
||||
const payment =
|
||||
await paymentModuleService.createPaymentSession(
|
||||
"pay_col_1",
|
||||
{
|
||||
provider_id: "stripe",
|
||||
amount: 1000,
|
||||
currency_code: "usd",
|
||||
data: {
|
||||
// necessary data for the payment provider
|
||||
},
|
||||
}
|
||||
)
|
||||
const payment = await paymentModuleService.createPaymentSession("pay_col_1", {
|
||||
provider_id: "stripe",
|
||||
amount: 1000,
|
||||
currency_code: "usd",
|
||||
data: {
|
||||
// necessary data for the payment provider
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
### Handle Webhook Events
|
||||
@@ -85,64 +80,60 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const paymentModuleService: IPaymentModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
res.json({
|
||||
payment_collections:
|
||||
await paymentModuleService.listPaymentCollections(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
payment_collections: await paymentModuleService.listPaymentCollections(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
container.resolve(ModuleRegistrationName.PAYMENT)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const paymentModuleService: IPaymentModuleService = container.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const payment_collections =
|
||||
await paymentModuleService.listPaymentCollections()
|
||||
}
|
||||
```
|
||||
const payment_collections =
|
||||
await paymentModuleService.listPaymentCollections()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const paymentModuleService: IPaymentModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const paymentModuleService: IPaymentModuleService = container.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const payment_collections =
|
||||
await paymentModuleService.listPaymentCollections()
|
||||
})
|
||||
```
|
||||
const payment_collections =
|
||||
await paymentModuleService.listPaymentCollections()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,67 +13,66 @@ In this document, you’ll find common examples of how you can use the Pricing M
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSet = await pricingModuleService.createPriceSets([
|
||||
{
|
||||
prices: [
|
||||
{
|
||||
currency_code: "eur",
|
||||
amount: 10,
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
const priceSet = await pricingModuleService.createPriceSets([
|
||||
{
|
||||
prices: [
|
||||
{
|
||||
currency_code: "eur",
|
||||
amount: 10,
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
res.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
res.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePricingModule,
|
||||
} from "@medusajs/pricing"
|
||||
import { initialize as initializePricingModule } from "@medusajs/pricing"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
const body = await request.json()
|
||||
|
||||
const priceSet = await pricingModuleService.createPriceSets([
|
||||
{
|
||||
prices: [
|
||||
{
|
||||
currency_code: "eur",
|
||||
amount: 10,
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
const priceSet = await pricingModuleService.createPriceSets([
|
||||
{
|
||||
prices: [
|
||||
{
|
||||
currency_code: "eur",
|
||||
amount: 10,
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
return NextResponse.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -85,42 +84,41 @@ In this document, you’ll find common examples of how you can use the Pricing M
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
|
||||
res.json({ price_sets: priceSets })
|
||||
}
|
||||
```
|
||||
res.json({ price_sets: priceSets })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePricingModule,
|
||||
} from "@medusajs/pricing"
|
||||
import { initialize as initializePricingModule } from "@medusajs/pricing"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
export async function GET(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
|
||||
return NextResponse.json({ price_sets: priceSets })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ price_sets: priceSets })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -132,48 +130,41 @@ In this document, you’ll find common examples of how you can use the Pricing M
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSet = await pricingModuleService.retrievePriceSet(
|
||||
"pset_123"
|
||||
)
|
||||
const priceSet = await pricingModuleService.retrievePriceSet("pset_123")
|
||||
|
||||
res.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
res.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePricingModule,
|
||||
} from "@medusajs/pricing"
|
||||
import { initialize as initializePricingModule } from "@medusajs/pricing"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
export async function GET(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
|
||||
const priceSet = await pricingModuleService.retrievePriceSet(
|
||||
"pset_123"
|
||||
)
|
||||
const priceSet = await pricingModuleService.retrievePriceSet("pset_123")
|
||||
|
||||
return NextResponse.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -185,65 +176,64 @@ In this document, you’ll find common examples of how you can use the Pricing M
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSet = await pricingModuleService.addPrices({
|
||||
priceSetId: "pset_123",
|
||||
prices: [
|
||||
{
|
||||
amount: 500,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
const priceSet = await pricingModuleService.addPrices({
|
||||
priceSetId: "pset_123",
|
||||
prices: [
|
||||
{
|
||||
amount: 500,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
],
|
||||
})
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
res.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
res.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePricingModule,
|
||||
} from "@medusajs/pricing"
|
||||
import { initialize as initializePricingModule } from "@medusajs/pricing"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
const body = await request.json()
|
||||
|
||||
const priceSet = await pricingModuleService.addPrices({
|
||||
priceSetId: "pset_123",
|
||||
prices: [
|
||||
{
|
||||
amount: 500,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
const priceSet = await pricingModuleService.addPrices({
|
||||
priceSetId: "pset_123",
|
||||
prices: [
|
||||
{
|
||||
amount: 500,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "reg_123",
|
||||
},
|
||||
],
|
||||
})
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
return NextResponse.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ price_set: priceSet })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -255,83 +245,80 @@ In this document, you’ll find common examples of how you can use the Pricing M
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { PriceListType } from "@medusajs/utils"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { PriceListType } from "@medusajs/utils"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceLists =
|
||||
await pricingModuleService.createPriceLists([
|
||||
const priceLists = await pricingModuleService.createPriceLists([
|
||||
{
|
||||
title: "My Sale",
|
||||
description: "This is my sale",
|
||||
type: PriceListType.SALE,
|
||||
starts_at: Date.parse("01/10/2023").toString(),
|
||||
ends_at: Date.parse("31/10/2023").toString(),
|
||||
rules: {
|
||||
region_id: ["reg_123", "reg_321"],
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
title: "My Sale",
|
||||
description: "This is my sale",
|
||||
type: PriceListType.SALE,
|
||||
starts_at: Date.parse("01/10/2023").toString(),
|
||||
ends_at: Date.parse("31/10/2023").toString(),
|
||||
rules: {
|
||||
region_id: ["reg_123", "reg_321"],
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "pset_124",
|
||||
},
|
||||
],
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "pset_124",
|
||||
},
|
||||
])
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
res.json({ price_lists: priceLists })
|
||||
}
|
||||
```
|
||||
res.json({ price_lists: priceLists })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
import { PriceListType } from "@medusajs/medusa"
|
||||
|
||||
import {
|
||||
initialize as initializePricingModule,
|
||||
} from "@medusajs/pricing"
|
||||
import { PriceListType } from "@medusajs/utils"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
import { PriceListType } from "@medusajs/medusa"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
import { initialize as initializePricingModule } from "@medusajs/pricing"
|
||||
import { PriceListType } from "@medusajs/utils"
|
||||
|
||||
const priceLists =
|
||||
await pricingModuleService.createPriceLists([
|
||||
export async function POST(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
|
||||
const priceLists = await pricingModuleService.createPriceLists([
|
||||
{
|
||||
title: "My Sale",
|
||||
description: "This is my sale",
|
||||
type: PriceListType.SALE,
|
||||
starts_at: Date.parse("01/10/2023").toString(),
|
||||
ends_at: Date.parse("31/10/2023").toString(),
|
||||
rules: {
|
||||
region_id: ["reg_123", "reg_321"],
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
title: "My Sale",
|
||||
description: "This is my sale",
|
||||
type: PriceListType.SALE,
|
||||
starts_at: Date.parse("01/10/2023").toString(),
|
||||
ends_at: Date.parse("31/10/2023").toString(),
|
||||
rules: {
|
||||
region_id: ["reg_123", "reg_321"],
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "pset_124",
|
||||
},
|
||||
],
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "pset_124",
|
||||
},
|
||||
])
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
return NextResponse.json({ price_lists: priceLists })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ price_lists: priceLists })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -343,62 +330,59 @@ In this document, you’ll find common examples of how you can use the Pricing M
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const price = await pricingModuleService.calculatePrices(
|
||||
{
|
||||
id: ["pset_123"],
|
||||
const price = await pricingModuleService.calculatePrices(
|
||||
{
|
||||
id: ["pset_123"],
|
||||
},
|
||||
{
|
||||
context: {
|
||||
currency_code: "eur",
|
||||
},
|
||||
{
|
||||
context: {
|
||||
currency_code: "eur",
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
res.json({ price })
|
||||
}
|
||||
```
|
||||
res.json({ price })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePricingModule,
|
||||
} from "@medusajs/pricing"
|
||||
import { initialize as initializePricingModule } from "@medusajs/pricing"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
export async function GET(request: Request) {
|
||||
const pricingModuleService = await initializePricingModule()
|
||||
|
||||
const price = await pricingModuleService.calculatePrices(
|
||||
{
|
||||
id: ["pset_123"],
|
||||
const price = await pricingModuleService.calculatePrices(
|
||||
{
|
||||
id: ["pset_123"],
|
||||
},
|
||||
{
|
||||
context: {
|
||||
currency_code: "eur",
|
||||
},
|
||||
{
|
||||
context: {
|
||||
currency_code: "eur",
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
return NextResponse.json({ price })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ price })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -24,12 +24,12 @@ const priceSet = await pricingModuleService.createPriceSets({
|
||||
currency_code: "USD",
|
||||
},
|
||||
{
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
min_quantity: 0,
|
||||
max_quantity: 4,
|
||||
rules: {},
|
||||
},
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
min_quantity: 0,
|
||||
max_quantity: 4,
|
||||
rules: {},
|
||||
},
|
||||
],
|
||||
})
|
||||
```
|
||||
@@ -58,23 +58,25 @@ const priceSet = await pricingModuleService.addPrices({
|
||||
Price lists allow you to group prices and apply them only in specific conditions. You can also use them to override existing prices for the specified conditions.
|
||||
|
||||
```ts
|
||||
const priceList = await pricingModuleService.createPriceLists([{
|
||||
title: "My Sale",
|
||||
description: "Sale on selected items.",
|
||||
type: "sale",
|
||||
starts_at: Date.parse("01/10/2023").toString(),
|
||||
ends_at: Date.parse("31/10/2023").toString(),
|
||||
rules: {
|
||||
region_id: ["reg_123", "reg_321"],
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "pset_123",
|
||||
const priceList = await pricingModuleService.createPriceLists([
|
||||
{
|
||||
title: "My Sale",
|
||||
description: "Sale on selected items.",
|
||||
type: "sale",
|
||||
starts_at: Date.parse("01/10/2023").toString(),
|
||||
ends_at: Date.parse("31/10/2023").toString(),
|
||||
rules: {
|
||||
region_id: ["reg_123", "reg_321"],
|
||||
},
|
||||
],
|
||||
}])
|
||||
prices: [
|
||||
{
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "pset_123",
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
### Price Calculation Strategy
|
||||
@@ -104,59 +106,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRICING)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const pricingModuleService: IPricingModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
res.json({
|
||||
price_sets: await pricingModuleService.listPriceSets(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
price_sets: await pricingModuleService.listPriceSets(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRICING)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const pricingModuleService: IPricingModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
}
|
||||
```
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const pricingModuleService: IPricingModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRICING)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const pricingModuleService: IPricingModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
})
|
||||
```
|
||||
const priceSets = await pricingModuleService.listPriceSets()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,80 +13,76 @@ In this guide, you’ll find common examples of how you can use the Product Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const products = await productModuleService.createProducts([
|
||||
{
|
||||
title: "Medusa Shirt",
|
||||
options: [
|
||||
{
|
||||
title: "Color",
|
||||
},
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Black Shirt",
|
||||
options: [
|
||||
{
|
||||
value: "Black",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
const products = await productModuleService.createProducts([
|
||||
{
|
||||
title: "Medusa Shirt",
|
||||
options: [
|
||||
{
|
||||
title: "Color",
|
||||
},
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Black Shirt",
|
||||
options: [
|
||||
{
|
||||
value: "Black",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
res.json({ products })
|
||||
}
|
||||
```
|
||||
res.json({ products })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeProductModule,
|
||||
} from "@medusajs/product"
|
||||
import { initialize as initializeProductModule } from "@medusajs/product"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
export async function POST(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
|
||||
const products = await productModuleService.createProducts([
|
||||
{
|
||||
title: "Medusa Shirt",
|
||||
options: [
|
||||
{
|
||||
title: "Color",
|
||||
},
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Black Shirt",
|
||||
options: [
|
||||
{
|
||||
value: "Black",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
const products = await productModuleService.createProducts([
|
||||
{
|
||||
title: "Medusa Shirt",
|
||||
options: [
|
||||
{
|
||||
title: "Color",
|
||||
},
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Black Shirt",
|
||||
options: [
|
||||
{
|
||||
value: "Black",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
return NextResponse.json({ products })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ products })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -98,42 +94,38 @@ In this guide, you’ll find common examples of how you can use the Product Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function GET(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const products = await productModuleService.listProducts()
|
||||
const products = await productModuleService.listProducts()
|
||||
|
||||
res.json({ products })
|
||||
}
|
||||
```
|
||||
res.json({ products })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeProductModule,
|
||||
} from "@medusajs/product"
|
||||
import { initialize as initializeProductModule } from "@medusajs/product"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
export async function GET(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
|
||||
const products = await productModuleService.listProducts()
|
||||
const products = await productModuleService.listProducts()
|
||||
|
||||
return NextResponse.json({ products })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ products })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -145,50 +137,42 @@ In this guide, you’ll find common examples of how you can use the Product Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function GET(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const product = await productModuleService.retrieveProduct(
|
||||
request.params.id
|
||||
)
|
||||
const product = await productModuleService.retrieveProduct(request.params.id)
|
||||
|
||||
res.json({ product })
|
||||
}
|
||||
```
|
||||
res.json({ product })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeProductModule,
|
||||
} from "@medusajs/product"
|
||||
import { initialize as initializeProductModule } from "@medusajs/product"
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Record<string, any> }) {
|
||||
|
||||
const { id } = params
|
||||
const productModuleService = await initializeProductModule()
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Record<string, any> }
|
||||
) {
|
||||
const { id } = params
|
||||
const productModuleService = await initializeProductModule()
|
||||
|
||||
const product = await productModuleService.retrieveProduct(
|
||||
id
|
||||
)
|
||||
const product = await productModuleService.retrieveProduct(id)
|
||||
|
||||
return NextResponse.json({ product })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ product })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -200,46 +184,42 @@ In this guide, you’ll find common examples of how you can use the Product Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function GET(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const data = await productModuleService.listProducts({
|
||||
handle: "shirt",
|
||||
})
|
||||
const data = await productModuleService.listProducts({
|
||||
handle: "shirt",
|
||||
})
|
||||
|
||||
res.json({ product: data[0] })
|
||||
}
|
||||
```
|
||||
res.json({ product: data[0] })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeProductModule,
|
||||
} from "@medusajs/product"
|
||||
import { initialize as initializeProductModule } from "@medusajs/product"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
export async function GET(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
|
||||
const data = await productModuleService.listProducts({
|
||||
handle: "shirt",
|
||||
})
|
||||
const data = await productModuleService.listProducts({
|
||||
handle: "shirt",
|
||||
})
|
||||
|
||||
return NextResponse.json({ product: data[0] })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ product: data[0] })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -251,44 +231,38 @@ In this guide, you’ll find common examples of how you can use the Product Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const categories = await productModuleService
|
||||
.listProductCategories()
|
||||
const categories = await productModuleService.listProductCategories()
|
||||
|
||||
res.json({ categories })
|
||||
}
|
||||
```
|
||||
res.json({ categories })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeProductModule,
|
||||
} from "@medusajs/product"
|
||||
import { initialize as initializeProductModule } from "@medusajs/product"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
export async function GET(request: Request) {
|
||||
const productModuleService = await initializeProductModule()
|
||||
|
||||
const categories = await productModuleService
|
||||
.listProductCategories()
|
||||
const categories = await productModuleService.listProductCategories()
|
||||
|
||||
return NextResponse.json({ categories })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ categories })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -300,52 +274,46 @@ In this guide, you’ll find common examples of how you can use the Product Modu
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab value="medusa" label="Medusa API Router">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function POST(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const data = await productModuleService
|
||||
.listProductCategories({
|
||||
handle: request.params.handle,
|
||||
})
|
||||
const data = await productModuleService.listProductCategories({
|
||||
handle: request.params.handle,
|
||||
})
|
||||
|
||||
res.json({ category: data[0] })
|
||||
}
|
||||
```
|
||||
res.json({ category: data[0] })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab value="nextjs" label="Next.js App Router">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeProductModule,
|
||||
} from "@medusajs/product"
|
||||
import { initialize as initializeProductModule } from "@medusajs/product"
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Record<string, any> }) {
|
||||
|
||||
const { handle } = params
|
||||
const productModuleService = await initializeProductModule()
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Record<string, any> }
|
||||
) {
|
||||
const { handle } = params
|
||||
const productModuleService = await initializeProductModule()
|
||||
|
||||
const data = await productModuleService
|
||||
.listProductCategories({
|
||||
handle,
|
||||
})
|
||||
const data = await productModuleService.listProductCategories({
|
||||
handle,
|
||||
})
|
||||
|
||||
return NextResponse.json({ category: data[0] })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ category: data[0] })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -69,59 +69,55 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export async function GET(request: MedusaRequest, res: MedusaResponse) {
|
||||
const productModuleService: IProductModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
res.json({
|
||||
products: await productModuleService.listProducts(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
products: await productModuleService.listProducts(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const productModuleService: IProductModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const products = await productModuleService.listProducts()
|
||||
}
|
||||
```
|
||||
const products = await productModuleService.listProducts()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const productModuleService: IProductModuleService = container.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const products = await productModuleService.listProducts()
|
||||
})
|
||||
```
|
||||
const products = await productModuleService.listProducts()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,62 +13,60 @@ In this document, you’ll find common examples of how you can use the Promotion
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
},
|
||||
})
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
},
|
||||
})
|
||||
|
||||
res.json({ promotion })
|
||||
}
|
||||
```
|
||||
res.json({ promotion })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePromotionModule,
|
||||
} from "@medusajs/promotion"
|
||||
import { initialize as initializePromotionModule } from "@medusajs/promotion"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const promotionModuleService =
|
||||
await initializePromotionModule()
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const promotionModuleService = await initializePromotionModule()
|
||||
const body = await request.json()
|
||||
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
},
|
||||
})
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({ promotion })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ promotion })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -80,58 +78,52 @@ In this document, you’ll find common examples of how you can use the Promotion
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const campaign = await promotionModuleService.createCampaigns(
|
||||
{
|
||||
name: "Summer Discounts",
|
||||
campaign_identifier: "G-123445",
|
||||
starts_at: new Date("2024-05-02"),
|
||||
ends_at: new Date("2024-07-20"),
|
||||
}
|
||||
)
|
||||
const campaign = await promotionModuleService.createCampaigns({
|
||||
name: "Summer Discounts",
|
||||
campaign_identifier: "G-123445",
|
||||
starts_at: new Date("2024-05-02"),
|
||||
ends_at: new Date("2024-07-20"),
|
||||
})
|
||||
|
||||
res.json({ campaign })
|
||||
}
|
||||
```
|
||||
res.json({ campaign })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePromotionModule,
|
||||
} from "@medusajs/promotion"
|
||||
import { initialize as initializePromotionModule } from "@medusajs/promotion"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const promotionModuleService =
|
||||
await initializePromotionModule()
|
||||
const body = await request.json()
|
||||
export async function POST(request: Request) {
|
||||
const promotionModuleService = await initializePromotionModule()
|
||||
const body = await request.json()
|
||||
|
||||
const campaign = await promotionModuleService.createCampaigns(
|
||||
{
|
||||
name: "Summer Discounts",
|
||||
campaign_identifier: "G-123445",
|
||||
starts_at: new Date("2024-05-02"),
|
||||
ends_at: new Date("2024-07-20"),
|
||||
}
|
||||
)
|
||||
const campaign = await promotionModuleService.createCampaigns({
|
||||
name: "Summer Discounts",
|
||||
campaign_identifier: "G-123445",
|
||||
starts_at: new Date("2024-05-02"),
|
||||
ends_at: new Date("2024-07-20"),
|
||||
})
|
||||
|
||||
return NextResponse.json({ campaign })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ campaign })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -143,75 +135,73 @@ In this document, you’ll find common examples of how you can use the Promotion
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "customer_group_id",
|
||||
operator: "eq",
|
||||
values: ["VIP"],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "customer_group_id",
|
||||
operator: "eq",
|
||||
values: ["VIP"],
|
||||
},
|
||||
],
|
||||
})
|
||||
],
|
||||
})
|
||||
|
||||
res.json({ promotion })
|
||||
}
|
||||
```
|
||||
res.json({ promotion })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePromotionModule,
|
||||
} from "@medusajs/promotion"
|
||||
import { initialize as initializePromotionModule } from "@medusajs/promotion"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const promotionModuleService =
|
||||
await initializePromotionModule()
|
||||
export async function POST(request: Request) {
|
||||
const promotionModuleService = await initializePromotionModule()
|
||||
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
const promotion = await promotionModuleService.createPromotions({
|
||||
code: "10%OFF",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "percentage",
|
||||
target_type: "order",
|
||||
value: "10",
|
||||
currency_code: "usd",
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "customer_group_id",
|
||||
operator: "eq",
|
||||
values: ["VIP"],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "customer_group_id",
|
||||
operator: "eq",
|
||||
values: ["VIP"],
|
||||
},
|
||||
],
|
||||
})
|
||||
],
|
||||
})
|
||||
|
||||
return NextResponse.json({ promotion })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ promotion })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -223,43 +213,41 @@ In this document, you’ll find common examples of how you can use the Promotion
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
res.json({
|
||||
promotions: await promotionModuleService.listPromotions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
promotions: await promotionModuleService.listPromotions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializePromotionModule,
|
||||
} from "@medusajs/promotion"
|
||||
import { initialize as initializePromotionModule } from "@medusajs/promotion"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const promotionModuleService =
|
||||
await initializePromotionModule()
|
||||
export async function GET(request: Request) {
|
||||
const promotionModuleService = await initializePromotionModule()
|
||||
|
||||
return NextResponse.json({
|
||||
promotions: await promotionModuleService.listPromotions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
promotions: await promotionModuleService.listPromotions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -60,14 +60,12 @@ A campaign combines promotions under the same conditions, such as start and end
|
||||
A campaign can also have an identifier for tracking purposes, such as tracking through tools like Google Analytics.
|
||||
|
||||
```ts
|
||||
const campaign = await promotionModuleService.createCampaigns(
|
||||
{
|
||||
name: "Summer Discounts",
|
||||
campaign_identifier: "G-123445",
|
||||
starts_at: new Date("2024-05-02"),
|
||||
ends_at: new Date("2024-07-20"),
|
||||
}
|
||||
)
|
||||
const campaign = await promotionModuleService.createCampaigns({
|
||||
name: "Summer Discounts",
|
||||
campaign_identifier: "G-123445",
|
||||
starts_at: new Date("2024-05-02"),
|
||||
ends_at: new Date("2024-07-20"),
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
@@ -81,59 +79,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
res.json({
|
||||
promotions: await promotionModuleService.listPromotions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
promotions: await promotionModuleService.listPromotions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
container.resolve(ModuleRegistrationName.PROMOTION)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const promotionModuleService: IPromotionModuleService = container.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotions = await promotionModuleService.listPromotions()
|
||||
}
|
||||
```
|
||||
const promotions = await promotionModuleService.listPromotions()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const promotionModuleService: IPromotionModuleService =
|
||||
container.resolve(ModuleRegistrationName.PROMOTION)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const promotionModuleService: IPromotionModuleService = container.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotions = await promotionModuleService.listPromotions()
|
||||
})
|
||||
```
|
||||
const promotions = await promotionModuleService.listPromotions()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,52 +13,51 @@ In this guide, you’ll find common examples of how you can use the Region Modul
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.REGION)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
const region = await regionModuleService.createRegions({
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
})
|
||||
const region = await regionModuleService.createRegions({
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
})
|
||||
|
||||
res.json({
|
||||
region,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
region,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeRegionModule,
|
||||
} from "@medusajs/region"
|
||||
import { initialize as initializeRegionModule } from "@medusajs/region"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
export async function POST(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
|
||||
const region = await regionModuleService.createRegions({
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
})
|
||||
const region = await regionModuleService.createRegions({
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
})
|
||||
|
||||
res.json({
|
||||
region,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
region,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -70,42 +69,41 @@ In this guide, you’ll find common examples of how you can use the Region Modul
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.REGION)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
res.json({
|
||||
regions: await regionModuleService.listRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
regions: await regionModuleService.listRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeRegionModule,
|
||||
} from "@medusajs/region"
|
||||
import { initialize as initializeRegionModule } from "@medusajs/region"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
export async function GET(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
|
||||
return NextResponse.json({
|
||||
regions: await regionModuleService.listRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
regions: await regionModuleService.listRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -117,46 +115,41 @@ In this guide, you’ll find common examples of how you can use the Region Modul
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.REGION)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
const region = await regionModuleService
|
||||
.retrieveRegion("reg_123")
|
||||
const region = await regionModuleService.retrieveRegion("reg_123")
|
||||
|
||||
res.json({ region })
|
||||
}
|
||||
```
|
||||
res.json({ region })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeRegionModule,
|
||||
} from "@medusajs/region"
|
||||
import { initialize as initializeRegionModule } from "@medusajs/region"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
export async function GET(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
|
||||
const region = await regionModuleService
|
||||
.retrieveRegion("reg_123")
|
||||
const region = await regionModuleService.retrieveRegion("reg_123")
|
||||
|
||||
return NextResponse.json({ region })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ region })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -168,50 +161,45 @@ In this guide, you’ll find common examples of how you can use the Region Modul
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.REGION)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
const region = await regionModuleService
|
||||
.updateRegions("reg_123", {
|
||||
automatic_taxes: false,
|
||||
})
|
||||
const region = await regionModuleService.updateRegions("reg_123", {
|
||||
automatic_taxes: false,
|
||||
})
|
||||
|
||||
res.json({ region })
|
||||
}
|
||||
```
|
||||
res.json({ region })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeRegionModule,
|
||||
} from "@medusajs/region"
|
||||
import { initialize as initializeRegionModule } from "@medusajs/region"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
export async function POST(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
|
||||
const region = await regionModuleService
|
||||
.updateRegions("reg_123", {
|
||||
automatic_taxes: false,
|
||||
})
|
||||
const region = await regionModuleService.updateRegions("reg_123", {
|
||||
automatic_taxes: false,
|
||||
})
|
||||
|
||||
return NextResponse.json({ region })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ region })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -223,42 +211,39 @@ In this guide, you’ll find common examples of how you can use the Region Modul
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.REGION)
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
await regionModuleService.deleteRegions("reg_123")
|
||||
await regionModuleService.deleteRegions("reg_123")
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeRegionModule,
|
||||
} from "@medusajs/region"
|
||||
import { initialize as initializeRegionModule } from "@medusajs/region"
|
||||
|
||||
export async function DELETE(
|
||||
request: Request
|
||||
) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
export async function DELETE(request: Request) {
|
||||
const regionModuleService = await initializeRegionModule()
|
||||
|
||||
await regionModuleService.deleteRegions("reg_123")
|
||||
}
|
||||
```
|
||||
await regionModuleService.deleteRegions("reg_123")
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -76,61 +76,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.REGION)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const regionModuleService: IRegionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
res.json({
|
||||
regions: await regionModuleService.listRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
regions: await regionModuleService.listRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
container.resolve(ModuleRegistrationName.REGION)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const regionModuleService: IRegionModuleService = container.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
const regions = await regionModuleService.listRegions()
|
||||
}
|
||||
```
|
||||
const regions = await regionModuleService.listRegions()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const regionModuleService: IRegionModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
const regions = await regionModuleService.listRegions()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const regionModuleService: IRegionModuleService = container.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
const regions = await regionModuleService.listRegions()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,51 +13,46 @@ In this guide, you’ll find common examples of how you can use the Sales Channe
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
const salesChannel = await salesChannelModuleService
|
||||
.createSalesChannels({
|
||||
name: "B2B",
|
||||
})
|
||||
const salesChannel = await salesChannelModuleService.createSalesChannels({
|
||||
name: "B2B",
|
||||
})
|
||||
|
||||
res.json({
|
||||
sales_channel: salesChannel,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
sales_channel: salesChannel,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeSalesChannelModule,
|
||||
} from "@medusajs/sales-channel"
|
||||
import { initialize as initializeSalesChannelModule } from "@medusajs/sales-channel"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const salesChannelModuleService =
|
||||
await initializeSalesChannelModule()
|
||||
export async function POST(request: Request) {
|
||||
const salesChannelModuleService = await initializeSalesChannelModule()
|
||||
|
||||
const salesChannel = await salesChannelModuleService
|
||||
.createSalesChannels({
|
||||
name: "B2B",
|
||||
})
|
||||
const salesChannel = await salesChannelModuleService.createSalesChannels({
|
||||
name: "B2B",
|
||||
})
|
||||
|
||||
return NextResponse.json({ sales_channel: salesChannel })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ sales_channel: salesChannel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -69,45 +64,40 @@ In this guide, you’ll find common examples of how you can use the Sales Channe
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
res.json({
|
||||
sales_channels: salesChannelModuleService
|
||||
.listSalesChannels(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
sales_channels: salesChannelModuleService.listSalesChannels(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeSalesChannelModule,
|
||||
} from "@medusajs/sales-channel"
|
||||
import { initialize as initializeSalesChannelModule } from "@medusajs/sales-channel"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const salesChannelModuleService =
|
||||
await initializeSalesChannelModule()
|
||||
export async function GET(request: Request) {
|
||||
const salesChannelModuleService = await initializeSalesChannelModule()
|
||||
|
||||
const salesChannels = await salesChannelModuleService
|
||||
.listSalesChannels()
|
||||
const salesChannels = await salesChannelModuleService.listSalesChannels()
|
||||
|
||||
return NextResponse.json({ sales_channels: salesChannels })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ sales_channels: salesChannels })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -119,53 +109,46 @@ In this guide, you’ll find common examples of how you can use the Sales Channe
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
const salesChannel = await salesChannelModuleService
|
||||
.retrieveSalesChannel(
|
||||
"sc_123"
|
||||
)
|
||||
const salesChannel = await salesChannelModuleService.retrieveSalesChannel(
|
||||
"sc_123"
|
||||
)
|
||||
|
||||
res.json({
|
||||
sales_channel: salesChannel,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
sales_channel: salesChannel,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeSalesChannelModule,
|
||||
} from "@medusajs/sales-channel"
|
||||
import { initialize as initializeSalesChannelModule } from "@medusajs/sales-channel"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const salesChannelModuleService =
|
||||
await initializeSalesChannelModule()
|
||||
export async function GET(request: Request) {
|
||||
const salesChannelModuleService = await initializeSalesChannelModule()
|
||||
|
||||
const salesChannel = await salesChannelModuleService
|
||||
.retrieveSalesChannel(
|
||||
"sc_123"
|
||||
)
|
||||
const salesChannel = await salesChannelModuleService.retrieveSalesChannel(
|
||||
"sc_123"
|
||||
)
|
||||
|
||||
return NextResponse.json({ sales_channel: salesChannel })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ sales_channel: salesChannel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -177,55 +160,48 @@ In this guide, you’ll find common examples of how you can use the Sales Channe
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
const salesChannel = await salesChannelModuleService
|
||||
.updateSalesChannels({
|
||||
id: "sc_123",
|
||||
description: "Sales channel for B2B customers",
|
||||
})
|
||||
const salesChannel = await salesChannelModuleService.updateSalesChannels({
|
||||
id: "sc_123",
|
||||
description: "Sales channel for B2B customers",
|
||||
})
|
||||
|
||||
res.json({
|
||||
sales_channel: salesChannel,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
sales_channel: salesChannel,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeSalesChannelModule,
|
||||
} from "@medusajs/sales-channel"
|
||||
import { initialize as initializeSalesChannelModule } from "@medusajs/sales-channel"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const salesChannelModuleService =
|
||||
await initializeSalesChannelModule()
|
||||
export async function POST(request: Request) {
|
||||
const salesChannelModuleService = await initializeSalesChannelModule()
|
||||
|
||||
const salesChannel = await salesChannelModuleService
|
||||
.updateSalesChannels({
|
||||
id: "sc_123",
|
||||
description: "Sales channel for B2B customers",
|
||||
})
|
||||
const salesChannel = await salesChannelModuleService.updateSalesChannels({
|
||||
id: "sc_123",
|
||||
description: "Sales channel for B2B customers",
|
||||
})
|
||||
|
||||
return NextResponse.json({ sales_channel: salesChannel })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ sales_channel: salesChannel })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -237,43 +213,38 @@ In this guide, you’ll find common examples of how you can use the Sales Channe
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
await salesChannelModuleService.deleteSalesChannels("sc_123")
|
||||
await salesChannelModuleService.deleteSalesChannels("sc_123")
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeSalesChannelModule,
|
||||
} from "@medusajs/sales-channel"
|
||||
import { initialize as initializeSalesChannelModule } from "@medusajs/sales-channel"
|
||||
|
||||
export async function DELETE(
|
||||
request: Request
|
||||
) {
|
||||
const salesChannelModuleService =
|
||||
await initializeSalesChannelModule()
|
||||
export async function DELETE(request: Request) {
|
||||
const salesChannelModuleService = await initializeSalesChannelModule()
|
||||
|
||||
await salesChannelModuleService.deleteSalesChannels("sc_123")
|
||||
}
|
||||
```
|
||||
await salesChannelModuleService.deleteSalesChannels("sc_123")
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -27,15 +27,14 @@ Some use case examples for using a sales channel:
|
||||
Manage sales channels in your store. Each sales channel has different meta information such as name or description, allowing you to easily differentiate between sales channels.
|
||||
|
||||
```ts
|
||||
const salesChannels = await salesChannelModuleService
|
||||
.createSalesChannels([
|
||||
{
|
||||
name: "B2B",
|
||||
},
|
||||
{
|
||||
name: "Mobile App",
|
||||
},
|
||||
])
|
||||
const salesChannels = await salesChannelModuleService.createSalesChannels([
|
||||
{
|
||||
name: "B2B",
|
||||
},
|
||||
{
|
||||
name: "Mobile App",
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
### Product Availability
|
||||
@@ -61,64 +60,55 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
res.json({
|
||||
sales_channels: await salesChannelModuleService
|
||||
.listSalesChannels(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
sales_channels: await salesChannelModuleService.listSalesChannels(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
container.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
container.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
const salesChannels = await salesChannelModuleService
|
||||
.listSalesChannels()
|
||||
}
|
||||
```
|
||||
const salesChannels = await salesChannelModuleService.listSalesChannels()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
const salesChannels = await salesChannelModuleService
|
||||
.listSalesChannels()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const salesChannelModuleService: ISalesChannelModuleService =
|
||||
container.resolve(ModuleRegistrationName.SALES_CHANNEL)
|
||||
|
||||
const salesChannels = await salesChannelModuleService.listSalesChannels()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,55 +13,46 @@ In this document, you’ll find common examples of how you can use the Stock Loc
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
|
||||
const stockLocation = await stockLocationModuleService
|
||||
.createStockLocations(
|
||||
{
|
||||
name: "Warehouse 1",
|
||||
}
|
||||
)
|
||||
const stockLocation = await stockLocationModuleService.createStockLocations({
|
||||
name: "Warehouse 1",
|
||||
})
|
||||
|
||||
res.json({
|
||||
stock_location: stockLocation,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
stock_location: stockLocation,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStockLocationModule,
|
||||
} from "@medusajs/stock-location-next"
|
||||
import { initialize as initializeStockLocationModule } from "@medusajs/stock-location-next"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const stockLocationModuleService =
|
||||
await initializeStockLocationModule({})
|
||||
export async function POST(request: Request) {
|
||||
const stockLocationModuleService = await initializeStockLocationModule({})
|
||||
|
||||
const stockLocation = await stockLocationModuleService
|
||||
.createStockLocations(
|
||||
{
|
||||
name: "Warehouse 1",
|
||||
}
|
||||
)
|
||||
const stockLocation = await stockLocationModuleService.createStockLocations({
|
||||
name: "Warehouse 1",
|
||||
})
|
||||
|
||||
return NextResponse.json({ stock_location: stockLocation })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ stock_location: stockLocation })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -73,45 +64,40 @@ In this document, you’ll find common examples of how you can use the Stock Loc
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
|
||||
res.json({
|
||||
stock_locations: await stockLocationModuleService
|
||||
.listStockLocations({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
stock_locations: await stockLocationModuleService.listStockLocations({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStockLocationModule,
|
||||
} from "@medusajs/stock-location-next"
|
||||
import { initialize as initializeStockLocationModule } from "@medusajs/stock-location-next"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const stockLocationModuleService =
|
||||
await initializeStockLocationModule({})
|
||||
export async function GET(request: Request) {
|
||||
const stockLocationModuleService = await initializeStockLocationModule({})
|
||||
|
||||
return NextResponse.json({
|
||||
stock_locations: await stockLocationModuleService
|
||||
.listStockLocations({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
stock_locations: await stockLocationModuleService.listStockLocations({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -123,67 +109,60 @@ In this document, you’ll find common examples of how you can use the Stock Loc
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
|
||||
const stockLocation = await stockLocationModuleService
|
||||
.updateStockLocations({
|
||||
id: "sloc_123",
|
||||
address: {
|
||||
country_code: "US",
|
||||
city: "New York City",
|
||||
address_1: "52 Stone St",
|
||||
postal_code: "10004",
|
||||
},
|
||||
})
|
||||
const stockLocation = await stockLocationModuleService.updateStockLocations({
|
||||
id: "sloc_123",
|
||||
address: {
|
||||
country_code: "US",
|
||||
city: "New York City",
|
||||
address_1: "52 Stone St",
|
||||
postal_code: "10004",
|
||||
},
|
||||
})
|
||||
|
||||
res.json({
|
||||
stock_location: stockLocation,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
stock_location: stockLocation,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStockLocationModule,
|
||||
} from "@medusajs/stock-location-next"
|
||||
import { initialize as initializeStockLocationModule } from "@medusajs/stock-location-next"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const stockLocationModuleService =
|
||||
await initializeStockLocationModule({})
|
||||
export async function POST(request: Request) {
|
||||
const stockLocationModuleService = await initializeStockLocationModule({})
|
||||
|
||||
const stockLocation = await stockLocationModuleService
|
||||
.updateStockLocations({
|
||||
id: "sloc_123",
|
||||
address: {
|
||||
country_code: "us",
|
||||
city: "New York City",
|
||||
address_1: "52 Stone St",
|
||||
postal_code: "10004",
|
||||
},
|
||||
})
|
||||
const stockLocation = await stockLocationModuleService.updateStockLocations({
|
||||
id: "sloc_123",
|
||||
address: {
|
||||
country_code: "us",
|
||||
city: "New York City",
|
||||
address_1: "52 Stone St",
|
||||
postal_code: "10004",
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
stock_location: stockLocation,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
stock_location: stockLocation,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -195,47 +174,38 @@ In this document, you’ll find common examples of how you can use the Stock Loc
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
|
||||
await stockLocationModuleService.deleteStockLocations(
|
||||
"sloc_123"
|
||||
)
|
||||
await stockLocationModuleService.deleteStockLocations("sloc_123")
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStockLocationModule,
|
||||
} from "@medusajs/stock-location-next"
|
||||
import { initialize as initializeStockLocationModule } from "@medusajs/stock-location-next"
|
||||
|
||||
export async function DELETE(
|
||||
request: Request
|
||||
) {
|
||||
const stockLocationModuleService =
|
||||
await initializeStockLocationModule({})
|
||||
export async function DELETE(request: Request) {
|
||||
const stockLocationModuleService = await initializeStockLocationModule({})
|
||||
|
||||
await stockLocationModuleService.deleteStockLocations(
|
||||
"sloc_123"
|
||||
)
|
||||
}
|
||||
```
|
||||
await stockLocationModuleService.deleteStockLocations("sloc_123")
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -15,12 +15,9 @@ The Stock Location Module is the `@medusajs/stock-location-next` NPM package tha
|
||||
Store and manage stock locations. Stock locations are associated with data models of other modules that require a location, such as the [Inventory Module's InventoryLevel](../inventory/concepts/page.mdx#inventory-level).
|
||||
|
||||
```ts
|
||||
const stockLocation = await stockLocationModuleService
|
||||
.createStockLocations(
|
||||
{
|
||||
name: "Warehouse 1",
|
||||
}
|
||||
)
|
||||
const stockLocation = await stockLocationModuleService.createStockLocations({
|
||||
name: "Warehouse 1",
|
||||
})
|
||||
```
|
||||
|
||||
### Address Management
|
||||
@@ -28,18 +25,15 @@ const stockLocation = await stockLocationModuleService
|
||||
Manage the address of each stock location.
|
||||
|
||||
```ts
|
||||
const stockLocation = await stockLocationModuleService
|
||||
.updateStockLocations(
|
||||
{
|
||||
id: "sloc_123",
|
||||
address: {
|
||||
country_code: "us",
|
||||
city: "New York City",
|
||||
address_1: "52 Stone St",
|
||||
postal_code: "10004",
|
||||
},
|
||||
}
|
||||
)
|
||||
const stockLocation = await stockLocationModuleService.updateStockLocations({
|
||||
id: "sloc_123",
|
||||
address: {
|
||||
country_code: "us",
|
||||
city: "New York City",
|
||||
address_1: "52 Stone St",
|
||||
postal_code: "10004",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
@@ -53,64 +47,57 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
|
||||
res.json({
|
||||
stock_locations: await stockLocationModuleService
|
||||
.listStockLocations({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
stock_locations: await stockLocationModuleService.listStockLocations({}),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
container.resolve(ModuleRegistrationName.STOCK_LOCATION)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const stockLocationModuleService: IStockLocationService = container.resolve(
|
||||
ModuleRegistrationName.STOCK_LOCATION
|
||||
)
|
||||
|
||||
const stockLocations = await stockLocationModuleService
|
||||
.listStockLocations({})
|
||||
}
|
||||
```
|
||||
const stockLocations = await stockLocationModuleService.listStockLocations({})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const stockLocationModuleService: IStockLocationService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.STOCK_LOCATION
|
||||
)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const stockLocationModuleService: IStockLocationService = container.resolve(
|
||||
ModuleRegistrationName.STOCK_LOCATION
|
||||
)
|
||||
|
||||
const stockLocations = await stockLocationModuleService
|
||||
.listStockLocations({})
|
||||
})
|
||||
```
|
||||
const stockLocations = await stockLocationModuleService.listStockLocations({})
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,62 +13,61 @@ In this guide, you’ll find common examples of how you can use the Store Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.STORE)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const store = await storeModuleService.createStores({
|
||||
name: "My Store",
|
||||
supported_currencies: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
is_default: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
const store = await storeModuleService.createStores({
|
||||
name: "My Store",
|
||||
supported_currencies: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
is_default: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStoreModule,
|
||||
} from "@medusajs/store"
|
||||
import { initialize as initializeStoreModule } from "@medusajs/store"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
export async function POST(request: Request) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
|
||||
const store = await storeModuleService.createStores({
|
||||
name: "My Store",
|
||||
supported_currencies: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
is_default: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
const store = await storeModuleService.createStores({
|
||||
name: "My Store",
|
||||
supported_currencies: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
is_default: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -80,44 +79,43 @@ In this guide, you’ll find common examples of how you can use the Store Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.STORE)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
res.json({
|
||||
stores: await storeModuleService.listStores(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
stores: await storeModuleService.listStores(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStoreModule,
|
||||
} from "@medusajs/store"
|
||||
import { initialize as initializeStoreModule } from "@medusajs/store"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
export async function GET(request: Request) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
|
||||
const salesChannels = await storeModuleService.listStores()
|
||||
const salesChannels = await storeModuleService.listStores()
|
||||
|
||||
return NextResponse.json({
|
||||
stores: await storeModuleService.list(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
stores: await storeModuleService.list(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -129,51 +127,43 @@ In this guide, you’ll find common examples of how you can use the Store Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.STORE)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const store = await storeModuleService.retrieveStore(
|
||||
"store_123"
|
||||
)
|
||||
const store = await storeModuleService.retrieveStore("store_123")
|
||||
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStoreModule,
|
||||
} from "@medusajs/store"
|
||||
import { initialize as initializeStoreModule } from "@medusajs/store"
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
export async function GET(request: Request, { params }: ContextType) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
|
||||
const store = await storeModuleService.retrieveStore(
|
||||
"store_123"
|
||||
)
|
||||
const store = await storeModuleService.retrieveStore("store_123")
|
||||
|
||||
return NextResponse.json({ store })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ store })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -185,53 +175,47 @@ In this guide, you’ll find common examples of how you can use the Store Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.STORE)
|
||||
export async function POST(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const store = await storeModuleService
|
||||
.updateStores("store_123", {
|
||||
name: "Change Store",
|
||||
})
|
||||
const store = await storeModuleService.updateStores("store_123", {
|
||||
name: "Change Store",
|
||||
})
|
||||
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
store,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStoreModule,
|
||||
} from "@medusajs/store"
|
||||
import { initialize as initializeStoreModule } from "@medusajs/store"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
|
||||
const store = await storeModuleService
|
||||
.updateStores("store_123", {
|
||||
name: "Change Store",
|
||||
})
|
||||
const store = await storeModuleService.updateStores("store_123", {
|
||||
name: "Change Store",
|
||||
})
|
||||
|
||||
return NextResponse.json({ store })
|
||||
}
|
||||
```
|
||||
return NextResponse.json({ store })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -243,43 +227,39 @@ In this guide, you’ll find common examples of how you can use the Store Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.STORE)
|
||||
export async function DELETE(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
await storeModuleService.deleteStores("store_123")
|
||||
await storeModuleService.deleteStores("store_123")
|
||||
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeStoreModule,
|
||||
} from "@medusajs/store"
|
||||
import { initialize as initializeStoreModule } from "@medusajs/store"
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
export async function DELETE(request: Request, { params }: ContextType) {
|
||||
const storeModuleService = await initializeStoreModule()
|
||||
|
||||
await storeModuleService.deleteStores("store_123")
|
||||
}
|
||||
```
|
||||
await storeModuleService.deleteStores("store_123")
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -289,4 +269,3 @@ In this guide, you’ll find common examples of how you can use the Store Module
|
||||
## More Examples
|
||||
|
||||
The [Store Module's main service reference](/references/store) provides a reference to all the methods available for use with examples for each.
|
||||
|
||||
@@ -49,61 +49,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
request.scope.resolve(ModuleRegistrationName.STORE)
|
||||
export async function GET(
|
||||
request: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const storeModuleService: IStoreModuleService = request.scope.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
res.json({
|
||||
stores: await storeModuleService.listStores(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
stores: await storeModuleService.listStores(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
container.resolve(ModuleRegistrationName.STORE)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const storeModuleService: IStoreModuleService = container.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const stores = await storeModuleService.listStores()
|
||||
}
|
||||
```
|
||||
const stores = await storeModuleService.listStores()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const storeModuleService: IStoreModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const stores = await storeModuleService.listStores()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const storeModuleService: IStoreModuleService = container.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const stores = await storeModuleService.listStores()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,59 +13,57 @@ In this guide, you’ll find common examples of how you can use the Tax Module i
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
const taxRegion = await taxModuleService.createTaxRegions({
|
||||
country_code: "us",
|
||||
default_tax_rate: {
|
||||
rate: 10,
|
||||
name: "Default rate",
|
||||
},
|
||||
})
|
||||
const taxRegion = await taxModuleService.createTaxRegions({
|
||||
country_code: "us",
|
||||
default_tax_rate: {
|
||||
rate: 10,
|
||||
name: "Default rate",
|
||||
},
|
||||
})
|
||||
|
||||
res.json({
|
||||
tax_region: taxRegion,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
tax_region: taxRegion,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeTaxModule,
|
||||
} from "@medusajs/tax"
|
||||
import { initialize as initializeTaxModule } from "@medusajs/tax"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
export async function POST(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
|
||||
const taxRegion = await taxModuleService.createTaxRegions({
|
||||
country_code: "us",
|
||||
default_tax_rate: {
|
||||
rate: 10,
|
||||
name: "Default rate",
|
||||
},
|
||||
})
|
||||
const taxRegion = await taxModuleService.createTaxRegions({
|
||||
country_code: "us",
|
||||
default_tax_rate: {
|
||||
rate: 10,
|
||||
name: "Default rate",
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
tax_region: taxRegion,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
tax_region: taxRegion,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -77,43 +75,41 @@ In this guide, you’ll find common examples of how you can use the Tax Module i
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
res.json({
|
||||
tax_regions: await taxModuleService.listTaxRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
tax_regions: await taxModuleService.listTaxRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeTaxModule,
|
||||
} from "@medusajs/tax"
|
||||
import { initialize as initializeTaxModule } from "@medusajs/tax"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
export async function GET(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
|
||||
return NextResponse.json({
|
||||
tax_regions: await taxModuleService.listTaxRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
tax_regions: await taxModuleService.listTaxRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -125,77 +121,73 @@ In this guide, you’ll find common examples of how you can use the Tax Module i
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
const taxRate = await taxModuleService.createTaxRates({
|
||||
tax_region_id: "txreg_123",
|
||||
name: "Custom rate",
|
||||
rate: 15,
|
||||
rules: [
|
||||
{
|
||||
reference: "product_type",
|
||||
reference_id: "ptyp_1",
|
||||
},
|
||||
{
|
||||
reference: "product",
|
||||
reference_id: "prod_123",
|
||||
},
|
||||
],
|
||||
})
|
||||
const taxRate = await taxModuleService.createTaxRates({
|
||||
tax_region_id: "txreg_123",
|
||||
name: "Custom rate",
|
||||
rate: 15,
|
||||
rules: [
|
||||
{
|
||||
reference: "product_type",
|
||||
reference_id: "ptyp_1",
|
||||
},
|
||||
{
|
||||
reference: "product",
|
||||
reference_id: "prod_123",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
res.json({
|
||||
tax_rate: taxRate,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
tax_rate: taxRate,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeTaxModule,
|
||||
} from "@medusajs/tax"
|
||||
import { initialize as initializeTaxModule } from "@medusajs/tax"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
export async function POST(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
|
||||
const taxRate = await taxModuleService.createTaxRates({
|
||||
tax_region_id: "txreg_123",
|
||||
name: "Custom rate",
|
||||
rate: 15,
|
||||
rules: [
|
||||
{
|
||||
reference: "product_type",
|
||||
reference_id: "ptyp_1",
|
||||
},
|
||||
{
|
||||
reference: "product",
|
||||
reference_id: "prod_123",
|
||||
},
|
||||
],
|
||||
})
|
||||
const taxRate = await taxModuleService.createTaxRates({
|
||||
tax_region_id: "txreg_123",
|
||||
name: "Custom rate",
|
||||
rate: 15,
|
||||
rules: [
|
||||
{
|
||||
reference: "product_type",
|
||||
reference_id: "ptyp_1",
|
||||
},
|
||||
{
|
||||
reference: "product",
|
||||
reference_id: "prod_123",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
tax_rate: taxRate,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
tax_rate: taxRate,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -207,45 +199,41 @@ In this guide, you’ll find common examples of how you can use the Tax Module i
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
res.json({
|
||||
tax_rates: await taxModuleService.listTaxRates(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
tax_rates: await taxModuleService.listTaxRates(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeTaxModule,
|
||||
} from "@medusajs/tax"
|
||||
import { initialize as initializeTaxModule } from "@medusajs/tax"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
export async function GET(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
|
||||
return NextResponse.json({
|
||||
tax_rates: await taxModuleService.listTaxRates(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
tax_rates: await taxModuleService.listTaxRates(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -257,97 +245,93 @@ In this guide, you’ll find common examples of how you can use the Tax Module i
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
const taxLines = await taxModuleService.getTaxLines(
|
||||
[
|
||||
{
|
||||
id: "cali_123",
|
||||
product_id: "prod_123",
|
||||
unit_price: 1000,
|
||||
},
|
||||
{
|
||||
id: "casm_123",
|
||||
shipping_option_id: "so_123",
|
||||
unit_price: 2000,
|
||||
},
|
||||
],
|
||||
const taxLines = await taxModuleService.getTaxLines(
|
||||
[
|
||||
{
|
||||
address: {
|
||||
country_code: "us",
|
||||
postal_code: "123456",
|
||||
},
|
||||
customer: {
|
||||
id: "cus_123",
|
||||
email: "user@example.com",
|
||||
customer_groups: ["VIP"],
|
||||
},
|
||||
}
|
||||
)
|
||||
id: "cali_123",
|
||||
product_id: "prod_123",
|
||||
unit_price: 1000,
|
||||
},
|
||||
{
|
||||
id: "casm_123",
|
||||
shipping_option_id: "so_123",
|
||||
unit_price: 2000,
|
||||
},
|
||||
],
|
||||
{
|
||||
address: {
|
||||
country_code: "us",
|
||||
postal_code: "123456",
|
||||
},
|
||||
customer: {
|
||||
id: "cus_123",
|
||||
email: "user@example.com",
|
||||
customer_groups: ["VIP"],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
tax_lines: taxLines,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
tax_lines: taxLines,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeTaxModule,
|
||||
} from "@medusajs/tax"
|
||||
import { initialize as initializeTaxModule } from "@medusajs/tax"
|
||||
|
||||
export async function GET(
|
||||
request: Request
|
||||
) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
export async function GET(request: Request) {
|
||||
const taxModuleService = await initializeTaxModule()
|
||||
|
||||
const taxLines = await taxModuleService.getTaxLines(
|
||||
[
|
||||
{
|
||||
id: "cali_123",
|
||||
product_id: "prod_123",
|
||||
unit_price: 1000,
|
||||
},
|
||||
{
|
||||
id: "casm_123",
|
||||
shipping_option_id: "so_123",
|
||||
unit_price: 2000,
|
||||
},
|
||||
],
|
||||
const taxLines = await taxModuleService.getTaxLines(
|
||||
[
|
||||
{
|
||||
address: {
|
||||
country_code: "us",
|
||||
postal_code: "123456",
|
||||
},
|
||||
customer: {
|
||||
id: "cus_123",
|
||||
email: "user@example.com",
|
||||
customer_groups: ["VIP"],
|
||||
},
|
||||
}
|
||||
)
|
||||
id: "cali_123",
|
||||
product_id: "prod_123",
|
||||
unit_price: 1000,
|
||||
},
|
||||
{
|
||||
id: "casm_123",
|
||||
shipping_option_id: "so_123",
|
||||
unit_price: 2000,
|
||||
},
|
||||
],
|
||||
{
|
||||
address: {
|
||||
country_code: "us",
|
||||
postal_code: "123456",
|
||||
},
|
||||
customer: {
|
||||
id: "cus_123",
|
||||
email: "user@example.com",
|
||||
customer_groups: ["VIP"],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
tax_lines: taxLines,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
tax_lines: taxLines,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -91,62 +91,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const taxModuleService: ITaxModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
res.json({
|
||||
tax_regions: await taxModuleService.listTaxRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
tax_regions: await taxModuleService.listTaxRegions(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const taxModuleService: ITaxModuleService =
|
||||
container.resolve(ModuleRegistrationName.TAX)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const taxModuleService: ITaxModuleService = container.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
const taxRegions = await taxModuleService.listTaxRegions()
|
||||
}
|
||||
```
|
||||
const taxRegions = await taxModuleService.listTaxRegions()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { ITaxModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const taxModuleService: ITaxModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const taxModuleService: ITaxModuleService = container.resolve(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
const taxRegions = await taxModuleService.listTaxRegions()
|
||||
})
|
||||
```
|
||||
const taxRegions = await taxModuleService.listTaxRegions()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -13,52 +13,51 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const user = await userModuleService.createUsers({
|
||||
email: "user@example.com",
|
||||
first_name: "John",
|
||||
last_name: "Smith",
|
||||
})
|
||||
const user = await userModuleService.createUsers({
|
||||
email: "user@example.com",
|
||||
first_name: "John",
|
||||
last_name: "Smith",
|
||||
})
|
||||
|
||||
res.json({ user })
|
||||
}
|
||||
```
|
||||
res.json({ user })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function POST(request: Request) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
const user = await userModuleService.createUsers({
|
||||
email: "user@example.com",
|
||||
first_name: "John",
|
||||
last_name: "Smith",
|
||||
})
|
||||
const user = await userModuleService.createUsers({
|
||||
email: "user@example.com",
|
||||
first_name: "John",
|
||||
last_name: "Smith",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -70,42 +69,41 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
res.json({
|
||||
users: await userModuleService.listUsers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
users: await userModuleService.listUsers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function GET(request: Request) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
return NextResponse.json({
|
||||
users: await userModuleService.listUsers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
users: await userModuleService.listUsers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -117,52 +115,49 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const user = await userModuleService.updateUsers({
|
||||
id: "user_123",
|
||||
last_name: "Smith",
|
||||
})
|
||||
const user = await userModuleService.updateUsers({
|
||||
id: "user_123",
|
||||
last_name: "Smith",
|
||||
})
|
||||
|
||||
res.json({ user })
|
||||
}
|
||||
```
|
||||
res.json({ user })
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function POST(
|
||||
request: Request
|
||||
) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function POST(request: Request) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
const user = await userModuleService.updateUsers({
|
||||
id: "user_123",
|
||||
last_name: "Smith",
|
||||
})
|
||||
const user = await userModuleService.updateUsers({
|
||||
id: "user_123",
|
||||
last_name: "Smith",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -174,40 +169,37 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
await userModuleService.deleteUsers(["user_123"])
|
||||
}
|
||||
```
|
||||
await userModuleService.deleteUsers(["user_123"])
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function DELETE(
|
||||
request: Request
|
||||
) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function DELETE(request: Request) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
await userModuleService.deleteUsers(["user_123"])
|
||||
}
|
||||
```
|
||||
await userModuleService.deleteUsers(["user_123"])
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -219,53 +211,49 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const invite = await userModuleService.createInvites({
|
||||
email: "user2@example.com",
|
||||
})
|
||||
const invite = await userModuleService.createInvites({
|
||||
email: "user2@example.com",
|
||||
})
|
||||
|
||||
res.json({
|
||||
invite,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
invite,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
const invite = await userModuleService.createInvites({
|
||||
email: "user2@example.com",
|
||||
})
|
||||
const invite = await userModuleService.createInvites({
|
||||
email: "user2@example.com",
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
invite,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
invite,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -277,71 +265,65 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const invite =
|
||||
await userModuleService.validateInviteToken("secret_123")
|
||||
const invite = await userModuleService.validateInviteToken("secret_123")
|
||||
|
||||
const user = await userModuleService.createUsers({
|
||||
email: invite.email,
|
||||
metadata: invite.metadata,
|
||||
})
|
||||
const user = await userModuleService.createUsers({
|
||||
email: invite.email,
|
||||
metadata: invite.metadata,
|
||||
})
|
||||
|
||||
await userModuleService.updateInvites({
|
||||
id: invite.id,
|
||||
accepted: true,
|
||||
})
|
||||
await userModuleService.updateInvites({
|
||||
id: invite.id,
|
||||
accepted: true,
|
||||
})
|
||||
|
||||
res.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
const invite =
|
||||
await userModuleService.validateInviteToken("secret_123")
|
||||
const invite = await userModuleService.validateInviteToken("secret_123")
|
||||
|
||||
const user = await userModuleService.createUsers({
|
||||
email: invite.email,
|
||||
metadata: invite.metadata,
|
||||
})
|
||||
const user = await userModuleService.createUsers({
|
||||
email: invite.email,
|
||||
metadata: invite.metadata,
|
||||
})
|
||||
|
||||
await userModuleService.updateInvites({
|
||||
id: invite.id,
|
||||
accepted: true,
|
||||
})
|
||||
await userModuleService.updateInvites({
|
||||
id: invite.id,
|
||||
accepted: true,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
user,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
@@ -353,53 +335,45 @@ In this guide, you’ll find common examples of how you can use the User Module
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const invites = await userModuleService.refreshInviteTokens([
|
||||
"invite_123",
|
||||
])
|
||||
const invites = await userModuleService.refreshInviteTokens(["invite_123"])
|
||||
|
||||
res.json({
|
||||
invite: invites[0],
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
invite: invites[0],
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Next.js App Router" value="nextjs">
|
||||
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
```ts
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import {
|
||||
initialize as initializeUserModule,
|
||||
} from "@medusajs/user"
|
||||
import { initialize as initializeUserModule } from "@medusajs/user"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: ContextType
|
||||
) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
export async function POST(request: Request, { params }: ContextType) {
|
||||
const userModuleService = await initializeUserModule()
|
||||
|
||||
const invites = await userModuleService.refreshInviteTokens([
|
||||
"invite_123",
|
||||
])
|
||||
const invites = await userModuleService.refreshInviteTokens(["invite_123"])
|
||||
|
||||
return NextResponse.json({
|
||||
invite: invites[0],
|
||||
})
|
||||
}
|
||||
```
|
||||
return NextResponse.json({
|
||||
invite: invites[0],
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
@@ -52,61 +52,58 @@ For example:
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService =
|
||||
req.scope.resolve(ModuleRegistrationName.USER)
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
res.json({
|
||||
users: await userModuleService.listUsers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
res.json({
|
||||
users: await userModuleService.listUsers(),
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/medusa"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function subscriberHandler({
|
||||
container,
|
||||
}: SubscriberArgs) {
|
||||
const userModuleService: IUserModuleService =
|
||||
container.resolve(ModuleRegistrationName.USER)
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const userModuleService: IUserModuleService = container.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const users = await userModuleService.listUsers()
|
||||
}
|
||||
```
|
||||
const users = await userModuleService.listUsers()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
const step1 = createStep(
|
||||
"step-1",
|
||||
async (_, { container }) => {
|
||||
const userModuleService: IUserModuleService =
|
||||
container.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const users = await userModuleService.listUsers()
|
||||
})
|
||||
```
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const userModuleService: IUserModuleService = container.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
const users = await userModuleService.listUsers()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -37,11 +37,11 @@ Then, resolve the module's main service in other resources, such as API routes o
|
||||
|
||||
<Details summaryContent="Example: Create a module integrating an ERP system">
|
||||
|
||||
This example showcases how to create a module that integrates to a dummy ERP system.
|
||||
This example showcases how to create a module that integrates to a dummy ERP system.
|
||||
|
||||
Start by creating the directory `src/modules/erp` for your module.
|
||||
Start by creating the directory `src/modules/erp` for your module.
|
||||
|
||||
Then, create the file `src/modules/erp/service.ts` with the following content:
|
||||
Then, create the file `src/modules/erp/service.ts` with the following content:
|
||||
|
||||
export const serviceHighlights = [
|
||||
["4", "ErpModuleOptions", "The module's expected options."],
|
||||
@@ -56,12 +56,12 @@ export const serviceHighlights = [
|
||||
import axios, { AxiosInstance } from "axios"
|
||||
import { ProductDTO } from "@medusajs/types"
|
||||
|
||||
type ErpModuleOptions = {
|
||||
apiKey: string
|
||||
}
|
||||
type ErpModuleOptions = {
|
||||
apiKey: string
|
||||
}
|
||||
|
||||
class ErpModuleService {
|
||||
private client_: AxiosInstance
|
||||
class ErpModuleService {
|
||||
private client\_: AxiosInstance
|
||||
|
||||
constructor({}, { apiKey }: ErpModuleOptions) {
|
||||
this.client_ = axios.create({
|
||||
@@ -74,66 +74,68 @@ export const serviceHighlights = [
|
||||
|
||||
async getProductData(id: string) {
|
||||
const { data: erpProduct } = await this.client_.get(`/product/${id}`)
|
||||
|
||||
|
||||
return erpProduct
|
||||
}
|
||||
|
||||
async createProduct(data: ProductDTO) {
|
||||
const { data: erpProduct } = await this.client_.post(`/product`, data)
|
||||
|
||||
|
||||
return erpProduct
|
||||
}
|
||||
|
||||
async deleteProduct(id: string) {
|
||||
await this.client_.delete(`/product/${id}`)
|
||||
}
|
||||
}
|
||||
|
||||
export default ErpModuleService
|
||||
```
|
||||
}
|
||||
|
||||
This creates the module's main service. Few things to note:
|
||||
export default ErpModuleService
|
||||
|
||||
- The module accepts an `apiKey` option, used to authenticate to the dummy ERP system. The module's main service accesses this option in the second parameter of the constructor.
|
||||
- The module uses axios to create a client in the constructor. The client is used in the service's methods when connecting to the ERP system. If the system you're integrating has an SDK, you can initialize it in the constructor, instead.
|
||||
- The `getProductData` method retrieves a product's details from the ERP system by sending a `GET` request using the client.
|
||||
- The `createProduct` method creates a product in the ERP system by sending a `POST` request using the client.
|
||||
- The `deleteProduct` method deletes a product in the ERP system by sending a `DELETE` request using the client.
|
||||
````
|
||||
|
||||
<Note title="Tip">
|
||||
This creates the module's main service. Few things to note:
|
||||
|
||||
You can store the product's ID in the external system using the `metadata` property of the `Product` data model in the Product Module. Alternatively, you can create a [data model](!docs!/basics/data-models) in your module to store data related to the external system.
|
||||
- The module accepts an `apiKey` option, used to authenticate to the dummy ERP system. The module's main service accesses this option in the second parameter of the constructor.
|
||||
- The module uses axios to create a client in the constructor. The client is used in the service's methods when connecting to the ERP system. If the system you're integrating has an SDK, you can initialize it in the constructor, instead.
|
||||
- The `getProductData` method retrieves a product's details from the ERP system by sending a `GET` request using the client.
|
||||
- The `createProduct` method creates a product in the ERP system by sending a `POST` request using the client.
|
||||
- The `deleteProduct` method deletes a product in the ERP system by sending a `DELETE` request using the client.
|
||||
|
||||
</Note>
|
||||
<Note title="Tip">
|
||||
|
||||
Then, create the module's definition file at `src/modules/erp/index.ts` with the following content:
|
||||
You can store the product's ID in the external system using the `metadata` property of the `Product` data model in the Product Module. Alternatively, you can create a [data model](!docs!/basics/data-models) in your module to store data related to the external system.
|
||||
|
||||
```ts title="src/modules/erp/index.ts"
|
||||
import ErpModuleService from "./service"
|
||||
|
||||
export default {
|
||||
service: ErpModuleService,
|
||||
}
|
||||
```
|
||||
</Note>
|
||||
|
||||
Finally, add the module to the `modules` object in `medusa-config.js`:
|
||||
Then, create the module's definition file at `src/modules/erp/index.ts` with the following content:
|
||||
|
||||
```js title="medusa-config.js" highlights={[["7", "ERP_API_KEY", "The environment variable holding the API key of the ERP system."]]}
|
||||
module.exports = defineConfig({
|
||||
// ...
|
||||
modules: {
|
||||
erpModuleService: {
|
||||
resolve: "./modules/erp",
|
||||
options: {
|
||||
apiKey: process.env.ERP_API_KEY,
|
||||
},
|
||||
```ts title="src/modules/erp/index.ts"
|
||||
import ErpModuleService from "./service"
|
||||
|
||||
export default {
|
||||
service: ErpModuleService,
|
||||
}
|
||||
````
|
||||
|
||||
Finally, add the module to the `modules` object in `medusa-config.js`:
|
||||
|
||||
```js title="medusa-config.js" highlights={[["7", "ERP_API_KEY", "The environment variable holding the API key of the ERP system."]]}
|
||||
module.exports = defineConfig({
|
||||
// ...
|
||||
modules: {
|
||||
erpModuleService: {
|
||||
resolve: "./modules/erp",
|
||||
options: {
|
||||
apiKey: process.env.ERP_API_KEY,
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
</Details>
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
@@ -167,12 +169,28 @@ export const workflowHighlights = [
|
||||
["24", "productModuleService", "Resolve the Product Module's main service."],
|
||||
["28", "retrieve", "Retrieve the created product's data."],
|
||||
["30", "createProduct", "Create the product in the ERP system."],
|
||||
["34", "update", "Update the product in Medusa with the ID of the ERP product."],
|
||||
[
|
||||
"34",
|
||||
"update",
|
||||
"Update the product in Medusa with the ID of the ERP product.",
|
||||
],
|
||||
["43", "erpId", "Pass the ERP product's ID to the compensation function."],
|
||||
["44", "productId", "Pass the product's ID to the compensation function."],
|
||||
["46", "", "Define a compensation function that rolls back changes when an error occurs."],
|
||||
["53", "deleteProduct", "Undo creating the product in the ERP system by deleting it."],
|
||||
["54", "update", "Update the product in Medusa to remove the ERP product's ID."]
|
||||
[
|
||||
"46",
|
||||
"",
|
||||
"Define a compensation function that rolls back changes when an error occurs.",
|
||||
],
|
||||
[
|
||||
"53",
|
||||
"deleteProduct",
|
||||
"Undo creating the product in the ERP system by deleting it.",
|
||||
],
|
||||
[
|
||||
"54",
|
||||
"update",
|
||||
"Update the product in Medusa to remove the ERP product's ID.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/create-product.ts" highlights={workflowHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
@@ -182,7 +200,7 @@ export const workflowHighlights = [
|
||||
createWorkflow
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import ErpModuleService from "../modules/erp/service"
|
||||
|
||||
type WorkflowInput = {
|
||||
@@ -194,14 +212,14 @@ export const workflowHighlights = [
|
||||
}
|
||||
|
||||
const createInErpStep = createStep(
|
||||
"create-in-erp",
|
||||
"create-in-erp",
|
||||
async ({ productId }: WorkflowInput, { container }) => {
|
||||
const erpModuleService: ErpModuleService = container.resolve(
|
||||
"erpModuleService"
|
||||
)
|
||||
const productModuleService: IProductModuleService = container
|
||||
.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
|
||||
const createdProductData = await productModuleService
|
||||
.retrieveProduct(productId)
|
||||
|
||||
@@ -214,7 +232,7 @@ export const workflowHighlights = [
|
||||
erp_id: erpProduct.id
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return new StepResponse({
|
||||
erpProduct
|
||||
}, {
|
||||
@@ -250,20 +268,20 @@ export const workflowHighlights = [
|
||||
- Retrieves the product's data using the Product Module's main service.
|
||||
- Create the product in the ERP system using the ERP Module's main service.
|
||||
- Updates the product in Medusa by setting the ID of the ERP product in the product's `metadata` property.
|
||||
|
||||
|
||||
The step also has a compensation function that rolls back changes when an error occurs. It deletes the product in the ERP system and removes the ID of the ERP product in the Medusa product.
|
||||
|
||||
|
||||
Then, create the subscriber at `src/subscribers/create-product.ts`:
|
||||
|
||||
```ts title="src/subscribers/create-product.ts"
|
||||
import type {
|
||||
SubscriberConfig,
|
||||
import type {
|
||||
SubscriberConfig,
|
||||
SubscriberArgs,
|
||||
} from "@medusajs/medusa"
|
||||
import createProductWorkflow from "../workflows/create-product"
|
||||
|
||||
export default async function handleProductUpdate({
|
||||
data, container
|
||||
export default async function handleProductUpdate({
|
||||
data, container
|
||||
}: SubscriberArgs<{id: string}>) {
|
||||
createProductWorkflow(container)
|
||||
.run({
|
||||
@@ -306,12 +324,12 @@ For example, suppose an administrator changes the product data in the ERP system
|
||||
For example, create the file `src/api/webhooks/erp/update/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/webhooks/erp/update/route.ts" collapsibleLines="1-12" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
import {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import {
|
||||
IProductModuleService,
|
||||
import {
|
||||
IProductModuleService,
|
||||
UpdateProductDTO
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
@@ -324,7 +342,7 @@ For example, suppose an administrator changes the product data in the ERP system
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest<WebhookReq>,
|
||||
req: MedusaRequest<WebhookReq>,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const { id, updatedData} = req.body
|
||||
@@ -339,15 +357,15 @@ For example, suppose an administrator changes the product data in the ERP system
|
||||
res.status(200)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
This creates a webhook listener for an ERP system. It receives the ID of a product and its updated data, assuming that’s how your ERP system sends the data.
|
||||
|
||||
|
||||
Then, create the file `src/api/middlewares.ts` with the following content:
|
||||
|
||||
```ts title="src/api/middlewares.ts"
|
||||
import { MiddlewaresConfig } from "@medusajs/medusa"
|
||||
import { raw } from "body-parser"
|
||||
|
||||
|
||||
export const config: MiddlewaresConfig = {
|
||||
routes: [
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -51,65 +51,57 @@ To search through product variants by their barcode, create a custom API Route a
|
||||
|
||||

|
||||
|
||||
<CardList itemsPerRow={2} items={[
|
||||
{
|
||||
href: "/commerce-modules/product",
|
||||
title: "Product Module",
|
||||
text: "Learn about the Product Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
{
|
||||
href: "!docs!/basics/api-routes",
|
||||
title: "Create API Route",
|
||||
text: "Learn how to create an API Route.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
]} />
|
||||
<CardList
|
||||
itemsPerRow={2}
|
||||
items={[
|
||||
{
|
||||
href: "/commerce-modules/product",
|
||||
title: "Product Module",
|
||||
text: "Learn about the Product Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
{
|
||||
href: "!docs!/basics/api-routes",
|
||||
title: "Create API Route",
|
||||
text: "Learn how to create an API Route.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
<Details summaryContent="Example: Search Products By Barcode API Route">
|
||||
|
||||
Here’s an example of creating a custom API Route at `/store/pos/search-barcode` that searches product variants by a barcode:
|
||||
Here’s an example of creating a custom API Route at `/store/pos/search-barcode` that searches product variants by a barcode:
|
||||
|
||||
```ts title="src/api/store/pos/search-barcode/route.ts" collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
```ts title="src/api/store/pos/search-barcode/route.ts" collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
|
||||
export const GET = async (
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const barcode = (req.query.barcode as string) || ""
|
||||
if (!barcode) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Barcode is required"
|
||||
)
|
||||
}
|
||||
// get product service
|
||||
const productModuleService: IProductModuleService =
|
||||
req.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
// retrieve product variants by barcode
|
||||
const productVariants = await productModuleService
|
||||
.listProductVariants({
|
||||
// @ts-ignore
|
||||
barcode,
|
||||
})
|
||||
|
||||
res.json({
|
||||
product_variants: productVariants,
|
||||
})
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const barcode = (req.query.barcode as string) || ""
|
||||
if (!barcode) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, "Barcode is required")
|
||||
}
|
||||
```
|
||||
// get product service
|
||||
const productModuleService: IProductModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
// retrieve product variants by barcode
|
||||
const productVariants = await productModuleService.listProductVariants({
|
||||
// @ts-ignore
|
||||
barcode,
|
||||
})
|
||||
|
||||
res.json({
|
||||
product_variants: productVariants,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</Details>
|
||||
|
||||
@@ -127,29 +119,31 @@ Merchants can create a sales channel for their online store and a sales channel
|
||||
|
||||
This also opens the door for other business opportunities, such as an endless aisle experience. Suppose a product isn't available in-store but is available in different warehouses. You can allow customers to purchase that item in-store and deliver it to their address.
|
||||
|
||||
<CardList items={[
|
||||
{
|
||||
href: "/commerce-modules/inventory",
|
||||
title: "Inventory Module",
|
||||
text: "Learn about the Inventory Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
{
|
||||
href: "/commerce-modules/stock-location",
|
||||
title: "Stock Location Module",
|
||||
text: "Learn about the Stock Location Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
{
|
||||
href: "/commerce-modules/sales-channel",
|
||||
title: "Sales Channel Module",
|
||||
text: "Learn about the Sales Channel Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
]} />
|
||||
<CardList
|
||||
items={[
|
||||
{
|
||||
href: "/commerce-modules/inventory",
|
||||
title: "Inventory Module",
|
||||
text: "Learn about the Inventory Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
{
|
||||
href: "/commerce-modules/stock-location",
|
||||
title: "Stock Location Module",
|
||||
text: "Learn about the Stock Location Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
{
|
||||
href: "/commerce-modules/sales-channel",
|
||||
title: "Sales Channel Module",
|
||||
text: "Learn about the Sales Channel Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
---
|
||||
|
||||
@@ -161,29 +155,31 @@ In addition, using Medusa's Promotion Module, store operators can create promoti
|
||||
|
||||
You can also create custom modules to provide features for a better customer experience, such as a rewards system or loyalty points.
|
||||
|
||||
<CardList items={[
|
||||
{
|
||||
href: "/commerce-modules/customer",
|
||||
title: "Customer Module",
|
||||
text: "Learn about the Customer Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
{
|
||||
href: "/commerce-modules/promotion",
|
||||
title: "Promotion Module",
|
||||
text: "Learn about the Promotion Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
{
|
||||
href: "!docs!/basics/modules-and-services",
|
||||
title: "Create a Module",
|
||||
text: "Learn how to create a module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
]} />
|
||||
<CardList
|
||||
items={[
|
||||
{
|
||||
href: "/commerce-modules/customer",
|
||||
title: "Customer Module",
|
||||
text: "Learn about the Customer Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
{
|
||||
href: "/commerce-modules/promotion",
|
||||
title: "Promotion Module",
|
||||
text: "Learn about the Promotion Module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
{
|
||||
href: "!docs!/basics/modules-and-services",
|
||||
title: "Create a Module",
|
||||
text: "Learn how to create a module.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
---
|
||||
|
||||
@@ -195,19 +191,21 @@ Once you accept the payment, place an order in the POS system using the [Draft O
|
||||
|
||||
Then, merchants can view all orders coming from different sales channels using the Medusa Admin. This keeps logistics and order handling consistent between online and in-store customers.
|
||||
|
||||
<CardList items={[
|
||||
{
|
||||
href: "/references/payment/provider",
|
||||
title: "Create a Payment Module Provider",
|
||||
text: "Learn how to create a payment module provider.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
{
|
||||
href: "!api!/admin",
|
||||
title: "Admin REST APIs",
|
||||
text: "Check out available Admin REST APIs.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false
|
||||
},
|
||||
]} />
|
||||
<CardList
|
||||
items={[
|
||||
{
|
||||
href: "/references/payment/provider",
|
||||
title: "Create a Payment Module Provider",
|
||||
text: "Learn how to create a payment module provider.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
{
|
||||
href: "!api!/admin",
|
||||
title: "Admin REST APIs",
|
||||
text: "Check out available Admin REST APIs.",
|
||||
startIcon: <AcademicCapSolid />,
|
||||
showLinkIcon: false,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
Reference in new issue
Block a user