Examples of the Stock Location Module
In this document, you’ll find common examples of how you can use the Stock Location Module in your application.
Create a Stock Location
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStockLocationService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
const stockLocation = await stockLocationModuleService.create(
{
name: request.body.name,
}
)
res.json({
stock_location: stockLocation,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStockLocationModule,
} from "@medusajs/stock-location-next"
export async function POST(request: Request) {
const stockLocationModuleService =
await initializeStockLocationModule({})
const body = await request.json()
const stockLocation = await stockLocationModuleService.create(
{
name: body.name,
}
)
return NextResponse.json({ stock_location: stockLocation })
}
List Stock Locations
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStockLocationService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
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.list({}),
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStockLocationModule,
} from "@medusajs/stock-location-next"
export async function GET(request: Request) {
const stockLocationModuleService =
await initializeStockLocationModule({})
return NextResponse.json({
stock_locations: await stockLocationModuleService.list({}),
})
}
Add Address to Stock Location
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStockLocationService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
const stockLocation = await stockLocationModuleService.update(
{
id: request.params.id,
address: {
country_code: request.body.country_code,
city: request.body.city,
address_1: request.body.address_1,
postal_code: request.body.postal_code,
},
}
)
res.json({
stock_location: stockLocation,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStockLocationModule,
} from "@medusajs/stock-location-next"
type ContextType = {
params: {
id: string
}
}
export async function POST(
request: Request,
{ params }: ContextType
) {
const stockLocationModuleService =
await initializeStockLocationModule({})
const body = await request.json()
const stockLocation = await stockLocationModuleService.update(
{
id: params.id,
address: {
country_code: body.country_code,
city: body.city,
address_1: body.address_1,
postal_code: body.postal_code,
},
}
)
return NextResponse.json({
stock_location: stockLocation,
})
}
Delete a Stock Location
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStockLocationService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)
await stockLocationModuleService.delete(request.params.id)
res.status(200)
}
import { NextResponse } from "next/server"
import {
initialize as initializeStockLocationModule,
} from "@medusajs/stock-location-next"
type ContextType = {
params: {
id: string
}
}
export async function DELETE(
request: Request,
{ params }: ContextType
) {
const stockLocationModuleService =
await initializeStockLocationModule({})
await stockLocationModuleService.delete(params.id)
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?