Add missing logout files

This commit is contained in:
Luis Alvarez 2021-05-20 13:36:20 -05:00
parent 1d2451550b
commit f6ec88a817
2 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import type { LogoutSchema } from '../../types/logout'
import { CommerceAPIError } from '../utils/errors'
import isAllowedOperation from '../utils/is-allowed-operation'
import type { GetAPISchema } from '..'
const logoutEndpoint: GetAPISchema<
any,
LogoutSchema
>['endpoint']['handler'] = async (ctx) => {
const { req, res, operations } = ctx
if (
!isAllowedOperation(req, res, {
GET: operations['logout'],
})
) {
return
}
try {
const redirectTo = req.query.redirect_to
const body = typeof redirectTo === 'string' ? { redirectTo } : {}
return await operations['logout']({ ...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 logoutEndpoint

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

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