Files
medusa-store/www/apps/resources/app/commerce-modules/currency/examples/page.mdx
Shahed Nasser 3298cd3fd2 docs: improved commerce module docs [2/n] (#9501)
Improve + add docs for commerce modules from currency to inventory

[2/n]
2024-10-09 09:53:17 +00:00

106 lines
2.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `Examples of the Currency Module`,
}
# {metadata.title}
In this guide, youll find common examples of how you can use the Currency Module in your application.
## List Currencies
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const currencyModuleService = req.scope.resolve(
Modules.CURRENCY
)
res.json({
currencies: await currencyModuleService.listCurrencies(),
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import { initialize as initializeCurrencyModule } from "@medusajs/medusa/currency"
export async function GET(request: Request) {
const currencyModuleService = await initializeCurrencyModule()
return NextResponse.json({
currencies: await currencyModuleService.listCurrencies(),
})
}
```
</CodeTab>
</CodeTabs>
---
## Retrieve a Currency by its Code
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const currencyModuleService = req.scope.resolve(
Modules.CURRENCY
)
const currency = await currencyModuleService.retrieveCurrency("usd")
res.json({
currency,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import { initialize as initializeCurrencyModule } from "@medusajs/medusa/currency"
export async function GET(request: Request) {
const currencyModuleService = await initializeCurrencyModule()
const currency = await currencyModuleService.retrieveCurrency("usd")
return NextResponse.json({ currency })
}
```
</CodeTab>
</CodeTabs>
---
## More Examples
The [Currency Module's main service reference](/references/currency) provides a reference to all the methods available for use with examples for each.