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(useStrapi): improve types for v5 and v4 #445

Merged
merged 8 commits into from
Dec 19, 2024

Conversation

ChristopheCVB
Copy link
Collaborator

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

Added types for fields, sort and populate in Strapi5RequestParams

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 Nov 23, 2024

Deploy Preview for nuxt-strapi-module canceled.

Name Link
🔨 Latest commit 95e7169
🔍 Latest deploy log https://app.netlify.com/sites/nuxt-strapi-module/deploys/6748538e54e6be0008242555

@ChristopheCVB ChristopheCVB changed the title Added types for fields, sort and populate in Strapi5RequestParams Improve v5 types for RequestParams Nov 23, 2024
@benjamincanac benjamincanac changed the title Improve v5 types for RequestParams feat(useStrapi): improve typess for v5 and v4 Nov 28, 2024
@benjamincanac
Copy link
Member

@XanderD99 Could I ask you to review this?

@benjamincanac benjamincanac changed the title feat(useStrapi): improve typess for v5 and v4 feat(useStrapi): improve types for v5 and v4 Nov 28, 2024
@XanderD99
Copy link
Contributor

Implementation looks good to me. I would suggest tho to add something like this to it. Since the populate can be an object as well:

https://docs.strapi.io/dev-docs/api/rest/populate-select

type StrapiRequestParamPopulate<T> = {
  [K in keyof T]: T[K] extends object
  ? T[K] extends Array<infer I>
  ? `${Exclude<K, symbol>}` | `${Exclude<K, symbol>}.${StrapiRequestParamPopulate<I>}`
  : `${Exclude<K, symbol>}` | `${Exclude<K, symbol>}.${StrapiRequestParamPopulate<T[K]>}`
  : never;
}[keyof T]

interface Strapi5RequestParams<T> {
  populate?: ValidPopulateKeys<T> extends never
  ? never // Exclude `populate` if no sub-objects exist.
  : '*' // Populate all relations.
  | StrapiRequestParamPopulate<T> // String paths like "field.subfield".
  | Array<StrapiRequestParamPopulate<T>> // Array of string paths.
  | { // Nested object for advanced populates.
    [K in keyof T]?: T[K] extends object
    ? T[K] extends Array<infer I>
    ? Strapi5RequestParams<I> | '*' // Nested array population or wildcard.
    : Strapi5RequestParams<T[K]> | '*' // Nested object population or wildcard.
    : never; // Non-object keys cannot have nested populate.
  }
}

type ValidPopulateKeys<T> = keyof {
  [K in keyof T as T[K] extends object ? K : never]: T[K];
};

@benjamincanac
Copy link
Member

Merging this, @XanderD99 your suggestion can be done in another PR I guess.

@benjamincanac benjamincanac merged commit 8838d55 into nuxt-modules:dev Dec 19, 2024
5 checks passed
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.

3 participants