feat: Update customer related typings and sdk methods (#7440)

This commit is contained in:
Stevche Radevski
2024-05-24 13:25:10 +02:00
committed by GitHub
parent ff870482bb
commit 296473d994
41 changed files with 386 additions and 228 deletions

View File

@@ -1,7 +1,3 @@
import {
AdminCustomerGroupListResponse,
AdminCustomerGroupResponse,
} from "@medusajs/types"
import {
QueryKey,
UseMutationOptions,
@@ -16,6 +12,7 @@ import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateCustomerGroupSchema } from "../../routes/customer-groups/customer-group-create/components/create-customer-group-form"
import { EditCustomerGroupSchema } from "../../routes/customer-groups/customer-group-edit/components/edit-customer-group-form"
import { customersQueryKeys } from "./customers"
import { HttpTypes, PaginatedResponse } from "@medusajs/types"
const CUSTOMER_GROUPS_QUERY_KEY = "customer_groups" as const
export const customerGroupsQueryKeys = queryKeysFactory(
@@ -27,9 +24,9 @@ export const useCustomerGroup = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
AdminCustomerGroupResponse,
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
AdminCustomerGroupResponse,
{ customer_group: HttpTypes.AdminCustomerGroup },
QueryKey
>,
"queryFn" | "queryKey"
@@ -48,9 +45,9 @@ export const useCustomerGroups = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
AdminCustomerGroupListResponse,
PaginatedResponse<HttpTypes.AdminCustomerGroup[]>,
Error,
AdminCustomerGroupListResponse,
PaginatedResponse<HttpTypes.AdminCustomerGroup[]>,
QueryKey
>,
"queryFn" | "queryKey"
@@ -67,7 +64,7 @@ export const useCustomerGroups = (
export const useCreateCustomerGroup = (
options?: UseMutationOptions<
AdminCustomerGroupResponse,
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
z.infer<typeof CreateCustomerGroupSchema>
>
@@ -87,7 +84,7 @@ export const useCreateCustomerGroup = (
export const useUpdateCustomerGroup = (
id: string,
options?: UseMutationOptions<
AdminCustomerGroupResponse,
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
z.infer<typeof EditCustomerGroupSchema>
>
@@ -135,7 +132,7 @@ export const useDeleteCustomerGroup = (
export const useAddCustomersToGroup = (
id: string,
options?: UseMutationOptions<
AdminCustomerGroupResponse,
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
{ customer_ids: string[] }
>
@@ -162,7 +159,7 @@ export const useAddCustomersToGroup = (
export const useRemoveCustomersFromGroup = (
id: string,
options?: UseMutationOptions<
AdminCustomerGroupResponse,
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
{ customer_ids: string[] }
>

View File

@@ -1,8 +1,4 @@
import {
AdminCustomerListResponse,
AdminCustomerResponse,
DeleteResponse,
} from "@medusajs/types"
import { DeleteResponse, HttpTypes, PaginatedResponse } 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 { CreateCustomerReq, UpdateCustomerReq } from "../../types/api-payloads"
const CUSTOMERS_QUERY_KEY = "customers" as const
export const customersQueryKeys = queryKeysFactory(CUSTOMERS_QUERY_KEY)
@@ -23,9 +18,9 @@ export const useCustomer = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
AdminCustomerResponse,
{ customer: HttpTypes.AdminCustomer },
Error,
AdminCustomerResponse,
{ customer: HttpTypes.AdminCustomer },
QueryKey
>,
"queryFn" | "queryKey"
@@ -33,7 +28,7 @@ export const useCustomer = (
) => {
const { data, ...rest } = useQuery({
queryKey: customersQueryKeys.detail(id),
queryFn: async () => client.customers.retrieve(id, query),
queryFn: async () => sdk.admin.customer.retrieve(id, query),
...options,
})
@@ -44,16 +39,16 @@ export const useCustomers = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
AdminCustomerListResponse,
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>,
Error,
AdminCustomerListResponse,
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.customers.list(query),
queryFn: () => sdk.admin.customer.list(query),
queryKey: customersQueryKeys.list(query),
...options,
})
@@ -62,10 +57,14 @@ export const useCustomers = (
}
export const useCreateCustomer = (
options?: UseMutationOptions<AdminCustomerResponse, Error, CreateCustomerReq>
options?: UseMutationOptions<
{ customer: HttpTypes.AdminCustomer; token: string },
Error,
HttpTypes.AdminCreateCustomer
>
) => {
return useMutation({
mutationFn: (payload) => client.customers.create(payload),
mutationFn: (payload) => sdk.admin.customer.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
@@ -76,10 +75,14 @@ export const useCreateCustomer = (
export const useUpdateCustomer = (
id: string,
options?: UseMutationOptions<AdminCustomerResponse, Error, UpdateCustomerReq>
options?: UseMutationOptions<
{ customer: HttpTypes.AdminCustomer },
Error,
HttpTypes.AdminUpdateCustomer
>
) => {
return useMutation({
mutationFn: (payload) => client.customers.update(id, payload),
mutationFn: (payload) => sdk.admin.customer.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) })
@@ -92,10 +95,10 @@ export const useUpdateCustomer = (
export const useDeleteCustomer = (
id: string,
options?: UseMutationOptions<DeleteResponse, Error, void>
options?: UseMutationOptions<DeleteResponse<"customer">, Error, void>
) => {
return useMutation({
mutationFn: () => client.customers.delete(id),
mutationFn: () => sdk.admin.customer.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
queryClient.invalidateQueries({

View File

@@ -1,15 +1,14 @@
import { createColumnHelper } from "@tanstack/react-table"
import { useMemo } from "react"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import { useTranslation } from "react-i18next"
import {
TextCell,
TextHeader,
} from "../../../components/table/table-cells/common/text-cell"
import { HttpTypes } from "@medusajs/types"
const columnHelper =
createColumnHelper<AdminCustomerGroupResponse["customer_group"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomerGroup>()
export const useCustomerGroupTableColumns = () => {
const { t } = useTranslation()

View File

@@ -1,7 +1,6 @@
import { createColumnHelper } from "@tanstack/react-table"
import { useMemo } from "react"
import { AdminCustomerResponse } from "@medusajs/types"
import {
EmailCell,
EmailHeader,
@@ -18,8 +17,9 @@ import {
FirstSeenCell,
FirstSeenHeader,
} from "../../../components/table/table-cells/customer/first-seen-cell"
import { HttpTypes } from "@medusajs/types"
const columnHelper = createColumnHelper<AdminCustomerResponse["customer"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomer>()
export const useCustomerTableColumns = () => {
return useMemo(

View File

@@ -5,7 +5,6 @@ import { categories } from "./categories"
import { collections } from "./collections"
import { currencies } from "./currencies"
import { customerGroups } from "./customer-groups"
import { customers } from "./customers"
import { fulfillmentProviders } from "./fulfillment-providers"
import { fulfillments } from "./fulfillments"
import { inventoryItems } from "./inventory"
@@ -33,7 +32,6 @@ export const client = {
apiKeys: apiKeys,
campaigns: campaigns,
categories: categories,
customers: customers,
customerGroups: customerGroups,
currencies: currencies,
collections: collections,

View File

@@ -1,30 +1,26 @@
import {
AdminCustomerGroupListResponse,
AdminCustomerGroupResponse,
} from "@medusajs/types"
import { z } from "zod"
import { CreateCustomerGroupSchema } from "../../routes/customer-groups/customer-group-create/components/create-customer-group-form"
import { EditCustomerGroupSchema } from "../../routes/customer-groups/customer-group-edit/components/edit-customer-group-form"
import { deleteRequest, getRequest, postRequest } from "./common"
import { HttpTypes, PaginatedResponse } from "@medusajs/types"
async function retrieveCustomerGroup(id: string, query?: Record<string, any>) {
return getRequest<AdminCustomerGroupResponse>(
return getRequest<{ customer_group: HttpTypes.AdminCustomerGroup }>(
`/admin/customer-groups/${id}`,
query
)
}
async function listCustomerGroups(query?: Record<string, any>) {
return getRequest<AdminCustomerGroupListResponse>(
`/admin/customer-groups`,
query
)
return getRequest<
PaginatedResponse<{ customer_groups: HttpTypes.AdminCustomerGroup[] }>
>(`/admin/customer-groups`, query)
}
async function createCustomerGroup(
payload: z.infer<typeof CreateCustomerGroupSchema>
) {
return postRequest<AdminCustomerGroupResponse>(
return postRequest<{ customer_group: HttpTypes.AdminCustomerGroup }>(
`/admin/customer-groups`,
payload
)
@@ -34,7 +30,7 @@ async function updateCustomerGroup(
id: string,
payload: z.infer<typeof EditCustomerGroupSchema>
) {
return postRequest<AdminCustomerGroupResponse>(
return postRequest<{ customer_group: HttpTypes.AdminCustomerGroup }>(
`/admin/customer-groups/${id}`,
payload
)
@@ -52,7 +48,7 @@ async function batchAddCustomers(
id: string,
payload: { customer_ids: string[] }
) {
return postRequest<AdminCustomerGroupResponse>(
return postRequest<{ customer_group: HttpTypes.AdminCustomerGroup }>(
`/admin/customer-groups/${id}/customers`,
{
add: payload.customer_ids,
@@ -64,7 +60,7 @@ async function batchRemoveCustomers(
id: string,
payload: { customer_ids: string[] }
) {
return postRequest<AdminCustomerGroupResponse>(
return postRequest<{ customer_group: HttpTypes.AdminCustomerGroup }>(
`/admin/customer-groups/${id}/customers`,
{
remove: payload.customer_ids,

View File

@@ -1,35 +0,0 @@
import {
AdminCustomerListResponse,
AdminCustomerResponse,
DeleteResponse,
} from "@medusajs/types"
import { CreateCustomerReq, UpdateCustomerReq } from "../../types/api-payloads"
import { deleteRequest, getRequest, postRequest } from "./common"
async function retrieveCustomer(id: string, query?: Record<string, any>) {
return getRequest<AdminCustomerResponse>(`/admin/customers/${id}`, query)
}
async function listCustomers(query?: Record<string, any>) {
return getRequest<AdminCustomerListResponse>(`/admin/customers`, query)
}
async function createCustomer(payload: CreateCustomerReq) {
return postRequest<AdminCustomerResponse>(`/admin/customers`, payload)
}
async function updateCustomer(id: string, payload: UpdateCustomerReq) {
return postRequest<AdminCustomerResponse>(`/admin/customers/${id}`, payload)
}
async function deleteCustomer(id: string) {
return deleteRequest<DeleteResponse>(`/admin/customers/${id}`)
}
export const customers = {
retrieve: retrieveCustomer,
list: listCustomers,
create: createCustomer,
update: updateCustomer,
delete: deleteCustomer,
}

View File

@@ -1,7 +1,5 @@
import { AdminCollectionsRes, AdminProductsRes } from "@medusajs/medusa"
import {
AdminApiKeyResponse,
AdminCustomerGroupResponse,
AdminProductCategoryResponse,
AdminTaxRateResponse,
AdminTaxRegionResponse,
@@ -65,7 +63,8 @@ export const RouteMap: RouteObject[] = [
path: ":id",
lazy: () => import("../../routes/products/product-detail"),
handle: {
crumb: (data: AdminProductsRes) => data.product.title,
crumb: (data: { product: HttpTypes.AdminProduct }) =>
data.product.title,
},
children: [
{
@@ -261,7 +260,8 @@ export const RouteMap: RouteObject[] = [
lazy: () =>
import("../../routes/collections/collection-detail"),
handle: {
crumb: (data: AdminCollectionsRes) => data.collection.title,
crumb: (data: { collection: HttpTypes.AdminCollection }) =>
data.collection.title,
},
children: [
{
@@ -390,8 +390,9 @@ export const RouteMap: RouteObject[] = [
lazy: () =>
import("../../routes/customer-groups/customer-group-detail"),
handle: {
crumb: (data: AdminCustomerGroupResponse) =>
data.customer_group.name,
crumb: (data: {
customer_group: HttpTypes.AdminCustomerGroup
}) => data.customer_group.name,
},
children: [
{

View File

@@ -10,7 +10,6 @@ import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
import * as zod from "zod"
import { AdminCustomerResponse } from "@medusajs/types"
import {
RouteFocusModal,
useRouteModal,
@@ -22,6 +21,7 @@ import { useCustomerTableColumns } from "../../../../../hooks/table/columns/use-
import { useCustomerTableFilters } from "../../../../../hooks/table/filters/use-customer-table-filters"
import { useCustomerTableQuery } from "../../../../../hooks/table/query/use-customer-table-query"
import { useDataTable } from "../../../../../hooks/use-data-table"
import { HttpTypes } from "@medusajs/types"
type AddCustomersFormProps = {
customerGroupId: string
@@ -180,7 +180,7 @@ export const AddCustomersForm = ({
)
}
const columnHelper = createColumnHelper<AdminCustomerResponse["customer"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomer>()
const useColumns = () => {
const { t } = useTranslation()

View File

@@ -1,8 +1,5 @@
import { PencilSquare, Trash } from "@medusajs/icons"
import {
AdminCustomerGroupResponse,
AdminCustomerResponse,
} from "@medusajs/types"
import { HttpTypes } from "@medusajs/types"
import { Button, Checkbox, Container, Heading, usePrompt } from "@medusajs/ui"
import { RowSelectionState, createColumnHelper } from "@tanstack/react-table"
import { useMemo, useState } from "react"
@@ -19,7 +16,7 @@ import { useCustomerTableQuery } from "../../../../../hooks/table/query/use-cust
import { useDataTable } from "../../../../../hooks/use-data-table"
type CustomerGroupCustomerSectionProps = {
group: AdminCustomerGroupResponse["customer_group"]
group: HttpTypes.AdminCustomerGroup
}
const PAGE_SIZE = 10
@@ -138,7 +135,7 @@ const CustomerActions = ({
customer,
customerGroupId,
}: {
customer: AdminCustomerResponse["customer"]
customer: HttpTypes.AdminCustomer
customerGroupId: string
}) => {
const { t } = useTranslation()
@@ -193,7 +190,7 @@ const CustomerActions = ({
)
}
const columnHelper = createColumnHelper<AdminCustomerResponse["customer"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomer>()
const useColumns = () => {
const columns = useCustomerTableColumns()

View File

@@ -1,13 +1,13 @@
import { PencilSquare, Trash } from "@medusajs/icons"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import { Container, Heading, Text, toast, usePrompt } from "@medusajs/ui"
import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router-dom"
import { ActionMenu } from "../../../../../components/common/action-menu"
import { useDeleteCustomerGroup } from "../../../../../hooks/api/customer-groups"
import { HttpTypes } from "@medusajs/types"
type CustomerGroupGeneralSectionProps = {
group: AdminCustomerGroupResponse["customer_group"]
group: HttpTypes.AdminCustomerGroup
}
export const CustomerGroupGeneralSection = ({

View File

@@ -1,9 +1,8 @@
import { LoaderFunctionArgs } from "react-router-dom"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import { productsQueryKeys } from "../../../hooks/api/products"
import { client } from "../../../lib/client"
import { queryClient } from "../../../lib/query-client"
import { HttpTypes } from "@medusajs/types"
const customerGroupDetailQuery = (id: string) => ({
queryKey: productsQueryKeys.detail(id),
@@ -18,7 +17,8 @@ export const customerGroupLoader = async ({ params }: LoaderFunctionArgs) => {
const query = customerGroupDetailQuery(id!)
return (
queryClient.getQueryData<AdminCustomerGroupResponse>(query.queryKey) ??
(await queryClient.fetchQuery(query))
queryClient.getQueryData<{
customer_group: HttpTypes.AdminCustomerGroup
}>(query.queryKey) ?? (await queryClient.fetchQuery(query))
)
}

View File

@@ -1,5 +1,4 @@
import { zodResolver } from "@hookform/resolvers/zod"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import { Button, Input, toast } from "@medusajs/ui"
import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
@@ -10,9 +9,10 @@ import {
useRouteModal,
} from "../../../../../components/route-modal"
import { useUpdateCustomerGroup } from "../../../../../hooks/api/customer-groups"
import { HttpTypes } from "@medusajs/types"
type EditCustomerGroupFormProps = {
group: AdminCustomerGroupResponse["customer_group"]
group: HttpTypes.AdminCustomerGroup
}
export const EditCustomerGroupSchema = z.object({

View File

@@ -1,5 +1,4 @@
import { PencilSquare, Trash } from "@medusajs/icons"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import { Button, Container, Heading, toast, usePrompt } from "@medusajs/ui"
import { createColumnHelper } from "@tanstack/react-table"
import { useMemo } from "react"
@@ -16,6 +15,7 @@ import { useCustomerGroupTableColumns } from "../../../../../hooks/table/columns
import { useCustomerGroupTableFilters } from "../../../../../hooks/table/filters/use-customer-group-table-filters"
import { useCustomerGroupTableQuery } from "../../../../../hooks/table/query/use-customer-group-table-query"
import { useDataTable } from "../../../../../hooks/use-data-table"
import { HttpTypes } from "@medusajs/types"
const PAGE_SIZE = 20
@@ -77,7 +77,7 @@ export const CustomerGroupListTable = () => {
const CustomerGroupRowActions = ({
group,
}: {
group: AdminCustomerGroupResponse["customer_group"]
group: HttpTypes.AdminCustomerGroup
}) => {
const { t } = useTranslation()
const prompt = usePrompt()
@@ -142,8 +142,7 @@ const CustomerGroupRowActions = ({
)
}
const columnHelper =
createColumnHelper<AdminCustomerGroupResponse["customer_group"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomerGroup>()
const useColumns = () => {
const columns = useCustomerGroupTableColumns()

View File

@@ -1,5 +1,4 @@
import { PencilSquare, Trash } from "@medusajs/icons"
import { AdminCustomerResponse } from "@medusajs/types"
import {
Container,
Heading,
@@ -12,9 +11,10 @@ import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router-dom"
import { ActionMenu } from "../../../../../components/common/action-menu"
import { useDeleteCustomer } from "../../../../../hooks/api/customers"
import { HttpTypes } from "@medusajs/types"
type CustomerGeneralSectionProps = {
customer: AdminCustomerResponse["customer"]
customer: HttpTypes.AdminCustomer
}
export const CustomerGeneralSection = ({

View File

@@ -1,7 +1,4 @@
import {
AdminCustomerGroupResponse,
AdminCustomerResponse,
} from "@medusajs/types"
import { HttpTypes } from "@medusajs/types"
import {
Button,
Checkbox,
@@ -33,7 +30,7 @@ import { client } from "../../../../../lib/client/index.ts"
import { queryClient } from "../../../../../lib/query-client.ts"
type CustomerGroupSectionProps = {
customer: AdminCustomerResponse["customer"]
customer: HttpTypes.AdminCustomer
}
const PAGE_SIZE = 10
@@ -161,7 +158,7 @@ const CustomerGroupRowActions = ({
group,
customerId,
}: {
group: AdminCustomerGroupResponse["customer_group"]
group: HttpTypes.AdminCustomerGroup
customerId: string
}) => {
const prompt = usePrompt()
@@ -215,8 +212,7 @@ const CustomerGroupRowActions = ({
)
}
const columnHelper =
createColumnHelper<AdminCustomerGroupResponse["customer_group"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomerGroup>()
const useColumns = (customerId: string) => {
const columns = useCustomerGroupTableColumns()

View File

@@ -1,12 +1,12 @@
import { AdminCustomerResponse } from "@medusajs/types"
import { LoaderFunctionArgs } from "react-router-dom"
import { productsQueryKeys } from "../../../hooks/api/products"
import { client } from "../../../lib/client"
import { sdk } from "../../../lib/client"
import { queryClient } from "../../../lib/query-client"
import { HttpTypes } from "@medusajs/types"
const customerDetailQuery = (id: string) => ({
queryKey: productsQueryKeys.detail(id),
queryFn: async () => client.customers.retrieve(id),
queryFn: async () => sdk.admin.customer.retrieve(id),
})
export const customerLoader = async ({ params }: LoaderFunctionArgs) => {
@@ -14,7 +14,8 @@ export const customerLoader = async ({ params }: LoaderFunctionArgs) => {
const query = customerDetailQuery(id!)
return (
queryClient.getQueryData<AdminCustomerResponse>(query.queryKey) ??
(await queryClient.fetchQuery(query))
queryClient.getQueryData<{ customer: HttpTypes.AdminCustomer }>(
query.queryKey
) ?? (await queryClient.fetchQuery(query))
)
}

View File

@@ -1,5 +1,4 @@
import { zodResolver } from "@hookform/resolvers/zod"
import { AdminCustomerResponse } from "@medusajs/types"
import { Button, Input, toast } from "@medusajs/ui"
import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
@@ -17,9 +16,10 @@ import {
metadataToFormValues,
} from "../../../../../lib/metadata.ts"
import { metadataFormSchema } from "../../../../../lib/validation.ts"
import { HttpTypes } from "@medusajs/types"
type EditCustomerFormProps = {
customer: AdminCustomerResponse["customer"]
customer: HttpTypes.AdminCustomer
}
const EditCustomerSchema = zod.object({

View File

@@ -1,5 +1,4 @@
import { PencilSquare } from "@medusajs/icons"
import { AdminCustomerResponse } from "@medusajs/types"
import { Button, Container, Heading } from "@medusajs/ui"
import { keepPreviousData } from "@tanstack/react-query"
import { createColumnHelper } from "@tanstack/react-table"
@@ -14,6 +13,7 @@ import { useCustomerTableColumns } from "../../../../../hooks/table/columns/use-
import { useCustomerTableFilters } from "../../../../../hooks/table/filters/use-customer-table-filters"
import { useCustomerTableQuery } from "../../../../../hooks/table/query/use-customer-table-query"
import { useDataTable } from "../../../../../hooks/use-data-table"
import { HttpTypes } from "@medusajs/types"
const PAGE_SIZE = 20
@@ -82,7 +82,7 @@ export const CustomerListTable = () => {
const CustomerActions = ({
customer,
}: {
customer: AdminCustomerResponse["customer"]
customer: HttpTypes.AdminCustomer
}) => {
const { t } = useTranslation()
@@ -103,7 +103,7 @@ const CustomerActions = ({
)
}
const columnHelper = createColumnHelper<AdminCustomerResponse["customer"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomer>()
const useColumns = () => {
const columns = useCustomerTableColumns()

View File

@@ -10,7 +10,6 @@ import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
import * as zod from "zod"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import {
RouteFocusModal,
useRouteModal,
@@ -191,8 +190,7 @@ export const AddCustomerGroupsForm = ({
)
}
const columnHelper =
createColumnHelper<AdminCustomerGroupResponse["customer_group"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomerGroup>()
const useColumns = () => {
const { t } = useTranslation()

View File

@@ -14,7 +14,6 @@ import { useFieldArray, type UseFormReturn } from "react-hook-form"
import { useTranslation } from "react-i18next"
import { XMarkMini } from "@medusajs/icons"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import { keepPreviousData } from "@tanstack/react-query"
import {
OnChangeFn,
@@ -35,6 +34,7 @@ import type {
PricingCreateSchemaType,
PricingCustomerGroupsArrayType,
} from "./schema"
import { HttpTypes } from "@medusajs/types"
type PricingDetailsFormProps = {
form: UseFormReturn<PricingCreateSchemaType>
@@ -511,8 +511,7 @@ const CustomerGroupDrawer = ({
)
}
const columnHelper =
createColumnHelper<AdminCustomerGroupResponse["customer_group"]>()
const columnHelper = createColumnHelper<HttpTypes.AdminCustomerGroup>()
const useColumns = () => {
const base = useCustomerGroupTableColumns()

View File

@@ -5,7 +5,6 @@
import {
CreateApiKeyDTO,
CreateCampaignDTO,
CreateCustomerDTO,
CreateFulfillmentSetDTO,
CreateInviteDTO,
CreatePriceListDTO,
@@ -20,7 +19,6 @@ import {
InventoryNext,
UpdateApiKeyDTO,
UpdateCampaignDTO,
UpdateCustomerDTO,
UpdatePriceListDTO,
UpdateProductCollectionDTO,
UpdatePromotionDTO,
@@ -43,10 +41,6 @@ export type UpdateStoreReq = UpdateStoreDTO
export type CreateApiKeyReq = CreateApiKeyDTO
export type UpdateApiKeyReq = UpdateApiKeyDTO
// Customers
export type CreateCustomerReq = CreateCustomerDTO
export type UpdateCustomerReq = Omit<UpdateCustomerDTO, "id">
// Sales Channels
export type CreateSalesChannelReq = CreateSalesChannelDTO
export type UpdateSalesChannelReq = UpdateSalesChannelDTO

View File

@@ -28,8 +28,15 @@ const server = setupServer(
})
}
}),
http.get(`${baseUrl}/replaced-header`, ({ request, params, cookies }) => {
request.headers
if (request.headers.get("Content-Type") === "application/xml") {
return HttpResponse.json({
test: "test",
})
}
}),
http.get(`${baseUrl}/apikey`, ({ request, params, cookies }) => {
console.log(request.headers.get("authorization"))
if (request.headers.get("authorization")?.startsWith("Basic")) {
return HttpResponse.json({
test: "test",
@@ -85,6 +92,14 @@ describe("Client", () => {
expect(resp).toEqual({ test: "test" })
})
it("should allow replacing a default header", async () => {
const resp = await client.fetch<any>("replaced-header", {
headers: { "content-Type": "application/xml" },
})
expect(resp).toEqual({ test: "test" })
})
it("should allow passing global headers", async () => {
const headClient = new Client({
baseUrl,

View File

@@ -152,4 +152,68 @@ export class Admin {
)
},
}
public customer = {
create: async (
body: HttpTypes.AdminCreateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{
customer: HttpTypes.AdminCustomer
token: string
}>(`/admin/customers`, {
method: "POST",
headers,
body,
query,
})
},
update: async (
id: string,
body: HttpTypes.AdminUpdateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ customer: HttpTypes.AdminCustomer }>(
`/admin/customers/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
},
list: async (queryParams?: FindParams, headers?: ClientHeaders) => {
return this.client.fetch<
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>
>(`/admin/customers`, {
headers,
query: queryParams,
})
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ customer: HttpTypes.AdminCustomer }>(
`/admin/customers/${id}`,
{
query,
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return this.client.fetch<DeleteResponse<"customer">>(
`/admin/customers/${id}`,
{
method: "DELETE",
headers,
}
)
},
}
}

View File

@@ -32,6 +32,8 @@ export class Auth {
} else {
this.client.setToken(token)
}
return token
}
logout = async () => {

View File

@@ -20,7 +20,7 @@ const toBase64 = (str: string) => {
const sanitizeHeaders = (headers: Headers) => {
return {
...Object.fromEntries(headers.entries()),
Authorization: "<REDACTED>",
authorization: "<REDACTED>",
}
}
@@ -45,7 +45,9 @@ const normalizeRequest = (
const normalizeResponse = async (resp: Response, reqHeaders: Headers) => {
if (resp.status >= 300) {
const jsonError = await resp.json().catch(() => ({})) as { message?: string }
const jsonError = (await resp.json().catch(() => ({}))) as {
message?: string
}
throw new FetchError(
jsonError.message ?? resp.statusText,
resp.statusText,

View File

@@ -314,6 +314,17 @@ export class Store {
}
public order = {
list: async (
query?: FindParams & HttpTypes.StoreOrderFilters,
headers?: ClientHeaders
) => {
return this.client.fetch<
PaginatedResponse<{ orders: HttpTypes.StoreOrder[] }>
>(`/store/orders`, {
query,
headers,
})
},
retrieve: async (
id: string,
query?: SelectParams,
@@ -328,4 +339,46 @@ export class Store {
)
},
}
public customer = {
create: async (
body: HttpTypes.StoreCreateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{
customer: HttpTypes.StoreCustomer
token: string
}>(`/store/customers`, {
method: "POST",
headers,
body,
query,
})
},
update: async (
body: HttpTypes.StoreUpdateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ customer: HttpTypes.StoreCustomer }>(
`/store/customers/me`,
{
method: "POST",
headers,
body,
query,
}
)
},
retrieve: async (query?: SelectParams, headers?: ClientHeaders) => {
return this.client.fetch<{ customer: HttpTypes.StoreCustomer }>(
`/store/customers/me`,
{
query,
headers,
}
)
},
}
}

View File

@@ -0,0 +1,25 @@
import {
BaseCreateCustomer,
BaseCustomer,
BaseCustomerAddress,
BaseCustomerAddressFilters,
BaseCustomerFilters,
BaseCustomerGroup,
BaseUpdateCustomer,
CustomerGroupInCustomerFilters,
} from "./common"
export interface AdminCustomerGroup extends BaseCustomerGroup {}
export interface AdminCustomer extends BaseCustomer {
has_account: boolean
groups?: AdminCustomerGroup[]
}
export interface AdminCustomerAddress extends BaseCustomerAddress {}
export interface AdminCustomerFilters extends BaseCustomerFilters {
groups: CustomerGroupInCustomerFilters | string[] | string
}
export interface AdminCustomerAddressFilters
extends BaseCustomerAddressFilters {}
export interface AdminCreateCustomer extends BaseCreateCustomer {}
export interface AdminUpdateCustomer extends BaseUpdateCustomer {}

View File

@@ -1,66 +0,0 @@
import { PaginatedResponse } from "../../common"
export interface CustomerGroupResponse {
id: string
name: string | null
customers: CustomerResponse[]
metadata: Record<string, unknown> | null
created_at: string
updated_at: string
}
export interface CustomerAddressResponse {
id: string
address_name: string | null
is_default_shipping: boolean
is_default_billing: boolean
customer_id: string
company: string | null
first_name: string | null
last_name: string | null
address_1: string | null
address_2: string | null
city: string | null
country_code: string | null
province: string | null
postal_code: string | null
phone: string | null
metadata: Record<string, unknown> | null
created_at: string
updated_at: string
}
interface CustomerResponse {
id: string
email: string
default_billing_address_id: string | null
default_shipping_address_id: string | null
company_name: string | null
first_name: string | null
last_name: string | null
has_account: boolean
addresses: CustomerAddressResponse[]
phone?: string | null
groups?: CustomerGroupResponse[]
metadata?: Record<string, unknown>
created_by?: string | null
deleted_at?: Date | string | null
created_at?: Date | string
updated_at?: Date | string
}
export interface AdminCustomerResponse {
customer: CustomerResponse
}
export type AdminCustomerListResponse = PaginatedResponse<{
customers: CustomerResponse[]
}>
export interface AdminCustomerGroupResponse {
customer_group: CustomerGroupResponse
}
export type AdminCustomerGroupListResponse = PaginatedResponse<{
customer_groups: CustomerGroupResponse[]
}>

View File

@@ -1 +0,0 @@
export * from "./customer"

View File

@@ -0,0 +1,97 @@
import { BaseFilterable, OperatorMap } from "../../dal"
export interface BaseCustomerGroup {
id: string
name: string | null
customers: BaseCustomer[]
metadata: Record<string, unknown> | null
created_at: string
updated_at: string
}
export interface BaseCustomerAddress {
id: string
address_name: string | null
is_default_shipping: boolean
is_default_billing: boolean
customer_id: string
company: string | null
first_name: string | null
last_name: string | null
address_1: string | null
address_2: string | null
city: string | null
country_code: string | null
province: string | null
postal_code: string | null
phone: string | null
metadata: Record<string, unknown> | null
created_at: string
updated_at: string
}
export interface BaseCustomer {
id: string
email: string
default_billing_address_id: string | null
default_shipping_address_id: string | null
company_name: string | null
first_name: string | null
last_name: string | null
addresses: BaseCustomerAddress[]
phone?: string | null
metadata?: Record<string, unknown>
created_by?: string | null
deleted_at?: Date | string | null
created_at?: Date | string
updated_at?: Date | string
}
export interface CustomerGroupInCustomerFilters {
id: string[] | string
name: string[] | string
created_at: OperatorMap<string>
updated_at: OperatorMap<string>
deleted_at: OperatorMap<string>
}
export interface BaseCustomerFilters
extends BaseFilterable<BaseCustomerFilters> {
q?: string
id?: string[] | string | OperatorMap<string | string[]>
email?: string[] | string | OperatorMap<string>
company_name?: string[] | string | OperatorMap<string>
first_name?: string[] | string | OperatorMap<string>
last_name?: string[] | string | OperatorMap<string>
created_by?: string[] | string | OperatorMap<string>
created_at?: OperatorMap<string>
updated_at?: OperatorMap<string>
deleted_at?: OperatorMap<string>
}
export interface BaseCustomerAddressFilters
extends BaseFilterable<BaseCustomerAddressFilters> {
q?: string
company?: string[] | string
city?: string[] | string
country_code?: string[] | string
province?: string[] | string
postal_code?: string[] | string
}
export interface BaseCreateCustomer {
email: string
company_name?: string
first_name?: string
last_name?: string
phone?: string
metadata?: Record<string, unknown>
}
export interface BaseUpdateCustomer {
company_name?: string
first_name?: string
last_name?: string
phone?: string
metadata?: Record<string, unknown>
}

View File

@@ -1 +1,2 @@
export * from "./admin"
export * from "./store"

View File

@@ -0,0 +1,17 @@
import {
BaseCreateCustomer,
BaseCustomer,
BaseCustomerAddress,
BaseCustomerAddressFilters,
BaseCustomerFilters,
BaseUpdateCustomer,
} from "./common"
export interface StoreCustomer extends BaseCustomer {}
export interface StoreCustomerAddress extends BaseCustomerAddress {}
export interface StoreCustomerFilters extends BaseCustomerFilters {}
export interface StoreCustomerAddressFilters
extends BaseCustomerAddressFilters {}
export interface StoreCreateCustomer extends BaseCreateCustomer {}
export interface StoreUpdateCustomer extends BaseUpdateCustomer {}

View File

@@ -19,4 +19,3 @@ export * from "./sales-channel"
export * from "./stock-locations"
export * from "./tax"
export * from "./user"

View File

@@ -1,3 +1,4 @@
import { BaseFilterable, OperatorMap } from "../../dal"
import { BigNumberValue } from "../../totals"
import { BasePaymentCollection } from "../payment/common"
import { BaseProduct, BaseProductVariant } from "../product/common"
@@ -262,3 +263,8 @@ export interface BaseOrder {
original_shipping_subtotal: number
original_shipping_tax_total: number
}
export interface BaseOrderFilters extends BaseFilterable<BaseOrderFilters> {
id?: string[] | string | OperatorMap<string | string[]>
status?: string[] | string | OperatorMap<string | string[]>
}

View File

@@ -1,6 +1,7 @@
import {
BaseOrder,
BaseOrderAddress,
BaseOrderFilters,
BaseOrderLineItem,
BaseOrderShippingMethod,
} from "./common"
@@ -9,3 +10,5 @@ export interface StoreOrder extends BaseOrder {}
export interface StoreOrderLineItem extends BaseOrderLineItem {}
export interface StoreOrderAddress extends BaseOrderAddress {}
export interface StoreOrderShippingMethod extends BaseOrderShippingMethod {}
export interface StoreOrderFilters extends BaseOrderFilters {}

View File

@@ -2,7 +2,6 @@ import {
deleteCustomersWorkflow,
updateCustomersWorkflow,
} from "@medusajs/core-flows"
import { AdminCustomerResponse } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
@@ -10,10 +9,11 @@ import {
} from "../../../../types/routing"
import { refetchCustomer } from "../helpers"
import { AdminUpdateCustomerType } from "../validators"
import { AdminCustomer } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse<AdminCustomerResponse>
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const customer = await refetchCustomer(
req.params.id,
@@ -33,7 +33,7 @@ export const GET = async (
export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType>,
res: MedusaResponse<AdminCustomerResponse>
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const { errors } = await updateCustomersWorkflow(req.scope).run({
input: {

View File

@@ -1,8 +1,5 @@
import { createCustomersWorkflow } from "@medusajs/core-flows"
import {
AdminCustomerListResponse,
AdminCustomerResponse,
} from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
@@ -13,10 +10,11 @@ import {
} from "../../../types/routing"
import { AdminCreateCustomerType } from "./validators"
import { refetchCustomer } from "./helpers"
import { AdminCustomer, PaginatedResponse } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse<AdminCustomerListResponse>
res: MedusaResponse<PaginatedResponse<{ customers: AdminCustomer }>>
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
@@ -41,7 +39,7 @@ export const GET = async (
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerType>,
res: MedusaResponse<AdminCustomerResponse>
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const createCustomers = createCustomersWorkflow(req.scope)

View File

@@ -61,7 +61,7 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
actor_type,
auth_identity_id: authIdentity.id,
app_metadata: {
entityIdKey: entityId,
[entityIdKey]: entityId,
},
},
{

View File

@@ -59,5 +59,5 @@ export const retrieveTransformQueryConfig = {
export const listTransformQueryConfig = {
defaults: defaultStoreOrderFields,
isList: false,
isList: true,
}

View File

@@ -24,7 +24,7 @@ type MedusaSession = {
}
export const authenticate = (
actorType: string,
actorType: string | string[],
authType: AuthType | AuthType[],
options: { allowUnauthenticated?: boolean; allowUnregistered?: boolean } = {}
): RequestHandler => {