Added signup endpoint

This commit is contained in:
Luis Alvarez 2021-05-20 13:44:31 -05:00
parent f6ec88a817
commit 7d7e4342be
8 changed files with 145 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import type { CartAPI } from './cart'
import type { CustomerAPI } from './customer' import type { CustomerAPI } from './customer'
import type { LoginAPI } from './login' import type { LoginAPI } from './login'
import type { LogoutAPI } from './logout' import type { LogoutAPI } from './logout'
import type { SignupAPI } from './signup'
import login from './operations/login' import login from './operations/login'
export interface BigcommerceConfig extends CommerceAPIConfig { export interface BigcommerceConfig extends CommerceAPIConfig {
@ -114,7 +115,7 @@ export const provider = {
export type Provider = typeof provider export type Provider = typeof provider
export type APIs = CartAPI | CustomerAPI | LoginAPI | LogoutAPI export type APIs = CartAPI | CustomerAPI | LoginAPI | LogoutAPI | SignupAPI
export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P> export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>

View File

@ -0,0 +1,10 @@
import type { GetAPISchema } from '@commerce/api'
import type { SignupSchema } from '../../types/signup'
import type { BigcommerceAPI } from '..'
import signup from './signup'
export type SignupAPI = GetAPISchema<BigcommerceAPI, SignupSchema>
export type SignupEndpoint = SignupAPI['endpoint']
export const operations = { signup }

View File

@ -0,0 +1,62 @@
import { BigcommerceApiError } from '../utils/errors'
import type { SignupEndpoint } from '.'
const signup: SignupEndpoint['operations']['signup'] = async ({
res,
body: { firstName, lastName, email, password },
config,
commerce,
}) => {
// TODO: Add proper validations with something like Ajv
if (!(firstName && lastName && email && password)) {
return res.status(400).json({
data: null,
errors: [{ message: 'Invalid request' }],
})
}
// TODO: validate the password and email
// Passwords must be at least 7 characters and contain both alphabetic
// and numeric characters.
try {
await config.storeApiFetch('/v3/customers', {
method: 'POST',
body: JSON.stringify([
{
first_name: firstName,
last_name: lastName,
email,
authentication: {
new_password: password,
},
},
]),
})
} catch (error) {
if (error instanceof BigcommerceApiError && error.status === 422) {
const hasEmailError = '0.email' in error.data?.errors
// If there's an error with the email, it most likely means it's duplicated
if (hasEmailError) {
return res.status(400).json({
data: null,
errors: [
{
message: 'The email is already in use',
code: 'duplicated_email',
},
],
})
}
}
throw error
}
// Login the customer right after creating it
await commerce.login({ variables: { email, password }, res, config })
res.status(200).json({ data: null })
}
export default signup

View File

@ -0,0 +1 @@
export * from '@commerce/types/signup'

View File

@ -0,0 +1,38 @@
import type { SignupSchema } from '../../types/signup'
import { CommerceAPIError } from '../utils/errors'
import isAllowedOperation from '../utils/is-allowed-operation'
import type { GetAPISchema } from '..'
const signupEndpoint: GetAPISchema<
any,
SignupSchema
>['endpoint']['handler'] = async (ctx) => {
const { req, res, operations, config } = ctx
if (
!isAllowedOperation(req, res, {
POST: operations['signup'],
})
) {
return
}
const { cookies } = req
const cartId = cookies[config.cartCookie]
try {
const body = { ...req.body, cartId }
return await operations['signup']({ ...ctx, body })
} catch (error) {
console.error(error)
const message =
error instanceof CommerceAPIError
? 'An unexpected error ocurred with the Commerce API'
: 'An unexpected error ocurred'
res.status(500).json({ data: null, errors: [{ message }] })
}
}
export default signupEndpoint

View File

@ -5,6 +5,7 @@ import type { CartSchema } from '../types/cart'
import type { CustomerSchema } from '../types/customer' import type { CustomerSchema } from '../types/customer'
import type { LoginSchema } from '../types/login' import type { LoginSchema } from '../types/login'
import type { LogoutSchema } from '../types/logout' import type { LogoutSchema } from '../types/logout'
import type { SignupSchema } from '../types/signup'
import { import {
defaultOperations, defaultOperations,
OPERATIONS, OPERATIONS,
@ -17,6 +18,7 @@ export type APISchemas =
| CustomerSchema | CustomerSchema
| LoginSchema | LoginSchema
| LogoutSchema | LogoutSchema
| SignupSchema
export type GetAPISchema< export type GetAPISchema<
C extends CommerceAPI<any>, C extends CommerceAPI<any>,

View File

@ -0,0 +1,22 @@
export type SignupBody = {
firstName: string
lastName: string
email: string
password: string
}
export type SignupTypes = {
body: SignupBody
}
export type SignupSchema<T extends SignupTypes = SignupTypes> = {
endpoint: {
options: {}
operations: {
signup: {
data: null
body: T['body']
}
}
}
}

8
pages/api/signup.ts Normal file
View File

@ -0,0 +1,8 @@
import signup from '@commerce/api/endpoints/signup'
import { SignupAPI, operations } from '@framework/api/signup'
import commerce from '@lib/api/commerce'
export default commerce.endpoint({
handler: signup as SignupAPI['endpoint']['handler'],
operations,
})