From 7db006f64a69acb1646ed01443a500cf8bec1de9 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Tue, 20 Oct 2020 12:51:35 -0500 Subject: [PATCH] Added login operation --- .../api/customers/handlers/create-customer.ts | 8 +-- lib/bigcommerce/api/customers/index.ts | 3 +- lib/bigcommerce/api/operations/login.ts | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 lib/bigcommerce/api/operations/login.ts diff --git a/lib/bigcommerce/api/customers/handlers/create-customer.ts b/lib/bigcommerce/api/customers/handlers/create-customer.ts index caefeb245..d0993a62e 100644 --- a/lib/bigcommerce/api/customers/handlers/create-customer.ts +++ b/lib/bigcommerce/api/customers/handlers/create-customer.ts @@ -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 diff --git a/lib/bigcommerce/api/customers/index.ts b/lib/bigcommerce/api/customers/index.ts index 25b2fd270..c39f55f6a 100644 --- a/lib/bigcommerce/api/customers/index.ts +++ b/lib/bigcommerce/api/customers/index.ts @@ -8,7 +8,7 @@ import createCustomer from './handlers/create-customer' type Body = Partial | undefined -export type Customer = any +export type Customer = null export type CreateCustomerBody = { firstName: string @@ -38,7 +38,6 @@ const customersApi: BigcommerceApiHandler = async ( try { if (req.method === 'POST') { - console.log('BODY', req.body) const body = { cartId, ...req.body } return await handlers['createCustomer']({ req, res, config, body }) } diff --git a/lib/bigcommerce/api/operations/login.ts b/lib/bigcommerce/api/operations/login.ts new file mode 100644 index 000000000..a73e6beb4 --- /dev/null +++ b/lib/bigcommerce/api/operations/login.ts @@ -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 + +export type LoginVariables = LoginMutationVariables + +async function login(opts: { + variables: LoginVariables + config?: BigcommerceConfig +}): Promise + +async function login(opts: { + query: string + variables: V + config?: BigcommerceConfig +}): Promise> + +async function login({ + query = loginMutation, + variables, + config, +}: { + query?: string + variables: LoginVariables + config?: BigcommerceConfig +}): Promise { + config = getConfig(config) + + const data = await config.fetch>(query, { + variables, + }) + + return { + result: data.login?.result, + } +} + +export default login