feat: Update customer related typings and sdk methods (#7440)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ export class Auth {
|
||||
} else {
|
||||
this.client.setToken(token)
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
logout = async () => {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
25
packages/core/types/src/http/customer/admin.ts
Normal file
25
packages/core/types/src/http/customer/admin.ts
Normal 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 {}
|
||||
@@ -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[]
|
||||
}>
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./customer"
|
||||
97
packages/core/types/src/http/customer/common.ts
Normal file
97
packages/core/types/src/http/customer/common.ts
Normal 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>
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./admin"
|
||||
export * from "./store"
|
||||
|
||||
17
packages/core/types/src/http/customer/store.ts
Normal file
17
packages/core/types/src/http/customer/store.ts
Normal 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 {}
|
||||
@@ -19,4 +19,3 @@ export * from "./sales-channel"
|
||||
export * from "./stock-locations"
|
||||
export * from "./tax"
|
||||
export * from "./user"
|
||||
|
||||
|
||||
@@ -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[]>
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
Reference in New Issue
Block a user