Skip to content

Commit

Permalink
Add frontend http client (#7000)
Browse files Browse the repository at this point in the history
* feat(http): create http frontend client

- Create HTTP client based on old services
- Create HTTP client request interceptor: request
- Create HTTP client generic: GenericRequest
- Create HTTP client server: WzRequest, ApiCheck and WzAuthentication
- Enhance server API backend client
  See #6995
- Rename ILogger type to Logger

* fix: add VSCode settings file

* chore: remove comment

* feat: add suggestions of code review

* feat(core): add TableData and ServerTable components to core plugin

- Add TableData component (based on TableData of main plugin)
- Add ServerTable component (based on TableWzAPI of main plugin)
  - Add SearchBar (copied from main plugin)
  - Add FileSaver (copied from main plugin)

* test(core): fix ExportTableCsv test

* chore(prettier): fix some code syntax

* chore(prettier): fix some code syntax

* chore: remove console.log

* feat(core): enhance typing

* fix(http): move type definitions

* fix: tests

* fix: move types

* chore(changelog): add entry)

* fix(lint): code lint

* fix(prettier): code prettier

* fix(lint): code lint

* fix(lint): code lint

* Update plugins/wazuh-core/public/services/http/server-client.ts

Co-authored-by: Guido Modarelli <[email protected]>

* fix(http): review suggestions

* fix(http): review suggestions

* fix(http): fix options paramenter in http client

* fix(lint): simplify arrow-body-style configuration in ESLint settings

* fix(types): enhance SearchBarQueryLanguage and refine query language initialization logic

* fix(lint): update ESLint rules for unicorn and TypeScript to improve code quality and reduce false positives

* fix(lint): refine ESLint rules and clean up code with consistent error handling and improved variable naming conventions

* fix(lint): add rule for optional chaining and update WazuhApiCtrl for consistent use of optional chaining operator throughout

* fix(lint): simplify state update with optional chaining and improve clarity in search bar component code structure

* fix(lint): optimize settings update and clean up settings grouping logic for improved readability and performance in configuration.ts

* fix(lint): enhance rule for optional chaining to include empty ObjectExpression for better code quality and consistency

* fix(lint): simplify array mapping in AQL tests for improved readability and consistency in test case logic

* fix(lint): refactor object mapping in AQL component for improved readability and consistency across query suggestion logic

* fix(lint): streamline object mapping in WQL for enhanced readability and consistency across query language component logic

* fix(lint): simplify object mapping in TableData for improved readability and consistency across data table component logic

* fix(lint): refactor object mapping in ExportTableCsv for improved readability and consistency across export functionality logic

* fix(lint): refactor error handling in GenericRequest for improved readability and consistency in HTTP client logic

* fix(lint): streamline error message extraction in WzRequest for improved readability and consistency in HTTP response handling

* fix(lint): add rule for optional chaining with empty array expressions for improved code quality in logical expressions

* fix(lint): refactor token extraction using optional chaining for improved readability in ServerAPIClient response handling

* fix(enum): remove redundant enum definition for plugin settings, improving code clarity in configuration service file

* fix(lint): add rule for empty functions in test files to enhance code quality in TypeScript testing configuration

* fix(refactor): optimize settings categorization logic for clarity and maintainability in configuration service file

* fix(lint): remove unused variable warnings and improve code readability in suggest input and AQL test files

* fix(lint): eliminate unnecessary eslint-disable comments and enhance type safety in query language AQL component

* fix(refactor): enhance search bar query language structure and type safety, ensuring consistent initialization and prop typing

* fix(lint): turn off 'default-param-last' rule and clean up related eslint-disable comments in query language test files

* fix(lint): remove unused variables and clean up eslint-disable comments in WQL query language tests for better readability

* fix(lint): update enum formatting rules in eslint config for improved consistency and clarity in variable naming conventions

* fix(aql): refactor token types to use enums and constants for improved clarity and maintainability in AQL query language code

* fix(lint): expand ESLint variable rule to include boolean, number, and string types for enhanced linting versatility

* fix(types): update UseStateStorageHook to default type 'any' for improved flexibility in hook implementation

* fix(lint): add camelCase and PascalCase formats for object literal methods to ESLint rules for improved consistency in naming conventions

* fix(lint): include camelCase and PascalCase formats for variable names in ESLint rules for improved naming consistency

* fix(lint): update unused variable handling in table-data component to adhere to ESLint rules for cleaner code consistency

* fix(lint): enhance withServices HOC to set displayName for better debugging and component identification consistency

* fix(lint): remove unused eslint disables and update callback parameter naming for better adherence to coding standards

