Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/module-http/validate-request-init #2491

Merged
merged 10 commits into from
Oct 2, 2024

Conversation

odinr
Copy link
Collaborator

@odinr odinr commented Oct 1, 2024

Why

After moving services to the Azure Cluster with Nginx, it was discovered that some HTTP requests did not have uppercase verbs in the request method, leading to invalid requests.

Description

This pull request introduces several enhancements and new features to the @equinor/fusion-framework-module-http package. Below are the details of the changes:

Changes

New Operator: capitalizeRequestMethodOperator

  • Ensures that the HTTP method of a given request is in uppercase.
  • Logs a warning if the HTTP method was converted, providing information about the change and a reference to RFC 7231 Section 4.1.
  • Example usage:
    import { capitalizeRequestMethodOperator } from '@equinor/fusion-query';
    
    const operator = capitalizeRequestMethodOperator();
    const request = { method: 'get' };
    const updatedRequest = operator(request);
    console.log(updatedRequest.method); // Outputs: 'GET'

Default Operators in HttpClientConfigurator

  • The HttpClientConfigurator now has capitalizeRequestMethodOperator and requestValidationOperator enabled by default.
  • To disable these operators:
    httpConfig.defaultHttpRequestHandler.remove('capitalize-method');
    httpConfig.defaultHttpRequestHandler.remove('validate-request');

New Operator: requestValidationOperator

  • Validates incoming requests against a Zod schema with options for strict and parse.
  • Example usage with strict validation:
    const operator = requestValidationOperator({ strict: true });
    
    // This will throw an error because of invalid method and extra property.
    operator({
        method: 'post',
        body: 'foo',
        extraProperty: 'This should not be here',
    });
  • Example usage with parsing enabled:
    const operator = requestValidationOperator({ parse: true });
    
    // will return { method: 'GET' }
    const parsedRequest = operator({
        method: 'GET',
        extraProperty: 'This should not be here',
    });

New Method: [remove] in ProcessOperators

  • Allows for the removal of a specific operator.
  • Example:
    httpClient.requestHandler.remove('capitalize-method');

Refactor

Altered HTTP request verbs to uppercase in the following packages:

  • @equinor/fusion-framework-legacy-interopt
  • @equinor/fusion-framework-module-services

Tests

capitalizeRequestMethodOperator Tests:

Test 1: Ensures that the request method is capitalized (e.g., 'get' becomes 'GET'). It also checks that a warning message related to RFC 7231 is logged to the console.

requestValidationOperator Tests:

Test 1: Validates that a request with valid parameters passes without errors.
Test 2: Ensures that a request with valid parameters is correctly parsed and returned as-is.
Test 3: Checks that additional properties are allowed when strict validation is disabled.
Test 4: Ensures that additional properties are removed when strict validation is enabled.
Test 5: Verifies that an error is thrown for requests with invalid parameters (e.g., an invalid HTTP method).

Checklist

  • Confirm that I checked changes to the branch which I am merging into.
    • I have validated included files
    • My code does not generate new linting warnings
    • My PR is not a duplicate, check existing PRs
  • Confirm that I have completed the self-review checklist.
  • Confirm that my changes meet our code of conduct.

Copy link

changeset-bot bot commented Oct 1, 2024

🦋 Changeset detected

Latest commit: 49b89df

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 21 packages
Name Type
@equinor/fusion-framework-module-http Minor
@equinor/fusion-framework-legacy-interopt Major
@equinor/fusion-framework-module-services Patch
@equinor/fusion-framework-app Patch
@equinor/fusion-framework-cli Patch
@equinor/fusion-framework Patch
@equinor/fusion-framework-module-service-discovery Patch
@equinor/fusion-framework-module-widget Major
@equinor/fusion-framework-react-module-http Major
@equinor/fusion-framework-widget Patch
@equinor/fusion-framework-cookbook-app-react-feature-flag Patch
@equinor/fusion-framework-react-components-people-provider Patch
@equinor/fusion-framework-react-app Patch
@equinor/fusion-framework-react Patch
@equinor/fusion-framework-react-widget Patch
@equinor/fusion-framework-module-signalr Patch
@equinor/fusion-framework-cookbook-app-react-context-custom-error Patch
@equinor/fusion-framework-cookbook-app-react-context Patch
@equinor/fusion-framework-react-components-bookmark Patch
@equinor/fusion-framework-react-module-bookmark Patch
@equinor/fusion-framework-react-module-signalr Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added 📚 documentation Improvements or additions to documentation 🚀 feature New feature or request 🚧 chore maintaines work, (update deps, workflos ...) 🧬 Modules 👾 React labels Oct 1, 2024
@odinr odinr marked this pull request as ready for review October 1, 2024 19:26
@odinr odinr requested a review from a team as a code owner October 1, 2024 19:26
@odinr odinr self-assigned this Oct 1, 2024
Copy link
Contributor

