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

types(v4/v5): filter request parameter types #453

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

XanderD99
Copy link
Contributor

@XanderD99 XanderD99 commented Dec 20, 2024

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR introduces:

  1. Conditional typing for StrapiRequestParams:

    • Ensures the filters field is included only for the find HTTP method.
    • Excludes filters for other HTTP methods like findOne, create, update and delete, improving type safety and enforcing correct parameter usage.
  2. New types for the filters parameter:

    • Introduced a comprehensive type system for the filters parameter.
    • Filters allow defining conditions with supported operators ($eq, $lt, $gt, $contains, etc.) for querying fields in findMany requests.
    • Operators are context-sensitive, meaning only valid operators for a field's type (e.g., string, number, boolean) are allowed.
    • Prevents usage of operators on nested objects to enforce consistent API behavior.
    • Ensures only one operator can be used at a time for a given filter condition.

Key Improvements:

  • Strongly-typed filter conditions based on field types.
  • Validation of operator applicability to ensure correct usage.
  • Cleaner, more maintainable request parameters with utility types like Omit for conditional inclusion of fields.

Example Usage:

find with Filters:

const params: Strapi5RequestParams<{age: number, name: string}> = {
  fields: ['name', 'age'],
  sort: ['name'],
  filters: {
    age: { $gte: 18 },
    name: { $startsWith: 'John' },
  },
};

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes (if not applicable, please state why)

Copy link

netlify bot commented Dec 20, 2024

👷 Deploy request for nuxt-strapi-module pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit bf601e0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant