mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Added signup endpoint
This commit is contained in:
parent
f6ec88a817
commit
7d7e4342be
@ -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>
|
||||||
|
|
||||||
|
10
framework/bigcommerce/api/signup/index.ts
Normal file
10
framework/bigcommerce/api/signup/index.ts
Normal 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 }
|
62
framework/bigcommerce/api/signup/signup.ts
Normal file
62
framework/bigcommerce/api/signup/signup.ts
Normal 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
|
1
framework/bigcommerce/types/signup.ts
Normal file
1
framework/bigcommerce/types/signup.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from '@commerce/types/signup'
|
38
framework/commerce/api/endpoints/signup.ts
Normal file
38
framework/commerce/api/endpoints/signup.ts
Normal 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
|
@ -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>,
|
||||||
|
22
framework/commerce/types/signup.ts
Normal file
22
framework/commerce/types/signup.ts
Normal 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
8
pages/api/signup.ts
Normal 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,
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user