Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document JS SDK installation #9611

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions www/apps/api-reference/app/_mdx/client-libraries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ Check out the [Medusa v2 Documentation](https://docs.medusajs.com/v2).

<Space bottom={8} />

<Note type="soon">
<H3 className="!mt-0">Medusa JS SDK</H3>

JavaScript client libraries are coming soon for Medusa v2.
To use Medusa's JS SDK library, install the following packages in your project (not required for admin customizations):

</Note>
```bash
npm install @medusajs/js-sdk@rc @medusajs/types@rc
```

Learn more about the JS SDK in [this documentation](https://docs.medusajs.com/v2/resources/js-sdk).

### Download Full Reference

Expand Down
119 changes: 94 additions & 25 deletions www/apps/book/app/learn/advanced-development/admin/tips/page.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CodeTabs, CodeTab } from "docs-ui"

export const metadata = {
title: `${pageNumber} Admin Development Tips`,
}
Expand All @@ -8,54 +10,121 @@ In this chapter, you'll find some tips for your admin development.

## Send Requests to API Routes

To send a request to an API route in the Medusa Application, use the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).
To send a request to an API route in the Medusa Application, use Medusa's [JS SDK](!resources!/js-sdk) with [Tanstack Query](https://tanstack.com/query/latest). Both of these tools are installed in your project by default.

First, create the file `src/admin/lib/config.ts` to setup the SDK for use in your customizations:

```ts
import Medusa from "@medusajs/js-sdk"

export const sdk = new Medusa({
baseUrl: "http://localhost:9000",
debug: process.env.NODE_ENV === "development",
auth: {
type: "session",
},
})
```

<Note>

Learn more about the JS SDK's configurations [this documentation](!resources!/js-sdk#js-sdk-configurations).

</Note>

Then, use the configured SDK with the `useQuery` Tanstack Query hook to send `GET` requests, and `useMutation` hook to send `POST` or `DELETE` requests.

For example:

export const fetchHighlights = [
["14", "fetch", "Send a request to the `/admin/products` API route."]
<CodeTabs group="query-type">
<CodeTab label="Query" value="query">

export const queryHighlights = [
["8", "useQuery", "Use Tanstack Query's `useQuery` to send a `GET` request."],
["9", "sdk.admin.product.list", "Use the SDK to send the request."],
["10", "queryKey", "Specify the key used to cache data."]
]

```tsx title="src/admin/widgets/product-widget.tsx" highlights={fetchHighlights}
```tsx title="src/admin/widgets/product-widget.ts" highlights={queryHighlights}
import { defineWidgetConfig } from "@medusajs/admin-sdk"
import { Container } from "@medusajs/ui"
import { useEffect, useState } from "react"
import { Button, Container } from "@medusajs/ui"
import { useQuery } from "@tanstack/react-query"
import { sdk } from "../lib/config"
import { DetailWidgetProps, HttpTypes } from "@medusajs/framework/types"

const ProductWidget = () => {
const [productsCount, setProductsCount] = useState(0)
const [loading, setLoading] = useState(true)
const { data, isLoading } = useQuery({
queryFn: () => sdk.admin.product.list(),
queryKey: ["products"]
})

return (
<Container className="divide-y p-0">
{isLoading && <span>Loading...</span>}
{data?.products && (
<ul>
{data.products.map((product) => (
<li key={product.id}>{product.title}</li>
))}
</ul>
)}
</Container>
)
}

useEffect(() => {
if (!loading) {
return
}
export const config = defineWidgetConfig({
zone: "product.list.before",
})

fetch(`/admin/products`, {
credentials: "include",
})
.then((res) => res.json())
.then(({ count }) => {
setProductsCount(count)
setLoading(false)
})
}, [loading])
export default ProductWidget
```

</CodeTab>
<CodeTab label="Mutation" value="mutation">

export const mutationHighlights = [
["10", "useMutation", "Use Tanstack Query's `useMutation` to send `POST` or `DELETE` requests."],
["12", "sdk.admin.product.update", "Use the configured SDK to send the request."],
]

```tsx title="src/admin/widgets/product-widget.ts" highlights={mutationHighlights}
import { defineWidgetConfig } from "@medusajs/admin-sdk"
import { Button, Container } from "@medusajs/ui"
import { useMutation } from "@tanstack/react-query"
import { sdk } from "../lib/config"
import { DetailWidgetProps, HttpTypes } from "@medusajs/framework/types"

const ProductWidget = ({
data: productData
}: DetailWidgetProps<HttpTypes.AdminProduct>) => {
const { mutateAsync } = useMutation({
mutationFn: (payload: HttpTypes.AdminUpdateProduct) =>
sdk.admin.product.update(productData.id, payload),
onSuccess: () => alert("updated product")
})

const handleUpdate = () => {
mutateAsync({
title: "New Product Title"
})
}

return (
<Container className="divide-y p-0">
{loading && <span>Loading...</span>}
{!loading && <span>You have {productsCount} Product(s).</span>}
<Button onClick={handleUpdate}>Update Title</Button>
</Container>
)
}

export const config = defineWidgetConfig({
zone: "product.list.before",
zone: "product.details.before",
})

export default ProductWidget
```

In this example, you send a request to the [List Products API route](!api!/admin#products_getproducts) and show the count of products in a widget.
</CodeTab>
</CodeTabs>

---

Expand Down
2 changes: 1 addition & 1 deletion www/apps/book/app/learn/storefront-development/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You're free to choose how to build your storefront. You can start with our Next.

<Note title="Tip">

To learn how to build a storefront from scratch, check out the [Storefront Development guides](!resources!/storefront-development) in the Development Resources documentation.
To learn how to build a storefront from scratch, check out the [Storefront Development guides](!resources!/storefront-development).

</Note>

Expand Down
Loading
Loading