Skip to content

Commit

Permalink
add types for filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderD99 committed Dec 19, 2024
1 parent dd0acc9 commit bf601e0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
17 changes: 6 additions & 11 deletions src/runtime/composables-v5/useStrapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@ export const useStrapi = <T>(): StrapiV5Client<T> => {
*
* @param {string} contentType - Content type's name pluralized
* @param {string} documentId - ID of entry
* @param {Strapi5RequestParams<T>} [params] - Query parameters
* @param {Omit<Strapi5RequestParams<T>, 'filter'>} [params] - Query parameters
* @returns Promise<T>
*/
const findOne = <T>(contentType: string, documentId?: string | Strapi5RequestParams<T>, params?: Strapi5RequestParams<T>, fetchOptions?: FetchOptions): Promise<Strapi5ResponseSingle<T>> => {
if (typeof documentId === 'object') {
params = documentId
documentId = undefined
}

const findOne = <T>(contentType: string, documentId: string, params?: Omit<Strapi5RequestParams<T>, 'filter'>, fetchOptions?: FetchOptions): Promise<Strapi5ResponseSingle<T>> => {
const path = [contentType, documentId].filter(Boolean).join('/')

return client(path, { method: 'GET', params, ...fetchOptions })
Expand All @@ -48,10 +43,10 @@ export const useStrapi = <T>(): StrapiV5Client<T> => {
*
* @param {string} contentType - Content type's name pluralized
* @param {Record<string, any>} data - Form data
* @param {Strapi5RequestParams<T>} [params] - Query parameters
* @param {Omit<Strapi5RequestParams<T>, 'filter'>} [params] - Query parameters
* @returns Promise<T>
*/
const create = <T>(contentType: string, data: Partial<T>, params: Strapi5RequestParams<T> = {}): Promise<Strapi5ResponseSingle<T>> => {
const create = <T>(contentType: string, data: Partial<T>, params: Omit<Strapi5RequestParams<T>, 'filter'> = {}): Promise<Strapi5ResponseSingle<T>> => {
return client(`/${contentType}`, { method: 'POST', body: { data }, params })
}

Expand All @@ -61,10 +56,10 @@ export const useStrapi = <T>(): StrapiV5Client<T> => {
* @param {string} contentType - Content type's name pluralized
* @param {string} documentId - ID of entry to be updated
* @param {Record<string, any>} data - Form data
* @param {Strapi5RequestParams<T>} [params] - Query parameters
* @param {Omit<Strapi5RequestParams<T>, 'filter'>} [params] - Query parameters
* @returns Promise<T>
*/
const update = <T>(contentType: string, documentId: string | Partial<T>, data?: Partial<T>, params: Strapi5RequestParams<T> = {}): Promise<Strapi5ResponseSingle<T>> => {
const update = <T>(contentType: string, documentId: string | Partial<T>, data?: Partial<T>, params: Omit<Strapi5RequestParams<T>, 'filter'> = {}): Promise<Strapi5ResponseSingle<T>> => {
if (typeof documentId === 'object') {
data = documentId
documentId = undefined
Expand Down
39 changes: 39 additions & 0 deletions src/runtime/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,45 @@ export type StrapiRequestParamPopulate<T> = {
: never;
}[keyof T]

// General operator type for primitive values (string, number, boolean)
export type StrapiPrimitiveOperators<T> =
| { $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<T>[] } // "Or" condition
| { $and: StrapiFilterRequestParam<T>[] } // "And" condition
| { $not: StrapiFilterRequestParam<T> } // "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> = T extends object
? {
[K in keyof T]?: StrapiFilterRequestParam<T[K]>; // Nested filtering for objects
}
: StrapiPrimitiveOperators<T> | (T extends string ? StrapiStringOperators : T extends number ? StrapiNumberOperators : never) // Apply operators based on field type

export * from './v3'
export * from './v4'
export * from './v5'
4 changes: 2 additions & 2 deletions src/runtime/types/v4.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort } from '.'
import type { StrapiFilterRequestParam, StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort } from '.'

export interface Strapi4Error {
error: {
Expand Down Expand Up @@ -26,7 +26,7 @@ export interface Strapi4RequestParams<T> {
populate?: Strapi4RequestPopulateParam<T>
sort?: StrapiRequestParamSort<T> | Array<StrapiRequestParamSort<T>>
pagination?: PaginationByOffset | PaginationByPage
filters?: Record<string, unknown>
filters?: StrapiFilterRequestParam<T>
publicationState?: 'live' | 'preview'
locale?: StrapiLocale
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/types/v5.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -14,7 +14,7 @@ export interface Strapi5RequestParams<T> {
populate?: Strapi5RequestPopulateParam<T>
sort?: StrapiRequestParamSort<T> | Array<StrapiRequestParamSort<T>>
pagination?: PaginationByOffset | PaginationByPage
filters?: Record<string, unknown>
filters?: StrapiFilterRequestParam<T>
status?: 'published' | 'draft'
locale?: StrapiLocale | null
}
Expand Down

0 comments on commit bf601e0

Please sign in to comment.