Added login operation

This commit is contained in:
Luis Alvarez 2020-10-20 12:51:35 -05:00
parent c9ee2af3e5
commit 7db006f64a
3 changed files with 57 additions and 5 deletions

View File

@ -17,8 +17,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 +33,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 +53,8 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
throw error
}
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