forked from crowetic/commerce
Added login operation
This commit is contained in:
parent
c9ee2af3e5
commit
7db006f64a
@ -17,8 +17,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 +33,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 +53,8 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
|||||||
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user