* fix(lint): remove unnecessary eslint-disable comment in server-table-data component for improved code clarity and standards compliance

* fix(lint): clean up request-interceptor by removing unused eslint-disable comments and improving parameter naming for clarity

* fix(lint): remove unnecessary eslint-disable comment from configuration-store for enhanced code clarity and standards compliance

* fix(lint): remove unnecessary eslint-disable comments from configuration and search-bar components for improved code clarity and standards compliance

* fix(lint): remove unused eslint-disable comment from search-bar component to enhance code clarity and maintain coding standards

* fix(lint): remove unused eslint-disable comment from wql.test.tsx to improve code clarity and maintain coding standards

* fix(code): replace WQL.id with WQL_ID in query language file for better consistency and clarity in code structure

* fix(lint): remove unused eslint-disable comment from server-client.ts to enhance code clarity and maintain coding standards

* fix(lint): eliminate unused eslint-disable comments in wql.tsx to enhance code readability and maintain coding standards

* fix(lint): remove unnecessary eslint-disable comment in wql.tsx to improve code readability and maintain standards

* fix(types): replace hardcoded id with WQL_ID in ISearchBarModeWQL interface for improved type safety and consistency

* fix(lint): refactor conditional logic in wql.tsx to improve code clarity and maintain consistent formatting standards

* fix(api): use WAZUH_ERROR_DAEMONS_NOT_READY constant for error messaging consistency in WazuhApiCtrl error handling

* fix(api): update WazuhApiCtrl to use WAZUH_ERROR_CODES for consistent error messaging across server responses

* fix(lint): update eslint config to enforce UPPER_CASE for enum and add naming convention for constants files

* fix(settings-validator): refactor class to object, update methods for consistent argument handling and improve readability

* fix(lint): enhance eslint config to allow UPPER_CASE, camelCase, and PascalCase for global variables in naming conventions

* fix(constants): update import path and refactor configuration constants for improved readability and consistency with naming conventions

* fix(wazuh-api): replace WAZUH_ERROR_CODES with WAZUH_ERROR_DAEMONS_NOT_READY for improved clarity and error handling consistency

* fix(constants): rename pluginPlatformRequestHeaders to PLUGIN_PLATFORM_REQUEST_HEADERS for improved consistency in naming conventions

* fix(constants): refactor query language constants and improve code consistency by utilizing centralized definitions and types

---------

Co-authored-by: Guido Modarelli <[email protected]>
Co-authored-by: Guido Modarelli <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent 4d9375d commit bf0537c
Show file tree
Hide file tree
Showing 57 changed files with 7,518 additions and 773 deletions.
82 changes: 71 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ module.exports = {
message:
"Don't use arrow functions in class properties. Use a function instead.",
},
{
selector:
'MemberExpression > LogicalExpression[operator="||"]:has(Identifier):has(ObjectExpression[properties.length=0])',
message: 'Use optional chaining operator instead (?.).',
},
{
selector:
'MemberExpression > LogicalExpression[operator="||"]:has(Identifier):has(ArrayExpression[elements.length=0])',
message: 'Use optional chaining operator instead (?.).',
},
],
'prefer-arrow-callback': 'error',
'func-style': ['error', 'declaration', { allowArrowFunctions: true }],
'arrow-body-style': [
'error',
'as-needed',
{ requireReturnForObjectLiteral: true },
],
'arrow-body-style': ['error', 'as-needed'],
'no-unreachable': 'error',
'no-fallthrough': [
'error',
Expand All @@ -82,7 +88,7 @@ module.exports = {
'block-scoped-var': 'error',
'default-case': 'error',
'default-case-last': 'error',
'default-param-last': 'error',
'default-param-last': 'off',
eqeqeq: ['error', 'always'],
'no-var': 'error',
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -163,6 +169,7 @@ module.exports = {
/* -------------------------------------------------------------------------- */
/* unicorn */
/* -------------------------------------------------------------------------- */
'unicorn/prefer-module': 'off',
'unicorn/prefer-ternary': 'off',
// https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/custom-error-definition.md
'unicorn/custom-error-definition': 'error',
Expand Down Expand Up @@ -203,19 +210,43 @@ module.exports = {
/* -------------------------------------------------------------------------- */
/* @typescript-eslint */
/* -------------------------------------------------------------------------- */
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
// Whether to check all, some, or no arguments.
args: 'after-used',
// Regular expressions of argument names to not check for usage.
argsIgnorePattern: '^_',
// Whether to check catch block arguments.
caughtErrors: 'all',
// Regular expressions of catch block argument names to not check for usage.
caughtErrorsIgnorePattern: '^_',
// Regular expressions of destructured array variable names to not check for usage.
destructuredArrayIgnorePattern: '^_',
// Whether to ignore classes with at least one static initialization block.
ignoreClassWithStaticInitBlock: false,
// Whether to ignore sibling properties in `...` destructurings.
ignoreRestSiblings: false,
// Whether to report variables that match any of the valid ignore pattern options if they have been used.
reportUsedIgnorePattern: true,
// Whether to check all variables or only locally-declared variables.
vars: 'all',
// Regular expressions of variable names to not check for usage.
varsIgnorePattern: '[iI]gnored$',
},
],
'@typescript-eslint/prefer-readonly': 'error',
'@typescript-eslint/no-extraneous-class': 'off',
'@typescript-eslint/method-signature-style': ['error', 'property'],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/naming-convention': [
'error',
{ selector: 'default', format: ['camelCase'] },
{ selector: 'import', format: ['camelCase', 'PascalCase'] },
{
selector: 'variable',
types: ['function'],
selector: 'default',
format: ['camelCase', 'PascalCase'],
leadingUnderscore: 'allow',
},
{
selector: ['objectLiteralProperty', 'typeProperty'],
Expand All @@ -232,7 +263,12 @@ module.exports = {
{
selector: ['variable'],
modifiers: ['global'],
types: ['number', 'string'],
format: ['UPPER_CASE', 'camelCase', 'PascalCase'],
},
{
selector: ['variable'],
modifiers: ['global'],
types: ['boolean', 'number', 'string'],
format: ['UPPER_CASE'],
},
{
Expand Down Expand Up @@ -316,5 +352,29 @@ module.exports = {
],
},
},
{
// constants files
files: ['plugins/**/constants.{ts,js}'],
rules: {
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'variable',
modifiers: ['global'],
format: ['UPPER_CASE'],
},
],
},
},
{
files: ['plugins/**/*.test.{js,jsx,ts,tsx}'],
rules: {
'@typescript-eslint/no-empty-function': [
'error',
{ allow: ['arrowFunctions'] },
],
'@typescript-eslint/no-unused-vars': 'off',
},
},
],
};
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to the Wazuh app project will be documented in this file.

