258 lines
5.8 KiB
Plaintext
258 lines
5.8 KiB
Plaintext
import { CodeTabs, CodeTab } from "docs-ui"
|
||
|
||
export const metadata = {
|
||
title: `Examples of the Stock Location Module`,
|
||
}
|
||
|
||
# {metadata.title}
|
||
|
||
In this document, you’ll find common examples of how you can use the Stock Location Module in your application.
|
||
|
||
## Create a Stock Location
|
||
|
||
<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"
|
||
|
||
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,
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
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 })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## List Stock Locations
|
||
|
||
<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"
|
||
|
||
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({}),
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
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({}),
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## Add Address to Stock Location
|
||
|
||
<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"
|
||
|
||
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,
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
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,
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## Delete a Stock Location
|
||
|
||
<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"
|
||
|
||
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)
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
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)
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## More Examples
|
||
|
||
The [module interface reference](/references/stock-location) provides a reference to all the methods available for use with examples for each.
|