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
TResponse
objectRequiredThe response's type which defaults to
any
.Hook Parameters
path
stringRequiredThe path to the custom endpoint.
queryKey
QueryKeyRequiredA list of query keys, used to invalidate data.
relatedDomains
RelatedDomainsA list of related domains that should be invalidated and refetch when the mutation
function is invoked.
Mutation Function Returned Data
TResponse
TResponseRequiredThe 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
TPayload
Record<string, any>RequiredThe type of accepted body parameters which defaults to
Record<string, any>
.TResponse
objectRequiredThe type of response, which defaults to
any
.Hook Parameters
path
stringRequiredThe path to the custom endpoint.
queryKey
QueryKeyRequiredA list of query keys, used to invalidate data.
relatedDomains
RelatedDomainsA list of related domains that should be invalidated and refetch when the mutation
function is invoked.
Mutation Function Parameters
TPayload
TPayloadRequiredThe payload based on the specified type for
Mutation Function Returned Data
TResponse
TResponseRequiredThe 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
TQuery
Record<string, any>RequiredThe type of accepted query parameters which defaults to
Record<string, any>
.TResponse
objectRequiredThe type of response which defaults to
any
.Hook Parameters
path
stringRequiredThe path to the custom endpoint.
queryKey
QueryKeyRequiredA list of query keys, used to invalidate data.
query
TQueryQuery parameters to pass to the request.
Query Returned Data
TResponse
TResponseRequiredThe response based on the type specified for
Was this section helpful?