mirror of
https://github.com/vercel/commerce.git
synced 2025-06-30 18:31:21 +00:00
Uptate customer fetch types
This commit is contained in:
parent
cdf8005c21
commit
1fac8e187d
@ -1,6 +1,7 @@
|
|||||||
import { getConfig, ShopifyConfig } from '../api'
|
import { getConfig, ShopifyConfig } from '../api'
|
||||||
import getCustomerIdQuery from '../utils/queries/get-customer-id-query'
|
import getCustomerIdQuery from '../utils/queries/get-customer-id-query'
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
import { GetCustomerIdQuery } from '../schema'
|
||||||
|
|
||||||
async function getCustomerId({
|
async function getCustomerId({
|
||||||
customerToken: customerAccesToken,
|
customerToken: customerAccesToken,
|
||||||
@ -8,17 +9,19 @@ async function getCustomerId({
|
|||||||
}: {
|
}: {
|
||||||
customerToken: string
|
customerToken: string
|
||||||
config?: ShopifyConfig
|
config?: ShopifyConfig
|
||||||
}): Promise<number | undefined> {
|
}): Promise<string | undefined> {
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const { data } = await config.fetch(getCustomerIdQuery, {
|
const {
|
||||||
|
data: { customer },
|
||||||
|
} = await config.fetch<GetCustomerIdQuery>(getCustomerIdQuery, {
|
||||||
variables: {
|
variables: {
|
||||||
customerAccesToken:
|
customerAccesToken:
|
||||||
customerAccesToken || Cookies.get(config.customerCookie),
|
customerAccesToken || Cookies.get(config.customerCookie),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return data.customer?.id
|
return customer?.id
|
||||||
}
|
}
|
||||||
|
|
||||||
export default getCustomerId
|
export default getCustomerId
|
||||||
|
@ -10,11 +10,15 @@ export const handler: SWRHook<Customer | null> = {
|
|||||||
query: getCustomerQuery,
|
query: getCustomerQuery,
|
||||||
},
|
},
|
||||||
async fetcher({ options, fetch }) {
|
async fetcher({ options, fetch }) {
|
||||||
const data = await fetch<any | null>({
|
const customerAccessToken = getCustomerToken()
|
||||||
|
if (customerAccessToken) {
|
||||||
|
const data = await fetch({
|
||||||
...options,
|
...options,
|
||||||
variables: { customerAccessToken: getCustomerToken() },
|
variables: { customerAccessToken: getCustomerToken() },
|
||||||
})
|
})
|
||||||
return data.customer ?? null
|
return data.customer
|
||||||
|
}
|
||||||
|
return null
|
||||||
},
|
},
|
||||||
useHook: ({ useData }) => (input) => {
|
useHook: ({ useData }) => (input) => {
|
||||||
return useData({
|
return useData({
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { CollectionEdge } from '../schema'
|
import { GetSiteCollectionsQuery } from '../schema'
|
||||||
import { getConfig, ShopifyConfig } from '../api'
|
import { getConfig, ShopifyConfig } from '../api'
|
||||||
import getAllCollectionsQuery from '../utils/queries/get-all-collections-query'
|
import getAllCollectionsQuery from '../utils/queries/get-all-collections-query'
|
||||||
|
|
||||||
@ -10,11 +10,13 @@ const getAllCollections = async (options?: {
|
|||||||
let { config, variables = { first: 250 } } = options ?? {}
|
let { config, variables = { first: 250 } } = options ?? {}
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const { data } = await config.fetch(getAllCollectionsQuery, { variables })
|
const { data } = await config.fetch<GetSiteCollectionsQuery>(
|
||||||
const edges = data.collections?.edges ?? []
|
getAllCollectionsQuery,
|
||||||
|
{ variables }
|
||||||
|
)
|
||||||
|
|
||||||
const categories = edges.map(
|
const categories = data.collections.edges.map(
|
||||||
({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({
|
({ node: { id: entityId, title: name, handle } }) => ({
|
||||||
entityId,
|
entityId,
|
||||||
name,
|
name,
|
||||||
path: `/${handle}`,
|
path: `/${handle}`,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user