github-actions bot commented Oct 1, 2024

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 0.41% 1746 / 415839
🔵 Statements 0.41% 1746 / 415839
🔵 Functions 24.16% 216 / 894
🔵 Branches 37.99% 397 / 1045
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Changed Files
packages/modules/http/src/configurator.ts 94.11% 50% 66.66% 94.11% 158-159
packages/modules/http/src/lib/operators/capitalize-request-method.operator.ts 100% 87.5% 100% 100%
packages/modules/http/src/lib/operators/fetch-request.schema.ts 100% 100% 100% 100%
packages/modules/http/src/lib/operators/index.ts 100% 100% 100% 100%
packages/modules/http/src/lib/operators/process-operators.ts 75% 91.66% 57.14% 75% 50-53, 73-75, 83-84, 95-96
packages/modules/http/src/lib/operators/request-validation.operator.ts 88.23% 88.88% 100% 88.23% 48-49
packages/modules/http/src/lib/operators/types.ts 100% 100% 100% 100%
packages/modules/services/src/bookmarks/delete/generate-parameters.ts 0% 0% 0% 0% 1, 3, 6-7, 12-16, 18-22, 24-25
packages/modules/services/src/bookmarks/favorites/delete/generate-parameters.ts 0% 0% 0% 0% 1, 3, 6-7, 12-16, 18-22, 24-25
packages/modules/services/src/bookmarks/favorites/head/generate-parameters.ts 0% 0% 0% 0% 1, 3, 6-7, 12-16, 18-19, 21-25, 27-28
packages/modules/services/src/bookmarks/favorites/post/generate-parameters.ts 0% 0% 0% 0% 1, 3, 6-7, 12-16, 18-19, 21-25, 27-28
packages/modules/services/src/bookmarks/post/generate-parameters.ts 0% 0% 0% 0% 1, 3, 6-7, 12-16, 18-19, 21-25, 27-28
packages/modules/services/src/notification/notification/delete/generate-parameters.ts 0% 0% 0% 0% 1, 5, 8-9, 14-18, 20-21, 23-27, 29-30, 32
packages/modules/services/src/notification/notification/patch/generate-parameters.ts 0% 0% 0% 0% 1, 5, 8-9, 14-18, 20-21, 23-27, 29-30, 32
packages/modules/services/src/notification/notification/post/generate-parameters.ts 0% 0% 0% 0% 1, 5, 8-9, 14-18, 20-21, 23-27, 29-30, 32
packages/modules/services/src/notification/settings/put/generate-parameters.ts 0% 0% 0% 0% 1, 5, 8-9, 14-19, 21-23, 25-29, 31-32, 34
packages/react/legacy-interopt/src/legacy-api-signin.ts 0% 0% 0% 0% 1, 4-5, 7-11, 13
Generated in workflow #7811

Add a new operator `capitalizeRequestMethodOperator` to ensure that the `RequestInit` method of a given request is in uppercase.
Add a new feature to the `@equinor/fusion-framework-module-http` package. This feature introduces the `requestValidationOperator`, a utility function that validates incoming requests against a Zod schema.

The function has options for strict validation and parsing of the request object. This operator is meant to be used as a request operator in the Fusion API framework.
@odinr odinr force-pushed the feat/module-http/validate-request-init branch from da9124e to 49b89df Compare October 2, 2024 05:08
Copy link

@jaysencpp jaysencpp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@odinr odinr merged commit 73af73e into main Oct 2, 2024
8 checks passed
@odinr odinr deleted the feat/module-http/validate-request-init branch October 2, 2024 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚧 chore maintaines work, (update deps, workflos ...) 📚 documentation Improvements or additions to documentation 🚀 feature New feature or request 🧬 Modules 👾 React
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants