forked from crowetic/commerce
Merge branch 'master' of github.com:okbel/e-comm-example
This commit is contained in:
commit
1c423b926b
@ -49,6 +49,7 @@ const getProducts: ProductsHandlers['getProducts'] = async ({
|
|||||||
// We want the GraphQL version of each product
|
// We want the GraphQL version of each product
|
||||||
const graphqlData = await getAllProducts({
|
const graphqlData = await getAllProducts({
|
||||||
variables: { first: LIMIT, entityIds },
|
variables: { first: LIMIT, entityIds },
|
||||||
|
config,
|
||||||
})
|
})
|
||||||
// Put the products in an object that we can use to get them by id
|
// Put the products in an object that we can use to get them by id
|
||||||
const productsById = graphqlData.products.reduce<{
|
const productsById = graphqlData.products.reduce<{
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { BigcommerceApiError } from '../../utils/errors'
|
import { BigcommerceApiError } from '../../utils/errors'
|
||||||
import { CustomersHandlers } from '..'
|
import login from '../../operations/login'
|
||||||
|
import type { CustomersHandlers } from '..'
|
||||||
|
|
||||||
const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
||||||
res,
|
res,
|
||||||
@ -17,8 +18,10 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
|||||||
// Passwords must be at least 7 characters and contain both alphabetic
|
// Passwords must be at least 7 characters and contain both alphabetic
|
||||||
// and numeric characters.
|
// and numeric characters.
|
||||||
|
|
||||||
|
let result: { data?: any } = {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { data } = await config.storeApiFetch('/v3/customers', {
|
result = await config.storeApiFetch('/v3/customers', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify([
|
body: JSON.stringify([
|
||||||
{
|
{
|
||||||
@ -31,8 +34,6 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
|||||||
},
|
},
|
||||||
]),
|
]),
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ data })
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof BigcommerceApiError && error.status === 422) {
|
if (error instanceof BigcommerceApiError && error.status === 422) {
|
||||||
const hasEmailError = '0.email' in error.data?.errors
|
const hasEmailError = '0.email' in error.data?.errors
|
||||||
@ -53,6 +54,14 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
|||||||
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('DATA', result.data)
|
||||||
|
|
||||||
|
const loginData = await login({ variables: { email, password }, config })
|
||||||
|
|
||||||
|
console.log('LOGIN DATA', loginData)
|
||||||
|
|
||||||
|
res.status(200).json({ data: result.data ?? null })
|
||||||
}
|
}
|
||||||
|
|
||||||
export default createCustomer
|
export default createCustomer
|
||||||
|
@ -8,7 +8,7 @@ import createCustomer from './handlers/create-customer'
|
|||||||
|
|
||||||
type Body<T> = Partial<T> | undefined
|
type Body<T> = Partial<T> | undefined
|
||||||
|
|
||||||
export type Customer = any
|
export type Customer = null
|
||||||
|
|
||||||
export type CreateCustomerBody = {
|
export type CreateCustomerBody = {
|
||||||
firstName: string
|
firstName: string
|
||||||
@ -38,7 +38,6 @@ const customersApi: BigcommerceApiHandler<Customer, CustomersHandlers> = async (
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
console.log('BODY', req.body)
|
|
||||||
const body = { cartId, ...req.body }
|
const body = { cartId, ...req.body }
|
||||||
return await handlers['createCustomer']({ req, res, config, body })
|
return await handlers['createCustomer']({ req, res, config, body })
|
||||||
}
|
}
|
||||||
|
51
lib/bigcommerce/api/operations/login.ts
Normal file
51
lib/bigcommerce/api/operations/login.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import type {
|
||||||
|
LoginMutation,
|
||||||
|
LoginMutationVariables,
|
||||||
|
} from 'lib/bigcommerce/schema'
|
||||||
|
import type { RecursivePartial } from '../utils/types'
|
||||||
|
import { BigcommerceConfig, getConfig } from '..'
|
||||||
|
|
||||||
|
export const loginMutation = /* GraphQL */ `
|
||||||
|
mutation login($email: String!, $password: String!) {
|
||||||
|
login(email: $email, password: $password) {
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
export type LoginResult<T extends { result?: any } = { result?: string }> = T
|
||||||
|
|
||||||
|
export type LoginVariables = LoginMutationVariables
|
||||||
|
|
||||||
|
async function login(opts: {
|
||||||
|
variables: LoginVariables
|
||||||
|
config?: BigcommerceConfig
|
||||||
|
}): Promise<LoginResult>
|
||||||
|
|
||||||
|
async function login<T extends { result?: any }, V = any>(opts: {
|
||||||
|
query: string
|
||||||
|
variables: V
|
||||||
|
config?: BigcommerceConfig
|
||||||
|
}): Promise<LoginResult<T>>
|
||||||
|
|
||||||
|
async function login({
|
||||||
|
query = loginMutation,
|
||||||
|
variables,
|
||||||
|
config,
|
||||||
|
}: {
|
||||||
|
query?: string
|
||||||
|
variables: LoginVariables
|
||||||
|
config?: BigcommerceConfig
|
||||||
|
}): Promise<LoginResult> {
|
||||||
|
config = getConfig(config)
|
||||||
|
|
||||||
|
const data = await config.fetch<RecursivePartial<LoginMutation>>(query, {
|
||||||
|
variables,
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
result: data.login?.result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default login
|
9
lib/bigcommerce/schema.d.ts
vendored
9
lib/bigcommerce/schema.d.ts
vendored
@ -1926,3 +1926,12 @@ export type GetSiteInfoQuery = { __typename?: 'Query' } & {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type LoginMutationVariables = Exact<{
|
||||||
|
email: Scalars['String']
|
||||||
|
password: Scalars['String']
|
||||||
|
}>
|
||||||
|
|
||||||
|
export type LoginMutation = { __typename?: 'Mutation' } & {
|
||||||
|
login: { __typename?: 'LoginResult' } & Pick<LoginResult, 'result'>
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user