feat: Add tax region + rates to SDK and types (#7635)
* feat: Add tax region + rates to SDK and types * replace client with sdk in loaders * Address PR feedback
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
AdminPostTaxRatesReq,
|
||||
AdminPostTaxRatesTaxRateReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { AdminTaxRateListResponse, AdminTaxRateResponse } from "@medusajs/types"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
@@ -10,10 +6,9 @@ import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { TaxRateDeleteRes } from "../../types/api-responses"
|
||||
|
||||
const TAX_RATES_QUERY_KEY = "tax_rates" as const
|
||||
export const taxRatesQueryKeys = queryKeysFactory(TAX_RATES_QUERY_KEY)
|
||||
@@ -23,9 +18,9 @@ export const useTaxRate = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminTaxRateResponse,
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
Error,
|
||||
AdminTaxRateResponse,
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
@@ -33,7 +28,7 @@ export const useTaxRate = (
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: taxRatesQueryKeys.detail(id),
|
||||
queryFn: async () => client.taxes.retrieveTaxRate(id, query),
|
||||
queryFn: async () => sdk.admin.taxRate.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
@@ -44,16 +39,16 @@ export const useTaxRates = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminTaxRateListResponse,
|
||||
HttpTypes.AdminTaxRateListResponse,
|
||||
Error,
|
||||
AdminTaxRateListResponse,
|
||||
HttpTypes.AdminTaxRateListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.taxes.listTaxRates(query),
|
||||
queryFn: () => sdk.admin.taxRate.list(query),
|
||||
queryKey: taxRatesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
@@ -64,13 +59,13 @@ export const useTaxRates = (
|
||||
export const useUpdateTaxRate = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
AdminTaxRateResponse,
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
Error,
|
||||
AdminPostTaxRatesTaxRateReq
|
||||
HttpTypes.AdminUpdateTaxRate
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.taxes.updateTaxRate(id, payload),
|
||||
mutationFn: (payload) => sdk.admin.taxRate.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
@@ -85,13 +80,13 @@ export const useUpdateTaxRate = (
|
||||
|
||||
export const useCreateTaxRate = (
|
||||
options?: UseMutationOptions<
|
||||
AdminTaxRateResponse,
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
Error,
|
||||
AdminPostTaxRatesReq
|
||||
HttpTypes.AdminCreateTaxRate
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.taxes.createTaxRate(payload),
|
||||
mutationFn: (payload) => sdk.admin.taxRate.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
@@ -102,10 +97,14 @@ export const useCreateTaxRate = (
|
||||
|
||||
export const useDeleteTaxRate = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<TaxRateDeleteRes, Error, void>
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRateDeleteResponse,
|
||||
Error,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.taxes.deleteTaxRate(id),
|
||||
mutationFn: () => sdk.admin.taxRate.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import { AdminCreateTaxRegion } from "@medusajs/medusa"
|
||||
import {
|
||||
AdminTaxRegionListResponse,
|
||||
AdminTaxRegionResponse,
|
||||
} from "@medusajs/types"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
@@ -10,10 +6,9 @@ import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { TaxRegionDeleteRes } from "../../types/api-responses"
|
||||
|
||||
const TAX_REGIONS_QUERY_KEY = "tax_regions" as const
|
||||
export const taxRegionsQueryKeys = queryKeysFactory(TAX_REGIONS_QUERY_KEY)
|
||||
@@ -23,9 +18,9 @@ export const useTaxRegion = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminTaxRegionResponse,
|
||||
HttpTypes.AdminTaxRegionResponse,
|
||||
Error,
|
||||
AdminTaxRegionResponse,
|
||||
HttpTypes.AdminTaxRegionResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
@@ -33,7 +28,7 @@ export const useTaxRegion = (
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: taxRegionsQueryKeys.detail(id),
|
||||
queryFn: async () => client.taxes.retrieveTaxRegion(id, query),
|
||||
queryFn: async () => sdk.admin.taxRegion.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
@@ -44,16 +39,16 @@ export const useTaxRegions = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminTaxRegionListResponse,
|
||||
HttpTypes.AdminTaxRegionListResponse,
|
||||
Error,
|
||||
AdminTaxRegionListResponse,
|
||||
HttpTypes.AdminTaxRegionListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.taxes.listTaxRegions(query),
|
||||
queryFn: () => sdk.admin.taxRegion.list(query),
|
||||
queryKey: taxRegionsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
@@ -63,13 +58,13 @@ export const useTaxRegions = (
|
||||
|
||||
export const useCreateTaxRegion = (
|
||||
options?: UseMutationOptions<
|
||||
AdminTaxRegionResponse,
|
||||
HttpTypes.AdminTaxRegionResponse,
|
||||
Error,
|
||||
AdminCreateTaxRegion
|
||||
HttpTypes.AdminCreateTaxRegion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.taxes.createTaxRegion(payload),
|
||||
mutationFn: (payload) => sdk.admin.taxRegion.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.all })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
@@ -80,10 +75,14 @@ export const useCreateTaxRegion = (
|
||||
|
||||
export const useDeleteTaxRegion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<TaxRegionDeleteRes, Error, void>
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRegionDeleteResponse,
|
||||
Error,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.taxes.deleteTaxRegion(id),
|
||||
mutationFn: () => sdk.admin.taxRegion.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
|
||||
@@ -20,7 +20,6 @@ import { shippingProfiles } from "./shipping-profiles"
|
||||
import { stockLocations } from "./stock-locations"
|
||||
import { stores } from "./stores"
|
||||
import { tags } from "./tags"
|
||||
import { taxes } from "./taxes"
|
||||
import { users } from "./users"
|
||||
import { workflowExecutions } from "./workflow-executions"
|
||||
|
||||
@@ -40,7 +39,6 @@ export const client = {
|
||||
shippingProfiles: shippingProfiles,
|
||||
productTags: tags,
|
||||
users: users,
|
||||
taxes: taxes,
|
||||
invites: invites,
|
||||
inventoryItems: inventoryItems,
|
||||
reservations: reservations,
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import {
|
||||
AdminCreateTaxRegion,
|
||||
AdminPostTaxRatesTaxRateReq,
|
||||
} from "@medusajs/medusa"
|
||||
import {
|
||||
AdminTaxRateResponse,
|
||||
AdminTaxRegionListResponse,
|
||||
AdminTaxRegionResponse,
|
||||
} from "@medusajs/types"
|
||||
import { TaxRateDeleteRes, TaxRegionDeleteRes } from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveTaxRegion(id: string, query?: Record<string, any>) {
|
||||
return getRequest<AdminTaxRegionResponse>(`/admin/tax-regions/${id}`, query)
|
||||
}
|
||||
|
||||
async function listTaxRegions(query?: Record<string, any>) {
|
||||
return getRequest<AdminTaxRegionListResponse>(`/admin/tax-regions`, query)
|
||||
}
|
||||
|
||||
async function createTaxRegion(payload: AdminCreateTaxRegion) {
|
||||
return postRequest<AdminTaxRegionResponse>(`/admin/tax-regions`, payload)
|
||||
}
|
||||
|
||||
async function deleteTaxRegion(id: string) {
|
||||
return deleteRequest<TaxRegionDeleteRes>(`/admin/tax-regions/${id}`)
|
||||
}
|
||||
|
||||
async function retrieveTaxRate(id: string, query?: Record<string, any>) {
|
||||
return getRequest<AdminTaxRegionResponse>(`/admin/tax-rates/${id}`, query)
|
||||
}
|
||||
|
||||
async function listTaxRates(query?: Record<string, any>) {
|
||||
return getRequest<AdminTaxRegionListResponse>(`/admin/tax-rates`, query)
|
||||
}
|
||||
|
||||
async function updateTaxRate(id: string, payload: AdminPostTaxRatesTaxRateReq) {
|
||||
return postRequest<AdminTaxRateResponse>(`/admin/tax-rates/${id}`, payload)
|
||||
}
|
||||
|
||||
async function createTaxRate(payload: AdminCreateTaxRate) {
|
||||
return postRequest<AdminTaxRateResponse>(`/admin/tax-rates`, payload)
|
||||
}
|
||||
|
||||
async function deleteTaxRate(id: string) {
|
||||
return deleteRequest<TaxRateDeleteRes>(`/admin/tax-rates/${id}`)
|
||||
}
|
||||
|
||||
export const taxes = {
|
||||
retrieveTaxRegion,
|
||||
listTaxRegions,
|
||||
retrieveTaxRate,
|
||||
listTaxRates,
|
||||
updateTaxRate,
|
||||
createTaxRegion,
|
||||
deleteTaxRegion,
|
||||
createTaxRate,
|
||||
deleteTaxRate,
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
import { LoaderFunctionArgs } from "react-router-dom"
|
||||
|
||||
import { AdminTaxRateResponse } from "@medusajs/types"
|
||||
import { LoaderFunctionArgs } from "react-router-dom"
|
||||
import { taxRatesQueryKeys } from "../../../hooks/api/tax-rates"
|
||||
import { client } from "../../../lib/client"
|
||||
import { sdk } from "../../../lib/client"
|
||||
import { queryClient } from "../../../lib/query-client"
|
||||
|
||||
const taxRateDetailQuery = (id: string) => ({
|
||||
queryKey: taxRatesQueryKeys.detail(id),
|
||||
queryFn: async () => client.taxes.retrieveTaxRate(id),
|
||||
queryFn: async () => sdk.admin.taxRate.retrieve(id),
|
||||
})
|
||||
|
||||
export const taxRateLoader = async ({ params }: LoaderFunctionArgs) => {
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { LoaderFunctionArgs } from "react-router-dom"
|
||||
|
||||
import { AdminTaxRegionResponse } from "@medusajs/types"
|
||||
import { LoaderFunctionArgs } from "react-router-dom"
|
||||
import { taxRegionsQueryKeys } from "../../../hooks/api/tax-regions"
|
||||
import { client } from "../../../lib/client"
|
||||
import { sdk } from "../../../lib/client"
|
||||
import { queryClient } from "../../../lib/query-client"
|
||||
|
||||
const taxRegionDetailQuery = (id: string) => ({
|
||||
queryKey: taxRegionsQueryKeys.detail(id),
|
||||
queryFn: async () => client.taxes.retrieveTaxRegion(id),
|
||||
queryFn: async () => sdk.admin.taxRegion.retrieve(id),
|
||||
})
|
||||
|
||||
export const taxRegionLoader = async ({ params }: LoaderFunctionArgs) => {
|
||||
|
||||
Reference in New Issue
Block a user