Skip to main content
Skip to main content
You're viewing the documentation for v1, which isn't the latest Medusa version.Latest documentation

Custom

This class is used to send requests custom API Routes. All its method are available in the JS Client under the medusa.admin.custom property.

Mutations​

useAdminCustomDelete​

This hook sends a DELETE request to a custom API Route.

Example​

import React from "react"
import { useAdminCustomDelete } from "medusa-react"

type Props = {
customId: string
}

const Custom = ({ customId }: Props) => {
const customDelete = useAdminCustomDelete(
`/blog/posts/${customId}`,
["posts"]
)

// ...

const handleAction = (title: string) => {
customDelete.mutate(void 0, {
onSuccess: () => {
// Delete action successful
}
})
}

// ...
}

export default Custom

Type Parameters​

TResponseobjectRequired
The response's type which defaults to any.

Hook Parameters​

pathstringRequired
The path to the custom endpoint.
queryKeyQueryKeyRequired
A list of query keys, used to invalidate data.
relatedDomainsRelatedDomains
A list of related domains that should be invalidated and refetch when the mutation function is invoked.

Mutation Function Returned Data​

TResponseTResponseRequired
The response based on the type provided for

useAdminCustomPost​

This hook sends a POST request to a custom API Route.

Example​

import React from "react"
import { useAdminCustomPost } from "medusa-react"
import Post from "./models/Post"

type PostRequest = {
title: string
}
type PostResponse = {
post: Post
}

const Custom = () => {
const customPost = useAdminCustomPost
<PostRequest, PostResponse>(
"/blog/posts",
["posts"]
)

// ...

const handleAction = (title: string) => {
customPost.mutate({
title
}, {
onSuccess: ({ post }) => {
console.log(post)
}
})
}

// ...
}

export default Custom

Type Parameters​

TPayloadRecord<string, any>Required
The type of accepted body parameters which defaults to Record<string, any>.
TResponseobjectRequired
The type of response, which defaults to any.

Hook Parameters​

pathstringRequired
The path to the custom endpoint.
queryKeyQueryKeyRequired
A list of query keys, used to invalidate data.
relatedDomainsRelatedDomains
A list of related domains that should be invalidated and refetch when the mutation function is invoked.

Mutation Function Parameters​

TPayloadTPayloadRequired
The payload based on the specified type for

Mutation Function Returned Data​

TResponseTResponseRequired
The response based on the specified type for

useAdminCustomQuery​

This hook sends a GET request to a custom API Route.

Example​

import React from "react"
import { useAdminCustomQuery } from "medusa-react"
import Post from "./models/Post"

type RequestQuery = {
title: string
}

type ResponseData = {
posts: Post
}

const Custom = () => {
const { data, isLoading } = useAdminCustomQuery
<RequestQuery, ResponseData>(
"/blog/posts",
["posts"],
{
title: "My post"
}
)

return (
<div>
{isLoading && <span>Loading...</span>}
{data?.posts && !data.posts.length && (
<span>No Post</span>
)}
{data?.posts && data.posts?.length > 0 && (
<ul>
{data.posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
)}
</div>
)
}

export default Custom

Type Parameters​

TQueryRecord<string, any>Required
The type of accepted query parameters which defaults to Record<string, any>.
TResponseobjectRequired
The type of response which defaults to any.

Hook Parameters​

pathstringRequired
The path to the custom endpoint.
queryKeyQueryKeyRequired
A list of query keys, used to invalidate data.
queryTQuery
Query parameters to pass to the request.

Query Returned Data​

TResponseTResponseRequired
The response based on the type specified for
Was this section helpful?