-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO-ISSUE] hotfix: link between edge firewall and domain (#2025)
- Loading branch information
1 parent
09e6568
commit 9112643
Showing
10 changed files
with
515 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Extracts the first error message from an API response. | ||
* For generic errors, returns the detail message. | ||
* For field validation errors, returns "field: error message". | ||
* | ||
* @param {Object} httpResponse - The HTTP response object. | ||
* @param {Object} httpResponse.body - The response body. | ||
* @returns {string} The formatted error message. | ||
*/ | ||
export const extractApiError = (httpResponse) => { | ||
const errorBody = httpResponse.body | ||
|
||
if (errorBody.detail) { | ||
return errorBody.detail | ||
} | ||
|
||
const findFirstError = (obj) => { | ||
for (const [key, value] of Object.entries(obj)) { | ||
if (Array.isArray(value)) { | ||
return `${key}: ${value[0]}` | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
const nestedError = findFirstError(value) | ||
if (nestedError) { | ||
const cleanFieldName = nestedError.replace(/^[^:]+\./, '') | ||
return cleanFieldName | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
|
||
return findFirstError(errorBody) || 'Unknown error occurred' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/services/edge-firewall-services/v4/load-edge-firewall-service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { extractApiError } from '@/helpers/extract-api-error' | ||
import { AxiosHttpClientAdapter, parseHttpResponse } from '@/services/axios/AxiosHttpClientAdapter' | ||
import { makeEdgeFirewallBaseUrl } from './make-edge-firewall-base-url' | ||
|
||
export const loadEdgeFirewallService = async ({ id }) => { | ||
let httpResponse = await AxiosHttpClientAdapter.request({ | ||
url: `${makeEdgeFirewallBaseUrl()}/${id}`, | ||
method: 'GET' | ||
}) | ||
|
||
httpResponse = adapt(httpResponse) | ||
|
||
return parseHttpResponse(httpResponse) | ||
} | ||
|
||
const adapt = ({ body, statusCode }) => { | ||
if (statusCode !== 200) { | ||
throw new Error(extractApiError({ body })).message | ||
} | ||
|
||
const payload = body.data | ||
const parsedBody = { | ||
id: payload.id, | ||
name: payload.name, | ||
isActive: payload.active, | ||
edgeFunctionsEnabled: payload.modules.edge_functions_enabled, | ||
networkProtectionEnabled: payload.modules.network_protection_enabled, | ||
wafEnabled: payload.modules.waf_enabled, | ||
debugRules: payload.debug_rules, | ||
domains: payload.domains || [], | ||
ddosProtectionUnmetered: true | ||
} | ||
|
||
return { | ||
body: parsedBody, | ||
statusCode | ||
} | ||
} |
Oops, something went wrong.