Skip to content

Commit

Permalink
feat(types): Improve RequestParams for v4
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopheCVB committed Nov 24, 2024
1 parent 6563006 commit b32d715
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/runtime/composables-v4/useStrapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { Strapi4ResponseSingle, Strapi4RequestParams, Strapi4ResponseMany }
import { useStrapiClient } from '#imports'

interface StrapiV4Client<T> {
find<F = T>(contentType: string, params?: Strapi4RequestParams): Promise<Strapi4ResponseMany<F>>
findOne<F = T>(contentType: string, id?: string | number | Strapi4RequestParams, params?: Strapi4RequestParams): Promise<Strapi4ResponseSingle<F>>
find<F = T>(contentType: string, params?: Strapi4RequestParams<F>): Promise<Strapi4ResponseMany<F>>
findOne<F = T>(contentType: string, id?: string | number | Strapi4RequestParams<F>, params?: Strapi4RequestParams<F>): Promise<Strapi4ResponseSingle<F>>
create<F = T>(contentType: string, data: Partial<F>): Promise<Strapi4ResponseSingle<F>>
update<F = T>(contentType: string, id: string | number | Partial<F>, data?: Partial<F>): Promise<Strapi4ResponseSingle<F>>
delete<F = T>(contentType: string, id?: string | number): Promise<Strapi4ResponseSingle<F>>
Expand All @@ -20,7 +20,7 @@ export const useStrapi = <T>(): StrapiV4Client<T> => {
* @param {Strapi4RequestParams} [params] - Query parameters
* @returns Promise<T>
*/
const find = <T>(contentType: string, params?: Strapi4RequestParams, fetchOptions?: FetchOptions): Promise<Strapi4ResponseMany<T>> => {
const find = <T>(contentType: string, params?: Strapi4RequestParams<T>, fetchOptions?: FetchOptions): Promise<Strapi4ResponseMany<T>> => {
return client(`/${contentType}`, { method: 'GET', params, ...fetchOptions })
}

Expand All @@ -32,7 +32,7 @@ export const useStrapi = <T>(): StrapiV4Client<T> => {
* @param {Strapi4RequestParams} [params] - Query parameters
* @returns Promise<T>
*/
const findOne = <T>(contentType: string, id?: string | number | Strapi4RequestParams, params?: Strapi4RequestParams, fetchOptions?: FetchOptions): Promise<Strapi4ResponseSingle<T>> => {
const findOne = <T>(contentType: string, id?: string | number | Strapi4RequestParams<T>, params?: Strapi4RequestParams<T>, fetchOptions?: FetchOptions): Promise<Strapi4ResponseSingle<T>> => {
if (typeof id === 'object') {
params = id
id = undefined
Expand Down
16 changes: 16 additions & 0 deletions src/runtime/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,22 @@ export interface StrapiGraphqlVariables {
[variable: string]: unknown
}

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

export type StrapiRequestParamSort<T> = `${Exclude<keyof T, symbol>}${':asc' | ':desc' | ''}`

export 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]

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

export interface Strapi4Error {
error: {
Expand All @@ -21,10 +21,10 @@ export interface PaginationByOffset {
withCount?: boolean
}

export interface Strapi4RequestParams {
fields?: Array<string>
populate?: string | Array<string> | object
sort?: string | Array<string>
export interface Strapi4RequestParams<T> {
fields?: Array<StrapiRequestParamField<T>>
populate?: '*' | StrapiRequestParamPopulate<T> | Array<StrapiRequestParamPopulate<T>>
sort?: StrapiRequestParamSort<T> | Array<StrapiRequestParamSort<T>>
pagination?: PaginationByOffset | PaginationByPage
filters?: Record<string, unknown>
publicationState?: 'live' | 'preview'
Expand Down
24 changes: 4 additions & 20 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 } from '.'
import type { MetaResponsePaginationByOffset, MetaResponsePaginationByPage, PaginationByOffset, PaginationByPage, StrapiLocale, StrapiRequestParamField, StrapiRequestParamPopulate, StrapiRequestParamSort } from '.'

export interface Strapi5Error {
error: {
Expand All @@ -9,26 +9,10 @@ export interface Strapi5Error {
}
}

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

type Strapi5RequestParamSort<T> = `${Exclude<keyof T, symbol>}${':asc' | ':desc' | ''}`

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

export interface Strapi5RequestParams<T> {
fields?: Array<Strapi5RequestParamField<T>>
populate?: '*' | Strapi5RequestParamPopulate<T> | Array<Strapi5RequestParamPopulate<T>>
sort?: Strapi5RequestParamSort<T> | Array<Strapi5RequestParamSort<T>>
fields?: Array<StrapiRequestParamField<T>>
populate?: '*' | StrapiRequestParamPopulate<T> | Array<StrapiRequestParamPopulate<T>>
sort?: StrapiRequestParamSort<T> | Array<StrapiRequestParamSort<T>>
pagination?: PaginationByOffset | PaginationByPage
filters?: Record<string, unknown>
publicationState?: 'live' | 'preview'
Expand Down

0 comments on commit b32d715

Please sign in to comment.