Skip to content

Commit

Permalink
feat(medusa-js, medusa-react): Support expand fields in order retriev…
Browse files Browse the repository at this point in the history
…al (#2353)
  • Loading branch information
pKorsholm authored and adrien2p committed Oct 11, 2022
1 parent 3df8f70 commit 9e7b635
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/sour-beers-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/medusa-js": patch
"medusa-react": patch
---

feat(medusa-js, medusa-react): add expand fields to get order
10 changes: 9 additions & 1 deletion packages/medusa-js/src/resources/admin/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AdminPostOrdersOrderSwapsSwapFulfillmentsReq,
AdminPostOrdersOrderSwapsSwapShipmentsReq,
} from "@medusajs/medusa"
import { FindParams } from "@medusajs/medusa/dist/types/common"
import qs from "qs"
import { ResponsePromise } from "../../typings"
import BaseResource from "../base"
Expand All @@ -32,9 +33,16 @@ class AdminOrdersResource extends BaseResource {

retrieve(
id: string,
query?: FindParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}`
let path = `/admin/orders/${id}`

if (query) {
const queryString = qs.stringify(query)
path = `/admin/orders/${id}?${queryString}`
}

return this.client.request("GET", path, undefined, {}, customHeaders)
}

Expand Down
15 changes: 11 additions & 4 deletions packages/medusa-react/src/hooks/admin/orders/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import {
AdminGetOrdersParams,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { FindParams } from "@medusajs/medusa/dist/types/common"
import { useQuery } from "react-query"
import { useMedusa } from "../../../contexts"
import { UseQueryOptionsWrapper } from "../../../types"
import { queryKeysFactory } from "../../utils/index"

const ADMIN_ORDERS_QUERY_KEY = `admin_orders` as const

export const adminOrderKeys = queryKeysFactory(ADMIN_ORDERS_QUERY_KEY)
export const adminOrderKeys = {
...queryKeysFactory(ADMIN_ORDERS_QUERY_KEY),
detailOrder(id: string, query?: FindParams) {
return [...this.detail(id), { ...(query || {}) }]
}
}

type OrderQueryKeys = typeof adminOrderKeys

Expand All @@ -34,16 +40,17 @@ export const useAdminOrders = (

export const useAdminOrder = (
id: string,
query?: FindParams,
options?: UseQueryOptionsWrapper<
Response<AdminOrdersRes>,
Error,
ReturnType<OrderQueryKeys["detail"]>
ReturnType<OrderQueryKeys["detailOrder"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
adminOrderKeys.detail(id),
() => client.admin.orders.retrieve(id),
adminOrderKeys.detailOrder(id, query),
() => client.admin.orders.retrieve(id, query),
options
)
return { ...data, ...rest } as const
Expand Down

0 comments on commit 9e7b635

Please sign in to comment.