keyshade-xyz / keyshade

Realtime secret and configuration management tool, with the best in class security and seamless integration support
https://keyshade.xyz
Mozilla Public License 2.0
196 stars 96 forks source link

feat(api): Improve handling of edge cases for paginate module #402

Closed muntaxir4 closed 2 months ago

muntaxir4 commented 2 months ago

User description

Description

Developer's checklist

If changes are made in the code:

Documentation Update


PR Type

Enhancement, Tests


Description


Changes walkthrough šŸ“

Relevant files
Tests
paginate.spec.ts
Add and modify test cases for paginate function                   

apps/api/src/common/paginate.spec.ts
  • Added test case to return empty object when page exceeds maximum page
    limit.
  • Modified test case to return empty object when limit is 0 or
    undefined.
  • +15/-4   
    Enhancement
    paginate.ts
    Update paginate function for edge cases handling                 

    apps/api/src/common/paginate.ts
  • Updated paginate function to return empty object when limit is 0 or
    undefined.
  • Added condition to return empty object when page exceeds maximum page
    limit.
  • +3/-2     

    šŸ’” PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    codiumai-pr-agent-free[bot] commented 2 months ago

    PR Reviewer Guide šŸ”

    ā±ļø Estimated effort to review: 2 šŸ”µšŸ”µāšŖāšŖāšŖ
    šŸ§Ŗ PR contains tests
    šŸ”’ No security concerns identified
    āš” Key issues to review

    Type Safety
    The function `paginate` returns `{}` as `PaginatedMetadata` when `query.limit` is not defined or when `query.page` exceeds `metadata.pageCount`. This might lead to type safety issues if the empty object does not fulfill all properties expected by `PaginatedMetadata`. Consider returning a fully initialized `PaginatedMetadata` object with default or empty values for all properties. Type Safety
    Similar to the previous issue, returning `{}` as `PaginatedMetadata` when `query.page` exceeds `metadata.pageCount` could lead to type safety issues. Ensure that the returned object meets all the interface requirements of `PaginatedMetadata`.
    codiumai-pr-agent-free[bot] commented 2 months ago

    PR Code Suggestions āœØ

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Ensure query.limit is a positive number to prevent logical errors ___ **Add a check to ensure that query.limit is not only non-zero but also a positive
    number. This prevents potential issues with negative limits, which logically do not
    make sense and can lead to unexpected behavior.** [apps/api/src/common/paginate.ts [36]](https://github.com/keyshade-xyz/keyshade/pull/402/files#diff-f8cbb5867a757770be21dd63f5627457b0e0d358d0213f81032c51b828d04b75R36-R36) ```diff -if (!query.limit) return {} as PaginatedMetadata +if (!query.limit || query.limit <= 0) return { links: {}, pageCount: 0, totalCount: 0 } as PaginatedMetadata ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: This suggestion addresses a potential logical error by ensuring that `query.limit` is not only non-zero but also positive, which prevents unexpected behavior from negative limits.
    9
    Best practice
    Return a well-defined empty PaginatedMetadata object instead of a plain empty object ___ **Instead of returning an empty object directly, it would be more robust to return a
    well-defined empty PaginatedMetadata object with default values for all properties.
    This ensures that the structure of the response remains consistent, which can
    prevent potential runtime errors in consuming functions that might expect specific
    properties to always exist.** [apps/api/src/common/paginate.ts [36]](https://github.com/keyshade-xyz/keyshade/pull/402/files#diff-f8cbb5867a757770be21dd63f5627457b0e0d358d0213f81032c51b828d04b75R36-R36) ```diff -if (!query.limit) return {} as PaginatedMetadata +if (!query.limit) return { links: {}, pageCount: 0, totalCount: 0 } as PaginatedMetadata ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: This suggestion improves the robustness of the function by ensuring that the returned object has a consistent structure, which can prevent potential runtime errors in consuming functions.
    8
    Enhancement
    Add a test case for negative query.limit values ___ **Add a test case to verify that the function behaves correctly when query.limit is
    negative. This helps ensure that the function's robustness and error handling are
    adequate for unexpected input values.** [apps/api/src/common/paginate.spec.ts [95-102]](https://github.com/keyshade-xyz/keyshade/pull/402/files#diff-4f910ed52670f82fe739cf016c5e915b5829273c6731680dee2197b89b570db0R95-R102) ```diff -it('should return empty object when limit is 0 or undefined', () => { +it('should return empty object when limit is negative', () => { const totalCount = 10 const relativeUrl = '/items' - const query = { page: 0, limit: 0 } + const query = { page: 0, limit: -1 } const result = paginate(totalCount, relativeUrl, query) expect(result).toBeDefined() expect(result).toEqual({}) }) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Adding a test case for negative `query.limit` values enhances the robustness of the test suite by ensuring that the function handles unexpected input values correctly.
    7
    Maintainability
    Add descriptive logging for cases where query.page exceeds metadata.pageCount ___ **Consider adding a more descriptive error or log message before returning an empty
    object when query.page exceeds metadata.pageCount. This can aid in debugging and
    understanding the flow of data and decisions within the function.** [apps/api/src/common/paginate.ts [58]](https://github.com/keyshade-xyz/keyshade/pull/402/files#diff-f8cbb5867a757770be21dd63f5627457b0e0d358d0213f81032c51b828d04b75R58-R58) ```diff -if (query.page >= metadata.pageCount) return {} as PaginatedMetadata +if (query.page >= metadata.pageCount) { + console.error(`Page ${query.page} exceeds maximum page count of ${metadata.pageCount}. Returning empty metadata.`); + return { links: {}, pageCount: 0, totalCount: 0 } as PaginatedMetadata; +} ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: This suggestion improves maintainability by adding a descriptive log message, which aids in debugging and understanding the flow of data within the function.
    6