From f6ec88a817ac9fccd2eff52c10a8fb39e1b61599 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Thu, 20 May 2021 13:36:20 -0500 Subject: [PATCH] Add missing logout files --- framework/commerce/api/endpoints/logout.ts | 37 ++++++++++++++++++++++ pages/api/logout.ts | 8 +++++ 2 files changed, 45 insertions(+) create mode 100644 framework/commerce/api/endpoints/logout.ts create mode 100644 pages/api/logout.ts diff --git a/framework/commerce/api/endpoints/logout.ts b/framework/commerce/api/endpoints/logout.ts new file mode 100644 index 000000000..d89823d51 --- /dev/null +++ b/framework/commerce/api/endpoints/logout.ts @@ -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 diff --git a/pages/api/logout.ts b/pages/api/logout.ts new file mode 100644 index 000000000..20c51c66f --- /dev/null +++ b/pages/api/logout.ts @@ -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, +})