feat(sdk): Replace region calls with the SDK in admin, apply typings … (#7371)
This commit is contained in:
7
.changeset/silver-coats-learn.md
Normal file
7
.changeset/silver-coats-learn.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@medusajs/dashboard": patch
|
||||
"@medusajs/js-sdk": patch
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
Replace region calls with the SDK in admin, apply typings to sdk
|
||||
@@ -7,7 +7,7 @@ export const useEmailPassLogin = (
|
||||
options?: UseMutationOptions<void, Error, EmailPassReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.auth.login(payload),
|
||||
mutationFn: (payload) => sdk.auth.login("admin", "emailpass", payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
|
||||
@@ -5,15 +5,10 @@ import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/medusa"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { CreateRegionReq, UpdateRegionReq } from "../../types/api-payloads"
|
||||
import {
|
||||
RegionDeleteRes,
|
||||
RegionListRes,
|
||||
RegionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { DeleteResponse, HttpTypes, PaginatedResponse } from "@medusajs/types"
|
||||
|
||||
const REGIONS_QUERY_KEY = "regions" as const
|
||||
const regionsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY)
|
||||
@@ -22,13 +17,18 @@ export const useRegion = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<RegionRes, Error, RegionRes, QueryKey>,
|
||||
UseQueryOptions<
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
Error,
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: regionsQueryKeys.detail(id),
|
||||
queryFn: async () => client.regions.retrieve(id, query),
|
||||
queryFn: async () => sdk.admin.region.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
@@ -38,12 +38,17 @@ export const useRegion = (
|
||||
export const useRegions = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<RegionListRes, Error, RegionListRes, QueryKey>,
|
||||
UseQueryOptions<
|
||||
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>,
|
||||
Error,
|
||||
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.regions.list(query),
|
||||
queryFn: () => sdk.admin.region.list(query),
|
||||
queryKey: regionsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
@@ -52,10 +57,14 @@ export const useRegions = (
|
||||
}
|
||||
|
||||
export const useCreateRegion = (
|
||||
options?: UseMutationOptions<RegionRes, Error, CreateRegionReq>
|
||||
options?: UseMutationOptions<
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
Error,
|
||||
HttpTypes.AdminCreateRegion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.regions.create(payload),
|
||||
mutationFn: (payload) => sdk.admin.region.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
@@ -66,10 +75,14 @@ export const useCreateRegion = (
|
||||
|
||||
export const useUpdateRegion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<RegionRes, Error, UpdateRegionReq>
|
||||
options?: UseMutationOptions<
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
Error,
|
||||
HttpTypes.AdminUpdateRegion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.regions.update(id, payload),
|
||||
mutationFn: (payload) => sdk.admin.region.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
|
||||
@@ -82,10 +95,10 @@ export const useUpdateRegion = (
|
||||
|
||||
export const useDeleteRegion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<RegionDeleteRes, Error, void>
|
||||
options?: UseMutationOptions<DeleteResponse<"region">, Error, void>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.regions.delete(id),
|
||||
mutationFn: () => sdk.admin.region.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { RegionDTO } from "@medusajs/types"
|
||||
|
||||
import {
|
||||
CountriesCell,
|
||||
@@ -14,8 +13,9 @@ import {
|
||||
RegionCell,
|
||||
RegionHeader,
|
||||
} from "../../../components/table/table-cells/region/region-cell"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const columnHelper = createColumnHelper<RegionDTO>()
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegion>()
|
||||
|
||||
export const useRegionTableColumns = () => {
|
||||
return useMemo(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AdminGetRegionsParams } from "@medusajs/medusa"
|
||||
import { FindParams, HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseRegionTableQueryProps = {
|
||||
@@ -17,7 +17,7 @@ export const useRegionTableQuery = ({
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
|
||||
const searchParams: AdminGetRegionsParams = {
|
||||
const searchParams: FindParams & HttpTypes.AdminRegionFilters = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
|
||||
@@ -14,7 +14,6 @@ import { priceLists } from "./price-lists"
|
||||
import { productTypes } from "./product-types"
|
||||
import { products } from "./products"
|
||||
import { promotions } from "./promotions"
|
||||
import { regions } from "./regions"
|
||||
import { orders } from "./orders"
|
||||
import { fulfillments } from "./fulfillments"
|
||||
import { reservations } from "./reservations"
|
||||
@@ -45,7 +44,6 @@ export const client = {
|
||||
tags: tags,
|
||||
users: users,
|
||||
orders: orders,
|
||||
regions: regions,
|
||||
taxes: taxes,
|
||||
invites: invites,
|
||||
inventoryItems: inventoryItems,
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import { CreateRegionDTO, UpdateRegionDTO } from "@medusajs/types"
|
||||
import {
|
||||
RegionDeleteRes,
|
||||
RegionListRes,
|
||||
RegionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveRegion(id: string, query?: Record<string, any>) {
|
||||
return getRequest<RegionRes>(`/admin/regions/${id}`, query)
|
||||
}
|
||||
|
||||
async function listRegions(query?: Record<string, any>) {
|
||||
return getRequest<RegionListRes>(`/admin/regions`, query)
|
||||
}
|
||||
|
||||
async function createRegion(payload: CreateRegionDTO) {
|
||||
return postRequest<RegionRes>(`/admin/regions`, payload)
|
||||
}
|
||||
|
||||
async function updateRegion(id: string, payload: UpdateRegionDTO) {
|
||||
return postRequest<RegionRes>(`/admin/regions/${id}`, payload)
|
||||
}
|
||||
|
||||
async function deleteRegion(id: string) {
|
||||
return deleteRequest<RegionDeleteRes>(`/admin/regions/${id}`)
|
||||
}
|
||||
|
||||
export const regions = {
|
||||
retrieve: retrieveRegion,
|
||||
list: listRegions,
|
||||
create: createRegion,
|
||||
update: updateRegion,
|
||||
delete: deleteRegion,
|
||||
}
|
||||
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
AdminCollectionsRes,
|
||||
AdminProductsRes,
|
||||
AdminRegionsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { AdminCollectionsRes, AdminProductsRes } from "@medusajs/medusa"
|
||||
import {
|
||||
AdminApiKeyResponse,
|
||||
AdminCustomerGroupResponse,
|
||||
@@ -11,6 +7,7 @@ import {
|
||||
AdminTaxRegionResponse,
|
||||
SalesChannelDTO,
|
||||
UserDTO,
|
||||
HttpTypes,
|
||||
} from "@medusajs/types"
|
||||
import { Outlet, RouteObject } from "react-router-dom"
|
||||
|
||||
@@ -593,7 +590,8 @@ export const RouteMap: RouteObject[] = [
|
||||
path: ":id",
|
||||
lazy: () => import("../../v2-routes/regions/region-detail"),
|
||||
handle: {
|
||||
crumb: (data: AdminRegionsRes) => data.region.name,
|
||||
crumb: (data: { region: HttpTypes.AdminRegion }) =>
|
||||
data.region.name,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
CreateProductCollectionDTO,
|
||||
CreatePromotionDTO,
|
||||
CreatePromotionRuleDTO,
|
||||
CreateRegionDTO,
|
||||
CreateSalesChannelDTO,
|
||||
CreateServiceZoneDTO,
|
||||
CreateShippingOptionDTO,
|
||||
@@ -26,7 +25,6 @@ import {
|
||||
UpdateProductCollectionDTO,
|
||||
UpdatePromotionDTO,
|
||||
UpdatePromotionRuleDTO,
|
||||
UpdateRegionDTO,
|
||||
UpdateSalesChannelDTO,
|
||||
UpdateServiceZoneDTO,
|
||||
UpdateShippingOptionDTO,
|
||||
@@ -38,10 +36,6 @@ import {
|
||||
// Auth
|
||||
export type EmailPassReq = { email: string; password: string }
|
||||
|
||||
// Regions
|
||||
export type CreateRegionReq = CreateRegionDTO
|
||||
export type UpdateRegionReq = UpdateRegionDTO
|
||||
|
||||
// Stores
|
||||
export type UpdateStoreReq = UpdateStoreDTO
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import {
|
||||
ProductTypeDTO,
|
||||
ProductVariantDTO,
|
||||
PromotionDTO,
|
||||
RegionDTO,
|
||||
SalesChannelDTO,
|
||||
ShippingOptionDTO,
|
||||
ShippingProfileDTO,
|
||||
@@ -69,11 +68,6 @@ export type ExtendedStoreDTO = StoreDTO & {
|
||||
export type StoreRes = { store: ExtendedStoreDTO }
|
||||
export type StoreListRes = { stores: ExtendedStoreDTO[] } & ListRes
|
||||
|
||||
// Regions
|
||||
export type RegionRes = { region: RegionDTO }
|
||||
export type RegionListRes = { regions: RegionDTO[] } & ListRes
|
||||
export type RegionDeleteRes = DeleteRes
|
||||
|
||||
// Fulfillments
|
||||
export type FulfillmentRes = { fulfillment: FulfillmentDTO }
|
||||
export type FulfillmentListRes = { fulfillments: FulfillmentDTO[] } & ListRes
|
||||
|
||||
@@ -10,7 +10,6 @@ import { useTranslation } from "react-i18next"
|
||||
import * as zod from "zod"
|
||||
|
||||
import { Button, Checkbox, toast } from "@medusajs/ui"
|
||||
import { RegionCountryDTO, RegionDTO } from "@medusajs/types"
|
||||
import {
|
||||
RouteFocusModal,
|
||||
useRouteModal,
|
||||
@@ -22,9 +21,10 @@ import { useCountries } from "../../../common/hooks/use-countries"
|
||||
import { useCountryTableColumns } from "../../../common/hooks/use-country-table-columns"
|
||||
import { useCountryTableQuery } from "../../../common/hooks/use-country-table-query"
|
||||
import { useUpdateRegion } from "../../../../../hooks/api/regions"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
type AddCountriesFormProps = {
|
||||
region: RegionDTO
|
||||
region: HttpTypes.AdminRegion
|
||||
}
|
||||
|
||||
const AddCountriesSchema = zod.object({
|
||||
@@ -71,7 +71,7 @@ export const AddCountriesForm = ({ region }: AddCountriesFormProps) => {
|
||||
iso_3: c.iso_3,
|
||||
num_code: c.num_code,
|
||||
region_id: null,
|
||||
region: {} as RegionDTO,
|
||||
region: {} as HttpTypes.AdminRegion,
|
||||
})),
|
||||
...searchParams,
|
||||
})
|
||||
@@ -102,7 +102,7 @@ export const AddCountriesForm = ({ region }: AddCountriesFormProps) => {
|
||||
|
||||
const handleSubmit = form.handleSubmit(async (values) => {
|
||||
const payload = [
|
||||
...region.countries.map((c) => c.iso_2),
|
||||
...(region.countries?.map((c) => c.iso_2) ?? []),
|
||||
...values.countries,
|
||||
]
|
||||
|
||||
@@ -162,7 +162,7 @@ export const AddCountriesForm = ({ region }: AddCountriesFormProps) => {
|
||||
)
|
||||
}
|
||||
|
||||
const columnHelper = createColumnHelper<RegionCountryDTO>()
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegionCountry>()
|
||||
|
||||
const useColumns = () => {
|
||||
const base = useCountryTableColumns()
|
||||
@@ -203,5 +203,5 @@ const useColumns = () => {
|
||||
...base,
|
||||
],
|
||||
[base]
|
||||
) as ColumnDef<RegionCountryDTO>[]
|
||||
) as ColumnDef<HttpTypes.AdminRegionCountry>[]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { PlusMini, Trash } from "@medusajs/icons"
|
||||
import { Checkbox, Container, Heading, toast, usePrompt } from "@medusajs/ui"
|
||||
import { RegionCountryDTO, RegionDTO } from "@medusajs/types"
|
||||
import {
|
||||
ColumnDef,
|
||||
RowSelectionState,
|
||||
@@ -15,9 +14,10 @@ import { useCountries } from "../../../common/hooks/use-countries"
|
||||
import { useCountryTableColumns } from "../../../common/hooks/use-country-table-columns"
|
||||
import { useCountryTableQuery } from "../../../common/hooks/use-country-table-query"
|
||||
import { useUpdateRegion } from "../../../../../hooks/api/regions"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
type RegionCountrySectionProps = {
|
||||
region: RegionDTO
|
||||
region: HttpTypes.AdminRegion
|
||||
}
|
||||
|
||||
const PREFIX = "c"
|
||||
@@ -143,8 +143,8 @@ const CountryActions = ({
|
||||
country,
|
||||
region,
|
||||
}: {
|
||||
country: RegionCountryDTO
|
||||
region: RegionDTO
|
||||
country: HttpTypes.AdminRegionCountry
|
||||
region: HttpTypes.AdminRegion
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const prompt = usePrompt()
|
||||
@@ -204,7 +204,7 @@ const CountryActions = ({
|
||||
)
|
||||
}
|
||||
|
||||
const columnHelper = createColumnHelper<RegionCountryDTO>()
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegionCountry>()
|
||||
|
||||
const useColumns = () => {
|
||||
const base = useCountryTableColumns()
|
||||
@@ -243,12 +243,14 @@ const useColumns = () => {
|
||||
columnHelper.display({
|
||||
id: "actions",
|
||||
cell: ({ row, table }) => {
|
||||
const { region } = table.options.meta as { region: RegionDTO }
|
||||
const { region } = table.options.meta as {
|
||||
region: HttpTypes.AdminRegion
|
||||
}
|
||||
|
||||
return <CountryActions country={row.original} region={region} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[base]
|
||||
) as ColumnDef<RegionCountryDTO>[]
|
||||
) as ColumnDef<HttpTypes.AdminRegionCountry>[]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { PencilSquare, Trash } from "@medusajs/icons"
|
||||
import { RegionDTO } from "@medusajs/types"
|
||||
import { Badge, Container, Heading, Text, toast, usePrompt } from "@medusajs/ui"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
@@ -9,9 +8,10 @@ import { ListSummary } from "../../../../../components/common/list-summary"
|
||||
import { useDeleteRegion } from "../../../../../hooks/api/regions.tsx"
|
||||
import { currencies } from "../../../../../lib/currencies"
|
||||
import { formatProvider } from "../../../../../lib/format-provider"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
type RegionGeneralSectionProps = {
|
||||
region: RegionDTO
|
||||
region: HttpTypes.AdminRegion
|
||||
}
|
||||
|
||||
export const RegionGeneralSection = ({ region }: RegionGeneralSectionProps) => {
|
||||
@@ -54,7 +54,7 @@ export const RegionGeneralSection = ({ region }: RegionGeneralSectionProps) => {
|
||||
)
|
||||
}
|
||||
|
||||
const RegionActions = ({ region }: { region: RegionDTO }) => {
|
||||
const RegionActions = ({ region }: { region: HttpTypes.AdminRegion }) => {
|
||||
const navigate = useNavigate()
|
||||
const { t } = useTranslation()
|
||||
const { mutateAsync } = useDeleteRegion(region.id)
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import { AdminRegionsRes } from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { adminRegionKeys } from "medusa-react"
|
||||
import { LoaderFunctionArgs } from "react-router-dom"
|
||||
import { client } from "../../../lib/client"
|
||||
import { queryClient } from "../../../lib/medusa"
|
||||
import { sdk } from "../../../lib/client"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const regionQuery = (id: string) => ({
|
||||
queryKey: adminRegionKeys.detail(id),
|
||||
queryFn: async () =>
|
||||
client.regions.retrieve(id, { fields: "*payment_providers" }),
|
||||
sdk.admin.region.retrieve(id, { fields: "*payment_providers" }),
|
||||
})
|
||||
|
||||
export const regionLoader = async ({ params }: LoaderFunctionArgs) => {
|
||||
@@ -16,7 +15,8 @@ export const regionLoader = async ({ params }: LoaderFunctionArgs) => {
|
||||
const query = regionQuery(id!)
|
||||
|
||||
return (
|
||||
queryClient.getQueryData<Response<AdminRegionsRes>>(query.queryKey) ??
|
||||
(await queryClient.fetchQuery(query))
|
||||
queryClient.getQueryData<{ region: HttpTypes.AdminRegion }>(
|
||||
query.queryKey
|
||||
) ?? (await queryClient.fetchQuery(query))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PaymentProviderDTO, RegionDTO } from "@medusajs/types"
|
||||
import { PaymentProviderDTO, HttpTypes } from "@medusajs/types"
|
||||
import { Button, Input, Select, Text, toast } from "@medusajs/ui"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { useTranslation } from "react-i18next"
|
||||
@@ -15,7 +15,7 @@ import { CurrencyInfo } from "../../../../../lib/currencies"
|
||||
import { formatProvider } from "../../../../../lib/format-provider"
|
||||
|
||||
type EditRegionFormProps = {
|
||||
region: RegionDTO
|
||||
region: HttpTypes.AdminRegion
|
||||
currencies: CurrencyInfo[]
|
||||
paymentProviders: PaymentProviderDTO[]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { PencilSquare, Trash } from "@medusajs/icons"
|
||||
import { RegionDTO } from "@medusajs/types"
|
||||
import { Button, Container, Heading, usePrompt, toast } from "@medusajs/ui"
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
@@ -14,6 +13,7 @@ import { useRegionTableColumns } from "../../../../../hooks/table/columns/use-re
|
||||
import { useRegionTableFilters } from "../../../../../hooks/table/filters/use-region-table-filters"
|
||||
import { useRegionTableQuery } from "../../../../../hooks/table/query/use-region-table-query"
|
||||
import { useDataTable } from "../../../../../hooks/use-data-table"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const PAGE_SIZE = 20
|
||||
|
||||
@@ -41,7 +41,7 @@ export const RegionListTable = () => {
|
||||
const columns = useColumns()
|
||||
|
||||
const { table } = useDataTable({
|
||||
data: (regions ?? []) as RegionDTO[],
|
||||
data: (regions ?? []) as HttpTypes.AdminRegion[],
|
||||
columns,
|
||||
count,
|
||||
enablePagination: true,
|
||||
@@ -81,7 +81,7 @@ export const RegionListTable = () => {
|
||||
)
|
||||
}
|
||||
|
||||
const RegionActions = ({ region }: { region: RegionDTO }) => {
|
||||
const RegionActions = ({ region }: { region: HttpTypes.AdminRegion }) => {
|
||||
const { t } = useTranslation()
|
||||
const prompt = usePrompt()
|
||||
|
||||
@@ -143,7 +143,7 @@ const RegionActions = ({ region }: { region: RegionDTO }) => {
|
||||
)
|
||||
}
|
||||
|
||||
const columnHelper = createColumnHelper<RegionDTO>()
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegion>()
|
||||
|
||||
const useColumns = () => {
|
||||
const base = useRegionTableColumns()
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
Text,
|
||||
toast,
|
||||
} from "@medusajs/ui"
|
||||
import { RegionCountryDTO, RegionDTO, ServiceZoneDTO } from "@medusajs/types"
|
||||
import { ServiceZoneDTO, HttpTypes } from "@medusajs/types"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { XMarkMini } from "@medusajs/icons"
|
||||
|
||||
@@ -26,10 +26,7 @@ import {
|
||||
useRouteModal,
|
||||
} from "../../../../../components/route-modal"
|
||||
import { SplitView } from "../../../../../components/layout/split-view"
|
||||
import {
|
||||
useCreateServiceZone,
|
||||
useUpdateServiceZone,
|
||||
} from "../../../../../hooks/api/stock-locations"
|
||||
import { useUpdateServiceZone } from "../../../../../hooks/api/stock-locations"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { useCountryTableQuery } from "../../../../regions/common/hooks/use-country-table-query"
|
||||
import { useCountries } from "../../../../regions/common/hooks/use-countries"
|
||||
@@ -130,7 +127,7 @@ export function EditServiceZoneAreasForm({
|
||||
iso_3: c.iso_3,
|
||||
num_code: c.num_code,
|
||||
region_id: null,
|
||||
region: {} as RegionDTO,
|
||||
region: {} as HttpTypes.AdminRegion,
|
||||
})),
|
||||
...searchParams,
|
||||
})
|
||||
@@ -299,7 +296,7 @@ export function EditServiceZoneAreasForm({
|
||||
)
|
||||
}
|
||||
|
||||
const columnHelper = createColumnHelper<RegionCountryDTO>()
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegionCountry>()
|
||||
|
||||
const useColumns = () => {
|
||||
const base = useCountryTableColumns()
|
||||
@@ -340,5 +337,5 @@ const useColumns = () => {
|
||||
...base,
|
||||
],
|
||||
[base]
|
||||
) as ColumnDef<RegionCountryDTO>[]
|
||||
) as ColumnDef<HttpTypes.AdminRegionCountry>[]
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
Text,
|
||||
toast,
|
||||
} from "@medusajs/ui"
|
||||
import { FulfillmentSetDTO, RegionCountryDTO, RegionDTO } from "@medusajs/types"
|
||||
import { FulfillmentSetDTO, HttpTypes } from "@medusajs/types"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { XMarkMini } from "@medusajs/icons"
|
||||
|
||||
@@ -123,7 +123,7 @@ export function CreateServiceZoneForm({
|
||||
iso_3: c.iso_3,
|
||||
num_code: c.num_code,
|
||||
region_id: null,
|
||||
region: {} as RegionDTO,
|
||||
region: {} as HttpTypes.AdminRegion,
|
||||
})),
|
||||
...searchParams,
|
||||
})
|
||||
@@ -322,7 +322,7 @@ export function CreateServiceZoneForm({
|
||||
)
|
||||
}
|
||||
|
||||
const columnHelper = createColumnHelper<RegionCountryDTO>()
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegionCountry>()
|
||||
|
||||
const useColumns = () => {
|
||||
const base = useCountryTableColumns()
|
||||
@@ -363,5 +363,5 @@ const useColumns = () => {
|
||||
...base,
|
||||
],
|
||||
[base]
|
||||
) as ColumnDef<RegionCountryDTO>[]
|
||||
) as ColumnDef<HttpTypes.AdminRegionCountry>[]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { PropsWithChildren } from "react"
|
||||
import { CurrencyDTO, ProductVariantDTO, RegionDTO } from "@medusajs/types"
|
||||
import { CurrencyDTO, HttpTypes, ProductVariantDTO } from "@medusajs/types"
|
||||
import { ColumnDef, createColumnHelper } from "@tanstack/react-table"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { UseFormReturn } from "react-hook-form"
|
||||
@@ -104,7 +103,7 @@ const useColumns = ({
|
||||
regions = [],
|
||||
}: {
|
||||
currencies?: CurrencyDTO[]
|
||||
regions?: RegionDTO[]
|
||||
regions?: HttpTypes.AdminRegion[]
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
CurrencyDTO,
|
||||
PriceDTO,
|
||||
ProductVariantDTO,
|
||||
RegionDTO,
|
||||
ShippingOptionDTO,
|
||||
} from "@medusajs/types"
|
||||
import { useTranslation } from "react-i18next"
|
||||
@@ -30,6 +29,7 @@ import { CurrencyCell } from "../../../../../components/grid/grid-cells/common/c
|
||||
import { DataGridMeta } from "../../../../../components/grid/types"
|
||||
import { DataGrid } from "../../../../../components/grid/data-grid"
|
||||
import { useUpdateShippingOptions } from "../../../../../hooks/api/shipping-options.ts"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const getInitialCurrencyPrices = (prices: PriceDTO[]) => {
|
||||
const ret: Record<string, number> = {}
|
||||
@@ -271,7 +271,7 @@ const useColumns = ({
|
||||
regions = [],
|
||||
}: {
|
||||
currencies?: CurrencyDTO[]
|
||||
regions?: RegionDTO[]
|
||||
regions?: HttpTypes.AdminRegion[]
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
|
||||
@@ -1,8 +1,83 @@
|
||||
import {
|
||||
DeleteResponse,
|
||||
FindParams,
|
||||
HttpTypes,
|
||||
PaginatedResponse,
|
||||
SelectParams,
|
||||
} from "@medusajs/types"
|
||||
import { Client } from "../client"
|
||||
import { ClientHeaders } from "../types"
|
||||
|
||||
export class Admin {
|
||||
private client: Client
|
||||
constructor(client: Client) {
|
||||
this.client = client
|
||||
}
|
||||
|
||||
public region = {
|
||||
create: async (
|
||||
body: HttpTypes.AdminCreateRegion,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<{ region: HttpTypes.AdminRegion }>(
|
||||
`/admin/regions`,
|
||||
{
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
}
|
||||
)
|
||||
},
|
||||
update: async (
|
||||
id: string,
|
||||
body: HttpTypes.AdminUpdateRegion,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<{ region: HttpTypes.AdminRegion }>(
|
||||
`/admin/regions/${id}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
}
|
||||
)
|
||||
},
|
||||
list: async (
|
||||
queryParams?: FindParams & HttpTypes.AdminRegionFilters,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<
|
||||
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>
|
||||
>(`/admin/regions`, {
|
||||
query: queryParams,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<{ region: HttpTypes.AdminRegion }>(
|
||||
`/admin/regions/${id}`,
|
||||
{
|
||||
query,
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
delete: async (id: string, headers?: ClientHeaders) => {
|
||||
return this.client.fetch<DeleteResponse<"region">>(
|
||||
`/admin/regions/${id}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,13 @@ export class Auth {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
login = async (payload: { email: string; password: string }) => {
|
||||
// TODO: It is a bit strange to eg. require to pass `scope` in `login`, it might be better for us to have auth methods in both `admin` and `store` classes instead?
|
||||
login = async (
|
||||
scope: "admin" | "store",
|
||||
method: "emailpass",
|
||||
payload: { email: string; password: string }
|
||||
) => {
|
||||
const { token } = await this.client.fetch<{ token: string }>(
|
||||
"/auth/admin/emailpass",
|
||||
`/auth/${scope}/${method}`,
|
||||
{
|
||||
method: "POST",
|
||||
body: payload,
|
||||
@@ -32,9 +35,11 @@ export class Auth {
|
||||
}
|
||||
|
||||
logout = async () => {
|
||||
await this.client.fetch("/auth/session", {
|
||||
method: "DELETE",
|
||||
})
|
||||
if (this.config?.auth?.type === "session") {
|
||||
await this.client.fetch("/auth/session", {
|
||||
method: "DELETE",
|
||||
})
|
||||
}
|
||||
|
||||
this.client.clearToken()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { StoreRegion } from "@medusajs/types"
|
||||
import {
|
||||
FindParams,
|
||||
HttpTypes,
|
||||
PaginatedResponse,
|
||||
SelectParams,
|
||||
} from "@medusajs/types"
|
||||
import { Client } from "../client"
|
||||
import { ClientHeaders } from "../types"
|
||||
|
||||
@@ -11,153 +16,188 @@ export class Store {
|
||||
|
||||
public region = {
|
||||
list: async (
|
||||
queryParams?: Record<string, any>,
|
||||
query?: FindParams & HttpTypes.StoreRegionFilters,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/regions`, {
|
||||
query: queryParams,
|
||||
return this.client.fetch<
|
||||
PaginatedResponse<{ regions: HttpTypes.StoreRegion[] }>
|
||||
>(`/store/regions`, {
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
queryParams?: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<StoreRegion>(`/store/regions/${id}`, {
|
||||
query: queryParams,
|
||||
headers,
|
||||
})
|
||||
return this.client.fetch<{ region: HttpTypes.StoreRegion }>(
|
||||
`/store/regions/${id}`,
|
||||
{
|
||||
query,
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
public collection = {
|
||||
list: async (
|
||||
queryParams?: Record<string, any>,
|
||||
query?: FindParams & HttpTypes.StoreCollectionFilters,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/collections`, {
|
||||
query: queryParams,
|
||||
return this.client.fetch<
|
||||
PaginatedResponse<{ collections: HttpTypes.StoreCollection }>
|
||||
>(`/store/collections`, {
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
queryParams?: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/collections/${id}`, {
|
||||
query: queryParams,
|
||||
headers,
|
||||
})
|
||||
return this.client.fetch<{ collection: HttpTypes.StoreCollection }>(
|
||||
`/store/collections/${id}`,
|
||||
{
|
||||
query,
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
public category = {
|
||||
list: async (
|
||||
queryParams?: Record<string, any>,
|
||||
query?: FindParams & HttpTypes.StoreProductCategoryFilters,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/product-categories`, {
|
||||
query: queryParams,
|
||||
return this.client.fetch<
|
||||
PaginatedResponse<{ categories: HttpTypes.StoreProductCategory }>
|
||||
>(`/store/product-categories`, {
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
queryParams?: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/product-categories/${id}`, {
|
||||
query: queryParams,
|
||||
headers,
|
||||
})
|
||||
return this.client.fetch<{ category: HttpTypes.StoreProductCategory }>(
|
||||
`/store/product-categories/${id}`,
|
||||
{
|
||||
query,
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
public product = {
|
||||
list: async (
|
||||
queryParams?: Record<string, any>,
|
||||
query?: FindParams & HttpTypes.StoreProductFilters,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/products`, {
|
||||
query: queryParams,
|
||||
return this.client.fetch<
|
||||
PaginatedResponse<{ products: HttpTypes.StoreProduct }>
|
||||
>(`/store/products`, {
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
queryParams?: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/products/${id}`, {
|
||||
query: queryParams,
|
||||
headers,
|
||||
})
|
||||
return this.client.fetch<{ product: HttpTypes.StoreProduct }>(
|
||||
`/store/products/${id}`,
|
||||
{
|
||||
query,
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
public order = {
|
||||
retrieve: async (
|
||||
id: string,
|
||||
queryParams?: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/orders/${id}`, {
|
||||
query: queryParams,
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
public cart = {
|
||||
create: async (body: any, headers?: ClientHeaders) => {
|
||||
create: async (
|
||||
body: any,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/carts`, {
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
})
|
||||
},
|
||||
update: async (id: string, body: any, headers?: ClientHeaders) => {
|
||||
update: async (
|
||||
id: string,
|
||||
body: any,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/carts/${id}`, {
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
})
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
queryParams?: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/carts/${id}`, {
|
||||
query: queryParams,
|
||||
headers,
|
||||
query,
|
||||
})
|
||||
},
|
||||
createLineItem: async (
|
||||
cartId: string,
|
||||
body: any,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/carts/${cartId}/line-items`, {
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
})
|
||||
},
|
||||
updateLineItem: async (
|
||||
cartId: string,
|
||||
lineItemId: string,
|
||||
body: any,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(
|
||||
`/store/carts/${cartId}/line-items/${lineItemId}`,
|
||||
{
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
}
|
||||
)
|
||||
},
|
||||
@@ -169,37 +209,44 @@ export class Store {
|
||||
return this.client.fetch<any>(
|
||||
`/store/carts/${cartId}/line-items/${lineItemId}`,
|
||||
{
|
||||
headers,
|
||||
method: "DELETE",
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
addShippingMethod: async (
|
||||
cartId: string,
|
||||
body: any,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/carts/${cartId}/shipping-methods`, {
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
})
|
||||
},
|
||||
complete: async (cartId: string, headers?: ClientHeaders) => {
|
||||
complete: async (
|
||||
cartId: string,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/carts/${cartId}/complete`, {
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
query,
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
public fulfillment = {
|
||||
listCartOptions: async (
|
||||
queryParams?: Record<string, any>,
|
||||
query?: Record<string, any>,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/shipping-options`, {
|
||||
query: queryParams,
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
@@ -207,11 +254,11 @@ export class Store {
|
||||
|
||||
public payment = {
|
||||
listPaymentProviders: async (
|
||||
queryParams?: Record<string, any>,
|
||||
query?: Record<string, any>,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<any>(`/store/payment-providers`, {
|
||||
query: queryParams,
|
||||
query,
|
||||
headers,
|
||||
})
|
||||
},
|
||||
@@ -219,6 +266,7 @@ export class Store {
|
||||
initiatePaymentSession: async (
|
||||
cart: any,
|
||||
body: Record<string, any>,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
let paymentCollectionId = (cart as any).payment_collection?.id
|
||||
@@ -231,8 +279,8 @@ export class Store {
|
||||
}
|
||||
paymentCollectionId = (
|
||||
await this.client.fetch<any>(`/store/payment-collections`, {
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body: collectionBody,
|
||||
})
|
||||
).payment_collection.id
|
||||
@@ -241,9 +289,10 @@ export class Store {
|
||||
return this.client.fetch<any>(
|
||||
`/store/payment-collections/${paymentCollectionId}/payment-sessions`,
|
||||
{
|
||||
headers,
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
@@ -14,5 +14,6 @@ export * from "./product-category"
|
||||
export * from "./reservation"
|
||||
export * from "./region"
|
||||
export * from "./product"
|
||||
export * from "./payment"
|
||||
export * from "./collection"
|
||||
export * from "./common"
|
||||
|
||||
5
packages/core/types/src/http/payment/admin.ts
Normal file
5
packages/core/types/src/http/payment/admin.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { BasePaymentProvider } from "./common"
|
||||
|
||||
export interface AdminPaymentProvider extends BasePaymentProvider {
|
||||
is_enabled: boolean
|
||||
}
|
||||
3
packages/core/types/src/http/payment/common.ts
Normal file
3
packages/core/types/src/http/payment/common.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface BasePaymentProvider {
|
||||
id: string
|
||||
}
|
||||
2
packages/core/types/src/http/payment/index.ts
Normal file
2
packages/core/types/src/http/payment/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./admin"
|
||||
export * from "./store"
|
||||
3
packages/core/types/src/http/payment/store.ts
Normal file
3
packages/core/types/src/http/payment/store.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { BasePaymentProvider } from "./common"
|
||||
|
||||
export interface StorePaymentProvider extends BasePaymentProvider {}
|
||||
@@ -9,3 +9,21 @@ export interface AdminRegion extends BaseRegion {}
|
||||
export interface AdminRegionCountry extends BaseRegionCountry {}
|
||||
export interface AdminRegionFilters extends BaseRegionFilters {}
|
||||
export interface AdminRegionCountryFilters extends BaseRegionCountryFilters {}
|
||||
|
||||
export interface AdminCreateRegion {
|
||||
name: string
|
||||
currency_code: string
|
||||
countries?: string[]
|
||||
automatic_taxes?: boolean
|
||||
payment_providers?: string[]
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
export interface AdminUpdateRegion {
|
||||
name?: string
|
||||
currency_code?: string
|
||||
countries?: string[]
|
||||
automatic_taxes?: boolean
|
||||
payment_providers?: string[]
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import { BaseFilterable, OperatorMap } from "../../dal"
|
||||
import { AdminPaymentProvider } from "../payment"
|
||||
|
||||
export interface BaseRegion {
|
||||
id?: string
|
||||
name?: string
|
||||
currency_code?: string
|
||||
id: string
|
||||
name: string
|
||||
currency_code: string
|
||||
automatic_taxes?: boolean
|
||||
countries?: BaseRegionCountry[]
|
||||
payment_providers?: AdminPaymentProvider[]
|
||||
metadata?: Record<string, any> | null
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
export interface BaseRegionCountry {
|
||||
id?: string
|
||||
id: string
|
||||
iso_2?: string
|
||||
iso_3?: string
|
||||
num_code?: number
|
||||
|
||||
Reference in New Issue
Block a user