Skip to content

Commit

Permalink
types(v4/v5): upgrade populate request param to include nested object (
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderD99 authored Feb 24, 2025
1 parent eba3baf commit c6284e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/runtime/types/v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@ export interface PaginationByOffset {

export interface Strapi4RequestParams<T> {
fields?: Array<StrapiRequestParamField<T>>
populate?: '*' | StrapiRequestParamPopulate<T> | Array<StrapiRequestParamPopulate<T>>
populate?: Strapi4RequestPopulateParam<T>
sort?: StrapiRequestParamSort<T> | Array<StrapiRequestParamSort<T>>
pagination?: PaginationByOffset | PaginationByPage
filters?: Record<string, unknown>
publicationState?: 'live' | 'preview'
locale?: StrapiLocale
}

export type Strapi4RequestPopulateParams<T> = Pick<Strapi4RequestParams<T>, 'fields' | 'sort' | 'populate' | 'filters'>

// Unified type for Strapi populate, combining both string paths and nested objects.
export type Strapi4RequestPopulateParam<T> =
| '*' // Populate all relations.
| { [K in keyof T]?: // Nested object population.
T[K] extends object
? T[K] extends Array<infer I>
? Strapi4RequestPopulateParam<I> | Strapi4RequestPopulateParams<I>
: Strapi4RequestPopulateParam<T[K]> | Strapi4RequestPopulateParams<T[K]>
: never
}
| StrapiRequestParamPopulate<T> // String paths like "field.subfield".
| Array<StrapiRequestParamPopulate<T>> // Array of string paths.

export interface Strapi4ResponseData<T> {
id: number
attributes: T
Expand Down
17 changes: 16 additions & 1 deletion src/runtime/types/v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ export interface Strapi5Error {

export interface Strapi5RequestParams<T> {
fields?: Array<StrapiRequestParamField<T>>
populate?: '*' | StrapiRequestParamPopulate<T> | Array<StrapiRequestParamPopulate<T>>
populate?: Strapi5RequestPopulateParam<T>
sort?: StrapiRequestParamSort<T> | Array<StrapiRequestParamSort<T>>
pagination?: PaginationByOffset | PaginationByPage
filters?: Record<string, unknown>
status?: 'published' | 'draft'
locale?: StrapiLocale | null
}

export type Strapi5RequestPopulateParams<T> = Pick<Strapi5RequestParams<T>, 'fields' | 'sort' | 'populate' | 'filters'>

// Unified type for Strapi populate, combining both string paths and nested objects.
export type Strapi5RequestPopulateParam<T> =
| '*' // Populate all relations.
| { [K in keyof T]?: // Nested object population.
T[K] extends object
? T[K] extends Array<infer I>
? Strapi5RequestPopulateParam<I> | Strapi5RequestPopulateParams<I>
: Strapi5RequestPopulateParam<T[K]> | Strapi5RequestPopulateParams<T[K]>
: never
}
| StrapiRequestParamPopulate<T> // String paths like "field.subfield".
| Array<StrapiRequestParamPopulate<T>> // Array of string paths.

export interface StrapiSystemFields {
documentId: string
locale?: string
Expand Down

0 comments on commit c6284e6

Please sign in to comment.