4
0
forked from crowetic/commerce

Merge branch 'master' of github.com:okbel/e-comm-example

This commit is contained in:
Belen Curcio 2020-10-20 15:49:15 -03:00
commit 1c423b926b
5 changed files with 75 additions and 6 deletions

View File

@ -49,6 +49,7 @@ const getProducts: ProductsHandlers['getProducts'] = async ({
// We want the GraphQL version of each product
const graphqlData = await getAllProducts({
variables: { first: LIMIT, entityIds },
config,
})
// Put the products in an object that we can use to get them by id
const productsById = graphqlData.products.reduce<{

View File

@ -1,5 +1,6 @@
import { BigcommerceApiError } from '../../utils/errors'
import { CustomersHandlers } from '..'
import login from '../../operations/login'
import type { CustomersHandlers } from '..'
const createCustomer: CustomersHandlers['createCustomer'] = async ({
res,
@ -17,8 +18,10 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
// Passwords must be at least 7 characters and contain both alphabetic
// and numeric characters.
let result: { data?: any } = {}
try {
const { data } = await config.storeApiFetch('/v3/customers', {
result = await config.storeApiFetch('/v3/customers', {
method: 'POST',
body: JSON.stringify([
{
@ -31,8 +34,6 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
},
]),
})
res.status(200).json({ data })
} catch (error) {
if (error instanceof BigcommerceApiError && error.status === 422) {
const hasEmailError = '0.email' in error.data?.errors
@ -53,6 +54,14 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
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

View File

@ -8,7 +8,7 @@ import createCustomer from './handlers/create-customer'
type Body<T> = Partial<T> | undefined
export type Customer = any
export type Customer = null
export type CreateCustomerBody = {
firstName: string
@ -38,7 +38,6 @@ const customersApi: BigcommerceApiHandler<Customer, CustomersHandlers> = async (
try {
if (req.method === 'POST') {
console.log('BODY', req.body)
const body = { cartId, ...req.body }
return await handlers['createCustomer']({ req, res, config, body })
}

View 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

View File

@ -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'>
}