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): upgrade populate request param to include nested object #452

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading