diff --git a/src/runtime/composables-v5/useStrapi.ts b/src/runtime/composables-v5/useStrapi.ts index f1c75d84..19bcabaa 100644 --- a/src/runtime/composables-v5/useStrapi.ts +++ b/src/runtime/composables-v5/useStrapi.ts @@ -29,15 +29,10 @@ export const useStrapi = (): StrapiV5Client => { * * @param {string} contentType - Content type's name pluralized * @param {string} documentId - ID of entry - * @param {Strapi5RequestParams} [params] - Query parameters + * @param {Omit, 'filter'>} [params] - Query parameters * @returns Promise */ - const findOne = (contentType: string, documentId?: string | Strapi5RequestParams, params?: Strapi5RequestParams, fetchOptions?: FetchOptions): Promise> => { - if (typeof documentId === 'object') { - params = documentId - documentId = undefined - } - + const findOne = (contentType: string, documentId: string, params?: Omit, 'filter'>, fetchOptions?: FetchOptions): Promise> => { const path = [contentType, documentId].filter(Boolean).join('/') return client(path, { method: 'GET', params, ...fetchOptions }) @@ -48,10 +43,10 @@ export const useStrapi = (): StrapiV5Client => { * * @param {string} contentType - Content type's name pluralized * @param {Record} data - Form data - * @param {Strapi5RequestParams} [params] - Query parameters + * @param {Omit, 'filter'>} [params] - Query parameters * @returns Promise */ - const create = (contentType: string, data: Partial, params: Strapi5RequestParams = {}): Promise> => { + const create = (contentType: string, data: Partial, params: Omit, 'filter'> = {}): Promise> => { return client(`/${contentType}`, { method: 'POST', body: { data }, params }) } @@ -61,10 +56,10 @@ export const useStrapi = (): StrapiV5Client => { * @param {string} contentType - Content type's name pluralized * @param {string} documentId - ID of entry to be updated * @param {Record} data - Form data - * @param {Strapi5RequestParams} [params] - Query parameters + * @param {Omit, 'filter'>} [params] - Query parameters * @returns Promise */ - const update = (contentType: string, documentId: string | Partial, data?: Partial, params: Strapi5RequestParams = {}): Promise> => { + const update = (contentType: string, documentId: string | Partial, data?: Partial, params: Omit, 'filter'> = {}): Promise> => { if (typeof documentId === 'object') { data = documentId documentId = undefined diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index cea49a97..4508c903 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -586,6 +586,45 @@ export type StrapiRequestParamPopulate = { : never; }[keyof T] +// General operator type for primitive values (string, number, boolean) +export type StrapiPrimitiveOperators = + | { $eq: T } // Equal to a value + | { $ne: T } // Not equal to a value + | { $in: T[] } // Included in an array of values + | { $notIn: T[] } // Not included in an array of values + | { $null: boolean } // Is null + | { $notNull: boolean } // Is not null + | { $or: StrapiFilterRequestParam[] } // "Or" condition + | { $and: StrapiFilterRequestParam[] } // "And" condition + | { $not: StrapiFilterRequestParam } // "Not" condition + +// String-specific operators +export type StrapiStringOperators = + | { $eqi: string } // Equal (case-insensitive) + | { $contains: string } // Contains a substring + | { $notContains: string } // Does not contain a substring + | { $containsi: string } // Contains a substring (case-insensitive) + | { $notContainsi: string } // Does not contain a substring (case-insensitive) + | { $startsWith: string } // Starts with a substring + | { $startsWithi: string } // Starts with a substring (case-insensitive) + | { $endsWith: string } // Ends with a substring + | { $endsWithi: string } // Ends with a substring (case-insensitive) + +// Numeric-specific operators +export type StrapiNumberOperators = + | { $lt: number } // Less than a value + | { $lte: number } // Less than or equal to a value + | { $gt: number } // Greater than a value + | { $gte: number } // Greater than or equal to a value + | { $between: [number, number] } // Is between two values + +// Base type for filters +export type StrapiFilterRequestParam = T extends object + ? { + [K in keyof T]?: StrapiFilterRequestParam; // Nested filtering for objects + } + : StrapiPrimitiveOperators | (T extends string ? StrapiStringOperators : T extends number ? StrapiNumberOperators : never) // Apply operators based on field type + export * from './v3' export * from './v4' export * from './v5' diff --git a/src/runtime/types/v4.ts b/src/runtime/types/v4.ts index 45bb999c..f394630e 100644 --- a/src/runtime/types/v4.ts +++ b/src/runtime/types/v4.ts @@ -1,4 +1,4 @@ -import type { StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort } from '.' +import type { StrapiFilterRequestParam, StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort } from '.' export interface Strapi4Error { error: { @@ -26,7 +26,7 @@ export interface Strapi4RequestParams { populate?: Strapi4RequestPopulateParam sort?: StrapiRequestParamSort | Array> pagination?: PaginationByOffset | PaginationByPage - filters?: Record + filters?: StrapiFilterRequestParam publicationState?: 'live' | 'preview' locale?: StrapiLocale } diff --git a/src/runtime/types/v5.ts b/src/runtime/types/v5.ts index 9d9a0bf4..e39a598e 100644 --- a/src/runtime/types/v5.ts +++ b/src/runtime/types/v5.ts @@ -1,4 +1,4 @@ -import type { MetaResponsePaginationByOffset, MetaResponsePaginationByPage, PaginationByOffset, PaginationByPage, StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort } from '.' +import type { MetaResponsePaginationByOffset, MetaResponsePaginationByPage, PaginationByOffset, PaginationByPage, StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort, StrapiFilterRequestParam } from '.' export interface Strapi5Error { error: { @@ -14,7 +14,7 @@ export interface Strapi5RequestParams { populate?: Strapi5RequestPopulateParam sort?: StrapiRequestParamSort | Array> pagination?: PaginationByOffset | PaginationByPage - filters?: Record + filters?: StrapiFilterRequestParam status?: 'published' | 'draft' locale?: StrapiLocale | null }