From 600d8bb3259ce9b0469063cc035db926d1d281db Mon Sep 17 00:00:00 2001 From: Catalin Pinte <1243434+cond0r@users.noreply.github.com> Date: Tue, 4 Oct 2022 15:40:59 +0300 Subject: [PATCH] Updates --- packages/commerce/src/api/endpoints/index.ts | 28 ++++++++++--------- .../src/api/utils/is-allowed-method.ts | 12 +++++++- .../src/api/utils/validate-handlers.ts | 7 ++++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/commerce/src/api/endpoints/index.ts b/packages/commerce/src/api/endpoints/index.ts index 0e8ab0808..466a79593 100644 --- a/packages/commerce/src/api/endpoints/index.ts +++ b/packages/commerce/src/api/endpoints/index.ts @@ -7,6 +7,7 @@ import { normalizeError } from '../utils/errors' * Handles the catch-all api endpoint for the Commerce API. * @param {CommerceAPI} commerce The Commerce API instance. * @param endpoints An object containing the handlers for each endpoint. + * @returns JSON response with the data or error. */ export default function createEndpoints
( commerce: CommerceAPI
, @@ -39,9 +40,7 @@ export default function createEndpoints
( ? req.query.commerce.join('/') : req.query.commerce - /** - * Check if the handler for this path exists and return a 404 if it doesn't - */ + // Check if the handler for this path exists and return a 404 if it doesn't if (!paths.includes(path)) { throw new Error( `Endpoint handler not implemented. Please use one of the available api endpoints: ${paths.join( @@ -51,22 +50,25 @@ export default function createEndpoints
( } const data = await handlers[path](req, res) - - /** - * If the handler returns a value but the response hasn't been sent yet, send it - */ + // If the handler returns a value but the response hasn't been sent yet, send it if (!res.headersSent) { res.status(200).json({ data, }) } } catch (error) { - console.error(error) - const { status, data, errors } = normalizeError(error) - res.status(status).json({ - data, - errors, - }) + /** + * Return the error as a JSON response only if the response hasn't been sent yet + * Eg. by the `isAllowedMethod` util returning a 405 status code + */ + if (!res.headersSent) { + console.error(error) + const { status, data, errors } = normalizeError(error) + res.status(status).json({ + data, + errors, + }) + } } } } diff --git a/packages/commerce/src/api/utils/is-allowed-method.ts b/packages/commerce/src/api/utils/is-allowed-method.ts index 51c37e221..f17f6f6af 100644 --- a/packages/commerce/src/api/utils/is-allowed-method.ts +++ b/packages/commerce/src/api/utils/is-allowed-method.ts @@ -14,7 +14,17 @@ export default function isAllowedMethod( if (!req.method || !methods.includes(req.method)) { res.status(405) res.setHeader('Allow', methods.join(', ')) - res.end() + res.json({ + errors: [ + { + message: `You are not allowed to use the ${ + req.method + } method for this route, please use one of the following methods: ${methods.join( + ', ' + )}`, + }, + ], + }) return false } diff --git a/packages/commerce/src/api/utils/validate-handlers.ts b/packages/commerce/src/api/utils/validate-handlers.ts index fa949a632..6a17d7913 100644 --- a/packages/commerce/src/api/utils/validate-handlers.ts +++ b/packages/commerce/src/api/utils/validate-handlers.ts @@ -3,7 +3,12 @@ import isAllowedMethod, { HTTP_METHODS } from './is-allowed-method' import { APIHandler } from './types' /** - * @throws Error if the method is not allowed + * Validates the request method and throws an error if it's not allowed, or if the handler is not implemented. + * and stops the execution of the handler. + * @param req The request object. + * @param res The response object. + * @param allowedOperations An object containing the handlers for each method. + * @throws Error when the method is not allowed or the handler is not implemented. */ export default function validateHandlers( req: NextApiRequest,