Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: add missing {{messages}}
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed Jan 7, 2024
1 parent 11fc9a3 commit 6e1d202
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log (@egomobile/api-utils)

## 5.0.0
## 5.0.1

- implement `throwOnUnexpectedApiResponse()` function
- code cleanups and improvements
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egomobile/api-utils",
"version": "5.0.0",
"version": "5.0.1",
"description": "REST API extensions for extensions for @egomobile/http-server module, a very fast alternative to Express.js",
"main": "lib/index.js",
"engines": {
Expand Down
20 changes: 12 additions & 8 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface IApiResponseValidatorContext {
* // following template placeholders are supported:
* // - {{body}} => response body as text with a maximum length of 131072 chars
* // - {{headers}} => list of HTTP response headers as one single text
* // - {{messages}} => list of extracted API messages items as one single text
* // - {{statusCode}} => HTTP status code, like 200, 404, 500 etc.
* // - {{statusText}} => text representation of HTTP status code, like `OK`, `Not Found`, `Internal Server Error` etc.
* //
Expand Down Expand Up @@ -189,6 +190,14 @@ export async function throwOnUnexpectedApiResponse(
strData = strData.substring(0, maxStringDataLength - 1) + "…";
}

const strMessages = messages.map((msg) => {
const codePrefix = typeof msg?.code === "number" ? `${msg.code} :: ` : "";
const messageText = String(msg?.message || "").trim();
const typePrefix = msg?.type ? `[${msg?.type}] ` : "";

return `- ${codePrefix}${typePrefix}${messageText}`;
}).join("\n");

let errorMessage: string;

const firstMatchingStatusErrorMessage = Object.entries(statusErrors).filter(([statusCodeRegex]) => {
Expand All @@ -206,17 +215,12 @@ export async function throwOnUnexpectedApiResponse(
errorMessage = errorMessage.split("{{statusText}}").join(response.statusText);
errorMessage = errorMessage.split("{{headers}}").join(strHeaders);
errorMessage = errorMessage.split("{{body}}").join(strData);
errorMessage = errorMessage.split("{{messages}}").join(strMessages);
}
else {
let suffix: string;
if (messages.length > 0) {
suffix = messages.map((msg) => {
const codePrefix = typeof msg?.code === "number" ? `${msg.code} :: ` : "";
const messageText = String(msg?.message || "").trim();
const typePrefix = msg?.type ? `[${msg?.type}] ` : "";

return `- ${codePrefix}${typePrefix}${messageText}`;
}).join("\n");
if (strMessages !== "") {
suffix = strMessages;
}
else {
suffix = `${strHeaders}\n\n${strData}`;
Expand Down

0 comments on commit 6e1d202

Please sign in to comment.