- Support for Wazuh 5.0.0
- Added creation of report definition when creating dashboard by reference and the button to reset the report [#7091](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7091)
- Added a frontend http client to core plugin [#7000](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7000)
- Added an initilization service to core plugin to run the initilization tasks related to user scope [#7145](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7145)

### Removed
Expand Down
68 changes: 47 additions & 21 deletions plugins/main/public/components/search-bar/query-language/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
import { suggestItem } from '../../wz-search-bar';
import { AQL } from './aql';
import { WQL } from './wql';

type SearchBarQueryLanguage = {
description: string;
documentationLink?: string;
interface SearchBarProps {
suggestions: suggestItem[];
onItemClick: (currentInput: string) => (item: suggestItem) => void;
prepend?: React.ReactNode;
disableFocusTrap?: boolean;
isInvalid?: boolean;
onKeyPress?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}

interface SearchBarQueryLanguage {
id: string;
label: string;
description: string;
documentationLink?: string;
getConfiguration?: () => any;
run: (input: string | undefined, params: any) => Promise<{
searchBarProps: any,
run: (
input: string | undefined,
params: any,
) => Promise<{
filterButtons?: React.ReactElement | null;
searchBarProps: SearchBarProps;
output: {
language: string,
unifiedQuery: string,
query: string
}
language: string;
unifiedQuery?: string;
apiQuery?: {
q: string;
};
query: string;
};
}>;
transformInput: (unifiedQuery: string, options: {configuration: any, parameters: any}) => string;
};
transformInput?: (
unifiedQuery: string,
options: { configuration: any; parameters: any },
) => string;
transformUQLToQL?: (unifiedQuery: string) => string;
}

// Register the query languages
export const searchBarQueryLanguages: {
[key: string]: SearchBarQueryLanguage;
} = [AQL, WQL].reduce((accum, item) => {
if (accum[item.id]) {
throw new Error(`Query language with id: ${item.id} already registered.`);
function initializeSearchBarQueryLanguages() {
const languages = [AQL, WQL];
const result: Record<string, SearchBarQueryLanguage> = {};

for (const item of languages) {
if (result[item.id]) {
throw new Error(`Query language with id: ${item.id} already registered.`);
}

result[item.id] = item;
}
return {
...accum,
[item.id]: item,
};
}, {});

return result;
}

export const searchBarQueryLanguages = initializeSearchBarQueryLanguages();
Loading

0 comments on commit bf0537c

Please sign in to comment.