Skip to content

Commit

Permalink
#319 - created the success handler response
Browse files Browse the repository at this point in the history
  • Loading branch information
malaquiasdev committed Feb 28, 2021
1 parent 0ab24aa commit cc8cc06
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/libs/http/response/handler-error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ function handlerResponseError(
) {
logError({ message: message, params: { stack } });
const code = getErrorCodePattern(httpStatusCode, suffixStatusCode);
return parseResponseToAWSGatewayPattern(
createErrorResponseBody({ code, message, requestId }),
cacheControl(),
httpStatusCode,
);
return parseResponseToAWSGatewayPattern({
body: createErrorResponseBody({ code, message, requestId }),
statusCode: httpStatusCode,
});
}

export { handlerResponseError };
23 changes: 23 additions & 0 deletions src/libs/http/response/handler-success/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { parseResponseToAWSGatewayPattern } from '../parse-response-to-aws-gateway';

/**
* Create a base response object that can be used in the send respons method
* @public
* @memberof Response
* @function create
* @param {Object} [body] - value to json.stringify
* @param {Object} [headers] - headers to set on the response
* @param {Number} headers.maxAge - cache control max-age in seconds
* @param {Number} [statusCode] - http status code
* @param {String} [requestId] - AWS request id
* @returns {Object} the value set to the header
*/
function handlerSuccess({ body, headers, statusCode }, requestId) {
return parseResponseToAWSGatewayPattern({
body: { data: body, requestId },
headers,
statusCode,
});
}

export { handlerSuccess };
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('# Lib - HTTP - parseResponseToAWSGatewayPattern', () => {
'Cache-Control': 'private, max-age=0, no-cache, no-store, must-revalidate',
};

const res = parseResponseToAWSGatewayPattern(body, headers);
const res = parseResponseToAWSGatewayPattern({ body, headers });

expect(res).toBeInstanceOf(Object);
expect(res).toHaveProperty('body');
Expand All @@ -39,7 +39,7 @@ describe('# Lib - HTTP - parseResponseToAWSGatewayPattern', () => {
'Cache-Control': 'private, max-age=0, no-cache, no-store, must-revalidate',
};

const res = parseResponseToAWSGatewayPattern(body, headers, 201);
const res = parseResponseToAWSGatewayPattern({ body, headers, statusCode: 201 });

expect(res).toBeInstanceOf(Object);
expect(res).toHaveProperty('body');
Expand Down
7 changes: 5 additions & 2 deletions src/libs/http/response/parse-response-to-aws-gateway/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getCORSHeaders } from './get-cors-headers';
/*
import { cacheControl } from '../cache-control';

/**
* Convert the params in a valid response obj to AWS serverless model
* @private
* @memberof Response
Expand All @@ -9,12 +11,13 @@ import { getCORSHeaders } from './get-cors-headers';
* @param {Number} [statusCode] - http status code
* @returns {Object} the response object
*/
function parseResponseToAWSGatewayPattern(body, headers, statusCode = 200) {
function parseResponseToAWSGatewayPattern({ body, headers, statusCode = 200 }) {
return {
statusCode,
headers: {
...headers,
...getCORSHeaders(),
...cacheControl(headers),
},
body: JSON.stringify(body),
};
Expand Down

0 comments on commit cc8cc06

Please sign in to comment.